Fix clocks update
[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 // NOTE: for live games only (all on server for corr)
71 update: function(gameId, obj) //move, fen, clocks, score, initime, ...
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 Object.keys(obj).forEach(k => {
78 if (k == "move")
79 game.moves.push(obj[k]);
80 else
81 game[k] = obj[k];
82 });
83 objectStore.put(game); //save updated data
84 }
85 });
86 },
87
88 // Retrieve all local games (running, completed, imported...)
89 getAll: function(callback)
90 {
91 dbOperation((db) => {
92 let objectStore = db.transaction('games').objectStore('games');
93 let games = [];
94 objectStore.openCursor().onsuccess = function(event) {
95 let cursor = event.target.result;
96 // if there is still another cursor to go, keep running this code
97 if (cursor)
98 {
99 games.push(cursor.value);
100 cursor.continue();
101 }
102 else
103 callback(games);
104 }
105 });
106 },
107
108 // Retrieve any game from its identifiers (locally or on server)
109 // NOTE: need callback because result is obtained asynchronously
110 get: function(gameId, callback)
111 {
112 // corr games identifiers are integers
113 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
114 {
115 ajax("/games", "GET", {gid:gameId}, res => {
116 callback(res.game);
117 });
118 }
119 else //local game
120 {
121 dbOperation((db) => {
122 let objectStore = db.transaction('games').objectStore('games');
123 objectStore.get(gameId).onsuccess = function(event) {
124 callback(event.target.result);
125 }
126 });
127 }
128 },
129
130 getCurrent: function(callback)
131 {
132 dbOperation((db) => {
133 let objectStore = db.transaction('games').objectStore('games');
134 objectStore.get("*").onsuccess = function(event) {
135 callback(event.target.result);
136 };
137 });
138 },
139
140 // Delete a game in indexedDB
141 remove: function(gameId, callback)
142 {
143 dbOperation((db) => {
144 let transaction = db.transaction(["games"], "readwrite");
145 if (callback)
146 {
147 transaction.oncomplete = function() {
148 callback({}); //everything's fine
149 }
150 transaction.onerror = function() {
151 callback({errmsg: "removeGame failed: " + transaction.error});
152 };
153 }
154 transaction.objectStore("games").delete(gameId);
155 });
156 },
157 };