X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fstorage.js;h=2e7a84fb421d91bb5894760914d70cebab6dfb69;hb=d4036efea5b57656478affd7d71f53dcea0f8017;hp=a30bb824e964b70ce855c973d1fefeac3b4e87d4;hpb=8a7452b520dc7ead6880a7e62e7c0e057db1ebda;p=vchess.git diff --git a/client/src/utils/storage.js b/client/src/utils/storage.js index a30bb824..2e7a84fb 100644 --- a/client/src/utils/storage.js +++ b/client/src/utils/storage.js @@ -51,8 +51,8 @@ function addGame(game, callback) // Clear current live game from localStorage function clear() { - localStorage.deleteItem("gameInfo"); - localStorage.deleteItem("gameState"); + localStorage.removeItem("gameInfo"); + localStorage.removeItem("gameState"); } // Current live game: @@ -116,15 +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; - if (!!o.elapsed) //NaN if first move in game - 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(); @@ -140,20 +140,28 @@ export const GameStorage = // TODO: option for remote retrieval (third arg, or just "gameRef") getLocal: function(gameId, callback) { - let games = []; dbOperation((db) => { - // TODO: if gameId is provided, limit search to gameId (just .get(gameId). ...) let objectStore = db.transaction('games').objectStore('games'); - objectStore.openCursor().onsuccess = function(event) { - var cursor = event.target.result; - // if there is still another cursor to go, keep running this code - if (cursor) - { - games.push(cursor.value); - cursor.continue(); + if (!gameId) //retrieve all + { + let games = []; + objectStore.openCursor().onsuccess = function(event) { + let cursor = event.target.result; + // if there is still another cursor to go, keep running this code + if (cursor) + { + games.push(cursor.value); + cursor.continue(); + } + else + callback(games); + } + } + else //just one game + { + objectStore.get(gameId).onsuccess = function(event) { + callback(event.target.result); } - else - callback(games); } }); },