e26e19ab70eb5eded8075ebe7e5266023270ef1d
[vchess.git] / client / src / utils / gameStorage.js
1 // Game object: {
2 // // Static informations:
3 // id: string
4 // vname: string,
5 // fenStart: string,
6 // players: array of sid+id+name,
7 // cadence: string,
8 // increment: integer (seconds),
9 // type: string ("live" or "corr")
10 // // Game (dynamic) state:
11 // fen: string,
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),
16 // }
17
18 import { store } from "@/store";
19
20 function dbOperation(callback) {
21 let db = null;
22 let DBOpenRequest = window.indexedDB.open("vchess", 5);
23
24 DBOpenRequest.onerror = function(event) {
25 alert(store.state.tr["Database error: stop private browsing, or update your browser"]);
26 callback("error", null);
27 };
28
29 DBOpenRequest.onsuccess = function() {
30 db = DBOpenRequest.result;
31 callback(null, db);
32 db.close();
33 };
34
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" });
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 });
49 };
50 }
51
52 export const GameStorage = {
53 // Optional callback to get error status
54 add: function(game, callback) {
55 dbOperation((err,db) => {
56 if (!!err) {
57 callback("error");
58 return;
59 }
60 let transaction = db.transaction("games", "readwrite");
61 transaction.oncomplete = function() {
62 // Everything's fine
63 callback();
64 };
65 transaction.onerror = function(err) {
66 // Duplicate key error (most likely)
67 callback(err);
68 };
69 transaction.objectStore("games").add(game);
70 });
71 },
72
73 // obj: chat, move, fen, clocks, score[Msg], initime, ...
74 update: function(gameId, obj) {
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]);
88 else game[k] = obj[k];
89 });
90 objectStore.put(game); //save updated data
91 }
92 };
93 });
94 },
95
96 // Retrieve (all) running local games
97 getRunning: function(callback) {
98 dbOperation((err,db) => {
99 let objectStore = db
100 .transaction("games", "readonly")
101 .objectStore("games");
102 let index = objectStore.index("score");
103 const range = IDBKeyRange.only("*");
104 let games = [];
105 index.openCursor(range).onsuccess = function(event) {
106 let cursor = event.target.result;
107 if (!cursor) callback(games);
108 else {
109 // If there is still another cursor to go, keep running this code
110 let g = cursor.value;
111 // Do not retrieve moves or clocks (unused in list mode)
112 g.movesCount = g.moves.length;
113 delete g.moves;
114 delete g.clocks;
115 delete g.initime;
116 games.push(g);
117 cursor.continue();
118 }
119 };
120 });
121 },
122
123 // Retrieve completed local games
124 getNext: function(upperDt, callback) {
125 dbOperation((err,db) => {
126 let objectStore = db
127 .transaction("games", "readonly")
128 .objectStore("games");
129 let index = objectStore.index("created");
130 const range = IDBKeyRange.upperBound(upperDt);
131 let games = [];
132 index.openCursor(range).onsuccess = function(event) {
133 let cursor = event.target.result;
134 if (!cursor) {
135 // Most recent games first:
136 games = games.sort((g1, g2) => g2.created - g1.created);
137 // TODO: 20 games showed per request is arbitrary
138 callback(games.slice(0, 20));
139 }
140 else {
141 // If there is still another cursor to go, keep running this code
142 let g = cursor.value;
143 if (g.score != "*") {
144 // Do not retrieve moves or clocks (unused in list mode)
145 g.movesCount = g.moves.length;
146 delete g.moves;
147 delete g.clocks;
148 delete g.initime;
149 games.push(g);
150 }
151 cursor.continue();
152 }
153 };
154 });
155 },
156
157 // Retrieve any game from its identifiers (locally or on server)
158 // NOTE: need callback because result is obtained asynchronously
159 get: function(gameId, callback) {
160 // Local game
161 dbOperation((err,db) => {
162 let objectStore = db.transaction("games").objectStore("games");
163 objectStore.get(gameId).onsuccess = function(event) {
164 // event.target.result is null if game not found
165 callback(event.target.result);
166 };
167 });
168 },
169
170 // Delete a game in indexedDB
171 remove: function(gameId, callback) {
172 dbOperation((err,db) => {
173 if (!err) {
174 let transaction = db.transaction("games", "readwrite");
175 transaction.oncomplete = function() {
176 callback(); //everything's fine
177 };
178 transaction.objectStore("games").delete(gameId);
179 }
180 });
181 }
182 };