b7cf85c18a111ff044d3041053e57d6dddcf3d1d
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";
21 function dbOperation(callback
)
24 let DBOpenRequest
= window
.indexedDB
.open("vchess", 4);
26 DBOpenRequest
.onerror = function(event
) {
27 alert("Database error: " + event
.target
.errorCode
);
30 DBOpenRequest
.onsuccess = function(event
) {
31 db
= DBOpenRequest
.result
;
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
);
41 // Create objectStore for vchess->games
42 let objectStore
= db
.createObjectStore("games", { keyPath: "id" });
43 objectStore
.createIndex("score", "score"); //to search by game result
47 export const GameStorage
=
49 // Optional callback to get error status
50 // TODO: this func called from Hall seems to not work now...
51 add: function(game
, callback
)
54 let transaction
= db
.transaction("games", "readwrite");
57 transaction
.oncomplete = function() {
58 callback({}); //everything's fine
60 transaction
.onerror = function() {
61 callback({errmsg: "addGame failed: " + transaction
.error
});
64 let objectStore
= transaction
.objectStore("games");
65 objectStore
.add(game
);
69 // TODO: also option to takeback a move ?
70 update: function(gameId
, obj
) //move, fen, clocks, score, initime, ...
72 if (Number
.isInteger(gameId
) || !isNaN(parseInt(gameId
)))
74 // corr: only move, fen and score
82 move: obj
.move, //may be undefined...
86 drawOffer: obj
.drawOffer
,
95 let objectStore
= db
.transaction("games", "readwrite").objectStore("games");
96 objectStore
.get(gameId
).onsuccess = function(event
) {
97 const game
= event
.target
.result
;
98 Object
.keys(obj
).forEach(k
=> {
100 game
.moves
.push(obj
[k
]);
104 objectStore
.put(game
); //save updated data
110 // Retrieve all local games (running, completed, imported...)
111 getAll: function(callback
)
113 dbOperation((db
) => {
114 let objectStore
= db
.transaction('games').objectStore('games');
116 objectStore
.openCursor().onsuccess = function(event
) {
117 let cursor
= event
.target
.result
;
118 // if there is still another cursor to go, keep running this code
121 games
.push(cursor
.value
);
130 // Retrieve any game from its identifiers (locally or on server)
131 // NOTE: need callback because result is obtained asynchronously
132 get: function(gameId
, callback
)
134 // corr games identifiers are integers
135 if (Number
.isInteger(gameId
) || !isNaN(parseInt(gameId
)))
137 ajax("/games", "GET", {gid:gameId
}, res
=> {
139 game
.moves
.forEach(m
=> {
140 m
.squares
= JSON
.parse(m
.squares
);
147 dbOperation((db
) => {
148 let objectStore
= db
.transaction('games').objectStore('games');
149 objectStore
.get(gameId
).onsuccess = function(event
) {
150 callback(event
.target
.result
);
156 getCurrent: function(callback
)
158 dbOperation((db
) => {
159 let objectStore
= db
.transaction('games').objectStore('games');
160 objectStore
.get("*").onsuccess = function(event
) {
161 callback(event
.target
.result
);
166 // Delete a game in indexedDB
167 remove: function(gameId
, callback
)
169 dbOperation((db
) => {
170 let transaction
= db
.transaction(["games"], "readwrite");
173 transaction
.oncomplete = function() {
174 callback({}); //everything's fine
176 transaction
.onerror = function() {
177 callback({errmsg: "removeGame failed: " + transaction
.error
});
180 transaction
.objectStore("games").delete(gameId
);