Fix clocks update + double move effect
[vchess.git] / client / src / utils / storage.js
index f5c3b25..e5978ae 100644 (file)
@@ -99,7 +99,7 @@ export const GameStorage =
       fen: o.fenStart,
       moves: [],
       clocks: [...Array(o.players.length)].fill(tc.mainTime),
-      started: [...Array(o.players.length)].fill(false),
+      initime: (o.initime ? Date.now() : undefined),
       score: "*",
     };
 
@@ -116,14 +116,15 @@ export const GameStorage =
   // localStorage:
   // TODO: also option to takeback a move ?
   // NOTE: for live games only (all on server for corr)
-  update: function(o) //colorIdx, move, fen, elapsed, increment, initime, score
+  update: function(o) //colorIdx, move, fen, addTime, initime, score
   {
     let gameState = JSON.parse(localStorage.getItem("gameState"));
     if (!!o.move)
     {
       gameState.moves.push(o.move);
       gameState.fen = o.fen;
-      gameState.clocks[o.colorIdx] += (o.increment - o.elapsed);
+      if (!!o.addTime) //NaN if first move in game
+        gameState.clocks[o.colorIdx] += o.addTime;
     }
     if (!!o.initime) //just a flag (true)
       gameState.initime = Date.now();
@@ -168,7 +169,7 @@ export const GameStorage =
           callback({}); //everything's fine
         }
         transaction.onerror = function() {
-          callback({errmsg: "deleteGame failed: " + transaction.error});
+          callback({errmsg: "game removal failed: " + transaction.error});
         };
       }
       transaction.objectStore("games").delete(gameId);