X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fstorage.js;h=2e7a84fb421d91bb5894760914d70cebab6dfb69;hb=d4036efea5b57656478affd7d71f53dcea0f8017;hp=86051584e736002f567f8d8843932c6bfba34305;hpb=6274a54591714db8988683c0e3fe69937e61c672;p=vchess.git diff --git a/client/src/utils/storage.js b/client/src/utils/storage.js index 86051584..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: @@ -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: "*", }; @@ -107,27 +107,24 @@ export const GameStorage = localStorage.setItem("gameState", JSON.stringify(gameState)); }, + getInitime: function() + { + const gameState = JSON.parse(localStorage.getItem("gameState")); + return gameState.initime; + }, + // localStorage: // TODO: also option to takeback a move ? // NOTE: for live games only (all on server for corr) - update: function(o) //move, clock, initime, score, colorIdx + update: function(o) //colorIdx, move, fen, addTime, initime, score { - // TODO: finish this --> colorIdx must be computed before entering the function let gameState = JSON.parse(localStorage.getItem("gameState")); if (!!o.move) { - // https://stackoverflow.com/a/38750895 - const allowed = ['appear', 'vanish', 'start', 'end']; - const filtered_move = Object.keys(o.move) - .filter(key => allowed.includes(key)) - .reduce((obj, key) => { - obj[key] = raw[key]; - return obj; - }, {}); - gameState.moves.push(filtered_move); - gameState.fen = o.move.fen; - const colorIdx = ["w","b","g","r"][o.move.color]; - gameState.clocks[colorIdx] = o.move.clock; + gameState.moves.push(o.move); + gameState.fen = o.fen; + 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(); @@ -143,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 runing 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); } }); }, @@ -172,7 +177,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);