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++;
}, 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
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)
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);
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)
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();
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);
// ...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)
// 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);
});
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,
+ });
},
},
};
</script>
-// 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
+<!-- TODO:
+// 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" ?
+// code "T" pour score "perte au temps" ?
+-->