From 6274a54591714db8988683c0e3fe69937e61c672 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Wed, 5 Jun 2019 17:04:52 +0200 Subject: [PATCH] On update + clocks thinking --- client/src/base_rules.js | 4 ++-- client/src/utils/storage.js | 32 +++++++++++++++++++++----------- client/src/views/Game.vue | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/client/src/base_rules.js b/client/src/base_rules.js index eefd04d9..bcd73b14 100644 --- a/client/src/base_rules.js +++ b/client/src/base_rules.js @@ -50,13 +50,13 @@ export const ChessRules = class ChessRules // Turn "wb" into "B" (for FEN) static board2fen(b) { - return b[0]=='w' ? b[1].toUpperCase() : b[1]; + return (b[0]=='w' ? b[1].toUpperCase() : b[1]); } // Turn "p" into "bp" (for board) static fen2board(f) { - return f.charCodeAt()<=90 ? "w"+f.toLowerCase() : "b"+f; + return (f.charCodeAt()<=90 ? "w"+f.toLowerCase() : "b"+f); } // Check if FEN describe a position diff --git a/client/src/utils/storage.js b/client/src/utils/storage.js index 8a7c5268..86051584 100644 --- a/client/src/utils/storage.js +++ b/client/src/utils/storage.js @@ -108,23 +108,33 @@ export const GameStorage = }, // 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, move, clocks, started, score) + update: function(o) //move, clock, initime, score, colorIdx { + // TODO: finish this --> colorIdx must be computed before entering the function let gameState = JSON.parse(localStorage.getItem("gameState")); - if (!!fen) + if (!!o.move) { - gameState.moves.push(move); - gameState.fen = fen; - gameState.clocks = clocks; + // 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; } - 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 }, diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue index 13dc4865..5a64b79b 100644 --- a/client/src/views/Game.vue +++ b/client/src/views/Game.vue @@ -246,6 +246,12 @@ export default { const vModule = await import("@/variants/" + game.vname + ".js"); window.V = vModule.VariantRules; this.vr = new V(game.fen); + // Post-processing: decorate each move with current FEN: + // (to be able to jump to any position quickly) + game.moves.forEach(move => { + vr.play(move); //side-effect: set move.fen + }); + this.vr.re_init(game.fen); }); // // Poll all players except me (if I'm playing) to know online status. // // --> Send ping to server (answer pong if players[s] are connected) @@ -284,3 +290,15 @@ export default { }, }; + +// TODO: utiliser "started" (renommer) pour se souvenir du timestamp où un user a commencé à réfléchir à un coup. Le sauvegarder dans update aussi... + +// variable initime à récupérer de game aussi (et pas "started") +// si initime à -1 ou undefined alors pas commencé. Abort possible à tout moment avec message +// Sorry I have to go / Game seems over / Game is not interesting + +move.clock à màj avec (current) clock - temps de réflexion (now() - initime) + increment +après chaque coup dans une partie live ou corr non terminée. +--> donc à Game.update on passe directement clock + +code "T" pour score "perte au temps" ? -- 2.44.0