Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[vchess.git] / client / src / utils / gameStorage.js
index fc179f1..62ad1fa 100644 (file)
@@ -19,10 +19,11 @@ import { store } from "@/store";
 
 function dbOperation(callback) {
   let db = null;
-  let DBOpenRequest = window.indexedDB.open("vchess", 4);
+  let DBOpenRequest = window.indexedDB.open("vchess", 5);
 
   DBOpenRequest.onerror = function(event) {
-    alert(store.state.tr["Database error: stop private browsing, or update your browser"]);
+    alert(store.state.tr[
+      "Database error: stop private browsing, or update your browser"]);
     callback("error", null);
   };
 
@@ -34,11 +35,18 @@ function dbOperation(callback) {
 
   DBOpenRequest.onupgradeneeded = function(event) {
     let db = event.target.result;
-    let objectStore = db.createObjectStore("games", { keyPath: "id" });
-    // To sarch games by score (useful for running games)
-    objectStore.createIndex("score", "score", { unique: false });
-    // To search by date intervals. Two games cannot start at the same time
-    objectStore.createIndex("created", "created", { unique: true });
+    let upgradeTransaction = event.target.transaction;
+    let objectStore = undefined;
+    if (!db.objectStoreNames.contains("games"))
+      objectStore = db.createObjectStore("games", { keyPath: "id" });
+    else
+      objectStore = upgradeTransaction.objectStore("games");
+    if (!objectStore.indexNames.contains("score"))
+      // To sarch games by score (useful for running games)
+      objectStore.createIndex("score", "score", { unique: false });
+    if (!objectStore.indexNames.contains("created"))
+      // To search by date intervals. Two games cannot start at the same time
+      objectStore.createIndex("created", "created", { unique: true });
   };
 }
 
@@ -78,6 +86,8 @@ export const GameStorage = {
           if (obj.moveIdx < game.moves.length) return;
           Object.keys(obj).forEach(k => {
             if (k == "move") game.moves.push(obj[k]);
+            else if (k == "chat") game.chats.push(obj[k]);
+            else if (k == "delchat") game.chats = [];
             else game[k] = obj[k];
           });
           objectStore.put(game); //save updated data