Show check(mate) indicators in moves list. No longer require the odd ?rid=... in...
[vchess.git] / client / src / utils / gameStorage.js
CommitLineData
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 18import { store } from "@/store";
fd7aea36 19
6808d7a1 20function dbOperation(callback) {
967a2686
BA
21 let db = null;
22 let DBOpenRequest = window.indexedDB.open("vchess", 4);
23
24 DBOpenRequest.onerror = function(event) {
8477e53d
BA
25 alert(store.state.tr["Database error: stop private browsing, or update your browser"]);
26 callback("error",null);
967a2686
BA
27 };
28
6808d7a1 29 DBOpenRequest.onsuccess = function() {
967a2686 30 db = DBOpenRequest.result;
8477e53d 31 callback(null,db);
967a2686
BA
32 db.close();
33 };
34
35 DBOpenRequest.onupgradeneeded = function(event) {
36 let db = event.target.result;
11667c79 37 let objectStore = db.createObjectStore("games", { keyPath: "id" });
42c15a75 38 objectStore.createIndex("score", "score"); //to search by game result
6808d7a1 39 };
967a2686
BA
40}
41
6808d7a1 42export const GameStorage = {
967a2686 43 // Optional callback to get error status
6808d7a1 44 add: function(game, callback) {
8477e53d 45 dbOperation((err,db) => {
c292ebb2 46 if (!!err) {
8477e53d
BA
47 callback("error");
48 return;
967a2686 49 }
8477e53d
BA
50 let transaction = db.transaction("games", "readwrite");
51 transaction.oncomplete = function() {
52 callback(); //everything's fine
53 };
c292ebb2
BA
54 transaction.onerror = function(err) {
55 callback(err); //duplicate key error (most likely)
56 };
967a2686
BA
57 let objectStore = transaction.objectStore("games");
58 objectStore.add(game);
59 });
60 },
61
dcd68c41 62 // obj: chat, move, fen, clocks, score[Msg], initime, ...
6808d7a1 63 update: function(gameId, obj) {
aae89b49
BA
64 // live
65 dbOperation((err,db) => {
66 let objectStore = db
67 .transaction("games", "readwrite")
68 .objectStore("games");
69 objectStore.get(gameId).onsuccess = function(event) {
70 // Ignoring error silently: shouldn't happen now. TODO?
71 if (event.target.result) {
72 let game = event.target.result;
73 // Hidden tabs are delayed, to prevent multi-updates:
74 if (obj.moveIdx < game.moves.length) return;
75 Object.keys(obj).forEach(k => {
76 if (k == "move") game.moves.push(obj[k]);
77 else game[k] = obj[k];
78 });
79 objectStore.put(game); //save updated data
3d55deea 80 }
aae89b49
BA
81 };
82 });
967a2686
BA
83 },
84
fd7aea36 85 // Retrieve all local games (running, completed, imported...)
6b7b2cf7 86 getAll: function(callback) {
8477e53d 87 dbOperation((err,db) => {
6808d7a1 88 let objectStore = db.transaction("games").objectStore("games");
fd7aea36
BA
89 let games = [];
90 objectStore.openCursor().onsuccess = function(event) {
91 let cursor = event.target.result;
92 // if there is still another cursor to go, keep running this code
6808d7a1 93 if (cursor) {
db1f1f9a 94 let g = cursor.value;
6b7b2cf7
BA
95 // Do not retrieve moves or clocks (unused in list mode)
96 g.movesCount = g.moves.length;
97 delete g.moves;
98 delete g.clocks;
99 delete g.initime;
db1f1f9a 100 games.push(g);
fd7aea36 101 cursor.continue();
6808d7a1
BA
102 } else callback(games);
103 };
fd7aea36
BA
104 });
105 },
106
107 // Retrieve any game from its identifiers (locally or on server)
108 // NOTE: need callback because result is obtained asynchronously
6808d7a1 109 get: function(gameId, callback) {
aae89b49
BA
110 // Local game
111 dbOperation((err,db) => {
112 let objectStore = db.transaction("games").objectStore("games");
113 objectStore.get(gameId).onsuccess = function(event) {
f54f4c26
BA
114 // event.target.result is null if game not found
115 callback(event.target.result);
aae89b49
BA
116 };
117 });
967a2686
BA
118 },
119
120 // Delete a game in indexedDB
6808d7a1 121 remove: function(gameId, callback) {
8477e53d
BA
122 dbOperation((err,db) => {
123 if (!err) {
124 let transaction = db.transaction(["games"], "readwrite");
967a2686 125 transaction.oncomplete = function() {
3b0f26c1 126 callback(); //everything's fine
6808d7a1 127 };
8477e53d 128 transaction.objectStore("games").delete(gameId);
967a2686 129 }
967a2686 130 });
6808d7a1 131 }
967a2686 132};