Finish corr implementation for draw offers (untested)
[vchess.git] / client / src / utils / gameStorage.js
CommitLineData
967a2686
BA
1// Game object: {
2// // Static informations:
11667c79 3// id: string
967a2686
BA
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
11667c79 42 let objectStore = db.createObjectStore("games", { keyPath: "id" });
42c15a75 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 ?
22efa391 70 update: function(gameId, obj) //move, fen, clocks, score, initime, ...
967a2686 71 {
3d55deea
BA
72 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
73 {
74 // corr: only move, fen and score
75 ajax(
76 "/games",
77 "PUT",
78 {
79 gid: gameId,
80 newObj:
81 {
f41ce580 82 move: obj.move, //may be undefined...
3d55deea
BA
83 fen: obj.fen,
84 score: obj.score,
b7cbbda1 85 drawOffer: obj.drawOffer,
3d55deea
BA
86 }
87 }
88 );
89 }
90 else
91 {
92 // live
93 dbOperation((db) => {
94 let objectStore = db.transaction("games", "readwrite").objectStore("games");
95 objectStore.get(gameId).onsuccess = function(event) {
96 const game = event.target.result;
97 Object.keys(obj).forEach(k => {
98 if (k == "move")
99 game.moves.push(obj[k]);
100 else
101 game[k] = obj[k];
102 });
103 objectStore.put(game); //save updated data
104 }
105 });
106 }
967a2686
BA
107 },
108
fd7aea36
BA
109 // Retrieve all local games (running, completed, imported...)
110 getAll: function(callback)
967a2686
BA
111 {
112 dbOperation((db) => {
113 let objectStore = db.transaction('games').objectStore('games');
fd7aea36
BA
114 let games = [];
115 objectStore.openCursor().onsuccess = function(event) {
116 let cursor = event.target.result;
117 // if there is still another cursor to go, keep running this code
118 if (cursor)
119 {
120 games.push(cursor.value);
121 cursor.continue();
967a2686 122 }
fd7aea36
BA
123 else
124 callback(games);
967a2686 125 }
fd7aea36
BA
126 });
127 },
128
129 // Retrieve any game from its identifiers (locally or on server)
130 // NOTE: need callback because result is obtained asynchronously
131 get: function(gameId, callback)
132 {
133 // corr games identifiers are integers
134 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
135 {
136 ajax("/games", "GET", {gid:gameId}, res => {
92b82def
BA
137 let game = res.game;
138 game.moves.forEach(m => {
139 m.squares = JSON.parse(m.squares);
140 });
141 callback(game);
fd7aea36
BA
142 });
143 }
144 else //local game
145 {
146 dbOperation((db) => {
147 let objectStore = db.transaction('games').objectStore('games');
967a2686
BA
148 objectStore.get(gameId).onsuccess = function(event) {
149 callback(event.target.result);
150 }
fd7aea36
BA
151 });
152 }
967a2686
BA
153 },
154
42c15a75
BA
155 getCurrent: function(callback)
156 {
157 dbOperation((db) => {
158 let objectStore = db.transaction('games').objectStore('games');
159 objectStore.get("*").onsuccess = function(event) {
160 callback(event.target.result);
161 };
162 });
163 },
164
967a2686
BA
165 // Delete a game in indexedDB
166 remove: function(gameId, callback)
167 {
168 dbOperation((db) => {
169 let transaction = db.transaction(["games"], "readwrite");
170 if (callback)
171 {
172 transaction.oncomplete = function() {
173 callback({}); //everything's fine
174 }
175 transaction.onerror = function() {
176 callback({errmsg: "removeGame failed: " + transaction.error});
177 };
178 }
179 transaction.objectStore("games").delete(gameId);
180 });
181 },
182};