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