05aaef7404fc4bb90e6ba72c0552804efb01802a
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 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: store
.state
.tr
["Game retrieval failed:"] + " " + transaction
.error
});
64 let objectStore
= transaction
.objectStore("games");
65 objectStore
.add(game
);
69 // TODO: also option to takeback a move ?
70 // obj: chat, move, fen, clocks, score[Msg], initime, ...
71 update: function(gameId
, obj
)
73 if (Number
.isInteger(gameId
) || !isNaN(parseInt(gameId
)))
75 // corr: only move, fen and score
83 // Some fields may be undefined:
88 scoreMsg: obj
.scoreMsg
,
89 drawOffer: obj
.drawOffer
,
98 let objectStore
= db
.transaction("games", "readwrite").objectStore("games");
99 objectStore
.get(gameId
).onsuccess = function(event
) {
100 const game
= event
.target
.result
;
101 Object
.keys(obj
).forEach(k
=> {
103 game
.moves
.push(obj
[k
]);
107 objectStore
.put(game
); //save updated data
113 // Retrieve all local games (running, completed, imported...)
114 getAll: function(callback
)
116 dbOperation((db
) => {
117 let objectStore
= db
.transaction('games').objectStore('games');
119 objectStore
.openCursor().onsuccess = function(event
) {
120 let cursor
= event
.target
.result
;
121 // if there is still another cursor to go, keep running this code
124 games
.push(cursor
.value
);
133 // Retrieve any game from its identifiers (locally or on server)
134 // NOTE: need callback because result is obtained asynchronously
135 get: function(gameId
, callback
)
137 // corr games identifiers are integers
138 if (Number
.isInteger(gameId
) || !isNaN(parseInt(gameId
)))
140 ajax("/games", "GET", {gid:gameId
}, res
=> {
142 game
.moves
.forEach(m
=> {
143 m
.squares
= JSON
.parse(m
.squares
);
150 dbOperation((db
) => {
151 let objectStore
= db
.transaction('games').objectStore('games');
152 objectStore
.get(gameId
).onsuccess = function(event
) {
153 callback(event
.target
.result
);
159 getCurrent: function(callback
)
161 dbOperation((db
) => {
162 let objectStore
= db
.transaction('games').objectStore('games');
163 objectStore
.get("*").onsuccess = function(event
) {
164 callback(event
.target
.result
);
169 // Delete a game in indexedDB
170 remove: function(gameId
, callback
)
172 dbOperation((db
) => {
173 let transaction
= db
.transaction(["games"], "readwrite");
176 transaction
.oncomplete = function() {
177 callback({}); //everything's fine
179 transaction
.onerror = function() {
180 callback({errmsg: store
.state
.tr
["Game removal failed:"] + " " + transaction
.error
});
183 transaction
.objectStore("games").delete(gameId
);