Modifs to send current live game
[vchess.git] / client / src / utils / gameStorage.js
CommitLineData
967a2686
BA
1// Game object: {
2// // Static informations:
3// gameId: string
4// vname: string,
5// fenStart: string,
6// players: array of sid+id+name,
7// timeControl: string,
8// increment: integer (seconds),
9// mode: string ("live" or "corr")
10// imported: boolean (optional, default false)
11// // Game (dynamic) state:
12// fen: string,
13// moves: array of Move objects,
14// clocks: array of integers,
809ba2aa 15// initime: array of integers (when clock start running),
967a2686
BA
16// score: string (several options; '*' == running),
17// }
18
19function dbOperation(callback)
20{
21 let db = null;
22 let DBOpenRequest = window.indexedDB.open("vchess", 4);
23
24 DBOpenRequest.onerror = function(event) {
25 alert("Database error: " + event.target.errorCode);
26 };
27
28 DBOpenRequest.onsuccess = function(event) {
29 db = DBOpenRequest.result;
30 callback(db);
31 db.close();
32 };
33
34 DBOpenRequest.onupgradeneeded = function(event) {
35 let db = event.target.result;
36 db.onerror = function(event) {
37 alert("Error while loading database: " + event.target.errorCode);
38 };
39 // Create objectStore for vchess->games
42c15a75
BA
40 let objectStore = db.createObjectStore("games", { keyPath: "gameId" });
41 objectStore.createIndex("score", "score"); //to search by game result
967a2686
BA
42 }
43}
44
45export const GameStorage =
46{
47 // Optional callback to get error status
48 add: function(game, callback)
49 {
50 dbOperation((db) => {
51 let transaction = db.transaction("games", "readwrite");
52 if (callback)
53 {
54 transaction.oncomplete = function() {
55 callback({}); //everything's fine
56 }
57 transaction.onerror = function() {
58 callback({errmsg: "addGame failed: " + transaction.error});
59 };
60 }
61 let objectStore = transaction.objectStore("games");
62 objectStore.add(game);
63 });
64 },
65
66 // TODO: also option to takeback a move ?
67 // NOTE: for live games only (all on server for corr)
809ba2aa 68 update: function(gameId, obj) //colorIdx, nextIdx, move, fen, addTime, score
967a2686
BA
69 {
70 dbOperation((db) => {
71 let objectStore = db.transaction("games", "readwrite").objectStore("games");
72 objectStore.get(gameId).onsuccess = function(event) {
73 const game = event.target.result;
74 if (!!obj.move)
75 {
76 game.moves.push(obj.move);
77 game.fen = obj.fen;
78 if (!!obj.addTime) //NaN if first move in game
79 game.clocks[obj.colorIdx] += obj.addTime;
809ba2aa 80 game.initime[obj.nextIdx] = Date.now();
967a2686 81 }
967a2686
BA
82 if (!!obj.score)
83 game.score = obj.score;
84 objectStore.put(game); //save updated data
85 }
86 });
87 },
88
89 // Retrieve any live game from its identifiers (locally, running or not)
90 // NOTE: need callback because result is obtained asynchronously
91 get: function(gameId, callback)
92 {
93 dbOperation((db) => {
94 let objectStore = db.transaction('games').objectStore('games');
95 if (!gameId) //retrieve all
96 {
97 let games = [];
98 objectStore.openCursor().onsuccess = function(event) {
99 let cursor = event.target.result;
100 // if there is still another cursor to go, keep running this code
101 if (cursor)
102 {
103 games.push(cursor.value);
104 cursor.continue();
105 }
106 else
107 callback(games);
108 }
109 }
110 else //just one game
111 {
112 objectStore.get(gameId).onsuccess = function(event) {
113 callback(event.target.result);
114 }
115 }
116 });
117 },
118
42c15a75
BA
119 getCurrent: function(callback)
120 {
121 dbOperation((db) => {
122 let objectStore = db.transaction('games').objectStore('games');
123 objectStore.get("*").onsuccess = function(event) {
124 callback(event.target.result);
125 };
126 });
127 },
128
967a2686
BA
129 // Delete a game in indexedDB
130 remove: function(gameId, callback)
131 {
132 dbOperation((db) => {
133 let transaction = db.transaction(["games"], "readwrite");
134 if (callback)
135 {
136 transaction.oncomplete = function() {
137 callback({}); //everything's fine
138 }
139 transaction.onerror = function() {
140 callback({errmsg: "removeGame failed: " + transaction.error});
141 };
142 }
143 transaction.objectStore("games").delete(gameId);
144 });
145 },
146};