ab459662d15379040da9f974f3500cc31f76d231
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 { storageState
} from "@/store";
7 export const GameStorage
=
9 init: function(myid
, oppid
, gameId
, variant
, mycolor
, fenStart
, mode
)
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", []);
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");
34 // TODO: also option to takeback a move ?
35 update: function(move)
37 let moves
= JSON
.parse(localStorage
.getItem("moves"));
39 localStorage
.setItem("moves", JSON
.stringify(moves
));
42 // "computer mode" clearing is done through the menu
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"];
55 // TODO: game or gameInfo ?!
56 get: function(gameRef
)
58 const gid
= gameRef
.id
;
59 const rid
= gameRef
.rid
; //may be blank
61 if (localStorage
.getItem("gameId") === gid
)
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");
74 game
.vname
= localStorage
.getItem("vname");
79 // Find the game in indexedDB, on server or remotely: TODO