X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fstorage.js;h=2e7a84fb421d91bb5894760914d70cebab6dfb69;hb=d4036efea5b57656478affd7d71f53dcea0f8017;hp=2c2d4be27f3c8b23b4870e68c27e7f0e4596a89e;hpb=8f5780d8cc67245c01cb381853a56cb510ad9ab2;p=vchess.git diff --git a/client/src/utils/storage.js b/client/src/utils/storage.js index 2c2d4be2..2e7a84fb 100644 --- a/client/src/utils/storage.js +++ b/client/src/utils/storage.js @@ -1,5 +1,4 @@ import { extractTime } from "@/utils/timeControl"; -import { shuffle } from "@/utils/alea"; // TODO: show game structure //const newItem = [ @@ -52,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: @@ -79,10 +78,6 @@ export const GameStorage = // localStorage: init: function(o) { - // NOTE: when >= 3 players, better use an array + shuffle for mycolor - const mycolor = (Math.random() < 0.5 ? "w" : "b"); - // Shuffle players order (white then black then other colors). - const players = shuffle(o.players); // Extract times (in [milli]seconds), set clocks, store in localStorage const tc = extractTime(o.timeControl); @@ -91,9 +86,8 @@ export const GameStorage = { gameId: o.gameId, vname: o.vname, - mycolor: mycolor, fenStart: o.fenStart, - players: players, + players: o.players, timeControl: o.timeControl, increment: tc.increment, mode: "live", //function for live games only @@ -105,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: "*", }; @@ -113,46 +107,61 @@ 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 ? Is fen included in move ? + // TODO: also option to takeback a move ? // NOTE: for live games only (all on server for corr) - update: function(fen, moves, clocks, started, score) + update: function(o) //colorIdx, move, fen, addTime, initime, score { let gameState = JSON.parse(localStorage.getItem("gameState")); - if (!!fen) + if (!!o.move) { - gameState.moves = moves; - gameState.fen = fen; - gameState.clocks = clocks; + 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 (!!started) - gameState.started = started; - if (!!score) - gameState.score = score; + if (!!o.initime) //just a flag (true) + gameState.initime = Date.now(); + if (!!o.score) + gameState.score = o.score; localStorage.setItem("gameState", JSON.stringify(gameState)); - if (!!score && score != "*") + if (!!o.score && o.score != "*") transferToDb(); //game is over }, // indexedDB: // Since DB requests are asynchronous, require a callback using the result // TODO: option for remote retrieval (third arg, or just "gameRef") - getLocal: function(callback, gameId) + 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); } }); }, @@ -168,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); @@ -200,6 +209,6 @@ export const GameStorage = } // Game is local and not running - getLocal(callback, gid); + GameStorage.getLocal(gid, callback); }, };