92db29915abf33081e61e6571c07f368ae72e9ae
[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", 4);
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 objectStore = db.createObjectStore("games", { keyPath: "id" });
38 objectStore.createIndex("score", "score"); //to search by game result
39 };
40 }
41
42 export const GameStorage = {
43 // Optional callback to get error status
44 add: function(game, callback) {
45 dbOperation((err,db) => {
46 if (err) {
47 callback("error");
48 return;
49 }
50 let transaction = db.transaction("games", "readwrite");
51 transaction.oncomplete = function() {
52 callback(); //everything's fine
53 };
54 let objectStore = transaction.objectStore("games");
55 objectStore.add(game);
56 });
57 },
58
59 // obj: chat, move, fen, clocks, score[Msg], initime, ...
60 update: function(gameId, obj) {
61 // live
62 dbOperation((err,db) => {
63 let objectStore = db
64 .transaction("games", "readwrite")
65 .objectStore("games");
66 objectStore.get(gameId).onsuccess = function(event) {
67 // Ignoring error silently: shouldn't happen now. TODO?
68 if (event.target.result) {
69 let game = event.target.result;
70 // Hidden tabs are delayed, to prevent multi-updates:
71 if (obj.moveIdx < game.moves.length) return;
72 Object.keys(obj).forEach(k => {
73 if (k == "move") game.moves.push(obj[k]);
74 else game[k] = obj[k];
75 });
76 objectStore.put(game); //save updated data
77 }
78 };
79 });
80 },
81
82 // Retrieve all local games (running, completed, imported...)
83 // light: do not retrieve moves or clocks (TODO: this is the only usage)
84 getAll: function(light, callback) {
85 dbOperation((err,db) => {
86 let objectStore = db.transaction("games").objectStore("games");
87 let games = [];
88 objectStore.openCursor().onsuccess = function(event) {
89 let cursor = event.target.result;
90 // if there is still another cursor to go, keep running this code
91 if (cursor) {
92 let g = cursor.value;
93 if (light) {
94 g.movesCount = g.moves.length;
95 delete g.moves;
96 delete g.clocks;
97 delete g.initime;
98 }
99 games.push(g);
100 cursor.continue();
101 } else callback(games);
102 };
103 });
104 },
105
106 // Retrieve any game from its identifiers (locally or on server)
107 // NOTE: need callback because result is obtained asynchronously
108 get: function(gameId, callback) {
109 // Local game
110 dbOperation((err,db) => {
111 let objectStore = db.transaction("games").objectStore("games");
112 objectStore.get(gameId).onsuccess = function(event) {
113 if (event.target.result)
114 callback(event.target.result);
115 };
116 });
117 },
118
119 // Delete a game in indexedDB
120 remove: function(gameId, callback) {
121 dbOperation((err,db) => {
122 if (!err) {
123 let transaction = db.transaction(["games"], "readwrite");
124 transaction.oncomplete = function() {
125 callback(); //everything's fine
126 };
127 transaction.objectStore("games").delete(gameId);
128 }
129 });
130 }
131 };