TODO: finish draw offer logic + fix inCheck bug (no highlight)
[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 19import { ajax } from "@/utils/ajax";
602d6bef 20import { store } from "@/store";
fd7aea36 21
967a2686
BA
22function dbOperation(callback)
23{
24 let db = null;
25 let DBOpenRequest = window.indexedDB.open("vchess", 4);
26
27 DBOpenRequest.onerror = function(event) {
602d6bef 28 alert(store.state.tr["Database error:"] + " " + event.target.errorCode);
967a2686
BA
29 };
30
31 DBOpenRequest.onsuccess = function(event) {
32 db = DBOpenRequest.result;
33 callback(db);
34 db.close();
35 };
36
37 DBOpenRequest.onupgradeneeded = function(event) {
38 let db = event.target.result;
39 db.onerror = function(event) {
602d6bef 40 alert(store.state.tr["Error while loading database:"] + " " + event.target.errorCode);
967a2686
BA
41 };
42 // Create objectStore for vchess->games
11667c79 43 let objectStore = db.createObjectStore("games", { keyPath: "id" });
42c15a75 44 objectStore.createIndex("score", "score"); //to search by game result
967a2686
BA
45 }
46}
47
48export const GameStorage =
49{
50 // Optional callback to get error status
c0b27606 51 // TODO: this func called from Hall seems to not work now...
967a2686
BA
52 add: function(game, callback)
53 {
54 dbOperation((db) => {
55 let transaction = db.transaction("games", "readwrite");
56 if (callback)
57 {
58 transaction.oncomplete = function() {
59 callback({}); //everything's fine
60 }
61 transaction.onerror = function() {
602d6bef 62 callback({errmsg: store.state.tr["Game retrieval failed:"] + " " + transaction.error});
967a2686
BA
63 };
64 }
65 let objectStore = transaction.objectStore("games");
66 objectStore.add(game);
67 });
68 },
69
70 // TODO: also option to takeback a move ?
dcd68c41
BA
71 // obj: chat, move, fen, clocks, score[Msg], initime, ...
72 update: function(gameId, obj)
967a2686 73 {
3d55deea
BA
74 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
75 {
76 // corr: only move, fen and score
77 ajax(
78 "/games",
79 "PUT",
80 {
81 gid: gameId,
82 newObj:
83 {
dfeb96ea 84 // Some fields may be undefined:
63ca2b89 85 chat: obj.chat,
dfeb96ea 86 move: obj.move,
3d55deea
BA
87 fen: obj.fen,
88 score: obj.score,
dcd68c41 89 scoreMsg: obj.scoreMsg,
b7cbbda1 90 drawOffer: obj.drawOffer,
3d55deea
BA
91 }
92 }
93 );
94 }
95 else
96 {
97 // live
98 dbOperation((db) => {
99 let objectStore = db.transaction("games", "readwrite").objectStore("games");
100 objectStore.get(gameId).onsuccess = function(event) {
101 const game = event.target.result;
102 Object.keys(obj).forEach(k => {
103 if (k == "move")
104 game.moves.push(obj[k]);
105 else
106 game[k] = obj[k];
107 });
108 objectStore.put(game); //save updated data
109 }
110 });
111 }
967a2686
BA
112 },
113
fd7aea36
BA
114 // Retrieve all local games (running, completed, imported...)
115 getAll: function(callback)
967a2686
BA
116 {
117 dbOperation((db) => {
118 let objectStore = db.transaction('games').objectStore('games');
fd7aea36
BA
119 let games = [];
120 objectStore.openCursor().onsuccess = function(event) {
121 let cursor = event.target.result;
122 // if there is still another cursor to go, keep running this code
123 if (cursor)
124 {
125 games.push(cursor.value);
126 cursor.continue();
967a2686 127 }
fd7aea36
BA
128 else
129 callback(games);
967a2686 130 }
fd7aea36
BA
131 });
132 },
133
134 // Retrieve any game from its identifiers (locally or on server)
135 // NOTE: need callback because result is obtained asynchronously
136 get: function(gameId, callback)
137 {
138 // corr games identifiers are integers
139 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
140 {
141 ajax("/games", "GET", {gid:gameId}, res => {
92b82def
BA
142 let game = res.game;
143 game.moves.forEach(m => {
144 m.squares = JSON.parse(m.squares);
145 });
146 callback(game);
fd7aea36
BA
147 });
148 }
149 else //local game
150 {
151 dbOperation((db) => {
152 let objectStore = db.transaction('games').objectStore('games');
967a2686
BA
153 objectStore.get(gameId).onsuccess = function(event) {
154 callback(event.target.result);
155 }
fd7aea36
BA
156 });
157 }
967a2686
BA
158 },
159
42c15a75
BA
160 getCurrent: function(callback)
161 {
162 dbOperation((db) => {
163 let objectStore = db.transaction('games').objectStore('games');
164 objectStore.get("*").onsuccess = function(event) {
165 callback(event.target.result);
166 };
167 });
168 },
169
967a2686
BA
170 // Delete a game in indexedDB
171 remove: function(gameId, callback)
172 {
173 dbOperation((db) => {
174 let transaction = db.transaction(["games"], "readwrite");
175 if (callback)
176 {
177 transaction.oncomplete = function() {
178 callback({}); //everything's fine
179 }
180 transaction.onerror = function() {
602d6bef 181 callback({errmsg: store.state.tr["Game removal failed:"] + " " + transaction.error});
967a2686
BA
182 };
183 }
184 transaction.objectStore("games").delete(gameId);
185 });
186 },
187};