TODO: game storage init + get
[vchess.git] / client / src / utils / storage.js
CommitLineData
59d58d7d
BA
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
ce87ac6a
BA
5import { storageState } from "@/store";
6
d2634386 7export const GameStorage =
59d58d7d 8{
ce87ac6a 9 init: function(myid, oppid, gameId, variant, mycolor, fenStart, mode)
d2634386
BA
10 {
11 localStorage.setItem("myid", myid);
12 localStorage.setItem("gameId", gameId);
13 localStorage.setItem("vname", variant);
14 localStorage.setItem("mycolor", mycolor);
15 localStorage.setItem("fenStart", fenStart);
16 localStorage.setItem("moves", []);
ce87ac6a
BA
17
18
19
20 game.score = localStorage.getItem("score");
21 game.mycolor = localStorage.getItem("mycolor");
22 game.fenStart = localStorage.getItem("fenStart");
23 game.fen = localStorage.getItem("fen");
24 game.moves = JSON.parse(localStorage.getItem("moves"));
25 game.players = JSON.parse(localStorage.getItem("players"));
26 game.started = JSON.parse(localStorage.getItem("started"));
27 game.clocks = JSON.parse(localStorage.getItem("clocks"));
28 game.timeControl = localStorage.getItem("timeControl");
29 game.increment = localStorage.getItem("increment");
30 game.vname = localStorage.getItem("vname");
31 game.mode = "live";
d2634386 32 },
59d58d7d 33
d2634386
BA
34 // TODO: also option to takeback a move ?
35 update: function(move)
36 {
37 let moves = JSON.parse(localStorage.getItem("moves"));
38 moves.push(move);
39 localStorage.setItem("moves", JSON.stringify(moves));
40 },
59d58d7d 41
d2634386
BA
42 // "computer mode" clearing is done through the menu
43 clear: function()
44 {
45 // TODO: refresh, and implement "transfert" function (to indexedDB)
46 delete localStorage["myid"];
47 delete localStorage["oppid"];
48 delete localStorage["gameId"];
49 delete localStorage["variant"];
50 delete localStorage["mycolor"];
51 delete localStorage["fenStart"];
52 delete localStorage["moves"];
53 },
59d58d7d 54
4fe5664d 55 // TODO: game or gameInfo ?!
d2634386
BA
56 get: function(gameRef)
57 {
58 const gid = gameRef.id;
59 const rid = gameRef.rid; //may be blank
60 let game = {};
61 if (localStorage.getItem("gameId") === gid)
62 {
63 // Retrieve running game from localStorage
64 game.score = localStorage.getItem("score");
65 game.mycolor = localStorage.getItem("mycolor");
66 game.fenStart = localStorage.getItem("fenStart");
67 game.fen = localStorage.getItem("fen");
68 game.moves = JSON.parse(localStorage.getItem("moves"));
69 game.players = JSON.parse(localStorage.getItem("players"));
70 game.started = JSON.parse(localStorage.getItem("started"));
71 game.clocks = JSON.parse(localStorage.getItem("clocks"));
72 game.timeControl = localStorage.getItem("timeControl");
73 game.increment = localStorage.getItem("increment");
4fe5664d 74 game.vname = localStorage.getItem("vname");
d2634386
BA
75 game.mode = "live";
76 }
77 else
78 {
79 // Find the game in indexedDB, on server or remotely: TODO
80 }
81 return game;
82 },
83};