| 1 | <template lang="pug"> |
| 2 | div#baseGame |
| 3 | input#modalEog.modal(type="checkbox") |
| 4 | div#eogDiv( |
| 5 | role="dialog" |
| 6 | data-checkbox="modalEog" |
| 7 | ) |
| 8 | .card.text-center |
| 9 | label.modal-close(for="modalEog") |
| 10 | h3.section {{ endgameMessage }} |
| 11 | #gameContainer |
| 12 | #boardContainer |
| 13 | Board( |
| 14 | ref="board" |
| 15 | :vr="vr" |
| 16 | :last-move="lastMove" |
| 17 | :analyze="mode=='analyze'" |
| 18 | :score="game.score" |
| 19 | :user-color="game.mycolor" |
| 20 | :orientation="orientation" |
| 21 | :vname="game.vname" |
| 22 | :incheck="incheck" |
| 23 | @play-move="play" |
| 24 | @click-square="clickSquare" |
| 25 | @rendered="adjustSize" |
| 26 | ) |
| 27 | #turnIndicator(v-if="showTurn") {{ turn }} |
| 28 | #controls.button-group |
| 29 | button(@click="gotoBegin()") |
| 30 | img.inline(src="/images/icons/fast-forward_rev.svg") |
| 31 | button(@click="undo()") |
| 32 | img.inline(src="/images/icons/play_rev.svg") |
| 33 | button(v-if="canFlip" @click="flip()") |
| 34 | img.inline(src="/images/icons/flip.svg") |
| 35 | button( |
| 36 | @click="runAutoplay()" |
| 37 | :class="{'in-autoplay': autoplay}" |
| 38 | ) |
| 39 | img.inline(src="/images/icons/autoplay.svg") |
| 40 | button(@click="play()") |
| 41 | img.inline(src="/images/icons/play.svg") |
| 42 | button(@click="gotoEnd()") |
| 43 | img.inline(src="/images/icons/fast-forward.svg") |
| 44 | p#fenAnalyze(v-show="showFen") {{ (!!vr ? vr.getFen() : "") }} |
| 45 | #movesList |
| 46 | MoveList( |
| 47 | ref="moveslist" |
| 48 | :show="showMoves" |
| 49 | :canAnalyze="canAnalyze" |
| 50 | :canDownload="allowDownloadPGN" |
| 51 | :score="game.score" |
| 52 | :message="game.scoreMsg" |
| 53 | :firstNum="firstMoveNumber" |
| 54 | :moves="moves" |
| 55 | :cursor="cursor" |
| 56 | :vname="game.vname" |
| 57 | @download="download" |
| 58 | @showrules="showRules" |
| 59 | @analyze="toggleAnalyze" |
| 60 | @goto-move="gotoMove" |
| 61 | @redraw-board="redrawBoard" |
| 62 | ) |
| 63 | .clearer |
| 64 | </template> |
| 65 | |
| 66 | <script> |
| 67 | import Board from "@/components/Board.vue"; |
| 68 | import MoveList from "@/components/MoveList.vue"; |
| 69 | import params from "@/parameters"; |
| 70 | import { store } from "@/store"; |
| 71 | import { getSquareId } from "@/utils/squareId"; |
| 72 | import { getDate } from "@/utils/datetime"; |
| 73 | import { processModalClick } from "@/utils/modalClick"; |
| 74 | import { getScoreMessage } from "@/utils/scoring"; |
| 75 | import { getFullNotation } from "@/utils/notation"; |
| 76 | import { undoMove } from "@/utils/playUndo"; |
| 77 | export default { |
| 78 | name: "my-base-game", |
| 79 | components: { |
| 80 | Board, |
| 81 | MoveList |
| 82 | }, |
| 83 | props: ["game"], |
| 84 | data: function() { |
| 85 | return { |
| 86 | st: store.state, |
| 87 | // NOTE: all following variables must be reset at the beginning of a game |
| 88 | vr: null, //VariantRules object, game state |
| 89 | endgameMessage: "", |
| 90 | orientation: "w", |
| 91 | mode: "", |
| 92 | gameMode: "", |
| 93 | score: "*", //'*' means 'unfinished' |
| 94 | moves: [], |
| 95 | cursor: -1, //index of the move just played |
| 96 | lastMove: null, |
| 97 | touchLastClick: "", |
| 98 | firstMoveNumber: 0, //for printing |
| 99 | incheck: [], //for Board |
| 100 | inMultimove: false, |
| 101 | autoplay: false, |
| 102 | inPlay: false, |
| 103 | stackToPlay: [] |
| 104 | }; |
| 105 | }, |
| 106 | computed: { |
| 107 | turn: function() { |
| 108 | if (!this.vr) return ""; |
| 109 | if (this.vr.showMoves != "all") { |
| 110 | return this.st.tr[ |
| 111 | (this.vr.turn == 'w' ? "White" : "Black") + " to move"]; |
| 112 | } |
| 113 | // Cannot flip (racing king or circular chess), or Monochrome |
| 114 | return ( |
| 115 | this.vr.movesCount == 0 && this.game.mycolor == "w" |
| 116 | ? this.st.tr["It's your turn!"] |
| 117 | : "" |
| 118 | ); |
| 119 | }, |
| 120 | showFen: function() { |
| 121 | return ( |
| 122 | this.mode == "analyze" && |
| 123 | this.$router.currentRoute.path.indexOf("/analyse") === -1 |
| 124 | ); |
| 125 | }, |
| 126 | // TODO: is it OK to pass "computed" as properties? |
| 127 | // Also, some are seemingly not recomputed when vr is initialized. |
| 128 | showMoves: function() { |
| 129 | return ( |
| 130 | !!this.game.score && this.game.score != "*" |
| 131 | ? "all" |
| 132 | : (!!this.vr ? this.vr.showMoves : "none") |
| 133 | ); |
| 134 | }, |
| 135 | showTurn: function() { |
| 136 | return ( |
| 137 | !!this.game.score && this.game.score == '*' && |
| 138 | !!this.vr && |
| 139 | ( |
| 140 | this.vr.showMoves != "all" || |
| 141 | !this.vr.canFlip || |
| 142 | this.vr.showFirstTurn |
| 143 | ) |
| 144 | ); |
| 145 | }, |
| 146 | canAnalyze: function() { |
| 147 | return ( |
| 148 | (!this.game.mode || this.game.mode != "analyze") && |
| 149 | !!this.vr && this.vr.canAnalyze |
| 150 | ); |
| 151 | }, |
| 152 | canFlip: function() { |
| 153 | return !!this.vr && this.vr.canFlip; |
| 154 | }, |
| 155 | allowDownloadPGN: function() { |
| 156 | return ( |
| 157 | (!!this.game.score && this.game.score != "*") || |
| 158 | (!!this.vr && !this.vr.someHiddenMoves) |
| 159 | ); |
| 160 | } |
| 161 | }, |
| 162 | created: function() { |
| 163 | if (!!this.game.fenStart) this.re_setVariables(); |
| 164 | }, |
| 165 | mounted: function() { |
| 166 | if (!("ontouchstart" in window)) { |
| 167 | // Desktop browser: |
| 168 | const baseGameDiv = document.getElementById("baseGame"); |
| 169 | baseGameDiv.tabIndex = 0; |
| 170 | baseGameDiv.addEventListener("click", this.focusBg); |
| 171 | baseGameDiv.addEventListener("keydown", this.handleKeys); |
| 172 | if (this.st.settings.scrollmove) |
| 173 | baseGameDiv.addEventListener("wheel", this.handleScroll); |
| 174 | } |
| 175 | document.getElementById("eogDiv") |
| 176 | .addEventListener("click", processModalClick); |
| 177 | }, |
| 178 | beforeDestroy: function() { |
| 179 | // TODO: probably not required |
| 180 | this.autoplay = false; |
| 181 | }, |
| 182 | methods: { |
| 183 | focusBg: function() { |
| 184 | document.getElementById("baseGame").focus(); |
| 185 | }, |
| 186 | handleKeys: function(e) { |
| 187 | if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault(); |
| 188 | switch (e.keyCode) { |
| 189 | case 37: |
| 190 | this.undo(); |
| 191 | break; |
| 192 | case 39: |
| 193 | this.play(); |
| 194 | break; |
| 195 | case 38: |
| 196 | this.gotoBegin(); |
| 197 | break; |
| 198 | case 40: |
| 199 | this.gotoEnd(); |
| 200 | break; |
| 201 | case 32: |
| 202 | this.flip(); |
| 203 | break; |
| 204 | } |
| 205 | }, |
| 206 | handleScroll: function(e) { |
| 207 | e.preventDefault(); |
| 208 | if (e.deltaY < 0) this.undo(); |
| 209 | else if (e.deltaY > 0) this.play(); |
| 210 | }, |
| 211 | adjustSize: function() { |
| 212 | this.$refs["moveslist"].adjustBoard("vertical"); |
| 213 | }, |
| 214 | redrawBoard: function() { |
| 215 | this.$refs["board"].re_setDrawings(); |
| 216 | }, |
| 217 | showRules: function() { |
| 218 | // The button is here only on Game page: |
| 219 | document.getElementById("modalRules").checked = true; |
| 220 | }, |
| 221 | re_setVariables: function(game) { |
| 222 | if (!game) game = this.game; //in case of... |
| 223 | this.endgameMessage = ""; |
| 224 | // "w": default orientation for observed games |
| 225 | this.orientation = game.mycolor || "w"; |
| 226 | this.mode = game.mode || game.type; //TODO: merge... |
| 227 | this.moves = JSON.parse(JSON.stringify(game.moves || [])); |
| 228 | // Post-processing: decorate each move with notation and FEN |
| 229 | this.vr = new V(game.fenStart); |
| 230 | this.inMultimove = false; //in case of |
| 231 | if (!!this.$refs["board"]) |
| 232 | // Also in case of: |
| 233 | this.$refs["board"].resetCurrentAttempt(); |
| 234 | let analyseBtn = document.getElementById("analyzeBtn"); |
| 235 | if (!!analyseBtn) analyseBtn.classList.remove("active"); |
| 236 | const parsedFen = V.ParseFen(game.fenStart); |
| 237 | const firstMoveColor = parsedFen.turn; |
| 238 | this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2) + 1; |
| 239 | let L = this.moves.length; |
| 240 | this.moves.forEach((move,idx) => { |
| 241 | // Strategy working also for multi-moves: |
| 242 | if (!Array.isArray(move)) move = [move]; |
| 243 | move.forEach(m => { |
| 244 | m.notation = this.vr.getNotation(m); |
| 245 | m.unambiguous = V.GetUnambiguousNotation(m); |
| 246 | this.vr.play(m); |
| 247 | }); |
| 248 | const Lm = move.length; |
| 249 | move[Lm - 1].fen = this.vr.getFen(); |
| 250 | if (idx < L - 1 && this.vr.getCheckSquares().length > 0) |
| 251 | move[Lm - 1].notation += "+"; |
| 252 | }); |
| 253 | this.incheck = this.vr.getCheckSquares(); |
| 254 | this.score = this.vr.getCurrentScore(); |
| 255 | if (L >= 1) { |
| 256 | const move = |
| 257 | !Array.isArray(this.moves[L - 1]) |
| 258 | ? [this.moves[L - 1]] |
| 259 | : this.moves[L - 1]; |
| 260 | const Lm = move.length; |
| 261 | if (["1-0", "0-1"].includes(this.score)) move[Lm - 1].notation += "#"; |
| 262 | else if (this.incheck.length > 0) move[Lm - 1].notation += "+"; |
| 263 | } |
| 264 | if (this.score != '*') { |
| 265 | // Show score on screen |
| 266 | const message = getScoreMessage(this.score); |
| 267 | this.showEndgameMsg(this.score + " . " + this.st.tr[message]); |
| 268 | } |
| 269 | if (firstMoveColor == "b") { |
| 270 | // 'start' & 'end' is required for Board component |
| 271 | this.moves.unshift({ |
| 272 | notation: "...", |
| 273 | unambiguous: "...", |
| 274 | start: { x: -1, y: -1 }, |
| 275 | end: { x: -1, y: -1 }, |
| 276 | fen: game.fenStart |
| 277 | }); |
| 278 | L++; |
| 279 | } |
| 280 | this.positionCursorTo(L - 1); |
| 281 | }, |
| 282 | positionCursorTo: function(index) { |
| 283 | this.cursor = index; |
| 284 | // Note: last move in moves array might be a multi-move |
| 285 | if (index >= 0) this.lastMove = this.moves[index]; |
| 286 | else this.lastMove = null; |
| 287 | }, |
| 288 | toggleAnalyze: function() { |
| 289 | // Freeze while choices are shown (and autoplay has priority) |
| 290 | if ( |
| 291 | this.inPlay || |
| 292 | this.$refs["board"].choices.length > 0 || |
| 293 | this.autoplay |
| 294 | ) { |
| 295 | return; |
| 296 | } |
| 297 | if (this.mode != "analyze") { |
| 298 | // Enter analyze mode: |
| 299 | this.gameMode = this.mode; //was not 'analyze' |
| 300 | this.mode = "analyze"; |
| 301 | if (this.inMultimove) this.cancelCurrentMultimove(); |
| 302 | this.gameCursor = this.cursor; |
| 303 | this.gameMoves = JSON.parse(JSON.stringify(this.moves)); |
| 304 | document.getElementById("analyzeBtn").classList.add("active"); |
| 305 | } |
| 306 | else { |
| 307 | // Exit analyze mode: |
| 308 | this.mode = this.gameMode; |
| 309 | this.cursor = this.gameCursor; |
| 310 | this.moves = this.gameMoves; |
| 311 | let fen = this.game.fenStart; |
| 312 | if (this.cursor >= 0) { |
| 313 | let mv = this.moves[this.cursor]; |
| 314 | if (!Array.isArray(mv)) mv = [mv]; |
| 315 | fen = mv[mv.length-1].fen; |
| 316 | } |
| 317 | this.vr = new V(fen); |
| 318 | this.inMultimove = false; //in case of |
| 319 | this.$refs["board"].resetCurrentAttempt(); //also in case of |
| 320 | this.incheck = this.vr.getCheckSquares(); |
| 321 | if (this.cursor >= 0) this.lastMove = this.moves[this.cursor]; |
| 322 | else this.lastMove = null; |
| 323 | document.getElementById("analyzeBtn").classList.remove("active"); |
| 324 | } |
| 325 | }, |
| 326 | download: function() { |
| 327 | const content = this.getPgn(); |
| 328 | // Prepare and trigger download link |
| 329 | let downloadAnchor = document.getElementById("download"); |
| 330 | downloadAnchor.setAttribute("download", "game.pgn"); |
| 331 | downloadAnchor.href = |
| 332 | "data:text/plain;charset=utf-8," + encodeURIComponent(content); |
| 333 | downloadAnchor.click(); |
| 334 | }, |
| 335 | getPgn: function() { |
| 336 | let pgn = ""; |
| 337 | pgn += '[Site "vchess.club"]\n'; |
| 338 | pgn += '[Variant "' + this.game.vname + '"]\n'; |
| 339 | const gdt = getDate(new Date(this.game.created || Date.now())); |
| 340 | pgn += '[Date "' + gdt + '"]\n'; |
| 341 | pgn += '[White "' + this.game.players[0].name + '"]\n'; |
| 342 | pgn += '[Black "' + this.game.players[1].name + '"]\n'; |
| 343 | pgn += '[Fen "' + this.game.fenStart + '"]\n'; |
| 344 | pgn += '[Result "' + this.game.score + '"]\n'; |
| 345 | if (!!this.game.id) |
| 346 | pgn += '[Url "' + params.serverUrl + '/game/' + this.game.id + '"]\n'; |
| 347 | if (!!this.game.cadence) |
| 348 | pgn += '[Cadence "' + this.game.cadence + '"]\n'; |
| 349 | pgn += '[Options "' + JSON.stringify(this.game.options) + '"]\n'; |
| 350 | pgn += '\n'; |
| 351 | for (let i = 0; i < this.moves.length; i += 2) { |
| 352 | if (i > 0) pgn += " "; |
| 353 | // Adjust dots notation for a better display: |
| 354 | let fullNotation = getFullNotation(this.moves[i]); |
| 355 | if (fullNotation == "...") fullNotation = ".."; |
| 356 | pgn += (i / 2 + this.firstMoveNumber) + "." + fullNotation; |
| 357 | if (i+1 < this.moves.length) |
| 358 | pgn += " " + getFullNotation(this.moves[i+1]); |
| 359 | } |
| 360 | pgn += "\n\n"; |
| 361 | for (let i = 0; i < this.moves.length; i += 2) { |
| 362 | const moveNumber = i / 2 + this.firstMoveNumber; |
| 363 | // Skip "dots move", useless for machine reading: |
| 364 | if (this.moves[i].notation != "...") { |
| 365 | pgn += moveNumber + ".w " + |
| 366 | getFullNotation(this.moves[i], "unambiguous") + "\n"; |
| 367 | } |
| 368 | if (i+1 < this.moves.length) { |
| 369 | pgn += moveNumber + ".b " + |
| 370 | getFullNotation(this.moves[i+1], "unambiguous") + "\n"; |
| 371 | } |
| 372 | } |
| 373 | return pgn; |
| 374 | }, |
| 375 | showEndgameMsg: function(message) { |
| 376 | this.endgameMessage = message; |
| 377 | document.getElementById("modalEog").checked = true; |
| 378 | }, |
| 379 | runAutoplay: function() { |
| 380 | if (this.autoplay) { |
| 381 | this.autoplay = false; |
| 382 | if (this.stackToPlay.length > 0) |
| 383 | // Move(s) arrived in-between |
| 384 | this.play(this.stackToPlay.pop(), "received"); |
| 385 | } |
| 386 | else if (this.cursor < this.moves.length - 1) { |
| 387 | this.autoplay = true; |
| 388 | this.play(null, null, null, "autoplay"); |
| 389 | } |
| 390 | }, |
| 391 | // Animate an elementary move |
| 392 | animateMove: function(move, callback) { |
| 393 | let startSquare = document.getElementById(getSquareId(move.start)); |
| 394 | if (!startSquare) return; //shouldn't happen but... |
| 395 | let endSquare = document.getElementById(getSquareId(move.end)); |
| 396 | let rectStart = startSquare.getBoundingClientRect(); |
| 397 | let rectEnd = endSquare.getBoundingClientRect(); |
| 398 | let translation = { |
| 399 | x: rectEnd.x - rectStart.x, |
| 400 | y: rectEnd.y - rectStart.y |
| 401 | }; |
| 402 | let movingPiece = document.querySelector( |
| 403 | "#" + getSquareId(move.start) + " > img.piece" |
| 404 | ); |
| 405 | // For some unknown reasons Opera get "movingPiece == null" error |
| 406 | // TODO: is it calling 'animate()' twice ? One extra time ? |
| 407 | if (!movingPiece) return; |
| 408 | const squares = document.getElementsByClassName("board"); |
| 409 | for (let i = 0; i < squares.length; i++) { |
| 410 | let square = squares.item(i); |
| 411 | if (square.id != getSquareId(move.start)) |
| 412 | // HACK for animation: |
| 413 | // (with positive translate, image slides "under background") |
| 414 | square.style.zIndex = "-1"; |
| 415 | } |
| 416 | movingPiece.style.transform = |
| 417 | "translate(" + translation.x + "px," + translation.y + "px)"; |
| 418 | movingPiece.style.transitionDuration = "0.25s"; |
| 419 | movingPiece.style.zIndex = "3000"; |
| 420 | setTimeout(() => { |
| 421 | for (let i = 0; i < squares.length; i++) |
| 422 | squares.item(i).style.zIndex = "auto"; |
| 423 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap |
| 424 | callback(); |
| 425 | }, 250); |
| 426 | }, |
| 427 | // For Analyse mode: |
| 428 | emitFenIfAnalyze: function() { |
| 429 | if (this.game.mode == "analyze") { |
| 430 | let fen = this.game.fenStart; |
| 431 | if (!!this.lastMove) { |
| 432 | if (Array.isArray(this.lastMove)) { |
| 433 | const L = this.lastMove.length; |
| 434 | fen = this.lastMove[L-1].fen; |
| 435 | } |
| 436 | else fen = this.lastMove.fen; |
| 437 | } |
| 438 | this.$emit("fenchange", fen); |
| 439 | } |
| 440 | }, |
| 441 | clickSquare: function(square) { |
| 442 | // Some variants make use of a single click at specific times: |
| 443 | const move_s = this.vr.doClick(square); |
| 444 | if (!!move_s) { |
| 445 | const playMove = () => { |
| 446 | if (!Array.isArray(move_s)) this.play(move_s); |
| 447 | else this.$refs["board"].choices = move_s; |
| 448 | } |
| 449 | if ("ontouchstart" in window) { |
| 450 | const squareId = "sq-" + square[0] + "-" + square[1]; |
| 451 | const highlight = function(on, sq) { |
| 452 | let elt = document.getElementById(sq); |
| 453 | if (!!elt) { |
| 454 | if (on) elt.classList.add("touch-hover"); |
| 455 | else elt.classList.remove("touch-hover"); |
| 456 | } |
| 457 | } |
| 458 | // Touch screen (smartphone): require confirmation |
| 459 | const squareStr = square[0] + "_" + square[1] |
| 460 | if (this.touchLastClick == squareId) { |
| 461 | highlight(false, squareId); |
| 462 | playMove(); |
| 463 | } |
| 464 | else { |
| 465 | highlight(true, squareId); |
| 466 | highlight(false, this.touchLastClick); |
| 467 | } |
| 468 | this.touchLastClick = squareId; |
| 469 | } |
| 470 | else playMove(); |
| 471 | } |
| 472 | }, |
| 473 | // "light": if gotoMove() or gotoEnd() |
| 474 | play: function(move, received, light, autoplay) { |
| 475 | // Freeze while choices are shown: |
| 476 | if ( |
| 477 | !!this.$refs["board"].selectedPiece || |
| 478 | this.$refs["board"].choices.length > 0 |
| 479 | ) { |
| 480 | return; |
| 481 | } |
| 482 | const navigate = !move; |
| 483 | // Forbid navigation during autoplay: |
| 484 | if (navigate && this.autoplay && !autoplay) return; |
| 485 | // Forbid playing outside analyze mode, except if move is received. |
| 486 | // Sufficient condition because Board already knows which turn it is. |
| 487 | if ( |
| 488 | this.mode != "analyze" && |
| 489 | !navigate && |
| 490 | !received && |
| 491 | (this.game.score != "*" || this.cursor < this.moves.length - 1) |
| 492 | ) { |
| 493 | return; |
| 494 | } |
| 495 | if (!!received) { |
| 496 | if (this.autoplay || this.inPlay) { |
| 497 | // Received moves while autoplaying are stacked, |
| 498 | // and in observed games they could arrive too fast: |
| 499 | this.stackToPlay.unshift(move); |
| 500 | return; |
| 501 | } |
| 502 | if (this.mode == "analyze") this.toggleAnalyze(); |
| 503 | if (this.cursor < this.moves.length - 1) |
| 504 | // To play a received move, cursor must be at the end of the game: |
| 505 | this.gotoEnd(); |
| 506 | this.inPlay = true; |
| 507 | } |
| 508 | // The board may show some possible moves: (TODO: bad solution) |
| 509 | this.$refs["board"].resetCurrentAttempt(); |
| 510 | const playSubmove = (smove) => { |
| 511 | smove.notation = this.vr.getNotation(smove); |
| 512 | smove.unambiguous = V.GetUnambiguousNotation(smove); |
| 513 | this.vr.play(smove); |
| 514 | if (this.inMultimove && !!this.lastMove) { |
| 515 | if (!Array.isArray(this.lastMove)) |
| 516 | this.lastMove = [this.lastMove, smove]; |
| 517 | else this.lastMove.push(smove); |
| 518 | } |
| 519 | if (!this.inMultimove) { |
| 520 | // First sub-move: |
| 521 | this.lastMove = smove; |
| 522 | // Condition is "!navigate" but we mean "!this.autoplay" |
| 523 | if (!navigate) { |
| 524 | if (this.cursor < this.moves.length - 1) |
| 525 | this.moves = this.moves.slice(0, this.cursor + 1); |
| 526 | this.moves.push(smove); |
| 527 | } |
| 528 | this.inMultimove = true; //potentially |
| 529 | this.cursor++; |
| 530 | } |
| 531 | else if (!navigate) { |
| 532 | // Already in the middle of a multi-move |
| 533 | const L = this.moves.length; |
| 534 | if (!Array.isArray(this.moves[L-1])) |
| 535 | this.$set(this.moves, L-1, [this.moves[L-1], smove]); |
| 536 | else this.moves[L-1].push(smove); |
| 537 | } |
| 538 | }; |
| 539 | const playMove = () => { |
| 540 | const animate = ( |
| 541 | ["all", "highlight"].includes(V.ShowMoves) && |
| 542 | (this.autoplay || !!received) |
| 543 | ); |
| 544 | if (!Array.isArray(move)) move = [move]; |
| 545 | let moveIdx = 0; |
| 546 | let self = this; |
| 547 | const initurn = this.vr.turn; |
| 548 | (function executeMove() { |
| 549 | const smove = move[moveIdx++]; |
| 550 | // NOTE: condition "smove.start.x >= 0" required for Dynamo, |
| 551 | // because second move may be empty. noHighlight condition |
| 552 | // is used at least for Chakart. |
| 553 | if (animate && smove.start.x >= 0 && !smove.end.noHighlight) { |
| 554 | self.animateMove(smove, () => { |
| 555 | playSubmove(smove); |
| 556 | if (moveIdx < move.length) setTimeout(executeMove, 500); |
| 557 | else afterMove(smove, initurn); |
| 558 | }); |
| 559 | } |
| 560 | else { |
| 561 | playSubmove(smove); |
| 562 | if (moveIdx < move.length) executeMove(); |
| 563 | else afterMove(smove, initurn); |
| 564 | } |
| 565 | })(); |
| 566 | }; |
| 567 | const computeScore = () => { |
| 568 | const score = this.vr.getCurrentScore(); |
| 569 | if (!navigate) { |
| 570 | if (["1-0", "0-1"].includes(score)) { |
| 571 | if (Array.isArray(this.lastMove)) { |
| 572 | const L = this.lastMove.length; |
| 573 | this.lastMove[L - 1].notation += "#"; |
| 574 | } |
| 575 | else this.lastMove.notation += "#"; |
| 576 | } |
| 577 | } |
| 578 | if (score != "*" && ["analyze", "versus"].includes(this.mode)) { |
| 579 | const message = getScoreMessage(score, V.ReverseColors); |
| 580 | // Show score on screen |
| 581 | this.showEndgameMsg(score + " . " + this.st.tr[message]); |
| 582 | } |
| 583 | return score; |
| 584 | }; |
| 585 | const afterMove = (smove, initurn) => { |
| 586 | if (this.vr.turn != initurn) { |
| 587 | // Turn has changed: move is complete |
| 588 | if (!smove.fen) |
| 589 | // NOTE: only FEN of last sub-move is required (=> setting it here) |
| 590 | smove.fen = this.vr.getFen(); |
| 591 | this.emitFenIfAnalyze(); |
| 592 | this.inMultimove = false; |
| 593 | this.incheck = this.vr.getCheckSquares(); |
| 594 | if (this.incheck.length > 0) smove.notation += "+"; |
| 595 | this.score = computeScore(); |
| 596 | if (this.autoplay) { |
| 597 | if (this.cursor < this.moves.length - 1) |
| 598 | setTimeout(() => this.play(null, null, null, "autoplay"), 1000); |
| 599 | else { |
| 600 | this.autoplay = false; |
| 601 | if (this.stackToPlay.length > 0) |
| 602 | // Move(s) arrived in-between |
| 603 | this.play(this.stackToPlay.pop(), "received"); |
| 604 | } |
| 605 | } |
| 606 | if (this.mode != "analyze" && !navigate) { |
| 607 | if (!received) { |
| 608 | // Post-processing (e.g. computer play). |
| 609 | const L = this.moves.length; |
| 610 | // NOTE: always emit the score, even in unfinished |
| 611 | this.$emit("newmove", this.moves[L-1], { score: this.score }); |
| 612 | } |
| 613 | else { |
| 614 | this.inPlay = false; |
| 615 | if (this.stackToPlay.length > 0) |
| 616 | // Move(s) arrived in-between |
| 617 | this.play(this.stackToPlay.pop(), "received"); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | }; |
| 622 | // NOTE: navigate and received are mutually exclusive |
| 623 | if (navigate) { |
| 624 | // The move to navigate to is necessarily full: |
| 625 | if (this.cursor == this.moves.length - 1) return; //no more moves |
| 626 | move = this.moves[this.cursor + 1]; |
| 627 | if (!this.autoplay) { |
| 628 | // Just play the move: |
| 629 | if (!Array.isArray(move)) move = [move]; |
| 630 | for (let i=0; i < move.length; i++) this.vr.play(move[i]); |
| 631 | if (!light) { |
| 632 | this.lastMove = move; |
| 633 | this.incheck = this.vr.getCheckSquares(); |
| 634 | this.score = computeScore(); |
| 635 | this.emitFenIfAnalyze(); |
| 636 | } |
| 637 | this.cursor++; |
| 638 | return; |
| 639 | } |
| 640 | } |
| 641 | playMove(); |
| 642 | }, |
| 643 | cancelCurrentMultimove: function() { |
| 644 | const L = this.moves.length; |
| 645 | let move = this.moves[L-1]; |
| 646 | if (!Array.isArray(move)) move = [move]; |
| 647 | for (let i = move.length - 1; i >= 0; i--) this.vr.undo(move[i]); |
| 648 | this.moves.pop(); |
| 649 | this.cursor--; |
| 650 | this.inMultimove = false; |
| 651 | }, |
| 652 | cancelLastMove: function() { |
| 653 | // The last played move was canceled (corr game) |
| 654 | this.undo(); |
| 655 | this.moves.pop(); |
| 656 | }, |
| 657 | // "light": if gotoMove() or gotoBegin() |
| 658 | undo: function(move, light) { |
| 659 | if ( |
| 660 | this.autoplay || |
| 661 | !!this.$refs["board"].selectedPiece || |
| 662 | this.$refs["board"].choices.length > 0 |
| 663 | ) { |
| 664 | return; |
| 665 | } |
| 666 | this.$refs["board"].resetCurrentAttempt(); |
| 667 | if (this.inMultimove) { |
| 668 | this.cancelCurrentMultimove(); |
| 669 | this.incheck = this.vr.getCheckSquares(); |
| 670 | if (this.cursor >= 0) this.lastMove = this.moves[this.cursor]; |
| 671 | else this.lastMove = null; |
| 672 | } |
| 673 | else { |
| 674 | if (!move) { |
| 675 | const minCursor = |
| 676 | this.moves.length > 0 && this.moves[0].notation == "..." |
| 677 | ? 1 |
| 678 | : 0; |
| 679 | if (this.cursor < minCursor) return; //no more moves |
| 680 | move = this.moves[this.cursor]; |
| 681 | } |
| 682 | this.$refs["board"].resetCurrentAttempt(); |
| 683 | undoMove(move, this.vr); |
| 684 | if (light) this.cursor--; |
| 685 | else { |
| 686 | this.positionCursorTo(this.cursor - 1); |
| 687 | this.incheck = this.vr.getCheckSquares(); |
| 688 | this.emitFenIfAnalyze(); |
| 689 | } |
| 690 | } |
| 691 | }, |
| 692 | gotoMove: function(index) { |
| 693 | if ( |
| 694 | this.autoplay || |
| 695 | !!this.$refs["board"].selectedPiece || |
| 696 | this.$refs["board"].choices.length > 0 |
| 697 | ) { |
| 698 | return; |
| 699 | } |
| 700 | this.$refs["board"].resetCurrentAttempt(); |
| 701 | if (this.inMultimove) this.cancelCurrentMultimove(); |
| 702 | if (index == this.cursor) return; |
| 703 | if (index < this.cursor) { |
| 704 | while (this.cursor > index) |
| 705 | this.undo(null, null, "light"); |
| 706 | } |
| 707 | else { |
| 708 | // index > this.cursor) |
| 709 | while (this.cursor < index) |
| 710 | this.play(null, null, "light"); |
| 711 | } |
| 712 | // NOTE: next line also re-assign cursor, but it's very light |
| 713 | this.positionCursorTo(index); |
| 714 | this.incheck = this.vr.getCheckSquares(); |
| 715 | this.emitFenIfAnalyze(); |
| 716 | }, |
| 717 | gotoBegin: function() { |
| 718 | if ( |
| 719 | this.autoplay || |
| 720 | !!this.$refs["board"].selectedPiece || |
| 721 | this.$refs["board"].choices.length > 0 |
| 722 | ) { |
| 723 | return; |
| 724 | } |
| 725 | this.$refs["board"].resetCurrentAttempt(); |
| 726 | if (this.inMultimove) this.cancelCurrentMultimove(); |
| 727 | const minCursor = |
| 728 | this.moves.length > 0 && this.moves[0].notation == "..." |
| 729 | ? 1 |
| 730 | : 0; |
| 731 | while (this.cursor >= minCursor) this.undo(null, null, "light"); |
| 732 | this.lastMove = (minCursor == 1 ? this.moves[0] : null); |
| 733 | this.incheck = this.vr.getCheckSquares(); |
| 734 | this.emitFenIfAnalyze(); |
| 735 | }, |
| 736 | gotoEnd: function() { |
| 737 | if (this.cursor == this.moves.length - 1) return; |
| 738 | this.gotoMove(this.moves.length - 1); |
| 739 | }, |
| 740 | flip: function() { |
| 741 | if (this.$refs["board"].choices.length > 0) return; |
| 742 | this.orientation = V.GetOppCol(this.orientation); |
| 743 | } |
| 744 | } |
| 745 | }; |
| 746 | </script> |
| 747 | |
| 748 | <style lang="sass" scoped> |
| 749 | [type="checkbox"]#modalEog+div .card |
| 750 | min-height: 45px |
| 751 | max-width: 350px |
| 752 | |
| 753 | #baseGame |
| 754 | width: 100% |
| 755 | &:focus |
| 756 | outline: none |
| 757 | |
| 758 | #gameContainer |
| 759 | margin-left: auto |
| 760 | margin-right: auto |
| 761 | |
| 762 | #downloadDiv |
| 763 | display: inline-block |
| 764 | |
| 765 | #controls |
| 766 | user-select: none |
| 767 | button |
| 768 | border: none |
| 769 | margin: 0 |
| 770 | padding-top: 5px |
| 771 | padding-bottom: 5px |
| 772 | |
| 773 | p#fenAnalyze |
| 774 | margin: 5px |
| 775 | |
| 776 | .in-autoplay |
| 777 | background-color: #FACF8C |
| 778 | |
| 779 | img.inline |
| 780 | height: 22px |
| 781 | padding-top: 5px |
| 782 | @media screen and (max-width: 767px) |
| 783 | height: 18px |
| 784 | |
| 785 | #turnIndicator |
| 786 | text-align: center |
| 787 | font-weight: bold |
| 788 | |
| 789 | #boardContainer |
| 790 | float: left |
| 791 | // TODO: later, maybe, allow movesList of variable width |
| 792 | // or e.g. between 250 and 350px (but more complicated) |
| 793 | |
| 794 | #movesList |
| 795 | width: 280px |
| 796 | float: left |
| 797 | |
| 798 | @media screen and (max-width: 767px) |
| 799 | #movesList |
| 800 | width: 100% |
| 801 | float: none |
| 802 | clear: both |
| 803 | </style> |