Experimental game upload added
[vchess.git] / client / src / utils / gameStorage.js
index 2e78994..f69de2e 100644 (file)
 //   score: string (several options; '*' == running),
 // }
 
-import { ajax } from "@/utils/ajax";
 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"]);
-    callback("error",null);
+    alert(store.state.tr[
+      "Database error: stop private browsing, or update your browser"]);
+    callback("error", null);
   };
 
   DBOpenRequest.onsuccess = function() {
     db = DBOpenRequest.result;
-    callback(null,db);
+    callback(null, db);
     db.close();
   };
 
   DBOpenRequest.onupgradeneeded = function(event) {
     let db = event.target.result;
-    let objectStore = db.createObjectStore("games", { keyPath: "id" });
-    objectStore.createIndex("score", "score"); //to search by game result
+    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 });
   };
 }
 
 export const GameStorage = {
   // Optional callback to get error status
   add: function(game, callback) {
-    dbOperation((err,db) => {
-      if (err) {
+    dbOperation((err, db) => {
+      if (!!err) {
         callback("error");
         return;
       }
       let transaction = db.transaction("games", "readwrite");
       transaction.oncomplete = function() {
-        callback(); //everything's fine
+        // Everything's fine
+        callback();
       };
-      let objectStore = transaction.objectStore("games");
-      objectStore.add(game);
+      transaction.onerror = function(err) {
+        // Duplicate key error (most likely)
+        callback(err);
+      };
+      transaction.objectStore("games").add(game);
     });
   },
 
-  // TODO: also option to takeback a move ?
   // obj: chat, move, fen, clocks, score[Msg], initime, ...
   update: function(gameId, obj) {
-    if (Number.isInteger(gameId) || !isNaN(parseInt(gameId))) {
-      // corr: only move, fen and score
-      ajax("/games", "PUT", {
-        gid: gameId,
-        newObj: {
-          // Some fields may be undefined:
-          chat: obj.chat,
-          move: obj.move,
-          fen: obj.fen,
-          score: obj.score,
-          scoreMsg: obj.scoreMsg,
-          drawOffer: obj.drawOffer
+    // live
+    dbOperation((err, db) => {
+      let objectStore = db
+        .transaction("games", "readwrite")
+        .objectStore("games");
+      objectStore.get(gameId).onsuccess = function(event) {
+        // Ignoring error silently: shouldn't happen now. TODO?
+        if (event.target.result) {
+          let game = event.target.result;
+          // Hidden tabs are delayed, to prevent multi-updates:
+          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 == "chatRead") game.chatRead = Date.now();
+            else if (k == "delchat") game.chats = [];
+            else game[k] = obj[k];
+          });
+          objectStore.put(game); //save updated data
         }
-      });
-    } else {
-      // live
-      dbOperation((err,db) => {
-        let objectStore = db
-          .transaction("games", "readwrite")
-          .objectStore("games");
-        objectStore.get(gameId).onsuccess = function(event) {
-          // Ignoring error silently: shouldn't happen now. TODO?
-          if (event.target.result) {
-            let game = event.target.result;
-            Object.keys(obj).forEach(k => {
-              if (k == "move") game.moves.push(obj[k]);
-              else game[k] = obj[k];
-            });
-            objectStore.put(game); //save updated data
-          }
-        };
-      });
-    }
+      };
+    });
   },
 
-  // Retrieve all local games (running, completed, imported...)
-  // light: do not retrieve moves or clocks (TODO: this is the only usage)
-  getAll: function(light, callback) {
-    dbOperation((err,db) => {
-      let objectStore = db.transaction("games").objectStore("games");
+  // Retrieve (all) running local games
+  getRunning: function(callback) {
+    dbOperation((err, db) => {
+      let objectStore = db
+        .transaction("games", "readonly")
+        .objectStore("games");
+      let index = objectStore.index("score");
+      const range = IDBKeyRange.only("*");
       let games = [];
-      objectStore.openCursor().onsuccess = function(event) {
+      index.openCursor(range).onsuccess = function(event) {
         let cursor = event.target.result;
-        // if there is still another cursor to go, keep running this code
-        if (cursor) {
+        if (!cursor) callback(games);
+        else {
+          // If there is still another cursor to go, keep running this code
           let g = cursor.value;
-          if (light) {
+          // Do not retrieve moves or clocks (unused in list mode)
+          g.movesCount = g.moves.length;
+          delete g.moves;
+          delete g.clocks;
+          delete g.initime;
+          games.push(g);
+          cursor.continue();
+        }
+      };
+    });
+  },
+
+  // Retrieve completed local games
+  getNext: function(upperDt, callback) {
+    dbOperation((err, db) => {
+      let objectStore = db
+        .transaction("games", "readonly")
+        .objectStore("games");
+      let index = objectStore.index("created");
+      const range = IDBKeyRange.upperBound(upperDt);
+      let games = [];
+      index.openCursor(range).onsuccess = function(event) {
+        let cursor = event.target.result;
+        if (!cursor) {
+          // Most recent games first:
+          games = games.sort((g1, g2) => g2.created - g1.created);
+          // TODO: 20 games showed per request is arbitrary
+          callback(games.slice(0, 20));
+        }
+        else {
+          // If there is still another cursor to go, keep running this code
+          let g = cursor.value;
+          if (g.score != "*") {
+            // Do not retrieve moves or clocks (unused in list mode)
             g.movesCount = g.moves.length;
             delete g.moves;
             delete g.clocks;
             delete g.initime;
+            games.push(g);
           }
-          games.push(g);
           cursor.continue();
-        } else callback(games);
+        }
       };
     });
   },
 
-  // Retrieve any game from its identifiers (locally or on server)
+  // Retrieve any game from its identifier.
   // NOTE: need callback because result is obtained asynchronously
   get: function(gameId, callback) {
-    // corr games identifiers are integers
-    if (Number.isInteger(gameId) || !isNaN(parseInt(gameId))) {
-      ajax("/games", "GET", { gid: gameId }, res => {
-        let game = res.game;
-        game.moves.forEach(m => {
-          m.squares = JSON.parse(m.squares);
-        });
-        callback(game);
-      });
-    }
-    else {
-      // Local game
-      dbOperation((err,db) => {
-        let objectStore = db.transaction("games").objectStore("games");
-        objectStore.get(gameId).onsuccess = function(event) {
-          if (event.target.result)
-            callback(event.target.result);
-        };
-      });
-    }
+    dbOperation((err, db) => {
+      let objectStore = db.transaction("games").objectStore("games");
+      objectStore.get(gameId).onsuccess = function(event) {
+        // event.target.result is null if game not found
+        callback(event.target.result);
+      };
+    });
   },
 
   // Delete a game in indexedDB
   remove: function(gameId, callback) {
-    dbOperation((err,db) => {
+    dbOperation((err, db) => {
       if (!err) {
-        let transaction = db.transaction(["games"], "readwrite");
+        let transaction = db.transaction("games", "readwrite");
         transaction.oncomplete = function() {
-          callback({}); //everything's fine
+          callback(); //everything's fine
         };
         transaction.objectStore("games").delete(gameId);
       }