'update'
[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 get: function(gameRef)
39 {
40 const gid = gameRef.id;
41 const rid = gameRef.rid; //may be blank
42 let game = {};
43 if (localStorage.getItem("gameId") === gid)
44 {
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");
56 game.mode = "live";
57 }
58 else
59 {
60 // Find the game in indexedDB, on server or remotely: TODO
61 }
62 return game;
63 },
64 };