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: "gameId" });
43 objectStore
.createIndex("score", "score"); //to search by game result
47 export const GameStorage
=
49 // Optional callback to get error status
50 add: function(game
, callback
)
53 let transaction
= db
.transaction("games", "readwrite");
56 transaction
.oncomplete = function() {
57 callback({}); //everything's fine
59 transaction
.onerror = function() {
60 callback({errmsg: "addGame failed: " + transaction
.error
});
63 let objectStore
= transaction
.objectStore("games");
64 objectStore
.add(game
);
68 // TODO: also option to takeback a move ?
69 // NOTE: for live games only (all on server for corr)
70 update: function(gameId
, obj
) //colorIdx, nextIdx, move, fen, addTime, score
73 let objectStore
= db
.transaction("games", "readwrite").objectStore("games");
74 objectStore
.get(gameId
).onsuccess = function(event
) {
75 const game
= event
.target
.result
;
78 game
.moves
.push(obj
.move);
80 game
.clocks
[obj
.colorIdx
] += obj
.addTime
;
81 game
.initime
[obj
.nextIdx
] = Date
.now();
84 game
.score
= obj
.score
;
85 objectStore
.put(game
); //save updated data
90 // Retrieve all local games (running, completed, imported...)
91 getAll: function(callback
)
94 let objectStore
= db
.transaction('games').objectStore('games');
96 objectStore
.openCursor().onsuccess = function(event
) {
97 let cursor
= event
.target
.result
;
98 // if there is still another cursor to go, keep running this code
101 games
.push(cursor
.value
);
110 // Retrieve any game from its identifiers (locally or on server)
111 // NOTE: need callback because result is obtained asynchronously
112 get: function(gameId
, callback
)
114 // corr games identifiers are integers
115 if (Number
.isInteger(gameId
) || !isNaN(parseInt(gameId
)))
117 ajax("/games", "GET", {gid:gameId
}, res
=> {
123 dbOperation((db
) => {
124 let objectStore
= db
.transaction('games').objectStore('games');
125 objectStore
.get(gameId
).onsuccess = function(event
) {
126 callback(event
.target
.result
);
132 getCurrent: function(callback
)
134 dbOperation((db
) => {
135 let objectStore
= db
.transaction('games').objectStore('games');
136 objectStore
.get("*").onsuccess = function(event
) {
137 callback(event
.target
.result
);
142 // Delete a game in indexedDB
143 remove: function(gameId
, callback
)
145 dbOperation((db
) => {
146 let transaction
= db
.transaction(["games"], "readwrite");
149 transaction
.oncomplete = function() {
150 callback({}); //everything's fine
152 transaction
.onerror = function() {
153 callback({errmsg: "removeGame failed: " + transaction
.error
});
156 transaction
.objectStore("games").delete(gameId
);