Start to catch up with code. Need to fnish at least live + corr games before publish
[vchess.git] / client / src / utils / storage.js
1 // TODO: general methods to access/retrieve from storage, to be generalized
2 // https://developer.mozilla.org/fr/docs/Web/API/API_IndexedDB
3 // https://dexie.org/
4
5 export const GameStorage =
6 {
7 init: function(myid, oppid, gameId, variant, mycolor, fenStart)
8 {
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", []);
15 },
16
17 // TODO: also option to takeback a move ?
18 update: function(move)
19 {
20 let moves = JSON.parse(localStorage.getItem("moves"));
21 moves.push(move);
22 localStorage.setItem("moves", JSON.stringify(moves));
23 },
24
25 // "computer mode" clearing is done through the menu
26 clear: function()
27 {
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"];
36 },
37
38 // TODO: game or gameInfo ?!
39 get: function(gameRef)
40 {
41 const gid = gameRef.id;
42 const rid = gameRef.rid; //may be blank
43 let game = {};
44 if (localStorage.getItem("gameId") === gid)
45 {
46 // Retrieve running game from localStorage
47 game.score = localStorage.getItem("score");
48 game.mycolor = localStorage.getItem("mycolor");
49 game.fenStart = localStorage.getItem("fenStart");
50 game.fen = localStorage.getItem("fen");
51 game.moves = JSON.parse(localStorage.getItem("moves"));
52 game.players = JSON.parse(localStorage.getItem("players"));
53 game.started = JSON.parse(localStorage.getItem("started"));
54 game.clocks = JSON.parse(localStorage.getItem("clocks"));
55 game.timeControl = localStorage.getItem("timeControl");
56 game.increment = localStorage.getItem("increment");
57 game.vname = localStorage.getItem("vname");
58 game.mode = "live";
59 }
60 else
61 {
62 // Find the game in indexedDB, on server or remotely: TODO
63 }
64 return game;
65 },
66 };