| 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 | 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 |
| 34 | }, |
| 35 | |
| 36 | // TODO: also option to takeback a move ? |
| 37 | update: function(move) //TODO: take game ID also, and update according to mode? |
| 38 | { |
| 39 | let moves = JSON.parse(localStorage.getItem("moves")); |
| 40 | moves.push(move); |
| 41 | localStorage.setItem("moves", JSON.stringify(moves)); |
| 42 | }, |
| 43 | |
| 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 | }, |
| 56 | |
| 57 | // TODO: game or gameInfo ?! --> when moves are played, it's a game, otherwise info |
| 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"); |
| 76 | game.vname = localStorage.getItem("vname"); |
| 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 | }; |