From: Benjamin Auder Date: Tue, 11 Jun 2019 17:30:55 +0000 (+0200) Subject: Complete GameStorage.update. TODO: move transmission X-Git-Url: https://git.auder.net/%7B%7B%20asset%28%27mixstore/images/img/current/%7B%7B?a=commitdiff_plain;h=9d54ab8900b69876c3249b4ed1bfa355dccb7f97;p=vchess.git Complete GameStorage.update. TODO: move transmission --- diff --git a/client/src/base_rules.js b/client/src/base_rules.js index bcd73b14..15609eb1 100644 --- a/client/src/base_rules.js +++ b/client/src/base_rules.js @@ -1062,8 +1062,6 @@ export const ChessRules = class ChessRules move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo) if (V.HasEnpassant) this.epSquares.push( this.getEpSquare(move) ); - if (!move.color) - move.color = this.turn; //for interface V.PlayOnBoard(this.board, move); this.turn = V.GetOppCol(this.turn); this.movesCount++; diff --git a/client/src/components/BaseGame.vue b/client/src/components/BaseGame.vue index 5ee5e0c7..68fa83a2 100644 --- a/client/src/components/BaseGame.vue +++ b/client/src/components/BaseGame.vue @@ -176,7 +176,7 @@ export default { }, 250); }, play: function(move, programmatic) { - let navigate = !move; + const navigate = !move; // Forbid playing outside analyze mode when cursor isn't at moves.length-1 // (except if we receive opponent's move, human or computer) if (!navigate && !this.analyze && !programmatic @@ -190,6 +190,11 @@ export default { return; //no more moves move = this.moves[this.cursor+1]; } + else + { + move.color = this.vr.turn; + move.notation = this.vr.getNotation(move); + } if (!!programmatic) //computer or (remote) human opponent { if (this.cursor < this.moves.length-1) @@ -197,24 +202,22 @@ export default { return this.animateMove(move); } // Not programmatic, or animation is over - if (!move.notation) - move.notation = this.vr.getNotation(move); - if (!move.color) - move.color = this.vr.turn; this.vr.play(move); this.cursor++; this.lastMove = move; - if (!move.fen) - move.fen = this.vr.getFen(); if (this.st.settings.sound == 2) new Audio("/sounds/move.mp3").play().catch(err => {}); - if (!navigate && (this.score == "*" || this.analyze)) + if (!navigate) { - // Stack move on movesList at current cursor - if (this.cursor == this.moves.length) - this.moves.push(move); - else - this.moves = this.moves.slice(0,this.cursor).concat([move]); + move.fen = this.vr.getFen(); + if (this.score == "*" || this.analyze) + { + // Stack move on movesList at current cursor + if (this.cursor == this.moves.length) + this.moves.push(move); + else + this.moves = this.moves.slice(0,this.cursor).concat([move]); + } } // Is opponent in check? this.incheck = this.vr.getCheckSquares(this.vr.turn); @@ -230,7 +233,7 @@ export default { this.$emit("newmove", move); //post-processing (e.g. computer play) }, undo: function(move) { - let navigate = !move; + const navigate = !move; if (navigate) { if (this.cursor < 0) diff --git a/client/src/utils/storage.js b/client/src/utils/storage.js index 86051584..f5c3b253 100644 --- a/client/src/utils/storage.js +++ b/client/src/utils/storage.js @@ -107,27 +107,23 @@ 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, elapsed, increment, 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; + gameState.clocks[o.colorIdx] += (o.increment - o.elapsed); } if (!!o.initime) //just a flag (true) gameState.initime = Date.now(); @@ -149,7 +145,7 @@ export const GameStorage = 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 there is still another cursor to go, keep running this code if (cursor) { games.push(cursor.value); diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue index 5a64b79b..920e9c84 100644 --- a/client/src/views/Game.vue +++ b/client/src/views/Game.vue @@ -98,6 +98,18 @@ export default { // ...or just see nothing as on buho21 this.$refs["basegame"].play( data.move, this.game.vname!="Dark" ? "animate" : null); + this.processMove(data.move); + + + +// TODO: +// send filtered_move + elapsed time +// receive same. (update clock) + update (our) initime if it's my turn +// + update fen (using vr.getFen()) + + + + break; case "pong": //received if we sent a ping (game still alive on our side) if (this.gameRef.id != data.gameId) @@ -249,7 +261,9 @@ export default { // 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 + move.color = this.vr.turn; + vr.play(move); + move.fen = this.vr.getFen(); }); this.vr.re_init(game.fen); }); @@ -267,38 +281,56 @@ export default { oppConnected: function(uid) { return this.opponents.some(o => o.id == uid && o.online); }, + // Post-process a move (which was just played) processMove: function(move) { if (!this.game.mycolor) return; //I'm just an observer - // TODO: update storage (corr or live), - GameStorage.update({ - fen: move: clocks: ................ // TODO - }); + // Update storage (corr or live) + const colorIdx = ["w","b","g","r"][move.color]; + // https://stackoverflow.com/a/38750895 + const allowed_fields = ["appear", "vanish", "start", "end"]; + const filtered_move = Object.keys(move) + .filter(key => allowed_fields.includes(key)) + .reduce((obj, key) => { + obj[key] = raw[key]; + return obj; + }, {}); // Send move ("newmove" event) to opponent(s) (if ours) - if (move.disappear[0].c == this.game.mycolor) + // (otherwise move.elapsed is supposed to be already transmitted) + if (move.color == this.game.mycolor) { + const elapsed = Date.now() - GameStorage.getInitime(); this.game.players.forEach(p => { if (p.sid != this.st.user.sid) - this.st.conn.send("newmove", {target:p.sid, move:move}); + this.st.conn.send("newmove", + { + target: p.sid, + move: Object.assign({}, filtered_move, {elapsed: elapsed}), + }); }); + move.elapsed = elapsed; } + GameStorage.update({ + colorIdx: colorIdx, + move: filtered_move, + fen: move.fen, + elapsed: move.elapsed, + increment: this.game.increment, //redundant but faster + initime: (this.vr.turn == this.game.mycolor), //it's my turn + }); }, + // NOTE: this update function should also work for corr games gameOver: function(score) { - // TODO: GameStorage.update with score - // NOTE: this update function should also work for corr games + GameStorage.update({ + score: score, + }); }, }, }; -// 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 + donc à Game.update on passe directement clock - -code "T" pour score "perte au temps" ? +// code "T" pour score "perte au temps" ? +-->