1 // TODO: general methods to access/retrieve from storage, to be generalized
2 // https://developer.mozilla.org/fr/docs/Web/API/API_IndexedDB
5 import { extractTime
} from "@/utils/timeControl";
6 import { shuffle
} from "@/utils/alea";
8 export const GameStorage
=
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
36 // TODO: also option to takeback a move ?
37 update: function(move) //TODO: take game ID also, and update according to mode?
39 let moves
= JSON
.parse(localStorage
.getItem("moves"));
41 localStorage
.setItem("moves", JSON
.stringify(moves
));
44 // "computer mode" clearing is done through the menu
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"];
57 // TODO: game or gameInfo ?! --> when moves are played, it's a game, otherwise info
58 get: function(gameRef
)
60 const gid
= gameRef
.id
;
61 const rid
= gameRef
.rid
; //may be blank
63 if (localStorage
.getItem("gameId") === gid
)
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");
76 game
.vname
= localStorage
.getItem("vname");
81 // Find the game in indexedDB, on server or remotely: TODO