Remove unused move.color, format moves corr --> live after retrieval
[vchess.git] / client / src / utils / gameStorage.js
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,
15 // initime: array of integers (when clock start running),
16 // score: string (several options; '*' == running),
17 // }
18
19 import { ajax } from "@/utils/ajax";
20
21 function 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
42 let objectStore = db.createObjectStore("games", { keyPath: "gameId" });
43 objectStore.createIndex("score", "score"); //to search by game result
44 }
45 }
46
47 export const GameStorage =
48 {
49 // Optional callback to get error status
50 // TODO: this func called from Hall seems to not work now...
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 update: function(gameId, obj) //move, fen, clocks, score, initime, ...
71 {
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 {
82 move: obj.move, //may be undefined...
83 fen: obj.fen,
84 score: obj.score,
85 }
86 }
87 );
88 }
89 else
90 {
91 // live
92 dbOperation((db) => {
93 let objectStore = db.transaction("games", "readwrite").objectStore("games");
94 objectStore.get(gameId).onsuccess = function(event) {
95 const game = event.target.result;
96 Object.keys(obj).forEach(k => {
97 if (k == "move")
98 game.moves.push(obj[k]);
99 else
100 game[k] = obj[k];
101 });
102 objectStore.put(game); //save updated data
103 }
104 });
105 }
106 },
107
108 // Retrieve all local games (running, completed, imported...)
109 getAll: function(callback)
110 {
111 dbOperation((db) => {
112 let objectStore = db.transaction('games').objectStore('games');
113 let games = [];
114 objectStore.openCursor().onsuccess = function(event) {
115 let cursor = event.target.result;
116 // if there is still another cursor to go, keep running this code
117 if (cursor)
118 {
119 games.push(cursor.value);
120 cursor.continue();
121 }
122 else
123 callback(games);
124 }
125 });
126 },
127
128 // Retrieve any game from its identifiers (locally or on server)
129 // NOTE: need callback because result is obtained asynchronously
130 get: function(gameId, callback)
131 {
132 // corr games identifiers are integers
133 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
134 {
135 ajax("/games", "GET", {gid:gameId}, res => {
136 let game = res.game;
137 game.moves.forEach(m => {
138 m.squares = JSON.parse(m.squares);
139 });
140 callback(game);
141 });
142 }
143 else //local game
144 {
145 dbOperation((db) => {
146 let objectStore = db.transaction('games').objectStore('games');
147 objectStore.get(gameId).onsuccess = function(event) {
148 callback(event.target.result);
149 }
150 });
151 }
152 },
153
154 getCurrent: function(callback)
155 {
156 dbOperation((db) => {
157 let objectStore = db.transaction('games').objectStore('games');
158 objectStore.get("*").onsuccess = function(event) {
159 callback(event.target.result);
160 };
161 });
162 },
163
164 // Delete a game in indexedDB
165 remove: function(gameId, callback)
166 {
167 dbOperation((db) => {
168 let transaction = db.transaction(["games"], "readwrite");
169 if (callback)
170 {
171 transaction.oncomplete = function() {
172 callback({}); //everything's fine
173 }
174 transaction.onerror = function() {
175 callback({errmsg: "removeGame failed: " + transaction.error});
176 };
177 }
178 transaction.objectStore("games").delete(gameId);
179 });
180 },
181 };