- if (navigate)
- {
- if (this.cursor == this.moves.length-1)
- return; //no more moves
- move = this.moves[this.cursor+1];
- }
- if (!!programmatic) //computer or (remote) human opponent
- {
- if (this.cursor < this.moves.length-1)
- this.gotoEnd(); //required to play the move
- 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 => {});
- // Send the move to web worker (including his own moves) //TODO: doesn't work here --> need to send an event instead
- this.compWorker.postMessage(["newmove",move]);
- if (!navigate && (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);
- const score = this.vr.getCurrentScore();
- if (score != "*")
- {
- if (!this.analyze)
- this.endGame(score);
- else //just show score on screen (allow undo)
- this.showScoreMsg(score);
- }
- // subTurn condition for Marseille (and Avalanche) rules
- else if ((this.mode == "computer" && (!this.vr.subTurn || this.vr.subTurn <= 1))
- && (this.subMode == "auto" || this.vr.turn != this.mycolor))
- {
- this.playComputerMove();
- }
- // https://vuejs.org/v2/guide/list.html#Caveats (also for undo)
- //if (navigate)
- // this.$children[0].$forceUpdate(); //TODO!?
- },
- undo: function(move) {
- let navigate = !move;
- if (navigate)
- {
- if (this.cursor < 0)
- return; //no more moves
- move = this.moves[this.cursor];
- }
- this.vr.undo(move);
+ if (!!received) {
+ if (this.autoplay || this.inPlay) {
+ // Received moves while autoplaying are stacked,
+ // and in observed games they could arrive too fast:
+ this.stackToPlay.unshift(move);
+ return;
+ }
+ if (this.mode == "analyze") this.toggleAnalyze();
+ if (this.cursor < this.moves.length - 1)
+ // To play a received move, cursor must be at the end of the game:
+ this.gotoEnd();
+ this.inPlay = true;
+ }
+ // The board may show some possible moves: (TODO: bad solution)
+ this.$refs["board"].resetCurrentAttempt();
+ const playSubmove = (smove) => {
+ smove.notation = this.vr.getNotation(smove);
+ smove.unambiguous = V.GetUnambiguousNotation(smove);
+ this.vr.play(smove);
+ if (this.inMultimove && !!this.lastMove) {
+ if (!Array.isArray(this.lastMove))
+ this.lastMove = [this.lastMove, smove];
+ else this.lastMove.push(smove);
+ }
+ if (!this.inMultimove) {
+ // First sub-move:
+ this.lastMove = smove;
+ // Condition is "!navigate" but we mean "!this.autoplay"
+ if (!navigate) {
+ if (this.cursor < this.moves.length - 1)
+ this.moves = this.moves.slice(0, this.cursor + 1);
+ this.moves.push(smove);
+ }
+ this.inMultimove = true; //potentially
+ this.cursor++;
+ }
+ else if (!navigate) {
+ // Already in the middle of a multi-move
+ const L = this.moves.length;
+ if (!Array.isArray(this.moves[L-1]))
+ this.$set(this.moves, L-1, [this.moves[L-1], smove]);
+ else this.moves[L-1].push(smove);
+ }
+ };
+ const playMove = () => {
+ const animate = (
+ ["all", "highlight"].includes(V.ShowMoves) &&
+ (this.autoplay || !!received)
+ );
+ if (!Array.isArray(move)) move = [move];
+ let moveIdx = 0;
+ let self = this;
+ const initurn = this.vr.turn;
+ (function executeMove() {
+ const smove = move[moveIdx++];
+ // NOTE: condition "smove.start.x >= 0" required for Dynamo,
+ // because second move may be empty. noHighlight condition
+ // is used at least for Chakart.
+ if (animate && smove.start.x >= 0 && !smove.end.noHighlight) {
+ self.animateMove(smove, () => {
+ playSubmove(smove);
+ if (moveIdx < move.length) setTimeout(executeMove, 500);
+ else afterMove(smove, initurn);
+ });
+ }
+ else {
+ playSubmove(smove);
+ if (moveIdx < move.length) executeMove();
+ else afterMove(smove, initurn);
+ }
+ })();
+ };
+ const computeScore = () => {
+ const score = this.vr.getCurrentScore();
+ if (!navigate) {
+ if (["1-0", "0-1"].includes(score)) {
+ if (Array.isArray(this.lastMove)) {
+ const L = this.lastMove.length;
+ this.lastMove[L - 1].notation += "#";
+ }
+ else this.lastMove.notation += "#";
+ }
+ }
+ if (score != "*" && ["analyze", "versus"].includes(this.mode)) {
+ const message = getScoreMessage(score);
+ // Show score on screen
+ this.showEndgameMsg(score + " . " + this.st.tr[message]);
+ }
+ return score;
+ };
+ const afterMove = (smove, initurn) => {
+ if (this.vr.turn != initurn) {
+ // Turn has changed: move is complete
+ if (!smove.fen)
+ // NOTE: only FEN of last sub-move is required (=> setting it here)
+ smove.fen = this.vr.getFen();
+ this.emitFenIfAnalyze();
+ this.inMultimove = false;
+ this.incheck = this.vr.getCheckSquares();
+ if (this.incheck.length > 0) smove.notation += "+";
+ this.score = computeScore();
+ if (this.autoplay) {
+ if (this.cursor < this.moves.length - 1)
+ setTimeout(() => this.play(null, null, null, "autoplay"), 1000);
+ else {
+ this.autoplay = false;
+ if (this.stackToPlay.length > 0)
+ // Move(s) arrived in-between
+ this.play(this.stackToPlay.pop(), "received");
+ }
+ }
+ if (this.mode != "analyze" && !navigate) {
+ if (!received) {
+ // Post-processing (e.g. computer play).
+ const L = this.moves.length;
+ // NOTE: always emit the score, even in unfinished
+ this.$emit("newmove", this.moves[L-1], { score: this.score });
+ }
+ else {
+ this.inPlay = false;
+ if (this.stackToPlay.length > 0)
+ // Move(s) arrived in-between
+ this.play(this.stackToPlay.pop(), "received");
+ }
+ }
+ }
+ };
+ // NOTE: navigate and received are mutually exclusive
+ if (navigate) {
+ // The move to navigate to is necessarily full:
+ if (this.cursor == this.moves.length - 1) return; //no more moves
+ move = this.moves[this.cursor + 1];
+ if (!this.autoplay) {
+ // Just play the move:
+ if (!Array.isArray(move)) move = [move];
+ for (let i=0; i < move.length; i++) this.vr.play(move[i]);
+ if (!light) {
+ this.lastMove = move;
+ this.incheck = this.vr.getCheckSquares();
+ this.score = computeScore();
+ this.emitFenIfAnalyze();
+ }
+ this.cursor++;
+ return;
+ }
+ }
+ playMove();
+ },
+ cancelCurrentMultimove: function() {
+ const L = this.moves.length;
+ let move = this.moves[L-1];
+ if (!Array.isArray(move)) move = [move];
+ for (let i = move.length - 1; i >= 0; i--) this.vr.undo(move[i]);
+ this.moves.pop();