Commit | Line | Data |
---|---|---|
967a2686 BA |
1 | // Game object: { |
2 | // // Static informations: | |
11667c79 | 3 | // id: string |
967a2686 BA |
4 | // vname: string, |
5 | // fenStart: string, | |
6 | // players: array of sid+id+name, | |
71468011 | 7 | // cadence: string, |
967a2686 | 8 | // increment: integer (seconds), |
910d631b | 9 | // type: string ("live" or "corr") |
967a2686 BA |
10 | // // Game (dynamic) state: |
11 | // fen: string, | |
12 | // moves: array of Move objects, | |
13 | // clocks: array of integers, | |
809ba2aa | 14 | // initime: array of integers (when clock start running), |
967a2686 BA |
15 | // score: string (several options; '*' == running), |
16 | // } | |
17 | ||
fd7aea36 | 18 | import { ajax } from "@/utils/ajax"; |
602d6bef | 19 | import { store } from "@/store"; |
fd7aea36 | 20 | |
6808d7a1 | 21 | function dbOperation(callback) { |
967a2686 BA |
22 | let db = null; |
23 | let DBOpenRequest = window.indexedDB.open("vchess", 4); | |
24 | ||
25 | DBOpenRequest.onerror = function(event) { | |
8477e53d BA |
26 | alert(store.state.tr["Database error: stop private browsing, or update your browser"]); |
27 | callback("error",null); | |
967a2686 BA |
28 | }; |
29 | ||
6808d7a1 | 30 | DBOpenRequest.onsuccess = function() { |
967a2686 | 31 | db = DBOpenRequest.result; |
8477e53d | 32 | callback(null,db); |
967a2686 BA |
33 | db.close(); |
34 | }; | |
35 | ||
36 | DBOpenRequest.onupgradeneeded = function(event) { | |
37 | let db = event.target.result; | |
11667c79 | 38 | let objectStore = db.createObjectStore("games", { keyPath: "id" }); |
42c15a75 | 39 | objectStore.createIndex("score", "score"); //to search by game result |
6808d7a1 | 40 | }; |
967a2686 BA |
41 | } |
42 | ||
6808d7a1 | 43 | export const GameStorage = { |
967a2686 | 44 | // Optional callback to get error status |
6808d7a1 | 45 | add: function(game, callback) { |
8477e53d BA |
46 | dbOperation((err,db) => { |
47 | if (err) { | |
48 | callback("error"); | |
49 | return; | |
967a2686 | 50 | } |
8477e53d BA |
51 | let transaction = db.transaction("games", "readwrite"); |
52 | transaction.oncomplete = function() { | |
53 | callback(); //everything's fine | |
54 | }; | |
967a2686 BA |
55 | let objectStore = transaction.objectStore("games"); |
56 | objectStore.add(game); | |
57 | }); | |
58 | }, | |
59 | ||
60 | // TODO: also option to takeback a move ? | |
dcd68c41 | 61 | // obj: chat, move, fen, clocks, score[Msg], initime, ... |
6808d7a1 BA |
62 | update: function(gameId, obj) { |
63 | if (Number.isInteger(gameId) || !isNaN(parseInt(gameId))) { | |
3d55deea | 64 | // corr: only move, fen and score |
6808d7a1 BA |
65 | ajax("/games", "PUT", { |
66 | gid: gameId, | |
67 | newObj: { | |
68 | // Some fields may be undefined: | |
69 | chat: obj.chat, | |
70 | move: obj.move, | |
71 | fen: obj.fen, | |
72 | score: obj.score, | |
73 | scoreMsg: obj.scoreMsg, | |
74 | drawOffer: obj.drawOffer | |
3d55deea | 75 | } |
6808d7a1 BA |
76 | }); |
77 | } else { | |
3d55deea | 78 | // live |
8477e53d | 79 | dbOperation((err,db) => { |
6808d7a1 BA |
80 | let objectStore = db |
81 | .transaction("games", "readwrite") | |
82 | .objectStore("games"); | |
3d55deea | 83 | objectStore.get(gameId).onsuccess = function(event) { |
8477e53d BA |
84 | // Ignoring error silently: shouldn't happen now. TODO? |
85 | if (event.target.result) { | |
86 | const game = event.target.result; | |
87 | Object.keys(obj).forEach(k => { | |
88 | if (k == "move") game.moves.push(obj[k]); | |
89 | else game[k] = obj[k]; | |
90 | }); | |
91 | objectStore.put(game); //save updated data | |
92 | } | |
6808d7a1 | 93 | }; |
3d55deea BA |
94 | }); |
95 | } | |
967a2686 BA |
96 | }, |
97 | ||
fd7aea36 | 98 | // Retrieve all local games (running, completed, imported...) |
db1f1f9a BA |
99 | // light: do not retrieve moves or players or clocks (TODO: this is the only usage) |
100 | getAll: function(light, callback) { | |
8477e53d | 101 | dbOperation((err,db) => { |
6808d7a1 | 102 | let objectStore = db.transaction("games").objectStore("games"); |
fd7aea36 BA |
103 | let games = []; |
104 | objectStore.openCursor().onsuccess = function(event) { | |
105 | let cursor = event.target.result; | |
106 | // if there is still another cursor to go, keep running this code | |
6808d7a1 | 107 | if (cursor) { |
db1f1f9a BA |
108 | let g = cursor.value; |
109 | if (light) { | |
110 | g.movesCount = g.moves.length; | |
111 | delete g.moves; | |
112 | delete g.clocks; | |
113 | delete g.initime; | |
114 | delete g.players; | |
115 | } | |
116 | games.push(g); | |
fd7aea36 | 117 | cursor.continue(); |
6808d7a1 BA |
118 | } else callback(games); |
119 | }; | |
fd7aea36 BA |
120 | }); |
121 | }, | |
122 | ||
123 | // Retrieve any game from its identifiers (locally or on server) | |
124 | // NOTE: need callback because result is obtained asynchronously | |
6808d7a1 | 125 | get: function(gameId, callback) { |
fd7aea36 | 126 | // corr games identifiers are integers |
6808d7a1 BA |
127 | if (Number.isInteger(gameId) || !isNaN(parseInt(gameId))) { |
128 | ajax("/games", "GET", { gid: gameId }, res => { | |
92b82def BA |
129 | let game = res.game; |
130 | game.moves.forEach(m => { | |
131 | m.squares = JSON.parse(m.squares); | |
132 | }); | |
133 | callback(game); | |
fd7aea36 | 134 | }); |
8477e53d | 135 | } |
6808d7a1 | 136 | else { |
8477e53d BA |
137 | // Local game |
138 | dbOperation((err,db) => { | |
6808d7a1 | 139 | let objectStore = db.transaction("games").objectStore("games"); |
967a2686 | 140 | objectStore.get(gameId).onsuccess = function(event) { |
8477e53d BA |
141 | if (event.target.result) |
142 | callback(event.target.result); | |
6808d7a1 | 143 | }; |
fd7aea36 BA |
144 | }); |
145 | } | |
967a2686 BA |
146 | }, |
147 | ||
148 | // Delete a game in indexedDB | |
6808d7a1 | 149 | remove: function(gameId, callback) { |
8477e53d BA |
150 | dbOperation((err,db) => { |
151 | if (!err) { | |
152 | let transaction = db.transaction(["games"], "readwrite"); | |
967a2686 BA |
153 | transaction.oncomplete = function() { |
154 | callback({}); //everything's fine | |
6808d7a1 | 155 | }; |
8477e53d | 156 | transaction.objectStore("games").delete(gameId); |
967a2686 | 157 | } |
967a2686 | 158 | }); |
6808d7a1 | 159 | } |
967a2686 | 160 | }; |