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 | ||
602d6bef | 18 | import { store } from "@/store"; |
fd7aea36 | 19 | |
6808d7a1 | 20 | function dbOperation(callback) { |
967a2686 | 21 | let db = null; |
76373619 | 22 | let DBOpenRequest = window.indexedDB.open("vchess", 5); |
967a2686 BA |
23 | |
24 | DBOpenRequest.onerror = function(event) { | |
8477e53d | 25 | alert(store.state.tr["Database error: stop private browsing, or update your browser"]); |
934f7f70 | 26 | callback("error", null); |
967a2686 BA |
27 | }; |
28 | ||
6808d7a1 | 29 | DBOpenRequest.onsuccess = function() { |
967a2686 | 30 | db = DBOpenRequest.result; |
934f7f70 | 31 | callback(null, db); |
967a2686 BA |
32 | db.close(); |
33 | }; | |
34 | ||
35 | DBOpenRequest.onupgradeneeded = function(event) { | |
36 | let db = event.target.result; | |
bee36510 BA |
37 | let upgradeTransaction = event.target.transaction; |
38 | let objectStore = undefined; | |
39 | if (!db.objectStoreNames.contains("games")) | |
40 | objectStore = db.createObjectStore("games", { keyPath: "id" }); | |
41 | else | |
42 | objectStore = upgradeTransaction.objectStore("games"); | |
43 | if (!objectStore.indexNames.contains("score")) | |
44 | // To sarch games by score (useful for running games) | |
45 | objectStore.createIndex("score", "score", { unique: false }); | |
46 | if (!objectStore.indexNames.contains("created")) | |
47 | // To search by date intervals. Two games cannot start at the same time | |
48 | objectStore.createIndex("created", "created", { unique: true }); | |
6808d7a1 | 49 | }; |
967a2686 BA |
50 | } |
51 | ||
6808d7a1 | 52 | export const GameStorage = { |
967a2686 | 53 | // Optional callback to get error status |
6808d7a1 | 54 | add: function(game, callback) { |
8477e53d | 55 | dbOperation((err,db) => { |
c292ebb2 | 56 | if (!!err) { |
8477e53d BA |
57 | callback("error"); |
58 | return; | |
967a2686 | 59 | } |
8477e53d BA |
60 | let transaction = db.transaction("games", "readwrite"); |
61 | transaction.oncomplete = function() { | |
934f7f70 BA |
62 | // Everything's fine |
63 | callback(); | |
8477e53d | 64 | }; |
c292ebb2 | 65 | transaction.onerror = function(err) { |
934f7f70 BA |
66 | // Duplicate key error (most likely) |
67 | callback(err); | |
c292ebb2 | 68 | }; |
934f7f70 | 69 | transaction.objectStore("games").add(game); |
967a2686 BA |
70 | }); |
71 | }, | |
72 | ||
dcd68c41 | 73 | // obj: chat, move, fen, clocks, score[Msg], initime, ... |
6808d7a1 | 74 | update: function(gameId, obj) { |
aae89b49 BA |
75 | // live |
76 | dbOperation((err,db) => { | |
77 | let objectStore = db | |
78 | .transaction("games", "readwrite") | |
79 | .objectStore("games"); | |
80 | objectStore.get(gameId).onsuccess = function(event) { | |
81 | // Ignoring error silently: shouldn't happen now. TODO? | |
82 | if (event.target.result) { | |
83 | let game = event.target.result; | |
84 | // Hidden tabs are delayed, to prevent multi-updates: | |
85 | if (obj.moveIdx < game.moves.length) return; | |
86 | Object.keys(obj).forEach(k => { | |
87 | if (k == "move") game.moves.push(obj[k]); | |
1a021529 BA |
88 | else if (k == "chat") game.chats.push(obj[k]); |
89 | else if (k == "delchat") game.chats = []; | |
aae89b49 BA |
90 | else game[k] = obj[k]; |
91 | }); | |
92 | objectStore.put(game); //save updated data | |
3d55deea | 93 | } |
aae89b49 BA |
94 | }; |
95 | }); | |
967a2686 BA |
96 | }, |
97 | ||
934f7f70 BA |
98 | // Retrieve (all) running local games |
99 | getRunning: function(callback) { | |
8477e53d | 100 | dbOperation((err,db) => { |
934f7f70 BA |
101 | let objectStore = db |
102 | .transaction("games", "readonly") | |
103 | .objectStore("games"); | |
104 | let index = objectStore.index("score"); | |
105 | const range = IDBKeyRange.only("*"); | |
fd7aea36 | 106 | let games = []; |
934f7f70 | 107 | index.openCursor(range).onsuccess = function(event) { |
fd7aea36 | 108 | let cursor = event.target.result; |
934f7f70 BA |
109 | if (!cursor) callback(games); |
110 | else { | |
111 | // If there is still another cursor to go, keep running this code | |
db1f1f9a | 112 | let g = cursor.value; |
6b7b2cf7 BA |
113 | // Do not retrieve moves or clocks (unused in list mode) |
114 | g.movesCount = g.moves.length; | |
115 | delete g.moves; | |
116 | delete g.clocks; | |
117 | delete g.initime; | |
db1f1f9a | 118 | games.push(g); |
fd7aea36 | 119 | cursor.continue(); |
934f7f70 BA |
120 | } |
121 | }; | |
122 | }); | |
123 | }, | |
124 | ||
125 | // Retrieve completed local games | |
126 | getNext: function(upperDt, callback) { | |
127 | dbOperation((err,db) => { | |
128 | let objectStore = db | |
129 | .transaction("games", "readonly") | |
130 | .objectStore("games"); | |
131 | let index = objectStore.index("created"); | |
132 | const range = IDBKeyRange.upperBound(upperDt); | |
133 | let games = []; | |
134 | index.openCursor(range).onsuccess = function(event) { | |
135 | let cursor = event.target.result; | |
136 | if (!cursor) { | |
137 | // Most recent games first: | |
138 | games = games.sort((g1, g2) => g2.created - g1.created); | |
139 | // TODO: 20 games showed per request is arbitrary | |
140 | callback(games.slice(0, 20)); | |
141 | } | |
142 | else { | |
143 | // If there is still another cursor to go, keep running this code | |
144 | let g = cursor.value; | |
145 | if (g.score != "*") { | |
146 | // Do not retrieve moves or clocks (unused in list mode) | |
147 | g.movesCount = g.moves.length; | |
148 | delete g.moves; | |
149 | delete g.clocks; | |
150 | delete g.initime; | |
151 | games.push(g); | |
152 | } | |
153 | cursor.continue(); | |
154 | } | |
6808d7a1 | 155 | }; |
fd7aea36 BA |
156 | }); |
157 | }, | |
158 | ||
159 | // Retrieve any game from its identifiers (locally or on server) | |
160 | // NOTE: need callback because result is obtained asynchronously | |
6808d7a1 | 161 | get: function(gameId, callback) { |
aae89b49 BA |
162 | // Local game |
163 | dbOperation((err,db) => { | |
164 | let objectStore = db.transaction("games").objectStore("games"); | |
165 | objectStore.get(gameId).onsuccess = function(event) { | |
f54f4c26 BA |
166 | // event.target.result is null if game not found |
167 | callback(event.target.result); | |
aae89b49 BA |
168 | }; |
169 | }); | |
967a2686 BA |
170 | }, |
171 | ||
172 | // Delete a game in indexedDB | |
6808d7a1 | 173 | remove: function(gameId, callback) { |
8477e53d BA |
174 | dbOperation((err,db) => { |
175 | if (!err) { | |
934f7f70 | 176 | let transaction = db.transaction("games", "readwrite"); |
967a2686 | 177 | transaction.oncomplete = function() { |
3b0f26c1 | 178 | callback(); //everything's fine |
6808d7a1 | 179 | }; |
8477e53d | 180 | transaction.objectStore("games").delete(gameId); |
967a2686 | 181 | } |
967a2686 | 182 | }); |
6808d7a1 | 183 | } |
967a2686 | 184 | }; |