6b4c7113dd468cd41ac7b7386a92907db2018418
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
) {
25 alert(store
.state
.tr
["Database error: stop private browsing, or update your browser"]);
26 callback("error", null);
29 DBOpenRequest
.onsuccess = function() {
30 db
= DBOpenRequest
.result
;
35 DBOpenRequest
.onupgradeneeded = function(event
) {
36 let db
= event
.target
.result
;
37 let upgradeTransaction
= event
.target
.transaction
;
38 let objectStore
= undefined;
39 if (!db
.objectStoreNames
.contains("games"))
40 objectStore
= db
.createObjectStore("games", { keyPath: "id" });
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 });
52 export const GameStorage
= {
53 // Optional callback to get error status
54 add: function(game
, callback
) {
55 dbOperation((err
,db
) => {
60 let transaction
= db
.transaction("games", "readwrite");
61 transaction
.oncomplete = function() {
65 transaction
.onerror = function(err
) {
66 // Duplicate key error (most likely)
69 transaction
.objectStore("games").add(game
);
73 // obj: chat, move, fen, clocks, score[Msg], initime, ...
74 update: function(gameId
, obj
) {
76 dbOperation((err
,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
]);
88 else if (k
== "chat") game
.chats
.push(obj
[k
]);
89 else if (k
== "delchat") game
.chats
= [];
90 else game
[k
] = obj
[k
];
92 objectStore
.put(game
); //save updated data
98 // Retrieve (all) running local games
99 getRunning: function(callback
) {
100 dbOperation((err
,db
) => {
102 .transaction("games", "readonly")
103 .objectStore("games");
104 let index
= objectStore
.index("score");
105 const range
= IDBKeyRange
.only("*");
107 index
.openCursor(range
).onsuccess = function(event
) {
108 let cursor
= event
.target
.result
;
109 if (!cursor
) callback(games
);
111 // If there is still another cursor to go, keep running this code
112 let g
= cursor
.value
;
113 // Do not retrieve moves or clocks (unused in list mode)
114 g
.movesCount
= g
.moves
.length
;
125 // Retrieve completed local games
126 getNext: function(upperDt
, callback
) {
127 dbOperation((err
,db
) => {
129 .transaction("games", "readonly")
130 .objectStore("games");
131 let index
= objectStore
.index("created");
132 const range
= IDBKeyRange
.upperBound(upperDt
);
134 index
.openCursor(range
).onsuccess = function(event
) {
135 let cursor
= event
.target
.result
;
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));
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
;
159 // Retrieve any game from its identifiers (locally or on server)
160 // NOTE: need callback because result is obtained asynchronously
161 get: function(gameId
, callback
) {
163 dbOperation((err
,db
) => {
164 let objectStore
= db
.transaction("games").objectStore("games");
165 objectStore
.get(gameId
).onsuccess = function(event
) {
166 // event.target.result is null if game not found
167 callback(event
.target
.result
);
172 // Delete a game in indexedDB
173 remove: function(gameId
, callback
) {
174 dbOperation((err
,db
) => {
176 let transaction
= db
.transaction("games", "readwrite");
177 transaction
.oncomplete = function() {
178 callback(); //everything's fine
180 transaction
.objectStore("games").delete(gameId
);