| 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 { storageState } from "@/store"; |
| 6 | |
| 7 | export const GameStorage = |
| 8 | { |
| 9 | init: function(myid, oppid, gameId, variant, mycolor, fenStart, mode) |
| 10 | { |
| 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", []); |
| 17 | |
| 18 | |
| 19 | |
| 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"); |
| 31 | game.mode = "live"; |
| 32 | }, |
| 33 | |
| 34 | // TODO: also option to takeback a move ? |
| 35 | update: function(move) |
| 36 | { |
| 37 | let moves = JSON.parse(localStorage.getItem("moves")); |
| 38 | moves.push(move); |
| 39 | localStorage.setItem("moves", JSON.stringify(moves)); |
| 40 | }, |
| 41 | |
| 42 | // "computer mode" clearing is done through the menu |
| 43 | clear: function() |
| 44 | { |
| 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"]; |
| 53 | }, |
| 54 | |
| 55 | // TODO: game or gameInfo ?! |
| 56 | get: function(gameRef) |
| 57 | { |
| 58 | const gid = gameRef.id; |
| 59 | const rid = gameRef.rid; //may be blank |
| 60 | let game = {}; |
| 61 | if (localStorage.getItem("gameId") === gid) |
| 62 | { |
| 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"); |
| 75 | game.mode = "live"; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | // Find the game in indexedDB, on server or remotely: TODO |
| 80 | } |
| 81 | return game; |
| 82 | }, |
| 83 | }; |