1103a42dbf4caae7c4d1402aad1cff333d8bd321
[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 function setStorage(myid, oppid, gameId, variant, mycolor, fenStart)
6 {
7 localStorage.setItem("myid", myid);
8 localStorage.setItem("oppid", oppid);
9 localStorage.setItem("gameId", gameId);
10 localStorage.setItem("variant", variant);
11 localStorage.setItem("mycolor", mycolor);
12 localStorage.setItem("fenStart", fenStart);
13 localStorage.setItem("moves", []);
14 }
15
16 function updateStorage(move)
17 {
18 let moves = JSON.parse(localStorage.getItem("moves"));
19 moves.push(move);
20 localStorage.setItem("moves", JSON.stringify(moves));
21 }
22
23 // "computer mode" clearing is done through the menu
24 function clearStorage()
25 {
26 delete localStorage["myid"];
27 delete localStorage["oppid"];
28 delete localStorage["gameId"];
29 delete localStorage["variant"];
30 delete localStorage["mycolor"];
31 delete localStorage["fenStart"];
32 delete localStorage["moves"];
33 }
34
35 function getGameFromStorage(gameId)
36 {
37 const gid = this.gameRef.id;
38 const rid = this.gameRef.rid; //may be blank
39 let game = {};
40 if (localStorage.getItem("gameId") === gameId)
41 {
42 // Retrieve running game from localStorage
43 game.score = localStorage.getItem("score");
44 game.oppid = localStorage.getItem("oppid");
45 game.oppname = localStorage.getItem("oppname");
46 game.mycolor = localStorage.getItem("mycolor");
47 game.fenStart = localStorage.getItem("fenStart");
48 game.moves = localStorage.getItem("moves");
49 }
50 else
51 {
52 // Find the game in indexedDB: TODO
53 }
54 }