Commit | Line | Data |
---|---|---|
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 |
5 | import { extractTime } from "@/utils/timeControl"; |
6 | import { shuffle } from "@/utils/alea"; | |
ce87ac6a | 7 | |
d2634386 | 8 | export 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); | |
4c177b7f BA |
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); | |
6d01bb17 BA |
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). | |
4c177b7f | 21 | localStorage.setItem("players", JSON.stringify(shuffle(o.players))); |
6d01bb17 BA |
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( | |
4c177b7f BA |
26 | [...Array(o.players.length)].fill(tc.mainTime))); |
27 | localStorage.setItem("increment", tc.increment); | |
6d01bb17 | 28 | localStorage.setItem("started", JSON.stringify( |
4c177b7f | 29 | [...Array(o.players.length)].fill(false))); |
6d01bb17 BA |
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))); | |
4c177b7f | 35 | localStorage.setItem("mode", "live"); //function for live games only |
d2634386 | 36 | }, |
59d58d7d | 37 | |
d2634386 | 38 | // TODO: also option to takeback a move ? |
4c177b7f BA |
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() | |
d2634386 | 53 | { |
4c177b7f | 54 | // TODO: take finished game on localStorage and transfer it to indexedDB |
d2634386 | 55 | }, |
59d58d7d | 56 | |
d2634386 BA |
57 | // "computer mode" clearing is done through the menu |
58 | clear: function() | |
59 | { | |
4c177b7f BA |
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 | ||
d2634386 | 86 | // TODO: refresh, and implement "transfert" function (to indexedDB) |
4c177b7f BA |
87 | localStorage["myid"]; |
88 | localStorage["oppid"]; | |
d2634386 BA |
89 | delete localStorage["gameId"]; |
90 | delete localStorage["variant"]; | |
91 | delete localStorage["mycolor"]; | |
92 | delete localStorage["fenStart"]; | |
93 | delete localStorage["moves"]; | |
94 | }, | |
59d58d7d | 95 | |
6d01bb17 | 96 | // TODO: game or gameInfo ?! --> when moves are played, it's a game, otherwise info |
d2634386 BA |
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"); | |
4fe5664d | 115 | game.vname = localStorage.getItem("vname"); |
d2634386 BA |
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 | }; |