42f438843381ee29be06bf3530662e877ecd39f5
2 // // Static informations:
6 // players: array of sid+id+name,
8 // increment: integer (seconds),
9 // type: string ("live" or "corr")
10 // // Game (dynamic) state:
12 // moves: array of Move objects,
13 // clocks: array of integers,
14 // initime: array of integers (when clock start running),
15 // score: string (several options; '*' == running),
18 import { store
} from "@/store";
20 function dbOperation(callback
) {
22 let DBOpenRequest
= window
.indexedDB
.open("vchess", 5);
24 DBOpenRequest
.onerror = function(event
) {
26 "Database error: stop private browsing, or update your browser"]);
27 callback("error", null);
30 DBOpenRequest
.onsuccess = function() {
31 db
= DBOpenRequest
.result
;
36 DBOpenRequest
.onupgradeneeded = function(event
) {
37 let db
= event
.target
.result
;
38 let upgradeTransaction
= event
.target
.transaction
;
39 let objectStore
= undefined;
40 if (!db
.objectStoreNames
.contains("games"))
41 objectStore
= db
.createObjectStore("games", { keyPath: "id" });
43 objectStore
= upgradeTransaction
.objectStore("games");
44 if (!objectStore
.indexNames
.contains("score"))
45 // To sarch games by score (useful for running games)
46 objectStore
.createIndex("score", "score", { unique: false });
47 if (!objectStore
.indexNames
.contains("created"))
48 // To search by date intervals. Two games cannot start at the same time
49 objectStore
.createIndex("created", "created", { unique: true });
53 export const GameStorage
= {
54 // Optional callback to get error status
55 add: function(game
, callback
) {
56 dbOperation((err
, db
) => {
61 let transaction
= db
.transaction("games", "readwrite");
62 transaction
.oncomplete = function() {
66 transaction
.onerror = function(err
) {
67 // Duplicate key error (most likely)
70 transaction
.objectStore("games").add(game
);
74 // obj: chat, move, fen, clocks, score[Msg], initime, ...
75 update: function(gameId
, obj
) {
77 dbOperation((err
, db
) => {
79 .transaction("games", "readwrite")
80 .objectStore("games");
81 objectStore
.get(gameId
).onsuccess = function(event
) {
82 // Ignoring error silently: shouldn't happen now. TODO?
83 if (event
.target
.result
) {
84 let game
= event
.target
.result
;
85 // Hidden tabs are delayed, to prevent multi-updates:
86 if (obj
.moveIdx
< game
.moves
.length
) return;
87 Object
.keys(obj
).forEach(k
=> {
88 if (k
== "move") game
.moves
.push(obj
[k
]);
89 else if (k
== "chat") game
.chats
.push(obj
[k
]);
90 else if (k
== "chatRead") game
.chatRead
= Date
.now();
91 else if (k
== "delchat") game
.chats
= [];
92 else if (k
== "playerName") game
.players
[obj
.idx
] = obj
.name
;
93 else game
[k
] = obj
[k
];
95 objectStore
.put(game
); //save updated data
101 // Retrieve (all) running local games
102 getRunning: function(callback
) {
103 dbOperation((err
, db
) => {
105 .transaction("games", "readonly")
106 .objectStore("games");
107 let index
= objectStore
.index("score");
108 const range
= IDBKeyRange
.only("*");
110 index
.openCursor(range
).onsuccess = function(event
) {
111 let cursor
= event
.target
.result
;
112 if (!cursor
) callback(games
);
114 // If there is still another cursor to go, keep running this code
115 let g
= cursor
.value
;
116 // Do not retrieve moves or clocks (unused in list mode)
117 g
.movesCount
= g
.moves
.length
;
128 // Retrieve completed local games
129 getNext: function(upperDt
, callback
) {
130 dbOperation((err
, db
) => {
132 .transaction("games", "readonly")
133 .objectStore("games");
134 let index
= objectStore
.index("created");
135 const range
= IDBKeyRange
.upperBound(upperDt
);
137 index
.openCursor(range
).onsuccess = function(event
) {
138 let cursor
= event
.target
.result
;
140 // Most recent games first:
141 games
= games
.sort((g1
, g2
) => g2
.created
- g1
.created
);
142 // TODO: 20 games showed per request is arbitrary
143 callback(games
.slice(0, 20));
146 // If there is still another cursor to go, keep running this code
147 let g
= cursor
.value
;
148 if (g
.score
!= "*") {
149 // Do not retrieve moves or clocks (unused in list mode)
150 g
.movesCount
= g
.moves
.length
;
162 // Retrieve any game from its identifier.
163 // NOTE: need callback because result is obtained asynchronously
164 get: function(gameId
, callback
) {
165 dbOperation((err
, db
) => {
166 let objectStore
= db
.transaction("games").objectStore("games");
167 objectStore
.get(gameId
).onsuccess = function(event
) {
168 // event.target.result is null if game not found
169 callback(event
.target
.result
);
174 // Delete a game in indexedDB
175 remove: function(gameId
, callback
) {
176 dbOperation((err
, db
) => {
178 let transaction
= db
.transaction("games", "readwrite");
179 transaction
.oncomplete = function() {
180 callback(); //everything's fine
182 transaction
.objectStore("games").delete(gameId
);