Commit | Line | Data |
---|---|---|
98b94cc3 BA |
1 | // (Comp)Game object: { |
2 | // // Static informations: | |
3 | // vname: string (this is the ID) | |
4 | // fenStart: string, | |
5 | // mycolor: "w" or "b" | |
6 | // // Game (dynamic) state: | |
7 | // fen: string, | |
8 | // moves: array of Move objects, | |
9 | // } | |
10 | ||
11 | import { store } from "@/store"; | |
12 | ||
13 | function dbOperation(callback) { | |
14 | let db = null; | |
15 | let DBOpenRequest = window.indexedDB.open("vchess_comp", 4); | |
16 | ||
17 | DBOpenRequest.onerror = function(event) { | |
18 | alert(store.state.tr["Database error: stop private browsing, or update your browser"]); | |
934f7f70 | 19 | callback("error", null); |
98b94cc3 BA |
20 | }; |
21 | ||
22 | DBOpenRequest.onsuccess = function(event) { | |
23 | db = DBOpenRequest.result; | |
934f7f70 | 24 | callback(null, db); |
98b94cc3 BA |
25 | db.close(); |
26 | }; | |
27 | ||
28 | DBOpenRequest.onupgradeneeded = function(event) { | |
29 | let db = event.target.result; | |
bee36510 BA |
30 | let upgradeTransaction = event.target.transaction; |
31 | if (!db.objectStoreNames.contains("compgames")) | |
32 | db.createObjectStore("compgames", { keyPath: "vname" }); | |
33 | else | |
34 | upgradeTransaction.objectStore("compgames"); | |
98b94cc3 BA |
35 | }; |
36 | } | |
37 | ||
38 | export const CompgameStorage = { | |
39 | add: function(game) { | |
40 | dbOperation((err,db) => { | |
934f7f70 BA |
41 | if (err) return; |
42 | let objectStore = db | |
43 | .transaction("compgames", "readwrite") | |
44 | .objectStore("compgames"); | |
98b94cc3 BA |
45 | objectStore.add(game); |
46 | }); | |
47 | }, | |
48 | ||
49 | // obj: move and/or fen | |
50 | update: function(gameId, obj) { | |
51 | dbOperation((err,db) => { | |
52 | let objectStore = db | |
53 | .transaction("compgames", "readwrite") | |
54 | .objectStore("compgames"); | |
55 | objectStore.get(gameId).onsuccess = function(event) { | |
56 | // Ignoring error silently: shouldn't happen now. TODO? | |
57 | if (event.target.result) { | |
58 | const game = event.target.result; | |
59 | Object.keys(obj).forEach(k => { | |
60 | if (k == "move") game.moves.push(obj[k]); | |
61 | else game[k] = obj[k]; | |
62 | }); | |
63 | objectStore.put(game); //save updated data | |
64 | } | |
65 | }; | |
66 | }); | |
67 | }, | |
68 | ||
69 | // Retrieve any game from its identifier (variant name) | |
70 | // NOTE: need callback because result is obtained asynchronously | |
71 | get: function(gameId, callback) { | |
72 | dbOperation((err,db) => { | |
934f7f70 BA |
73 | let objectStore = db |
74 | .transaction("compgames", "readonly") | |
75 | .objectStore("compgames"); | |
98b94cc3 BA |
76 | objectStore.get(gameId).onsuccess = function(event) { |
77 | callback(event.target.result); | |
78 | }; | |
79 | }); | |
80 | }, | |
81 | ||
82 | // Delete a game in indexedDB | |
83 | remove: function(gameId) { | |
84 | dbOperation((err,db) => { | |
85 | if (!err) { | |
934f7f70 BA |
86 | db.transaction("compgames", "readwrite") |
87 | .objectStore("compgames") | |
88 | .delete(gameId); | |
98b94cc3 BA |
89 | } |
90 | }); | |
91 | } | |
92 | }; |