Attempt to fix countdown for live games. Some issues with GameStorage.add now
[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
fd7aea36
BA
19import { ajax } from "@/utils/ajax";
20
967a2686
BA
21function dbOperation(callback)
22{
23 let db = null;
24 let DBOpenRequest = window.indexedDB.open("vchess", 4);
25
26 DBOpenRequest.onerror = function(event) {
27 alert("Database error: " + event.target.errorCode);
28 };
29
30 DBOpenRequest.onsuccess = function(event) {
31 db = DBOpenRequest.result;
32 callback(db);
33 db.close();
34 };
35
36 DBOpenRequest.onupgradeneeded = function(event) {
37 let db = event.target.result;
38 db.onerror = function(event) {
39 alert("Error while loading database: " + event.target.errorCode);
40 };
41 // Create objectStore for vchess->games
42c15a75
BA
42 let objectStore = db.createObjectStore("games", { keyPath: "gameId" });
43 objectStore.createIndex("score", "score"); //to search by game result
967a2686
BA
44 }
45}
46
47export const GameStorage =
48{
49 // Optional callback to get error status
c0b27606 50 // TODO: this func called from Hall seems to not work now...
967a2686
BA
51 add: function(game, callback)
52 {
53 dbOperation((db) => {
54 let transaction = db.transaction("games", "readwrite");
55 if (callback)
56 {
57 transaction.oncomplete = function() {
58 callback({}); //everything's fine
59 }
60 transaction.onerror = function() {
61 callback({errmsg: "addGame failed: " + transaction.error});
62 };
63 }
64 let objectStore = transaction.objectStore("games");
65 objectStore.add(game);
66 });
67 },
68
69 // TODO: also option to takeback a move ?
70 // NOTE: for live games only (all on server for corr)
809ba2aa 71 update: function(gameId, obj) //colorIdx, nextIdx, move, fen, addTime, score
967a2686
BA
72 {
73 dbOperation((db) => {
74 let objectStore = db.transaction("games", "readwrite").objectStore("games");
75 objectStore.get(gameId).onsuccess = function(event) {
76 const game = event.target.result;
77 if (!!obj.move)
78 {
79 game.moves.push(obj.move);
80 game.fen = obj.fen;
66d03f23 81 game.clocks[obj.colorIdx] += obj.addTime;
809ba2aa 82 game.initime[obj.nextIdx] = Date.now();
967a2686 83 }
967a2686
BA
84 if (!!obj.score)
85 game.score = obj.score;
86 objectStore.put(game); //save updated data
87 }
88 });
89 },
90
fd7aea36
BA
91 // Retrieve all local games (running, completed, imported...)
92 getAll: function(callback)
967a2686
BA
93 {
94 dbOperation((db) => {
95 let objectStore = db.transaction('games').objectStore('games');
fd7aea36
BA
96 let games = [];
97 objectStore.openCursor().onsuccess = function(event) {
98 let cursor = event.target.result;
99 // if there is still another cursor to go, keep running this code
100 if (cursor)
101 {
102 games.push(cursor.value);
103 cursor.continue();
967a2686 104 }
fd7aea36
BA
105 else
106 callback(games);
967a2686 107 }
fd7aea36
BA
108 });
109 },
110
111 // Retrieve any game from its identifiers (locally or on server)
112 // NOTE: need callback because result is obtained asynchronously
113 get: function(gameId, callback)
114 {
115 // corr games identifiers are integers
116 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
117 {
118 ajax("/games", "GET", {gid:gameId}, res => {
119 callback(res.game);
120 });
121 }
122 else //local game
123 {
124 dbOperation((db) => {
125 let objectStore = db.transaction('games').objectStore('games');
967a2686
BA
126 objectStore.get(gameId).onsuccess = function(event) {
127 callback(event.target.result);
128 }
fd7aea36
BA
129 });
130 }
967a2686
BA
131 },
132
42c15a75
BA
133 getCurrent: function(callback)
134 {
135 dbOperation((db) => {
136 let objectStore = db.transaction('games').objectStore('games');
137 objectStore.get("*").onsuccess = function(event) {
138 callback(event.target.result);
139 };
140 });
141 },
142
967a2686
BA
143 // Delete a game in indexedDB
144 remove: function(gameId, callback)
145 {
146 dbOperation((db) => {
147 let transaction = db.transaction(["games"], "readwrite");
148 if (callback)
149 {
150 transaction.oncomplete = function() {
151 callback({}); //everything's fine
152 }
153 transaction.onerror = function() {
154 callback({errmsg: "removeGame failed: " + transaction.error});
155 };
156 }
157 transaction.objectStore("games").delete(gameId);
158 });
159 },
160};