8a7c52684813f35d11dfdcfa40aded8ee034aee4
1 import { extractTime
} from "@/utils/timeControl";
3 // TODO: show game structure
5 // { gameId: "", players: [], timeControl: "", clocks: [] }
8 function dbOperation(callback
)
11 let DBOpenRequest
= window
.indexedDB
.open("vchess", 4);
13 DBOpenRequest
.onerror = function(event
) {
14 alert("Database error: " + event
.target
.errorCode
);
17 DBOpenRequest
.onsuccess = function(event
) {
18 db
= DBOpenRequest
.result
;
23 DBOpenRequest
.onupgradeneeded = function(event
) {
24 let db
= event
.target
.result
;
25 db
.onerror = function(event
) {
26 alert("Error while loading database: " + event
.target
.errorCode
);
28 // Create objectStore for vchess->games
29 db
.createObjectStore("games", { keyPath: "gameId" });
33 // Optional callback to get error status
34 function addGame(game
, callback
)
37 let transaction
= db
.transaction(["games"], "readwrite");
40 transaction
.oncomplete = function() {
41 callback({}); //everything's fine
43 transaction
.onerror = function() {
44 callback({errmsg: "addGame failed: " + transaction
.error
});
47 let objectStore
= transaction
.objectStore("games");
48 objectStore
.add(game
);
52 // Clear current live game from localStorage
54 localStorage
.deleteItem("gameInfo");
55 localStorage
.deleteItem("gameState");
61 return Object
.assign({},
62 JSON
.parse(localStorage
.getItem("gameInfo")),
63 JSON
.parse(localStorage
.getItem("gameState")));
66 // Only called internally after a score update
67 function transferToDb()
69 addGame(getCurrent(), (err
) => {
76 export const GameStorage
=
81 // Extract times (in [milli]seconds), set clocks, store in localStorage
82 const tc
= extractTime(o
.timeControl
);
84 // game infos: constant
91 timeControl: o
.timeControl
,
92 increment: tc
.increment
,
93 mode: "live", //function for live games only
96 // game state: will be updated
101 clocks: [...Array(o
.players
.length
)].fill(tc
.mainTime
),
102 started: [...Array(o
.players
.length
)].fill(false),
106 localStorage
.setItem("gameInfo", JSON
.stringify(gameInfo
));
107 localStorage
.setItem("gameState", JSON
.stringify(gameState
));
111 // TODO: also option to takeback a move ? Is fen included in move ?
112 // NOTE: for live games only (all on server for corr)
113 update: function(fen
, move, clocks
, started
, score
)
115 let gameState
= JSON
.parse(localStorage
.getItem("gameState"));
118 gameState
.moves
.push(move);
120 gameState
.clocks
= clocks
;
123 gameState
.started
= started
;
125 gameState
.score
= score
;
126 localStorage
.setItem("gameState", JSON
.stringify(gameState
));
127 if (!!score
&& score
!= "*")
128 transferToDb(); //game is over
132 // Since DB requests are asynchronous, require a callback using the result
133 // TODO: option for remote retrieval (third arg, or just "gameRef")
134 getLocal: function(gameId
, callback
)
137 dbOperation((db
) => {
138 // TODO: if gameId is provided, limit search to gameId (just .get(gameId). ...)
139 let objectStore
= db
.transaction('games').objectStore('games');
140 objectStore
.openCursor().onsuccess = function(event
) {
141 var cursor
= event
.target
.result
;
142 // if there is still another cursor to go, keep runing this code
145 games
.push(cursor
.value
);
154 // Delete a game in indexedDB
155 remove: function(gameId
, callback
)
157 dbOperation((db
) => {
158 let transaction
= db
.transaction(["games"], "readwrite");
161 transaction
.oncomplete = function() {
162 callback({}); //everything's fine
164 transaction
.onerror = function() {
165 callback({errmsg: "deleteGame failed: " + transaction
.error
});
168 transaction
.objectStore("games").delete(gameId
);
172 // Retrieve any live game from its identifiers (remote or not, running or not)
173 // NOTE: need callback because result might be obtained asynchronously
174 get: function(gameRef
, callback
)
176 const gid
= gameRef
.id
;
177 const rid
= gameRef
.rid
; //may be blank
180 // TODO: send request to server which forward to user sid == rid,
181 // need to listen to "remote game" event in main hall ?
182 return callback({}); //means "the game will arrive later" (TODO...)
185 const gameInfoStr
= localStorage
.getItem("gameInfo");
188 const gameInfo
= JSON
.parse(gameInfoStr
);
189 if (gameInfo
.gameId
== gid
)
191 const gameState
= JSON
.parse(localStorage
.getItem("gameState"));
192 return callback(Object
.assign({}, gameInfo
, gameState
));
196 // Game is local and not running
197 GameStorage
.getLocal(gid
, callback
);