- // Post-process a move (which was just played in BaseGame)
- processMove: function(move) {
- if (this.game.type == "corr" && move.color == this.game.mycolor) {
- if (
- !confirm(
- this.st.tr["Move played:"] +
- " " +
- move.notation +
- "\n" +
- this.st.tr["Are you sure?"]
- )
- ) {
- this.$set(this.game, "moveToUndo", move);
- return;
- }
- }
- const colorIdx = ["w", "b"].indexOf(move.color);
- const nextIdx = ["w", "b"].indexOf(this.vr.turn);
- // https://stackoverflow.com/a/38750895
- if (this.game.mycolor) {
- const allowed_fields = ["appear", "vanish", "start", "end"];
- // NOTE: 'var' to see this variable outside this block
- var filtered_move = Object.keys(move)
- .filter(key => allowed_fields.includes(key))
- .reduce((obj, key) => {
- obj[key] = move[key];
- return obj;
- }, {});
- }
- // Send move ("newmove" event) to people in the room (if our turn)
- let addTime = 0;
- if (move.color == this.game.mycolor) {
- if (this.drawOffer == "received")
- //I refuse draw
- this.drawOffer = "";
- if (this.game.moves.length >= 2) {
- //after first move
- const elapsed = Date.now() - this.game.initime[colorIdx];
- // elapsed time is measured in milliseconds
- addTime = this.game.increment - elapsed / 1000;
- }
- const sendMove = Object.assign({}, filtered_move, {
- addTime: addTime,
- cancelDrawOffer: this.drawOffer == ""
- });
- this.send("newmove", { data: sendMove });
- // (Add)Time indication: useful in case of lastate infos requested
- move.addTime = addTime;
- } else addTime = move.addTime; //supposed transmitted
- // Update current game object:
- this.game.moves.push(move);
- this.game.fen = move.fen;
- this.game.clocks[colorIdx] += addTime;
- // move.initime is set only when I receive a "lastate" move from opponent
- this.game.initime[nextIdx] = move.initime || Date.now();
- this.re_setClocks();
- // If repetition detected, consider that a draw offer was received:
- const fenObj = V.ParseFen(move.fen);
- let repIdx = fenObj.position + "_" + fenObj.turn;
- if (fenObj.flags) repIdx += "_" + fenObj.flags;
- this.repeat[repIdx] = this.repeat[repIdx] ? this.repeat[repIdx] + 1 : 1;
- if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep";
- else if (this.drawOffer == "threerep") this.drawOffer = "";
- // Since corr games are stored at only one location, update should be
- // done only by one player for each move:
- if (
- !!this.game.mycolor &&
- (this.game.type == "live" || move.color == this.game.mycolor)
- ) {
- let drawCode = "";
- switch (this.drawOffer) {
- case "threerep":
- drawCode = "t";
- break;
- case "sent":
- drawCode = this.game.mycolor;
- break;
- case "received":
- drawCode = this.vr.turn;
- break;
+ // Post-process a (potentially partial) move (which was just played in BaseGame)
+ // TODO?: wait for AJAX return to finish processing a move,
+ // and for opponent pingback in case of live game : if none received after e.g. 500ms, re-send newmove
+ // ...and provide move index with newmove event for basic check after receiving
+ processMove: function(move, data) {
+ const moveCol = this.vr.turn;
+ const doProcessMove = () => {
+ const colorIdx = ["w", "b"].indexOf(moveCol);
+ const nextIdx = 1 - colorIdx;
+ if (this.game.mycolor) {
+ // NOTE: 'var' to see that variable outside this block
+ var filtered_move = getFilteredMove(move);