2 // // Static informations:
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:
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),
19 import { ajax
} from "@/utils/ajax";
20 import { store
} from "@/store";
22 function dbOperation(callback
)
25 let DBOpenRequest
= window
.indexedDB
.open("vchess", 4);
27 DBOpenRequest
.onerror = function(event
) {
28 alert(store
.state
.tr
["Database error:"] + " " + event
.target
.errorCode
);
31 DBOpenRequest
.onsuccess = function(event
) {
32 db
= DBOpenRequest
.result
;
37 DBOpenRequest
.onupgradeneeded = function(event
) {
38 let db
= event
.target
.result
;
39 db
.onerror = function(event
) {
40 alert(store
.state
.tr
["Error while loading database:"] + " " + event
.target
.errorCode
);
42 // Create objectStore for vchess->games
43 let objectStore
= db
.createObjectStore("games", { keyPath: "id" });
44 objectStore
.createIndex("score", "score"); //to search by game result
48 export const GameStorage
=
50 // Optional callback to get error status
51 // TODO: this func called from Hall seems to not work now...
52 add: function(game
, callback
)
55 let transaction
= db
.transaction("games", "readwrite");
58 transaction
.oncomplete = function() {
59 callback({}); //everything's fine
61 transaction
.onerror = function() {
62 callback({errmsg: store
.state
.tr
["Game retrieval failed:"] + " " + transaction
.error
});
65 let objectStore
= transaction
.objectStore("games");
66 objectStore
.add(game
);
70 // TODO: also option to takeback a move ?
71 // obj: chat, move, fen, clocks, score[Msg], initime, ...
72 update: function(gameId
, obj
)
74 if (Number
.isInteger(gameId
) || !isNaN(parseInt(gameId
)))
76 // corr: only move, fen and score
84 // Some fields may be undefined:
89 scoreMsg: obj
.scoreMsg
,
90 drawOffer: obj
.drawOffer
,
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
=> {
104 game
.moves
.push(obj
[k
]);
108 objectStore
.put(game
); //save updated data
114 // Retrieve all local games (running, completed, imported...)
115 getAll: function(callback
)
117 dbOperation((db
) => {
118 let objectStore
= db
.transaction('games').objectStore('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
125 games
.push(cursor
.value
);
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
)
138 // corr games identifiers are integers
139 if (Number
.isInteger(gameId
) || !isNaN(parseInt(gameId
)))
141 ajax("/games", "GET", {gid:gameId
}, res
=> {
143 game
.moves
.forEach(m
=> {
144 m
.squares
= JSON
.parse(m
.squares
);
151 dbOperation((db
) => {
152 let objectStore
= db
.transaction('games').objectStore('games');
153 objectStore
.get(gameId
).onsuccess = function(event
) {
154 callback(event
.target
.result
);
160 getCurrent: function(callback
)
162 dbOperation((db
) => {
163 let objectStore
= db
.transaction('games').objectStore('games');
164 objectStore
.get("*").onsuccess = function(event
) {
165 callback(event
.target
.result
);
170 // Delete a game in indexedDB
171 remove: function(gameId
, callback
)
173 dbOperation((db
) => {
174 let transaction
= db
.transaction(["games"], "readwrite");
177 transaction
.oncomplete = function() {
178 callback({}); //everything's fine
180 transaction
.onerror = function() {
181 callback({errmsg: store
.state
.tr
["Game removal failed:"] + " " + transaction
.error
});
184 transaction
.objectStore("games").delete(gameId
);