2e243a831c101011f6269e6fa71b5be97efbff86
[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 import { extractTime } from "@/utils/timeControl";
6 import { shuffle } from "@/utils/alea";
7
8 export const GameStorage =
9 {
10 init: function(o)
11 {
12 localStorage.setItem("gameId", o.gameId);
13 localStorage.setItem("vname", o.vname);
14 // NOTE: when >= 3 players, better use an array + shuffle for mycolor
15 const mycolor = (Math.random() < 0.5 ? "w" : "b");
16 localStorage.setItem("mycolor", mycolor);
17 localStorage.setItem("fenStart", o.fenStart);
18 localStorage.setItem("fen", o.fenStart);
19 localStorage.setItem("moves", JSON.stringify([]));
20 // Shuffle players order (white then black then other colors).
21 localStorage.setItem("players", JSON.stringify(shuffle(o.players)));
22 // Extract times (in [milli]seconds), set clocks, store in localStorage
23 const tc = extractTime(o.timeControl);
24 localStorage.setItem("timeControl", o.timeControl);
25 localStorage.setItem("clocks", JSON.stringify(
26 [...Array(o.players.length)].fill(tc.mainTime)));
27 localStorage.setItem("increment", tc.increment);
28 localStorage.setItem("started", JSON.stringify(
29 [...Array(o.players.length)].fill(false)));
30 localStorage.setItem("score", "*");
31 localStorage.setItem("started", JSON.stringify(
32 [...Array(o.players.length)].fill(false)));
33 localStorage.setItem("clocks", JSON.stringify(
34 [...Array(o.players.length)].fill(0)));
35 localStorage.setItem("mode", "live"); //function for live games only
36 },
37
38 // TODO: also option to takeback a move ?
39 // NOTE: for live games only (all on server for corr)
40 update: function(move, score) //game ID is not required
41 {
42 if (!!move)
43 {
44 let moves = JSON.parse(localStorage.getItem("moves"));
45 moves.push(move);
46 localStorage.setItem("moves", JSON.stringify(moves));
47 }
48 if (!!score)
49 localStorage.setItem("score", score);
50 },
51
52 transferToDb: function()
53 {
54 // TODO: take finished game on localStorage and transfer it to indexedDB
55 },
56
57 // "computer mode" clearing is done through the menu
58 clear: function()
59 {
60 localStorage.setItem("gameId", o.gameId);
61 localStorage.setItem("vname", o.vname);
62 // NOTE: when >= 3 players, better use an array + shuffle for mycolor
63 const mycolor = (Math.random() < 0.5 ? "w" : "b");
64 localStorage.setItem("mycolor", mycolor);
65 localStorage.setItem("fenStart", o.fenStart);
66 localStorage.setItem("fen", o.fenStart);
67 localStorage.setItem("moves", JSON.stringify([]));
68 // Shuffle players order (white then black then other colors).
69 localStorage.setItem("players", JSON.stringify(shuffle(o.players)));
70 // Extract times (in [milli]seconds), set clocks, store in localStorage
71 const tc = extractTime(o.timeControl);
72 localStorage.setItem("timeControl", o.timeControl);
73 localStorage.setItem("clocks", JSON.stringify(
74 [...Array(o.players.length)].fill(tc.mainTime)));
75 localStorage.setItem("increment", tc.increment);
76 localStorage.setItem("started", JSON.stringify(
77 [...Array(o.players.length)].fill(false)));
78 localStorage.setItem("score", "*");
79 localStorage.setItem("started", JSON.stringify(
80 [...Array(o.players.length)].fill(false)));
81 localStorage.setItem("clocks", JSON.stringify(
82 [...Array(o.players.length)].fill(0)));
83 localStorage.setItem("mode", "live"); //function for live games only
84
85
86 // TODO: refresh, and implement "transfert" function (to indexedDB)
87 localStorage["myid"];
88 localStorage["oppid"];
89 delete localStorage["gameId"];
90 delete localStorage["variant"];
91 delete localStorage["mycolor"];
92 delete localStorage["fenStart"];
93 delete localStorage["moves"];
94 },
95
96 // TODO: game or gameInfo ?! --> when moves are played, it's a game, otherwise info
97 get: function(gameRef)
98 {
99 const gid = gameRef.id;
100 const rid = gameRef.rid; //may be blank
101 let game = {};
102 if (localStorage.getItem("gameId") === gid)
103 {
104 // Retrieve running game from localStorage
105 game.score = localStorage.getItem("score");
106 game.mycolor = localStorage.getItem("mycolor");
107 game.fenStart = localStorage.getItem("fenStart");
108 game.fen = localStorage.getItem("fen");
109 game.moves = JSON.parse(localStorage.getItem("moves"));
110 game.players = JSON.parse(localStorage.getItem("players"));
111 game.started = JSON.parse(localStorage.getItem("started"));
112 game.clocks = JSON.parse(localStorage.getItem("clocks"));
113 game.timeControl = localStorage.getItem("timeControl");
114 game.increment = localStorage.getItem("increment");
115 game.vname = localStorage.getItem("vname");
116 game.mode = "live";
117 }
118 else
119 {
120 // Find the game in indexedDB, on server or remotely: TODO
121 }
122 return game;
123 },
124 };