| 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 | getStoragePrefix: function(mode) { |
| 5 | let prefix = ""; |
| 6 | if (mode == "computer") |
| 7 | prefix = "comp-"; |
| 8 | else if (mode == "friend") |
| 9 | prefix = "anlz-"; |
| 10 | return prefix; |
| 11 | }, |
| 12 | setStorage: function() { |
| 13 | if (this.mode=="human") |
| 14 | { |
| 15 | localStorage.setItem("myid", this.myid); |
| 16 | localStorage.setItem("oppid", this.oppid); |
| 17 | localStorage.setItem("gameId", this.gameId); |
| 18 | } |
| 19 | const prefix = this.getStoragePrefix(this.mode); |
| 20 | localStorage.setItem(prefix+"variant", variant); |
| 21 | localStorage.setItem(prefix+"mycolor", this.mycolor); |
| 22 | localStorage.setItem(prefix+"fenStart", this.fenStart); |
| 23 | localStorage.setItem(prefix+"moves", JSON.stringify(this.vr.moves)); |
| 24 | localStorage.setItem(prefix+"fen", this.vr.getFen()); |
| 25 | localStorage.setItem(prefix+"score", "*"); |
| 26 | }, |
| 27 | updateStorage: function() { |
| 28 | const prefix = this.getStoragePrefix(this.mode); |
| 29 | localStorage.setItem(prefix+"moves", JSON.stringify(this.vr.moves)); |
| 30 | localStorage.setItem(prefix+"fen", this.vr.getFen()); |
| 31 | if (this.score != "*") |
| 32 | localStorage.setItem(prefix+"score", this.score); |
| 33 | }, |
| 34 | // "computer mode" clearing is done through the menu |
| 35 | clearStorage: function() { |
| 36 | if (this.mode == "human") |
| 37 | { |
| 38 | delete localStorage["myid"]; |
| 39 | delete localStorage["oppid"]; |
| 40 | delete localStorage["gameId"]; |
| 41 | } |
| 42 | const prefix = this.getStoragePrefix(this.mode); |
| 43 | delete localStorage[prefix+"variant"]; |
| 44 | delete localStorage[prefix+"mycolor"]; |
| 45 | delete localStorage[prefix+"fenStart"]; |
| 46 | delete localStorage[prefix+"moves"]; |
| 47 | delete localStorage[prefix+"fen"]; |
| 48 | delete localStorage[prefix+"score"]; |
| 49 | }, |
| 50 | clearCurrentGame: function(e) { |
| 51 | this.getRidOfTooltip(e.currentTarget); |
| 52 | this.clearStorage(); |
| 53 | location.reload(); //to see clearing effects |
| 54 | }, |
| 55 | |