Move some bits of code from Hall to storage; finish game init function
[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
6d01bb17
BA
5import { extractTime } from "@/utils/timeControl";
6import { shuffle } from "@/utils/alea";
ce87ac6a 7
d2634386 8export const GameStorage =
59d58d7d 9{
6d01bb17 10 init: function(o)
d2634386 11 {
6d01bb17
BA
12 localStorage.setItem("gameId", o.gameId);
13 localStorage.setItem("vname", o.vname);
14 localStorage.setItem("mycolor", mycolor); //TODO: shuffle ["w","b"]
15 localStorage.setItem("fenStart", o.fenStart);
16 localStorage.setItem("fen", o.fenStart);
17 localStorage.setItem("moves", JSON.stringify([]));
18 // Shuffle players order (white then black then other colors).
19 localStorage.setItem("players", JSON.stringify(shuffle(players));
20 // Extract times (in [milli]seconds), set clocks, store in localStorage
21 const tc = extractTime(o.timeControl);
22 localStorage.setItem("timeControl", o.timeControl);
23 localStorage.setItem("clocks", JSON.stringify(
24 [...Array(o.players.length)].fill(tc.mainTime));
25 localStorage.setItem("increment", tc.increment;
26 localStorage.setItem("started", JSON.stringify(
27 [...Array(o.players.length)].fill(false));
28 localStorage.setItem("score", "*");
29 localStorage.setItem("started", JSON.stringify(
30 [...Array(o.players.length)].fill(false)));
31 localStorage.setItem("clocks", JSON.stringify(
32 [...Array(o.players.length)].fill(0)));
33 localStorage.SetItem("mode", "live"); //function for live games only
d2634386 34 },
59d58d7d 35
d2634386 36 // TODO: also option to takeback a move ?
6d01bb17 37 update: function(move) //TODO: take game ID also, and update according to mode?
d2634386
BA
38 {
39 let moves = JSON.parse(localStorage.getItem("moves"));
40 moves.push(move);
41 localStorage.setItem("moves", JSON.stringify(moves));
42 },
59d58d7d 43
d2634386
BA
44 // "computer mode" clearing is done through the menu
45 clear: function()
46 {
47 // TODO: refresh, and implement "transfert" function (to indexedDB)
48 delete localStorage["myid"];
49 delete localStorage["oppid"];
50 delete localStorage["gameId"];
51 delete localStorage["variant"];
52 delete localStorage["mycolor"];
53 delete localStorage["fenStart"];
54 delete localStorage["moves"];
55 },
59d58d7d 56
6d01bb17 57 // TODO: game or gameInfo ?! --> when moves are played, it's a game, otherwise info
d2634386
BA
58 get: function(gameRef)
59 {
60 const gid = gameRef.id;
61 const rid = gameRef.rid; //may be blank
62 let game = {};
63 if (localStorage.getItem("gameId") === gid)
64 {
65 // Retrieve running game from localStorage
66 game.score = localStorage.getItem("score");
67 game.mycolor = localStorage.getItem("mycolor");
68 game.fenStart = localStorage.getItem("fenStart");
69 game.fen = localStorage.getItem("fen");
70 game.moves = JSON.parse(localStorage.getItem("moves"));
71 game.players = JSON.parse(localStorage.getItem("players"));
72 game.started = JSON.parse(localStorage.getItem("started"));
73 game.clocks = JSON.parse(localStorage.getItem("clocks"));
74 game.timeControl = localStorage.getItem("timeControl");
75 game.increment = localStorage.getItem("increment");
4fe5664d 76 game.vname = localStorage.getItem("vname");
d2634386
BA
77 game.mode = "live";
78 }
79 else
80 {
81 // Find the game in indexedDB, on server or remotely: TODO
82 }
83 return game;
84 },
85};