Started code review + some fixes (unfinished)
[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 // mode: string ("live" or "corr")
10 // imported: boolean (optional, default false)
11 // // Game (dynamic) state:
12 // fen: string,
13 // moves: array of Move objects,
14 // clocks: array of integers,
15 // initime: array of integers (when clock start running),
16 // score: string (several options; '*' == running),
17 // }
18
19 import { ajax } from "@/utils/ajax";
20 import { store } from "@/store";
21
22 function dbOperation(callback) {
23 let db = null;
24 let DBOpenRequest = window.indexedDB.open("vchess", 4);
25
26 DBOpenRequest.onerror = function(event) {
27 alert(store.state.tr["Database error:"] + " " + event.target.errorCode);
28 };
29
30 DBOpenRequest.onsuccess = function() {
31 db = DBOpenRequest.result;
32 callback(db);
33 db.close();
34 };
35
36 DBOpenRequest.onupgradeneeded = function(event) {
37 let db = event.target.result;
38 db.onerror = function(event) {
39 alert(
40 store.state.tr["Error while loading database:"] +
41 " " +
42 event.target.errorCode
43 );
44 };
45 // Create objectStore for vchess->games
46 let objectStore = db.createObjectStore("games", { keyPath: "id" });
47 objectStore.createIndex("score", "score"); //to search by game result
48 };
49 }
50
51 export const GameStorage = {
52 // Optional callback to get error status
53 add: function(game, callback) {
54 dbOperation(db => {
55 let transaction = db.transaction("games", "readwrite");
56 if (callback) {
57 transaction.oncomplete = function() {
58 callback({}); //everything's fine
59 };
60 transaction.onerror = function() {
61 callback({
62 errmsg:
63 store.state.tr["Game retrieval failed:"] + " " + transaction.error
64 });
65 };
66 }
67 let objectStore = transaction.objectStore("games");
68 objectStore.add(game);
69 });
70 },
71
72 // TODO: also option to takeback a move ?
73 // obj: chat, move, fen, clocks, score[Msg], initime, ...
74 update: function(gameId, obj) {
75 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId))) {
76 // corr: only move, fen and score
77 ajax("/games", "PUT", {
78 gid: gameId,
79 newObj: {
80 // Some fields may be undefined:
81 chat: obj.chat,
82 move: obj.move,
83 fen: obj.fen,
84 score: obj.score,
85 scoreMsg: obj.scoreMsg,
86 drawOffer: obj.drawOffer
87 }
88 });
89 } else {
90 // live
91 dbOperation(db => {
92 let objectStore = db
93 .transaction("games", "readwrite")
94 .objectStore("games");
95 objectStore.get(gameId).onsuccess = function(event) {
96 const game = event.target.result;
97 Object.keys(obj).forEach(k => {
98 if (k == "move") game.moves.push(obj[k]);
99 else game[k] = obj[k];
100 });
101 objectStore.put(game); //save updated data
102 };
103 });
104 }
105 },
106
107 // Retrieve all local games (running, completed, imported...)
108 getAll: function(callback) {
109 dbOperation(db => {
110 let objectStore = db.transaction("games").objectStore("games");
111 let games = [];
112 objectStore.openCursor().onsuccess = function(event) {
113 let cursor = event.target.result;
114 // if there is still another cursor to go, keep running this code
115 if (cursor) {
116 games.push(cursor.value);
117 cursor.continue();
118 } else callback(games);
119 };
120 });
121 },
122
123 // Retrieve any game from its identifiers (locally or on server)
124 // NOTE: need callback because result is obtained asynchronously
125 get: function(gameId, callback) {
126 // corr games identifiers are integers
127 if (Number.isInteger(gameId) || !isNaN(parseInt(gameId))) {
128 ajax("/games", "GET", { gid: gameId }, res => {
129 let game = res.game;
130 game.moves.forEach(m => {
131 m.squares = JSON.parse(m.squares);
132 });
133 callback(game);
134 });
135 } //local game
136 else {
137 dbOperation(db => {
138 let objectStore = db.transaction("games").objectStore("games");
139 objectStore.get(gameId).onsuccess = function(event) {
140 callback(event.target.result);
141 };
142 });
143 }
144 },
145
146 getCurrent: function(callback) {
147 dbOperation(db => {
148 let objectStore = db.transaction("games").objectStore("games");
149 objectStore.get("*").onsuccess = function(event) {
150 callback(event.target.result);
151 };
152 });
153 },
154
155 // Delete a game in indexedDB
156 remove: function(gameId, callback) {
157 dbOperation(db => {
158 let transaction = db.transaction(["games"], "readwrite");
159 if (callback) {
160 transaction.oncomplete = function() {
161 callback({}); //everything's fine
162 };
163 transaction.onerror = function() {
164 callback({
165 errmsg:
166 store.state.tr["Game removal failed:"] + " " + transaction.error
167 });
168 };
169 }
170 transaction.objectStore("games").delete(gameId);
171 });
172 }
173 };