937d8c78e96add51502406a3f55cf59248a9273b
1 // TODO: general methods to access/retrieve from storage, to be generalized
2 // https://developer.mozilla.org/fr/docs/Web/API/API_IndexedDB
5 export const GameStorage
=
7 init: function(myid
, oppid
, gameId
, variant
, mycolor
, fenStart
)
9 localStorage
.setItem("myid", myid
);
10 localStorage
.setItem("gameId", gameId
);
11 localStorage
.setItem("vname", variant
);
12 localStorage
.setItem("mycolor", mycolor
);
13 localStorage
.setItem("fenStart", fenStart
);
14 localStorage
.setItem("moves", []);
17 // TODO: also option to takeback a move ?
18 update: function(move)
20 let moves
= JSON
.parse(localStorage
.getItem("moves"));
22 localStorage
.setItem("moves", JSON
.stringify(moves
));
25 // "computer mode" clearing is done through the menu
28 // TODO: refresh, and implement "transfert" function (to indexedDB)
29 delete localStorage
["myid"];
30 delete localStorage
["oppid"];
31 delete localStorage
["gameId"];
32 delete localStorage
["variant"];
33 delete localStorage
["mycolor"];
34 delete localStorage
["fenStart"];
35 delete localStorage
["moves"];
38 get: function(gameRef
)
40 const gid
= gameRef
.id
;
41 const rid
= gameRef
.rid
; //may be blank
43 if (localStorage
.getItem("gameId") === gid
)
45 // Retrieve running game from localStorage
46 game
.score
= localStorage
.getItem("score");
47 game
.mycolor
= localStorage
.getItem("mycolor");
48 game
.fenStart
= localStorage
.getItem("fenStart");
49 game
.fen
= localStorage
.getItem("fen");
50 game
.moves
= JSON
.parse(localStorage
.getItem("moves"));
51 game
.players
= JSON
.parse(localStorage
.getItem("players"));
52 game
.started
= JSON
.parse(localStorage
.getItem("started"));
53 game
.clocks
= JSON
.parse(localStorage
.getItem("clocks"));
54 game
.timeControl
= localStorage
.getItem("timeControl");
55 game
.increment
= localStorage
.getItem("increment");
60 // Find the game in indexedDB, on server or remotely: TODO