| 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 | :vr="vr" |
| 15 | :last-move="lastMove" |
| 16 | :analyze="game.mode=='analyze'" |
| 17 | :score="game.score" |
| 18 | :user-color="game.mycolor" |
| 19 | :orientation="orientation" |
| 20 | :vname="game.vname" |
| 21 | :incheck="incheck" |
| 22 | @play-move="play" |
| 23 | ) |
| 24 | #turnIndicator(v-if="showTurn") {{ turn }} |
| 25 | #controls.button-group |
| 26 | button(@click="gotoBegin()") |
| 27 | img.inline(src="/images/icons/fast-forward_rev.svg") |
| 28 | button(@click="undo()") |
| 29 | img.inline(src="/images/icons/play_rev.svg") |
| 30 | button(v-if="canFlip" @click="flip()") |
| 31 | img.inline(src="/images/icons/flip.svg") |
| 32 | button(@click="play()") |
| 33 | img.inline(src="/images/icons/play.svg") |
| 34 | button(@click="gotoEnd()") |
| 35 | img.inline(src="/images/icons/fast-forward.svg") |
| 36 | #movesList |
| 37 | MoveList( |
| 38 | :show="showMoves" |
| 39 | :canAnalyze="canAnalyze" |
| 40 | :canDownload="allowDownloadPGN" |
| 41 | :score="game.score" |
| 42 | :message="game.scoreMsg" |
| 43 | :firstNum="firstMoveNumber" |
| 44 | :moves="moves" |
| 45 | :cursor="cursor" |
| 46 | @download="download" |
| 47 | @showrules="showRules" |
| 48 | @analyze="analyzePosition" |
| 49 | @goto-move="gotoMove" |
| 50 | ) |
| 51 | .clearer |
| 52 | </template> |
| 53 | |
| 54 | <script> |
| 55 | import Board from "@/components/Board.vue"; |
| 56 | import MoveList from "@/components/MoveList.vue"; |
| 57 | import { store } from "@/store"; |
| 58 | import { getSquareId } from "@/utils/squareId"; |
| 59 | import { getDate } from "@/utils/datetime"; |
| 60 | import { processModalClick } from "@/utils/modalClick"; |
| 61 | import { getScoreMessage } from "@/utils/scoring"; |
| 62 | import { getFullNotation } from "@/utils/notation"; |
| 63 | import { undoMove } from "@/utils/playUndo"; |
| 64 | export default { |
| 65 | name: "my-base-game", |
| 66 | components: { |
| 67 | Board, |
| 68 | MoveList |
| 69 | }, |
| 70 | props: ["game"], |
| 71 | data: function() { |
| 72 | return { |
| 73 | st: store.state, |
| 74 | // NOTE: all following variables must be reset at the beginning of a game |
| 75 | vr: null, //VariantRules object, game state |
| 76 | endgameMessage: "", |
| 77 | orientation: "w", |
| 78 | score: "*", //'*' means 'unfinished' |
| 79 | moves: [], |
| 80 | cursor: -1, //index of the move just played |
| 81 | lastMove: null, |
| 82 | firstMoveNumber: 0, //for printing |
| 83 | incheck: [], //for Board |
| 84 | inMultimove: false, |
| 85 | inPlay: false, |
| 86 | stackToPlay: [] |
| 87 | }; |
| 88 | }, |
| 89 | computed: { |
| 90 | showMoves: function() { |
| 91 | return this.game.score != "*" |
| 92 | ? "all" |
| 93 | : (this.vr ? this.vr.showMoves : "none"); |
| 94 | }, |
| 95 | showTurn: function() { |
| 96 | return ( |
| 97 | this.game.score == '*' && |
| 98 | this.vr && |
| 99 | (this.vr.showMoves != "all" || !this.vr.canFlip) |
| 100 | ); |
| 101 | }, |
| 102 | turn: function() { |
| 103 | if (!this.vr) |
| 104 | return ""; |
| 105 | if (this.vr.showMoves != "all") |
| 106 | return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"] |
| 107 | // Cannot flip: racing king or circular chess |
| 108 | return this.vr.movesCount == 0 && this.game.mycolor == "w" |
| 109 | ? this.st.tr["It's your turn!"] |
| 110 | : ""; |
| 111 | }, |
| 112 | canAnalyze: function() { |
| 113 | return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze; |
| 114 | }, |
| 115 | canFlip: function() { |
| 116 | return this.vr && this.vr.canFlip; |
| 117 | }, |
| 118 | allowDownloadPGN: function() { |
| 119 | return this.game.score != "*" || (this.vr && this.vr.showMoves == "all"); |
| 120 | } |
| 121 | }, |
| 122 | created: function() { |
| 123 | if (!!this.game.fenStart) this.re_setVariables(); |
| 124 | }, |
| 125 | mounted: function() { |
| 126 | if (!("ontouchstart" in window)) { |
| 127 | // Desktop browser: |
| 128 | const baseGameDiv = document.getElementById("baseGame"); |
| 129 | baseGameDiv.tabIndex = 0; |
| 130 | baseGameDiv.addEventListener("click", this.focusBg); |
| 131 | baseGameDiv.addEventListener("keydown", this.handleKeys); |
| 132 | baseGameDiv.addEventListener("wheel", this.handleScroll); |
| 133 | } |
| 134 | document.getElementById("eogDiv") |
| 135 | .addEventListener("click", processModalClick); |
| 136 | }, |
| 137 | methods: { |
| 138 | focusBg: function() { |
| 139 | document.getElementById("baseGame").focus(); |
| 140 | }, |
| 141 | handleKeys: function(e) { |
| 142 | if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault(); |
| 143 | switch (e.keyCode) { |
| 144 | case 37: |
| 145 | this.undo(); |
| 146 | break; |
| 147 | case 39: |
| 148 | this.play(); |
| 149 | break; |
| 150 | case 38: |
| 151 | this.gotoBegin(); |
| 152 | break; |
| 153 | case 40: |
| 154 | this.gotoEnd(); |
| 155 | break; |
| 156 | case 32: |
| 157 | this.flip(); |
| 158 | break; |
| 159 | } |
| 160 | }, |
| 161 | handleScroll: function(e) { |
| 162 | e.preventDefault(); |
| 163 | if (e.deltaY < 0) this.undo(); |
| 164 | else if (e.deltaY > 0) this.play(); |
| 165 | }, |
| 166 | showRules: function() { |
| 167 | //this.$router.push("/variants/" + this.game.vname); |
| 168 | window.open("#/variants/" + this.game.vname, "_blank"); //better |
| 169 | }, |
| 170 | re_setVariables: function(game) { |
| 171 | if (!game) game = this.game; //in case of... |
| 172 | this.endgameMessage = ""; |
| 173 | // "w": default orientation for observed games |
| 174 | this.orientation = game.mycolor || "w"; |
| 175 | this.moves = JSON.parse(JSON.stringify(game.moves || [])); |
| 176 | // Post-processing: decorate each move with notation and FEN |
| 177 | this.vr = new V(game.fenStart); |
| 178 | const parsedFen = V.ParseFen(game.fenStart); |
| 179 | const firstMoveColor = parsedFen.turn; |
| 180 | this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2); |
| 181 | this.moves.forEach(move => { |
| 182 | // Strategy working also for multi-moves: |
| 183 | if (!Array.isArray(move)) move = [move]; |
| 184 | move.forEach(m => { |
| 185 | m.notation = this.vr.getNotation(m); |
| 186 | this.vr.play(m); |
| 187 | }); |
| 188 | }); |
| 189 | if (firstMoveColor == "b") { |
| 190 | // 'start' & 'end' is required for Board component |
| 191 | this.moves.unshift({ |
| 192 | notation: "...", |
| 193 | start: { x: -1, y: -1 }, |
| 194 | end: { x: -1, y: -1 }, |
| 195 | fen: game.fenStart |
| 196 | }); |
| 197 | } |
| 198 | this.positionCursorTo(this.moves.length - 1); |
| 199 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 200 | }, |
| 201 | positionCursorTo: function(index) { |
| 202 | this.cursor = index; |
| 203 | // Caution: last move in moves array might be a multi-move |
| 204 | if (index >= 0) { |
| 205 | if (Array.isArray(this.moves[index])) { |
| 206 | const L = this.moves[index].length; |
| 207 | this.lastMove = this.moves[index][L - 1]; |
| 208 | } else { |
| 209 | this.lastMove = this.moves[index]; |
| 210 | } |
| 211 | } else this.lastMove = null; |
| 212 | }, |
| 213 | analyzePosition: function() { |
| 214 | let newUrl = |
| 215 | "/analyse/" + |
| 216 | this.game.vname + |
| 217 | "/?fen=" + |
| 218 | this.vr.getFen().replace(/ /g, "_"); |
| 219 | if (this.game.mycolor) |
| 220 | newUrl += "&side=" + this.game.mycolor; |
| 221 | // Open in same tab in live games (against cheating) |
| 222 | if (this.game.type == "live") this.$router.push(newUrl); |
| 223 | else window.open("#" + newUrl); |
| 224 | }, |
| 225 | download: function() { |
| 226 | const content = this.getPgn(); |
| 227 | // Prepare and trigger download link |
| 228 | let downloadAnchor = document.getElementById("download"); |
| 229 | downloadAnchor.setAttribute("download", "game.pgn"); |
| 230 | downloadAnchor.href = |
| 231 | "data:text/plain;charset=utf-8," + encodeURIComponent(content); |
| 232 | downloadAnchor.click(); |
| 233 | }, |
| 234 | getPgn: function() { |
| 235 | let pgn = ""; |
| 236 | pgn += '[Site "vchess.club"]\n'; |
| 237 | pgn += '[Variant "' + this.game.vname + '"]\n'; |
| 238 | pgn += '[Date "' + getDate(new Date()) + '"]\n'; |
| 239 | pgn += '[White "' + this.game.players[0].name + '"]\n'; |
| 240 | pgn += '[Black "' + this.game.players[1].name + '"]\n'; |
| 241 | pgn += '[Fen "' + this.game.fenStart + '"]\n'; |
| 242 | pgn += '[Result "' + this.game.score + '"]\n\n'; |
| 243 | for (let i = 0; i < this.moves.length; i += 2) { |
| 244 | pgn += (i/2+1) + "." + getFullNotation(this.moves[i]) + " "; |
| 245 | if (i+1 < this.moves.length) |
| 246 | pgn += getFullNotation(this.moves[i+1]) + " "; |
| 247 | } |
| 248 | return pgn + "\n"; |
| 249 | }, |
| 250 | showEndgameMsg: function(message) { |
| 251 | this.endgameMessage = message; |
| 252 | document.getElementById("modalEog").checked = true; |
| 253 | }, |
| 254 | // Animate an elementary move |
| 255 | animateMove: function(move, callback) { |
| 256 | let startSquare = document.getElementById(getSquareId(move.start)); |
| 257 | if (!startSquare) return; //shouldn't happen but... |
| 258 | let endSquare = document.getElementById(getSquareId(move.end)); |
| 259 | let rectStart = startSquare.getBoundingClientRect(); |
| 260 | let rectEnd = endSquare.getBoundingClientRect(); |
| 261 | let translation = { |
| 262 | x: rectEnd.x - rectStart.x, |
| 263 | y: rectEnd.y - rectStart.y |
| 264 | }; |
| 265 | let movingPiece = document.querySelector( |
| 266 | "#" + getSquareId(move.start) + " > img.piece" |
| 267 | ); |
| 268 | // For some unknown reasons Opera get "movingPiece == null" error |
| 269 | // TOOO: is it calling 'animate()' twice ? One extra time ? |
| 270 | if (!movingPiece) return; |
| 271 | // HACK for animation (with positive translate, image slides "under background") |
| 272 | // Possible improvement: just alter squares on the piece's way... |
| 273 | const squares = document.getElementsByClassName("board"); |
| 274 | for (let i = 0; i < squares.length; i++) { |
| 275 | let square = squares.item(i); |
| 276 | if (square.id != getSquareId(move.start)) square.style.zIndex = "-1"; |
| 277 | } |
| 278 | movingPiece.style.transform = |
| 279 | "translate(" + translation.x + "px," + translation.y + "px)"; |
| 280 | movingPiece.style.transitionDuration = "0.25s"; |
| 281 | movingPiece.style.zIndex = "3000"; |
| 282 | setTimeout(() => { |
| 283 | for (let i = 0; i < squares.length; i++) |
| 284 | squares.item(i).style.zIndex = "auto"; |
| 285 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap |
| 286 | callback(); |
| 287 | }, 250); |
| 288 | }, |
| 289 | // For Analyse mode: |
| 290 | emitFenIfAnalyze: function() { |
| 291 | if (this.game.mode == "analyze") { |
| 292 | this.$emit( |
| 293 | "fenchange", |
| 294 | this.lastMove ? this.lastMove.fen : this.game.fenStart |
| 295 | ); |
| 296 | } |
| 297 | }, |
| 298 | // "light": if gotoMove() or gotoEnd() |
| 299 | play: function(move, received, light, noemit) { |
| 300 | if (!!noemit) { |
| 301 | if (this.inPlay) { |
| 302 | // Received moves in observed games can arrive too fast: |
| 303 | this.stackToPlay.unshift(move); |
| 304 | return; |
| 305 | } |
| 306 | this.inPlay = true; |
| 307 | } |
| 308 | const navigate = !move; |
| 309 | const playSubmove = (smove) => { |
| 310 | if (!navigate) smove.notation = this.vr.getNotation(smove); |
| 311 | this.vr.play(smove); |
| 312 | // Is opponent in check? |
| 313 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 314 | if (!navigate) { |
| 315 | if (!this.inMultimove) { |
| 316 | if (this.cursor < this.moves.length - 1) |
| 317 | this.moves = this.moves.slice(0, this.cursor + 1); |
| 318 | this.moves.push(smove); |
| 319 | this.inMultimove = true; //potentially |
| 320 | this.cursor++; |
| 321 | } else { |
| 322 | // Already in the middle of a multi-move |
| 323 | const L = this.moves.length; |
| 324 | if (!Array.isArray(this.moves[L-1])) |
| 325 | this.$set(this.moves, L-1, [this.moves[L-1], smove]); |
| 326 | else |
| 327 | this.$set(this.moves, L-1, this.moves.concat([smove])); |
| 328 | } |
| 329 | } |
| 330 | }; |
| 331 | const playMove = () => { |
| 332 | const animate = V.ShowMoves == "all" && (received || navigate); |
| 333 | if (!Array.isArray(move)) move = [move]; |
| 334 | let moveIdx = 0; |
| 335 | let self = this; |
| 336 | const initurn = this.vr.turn; |
| 337 | (function executeMove() { |
| 338 | const smove = move[moveIdx++]; |
| 339 | if (animate) { |
| 340 | self.animateMove(smove, () => { |
| 341 | playSubmove(smove); |
| 342 | if (moveIdx < move.length) |
| 343 | setTimeout(executeMove, 500); |
| 344 | else afterMove(smove, initurn); |
| 345 | }); |
| 346 | } else { |
| 347 | playSubmove(smove); |
| 348 | if (moveIdx < move.length) executeMove(); |
| 349 | else afterMove(smove, initurn); |
| 350 | } |
| 351 | })(); |
| 352 | }; |
| 353 | const afterMove = (smove, initurn) => { |
| 354 | if (this.vr.turn != initurn) { |
| 355 | // Turn has changed: move is complete |
| 356 | if (!smove.fen) |
| 357 | // NOTE: only FEN of last sub-move is required (thus setting it here) |
| 358 | smove.fen = this.vr.getFen(); |
| 359 | this.lastMove = smove; |
| 360 | this.emitFenIfAnalyze(); |
| 361 | this.inMultimove = false; |
| 362 | if (!noemit) { |
| 363 | var score = this.vr.getCurrentScore(); |
| 364 | if (score != "*" && this.game.mode == "analyze") { |
| 365 | const message = getScoreMessage(score); |
| 366 | // Just show score on screen (allow undo) |
| 367 | this.showEndgameMsg(score + " . " + this.st.tr[message]); |
| 368 | } |
| 369 | } |
| 370 | if (!navigate && this.game.mode != "analyze") { |
| 371 | const L = this.moves.length; |
| 372 | if (!noemit) |
| 373 | // Post-processing (e.g. computer play). |
| 374 | // NOTE: always emit the score, even in unfinished, |
| 375 | // to tell Game::processMove() that it's not a received move. |
| 376 | this.$emit("newmove", this.moves[L-1], { score: score }); |
| 377 | else { |
| 378 | this.inPlay = false; |
| 379 | if (this.stackToPlay.length > 0) |
| 380 | // Move(s) arrived in-between |
| 381 | this.play(this.stackToPlay.pop(), received, light, noemit); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | }; |
| 386 | // NOTE: navigate and received are mutually exclusive |
| 387 | if (navigate) { |
| 388 | // The move to navigate to is necessarily full: |
| 389 | if (this.cursor == this.moves.length - 1) return; //no more moves |
| 390 | move = this.moves[this.cursor + 1]; |
| 391 | if (light) { |
| 392 | // Just play the move, nothing else: |
| 393 | if (!Array.isArray(move)) move = [move]; |
| 394 | for (let i=0; i < move.length; i++) this.vr.play(move[i]); |
| 395 | } |
| 396 | else { |
| 397 | playMove(); |
| 398 | this.emitFenIfAnalyze(); |
| 399 | } |
| 400 | this.cursor++; |
| 401 | return; |
| 402 | } |
| 403 | // Forbid playing outside analyze mode, except if move is received. |
| 404 | // Sufficient condition because Board already knows which turn it is. |
| 405 | if ( |
| 406 | this.game.mode != "analyze" && |
| 407 | !received && |
| 408 | (this.game.score != "*" || this.cursor < this.moves.length - 1) |
| 409 | ) { |
| 410 | return; |
| 411 | } |
| 412 | // To play a received move, cursor must be at the end of the game: |
| 413 | if (received && this.cursor < this.moves.length - 1) |
| 414 | this.gotoEnd(); |
| 415 | playMove(); |
| 416 | }, |
| 417 | cancelCurrentMultimove: function() { |
| 418 | const L = this.moves.length; |
| 419 | let move = this.moves[L-1]; |
| 420 | if (!Array.isArray(move)) move = [move]; |
| 421 | for (let i=move.length -1; i >= 0; i--) this.vr.undo(move[i]); |
| 422 | this.moves.pop(); |
| 423 | this.cursor--; |
| 424 | this.inMultimove = false; |
| 425 | }, |
| 426 | cancelLastMove: function() { |
| 427 | // The last played move was canceled (corr game) |
| 428 | this.undo(); |
| 429 | this.moves.pop(); |
| 430 | }, |
| 431 | // "light": if gotoMove() or gotoBegin() |
| 432 | undo: function(move, light) { |
| 433 | if (this.inMultimove) { |
| 434 | this.cancelCurrentMultimove(); |
| 435 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 436 | } else { |
| 437 | if (!move) { |
| 438 | const minCursor = |
| 439 | this.moves.length > 0 && this.moves[0].notation == "..." |
| 440 | ? 1 |
| 441 | : 0; |
| 442 | if (this.cursor < minCursor) return; //no more moves |
| 443 | move = this.moves[this.cursor]; |
| 444 | } |
| 445 | // Caution; if multi-move, undo all submoves from last to first |
| 446 | undoMove(move, this.vr); |
| 447 | if (light) this.cursor--; |
| 448 | else { |
| 449 | this.positionCursorTo(this.cursor - 1); |
| 450 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 451 | this.emitFenIfAnalyze(); |
| 452 | } |
| 453 | } |
| 454 | }, |
| 455 | gotoMove: function(index) { |
| 456 | if (this.inMultimove) this.cancelCurrentMultimove(); |
| 457 | if (index == this.cursor) return; |
| 458 | if (index < this.cursor) { |
| 459 | while (this.cursor > index) |
| 460 | this.undo(null, null, "light"); |
| 461 | } |
| 462 | else { |
| 463 | // index > this.cursor) |
| 464 | while (this.cursor < index) |
| 465 | this.play(null, null, "light"); |
| 466 | } |
| 467 | // NOTE: next line also re-assign cursor, but it's very light |
| 468 | this.positionCursorTo(index); |
| 469 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 470 | this.emitFenIfAnalyze(); |
| 471 | }, |
| 472 | gotoBegin: function() { |
| 473 | if (this.inMultimove) this.cancelCurrentMultimove(); |
| 474 | const minCursor = |
| 475 | this.moves.length > 0 && this.moves[0].notation == "..." |
| 476 | ? 1 |
| 477 | : 0; |
| 478 | while (this.cursor >= minCursor) this.undo(null, null, "light"); |
| 479 | this.lastMove = (minCursor == 1 ? this.moves[0] : null); |
| 480 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 481 | this.emitFenIfAnalyze(); |
| 482 | }, |
| 483 | gotoEnd: function() { |
| 484 | if (this.cursor == this.moves.length - 1) return; |
| 485 | this.gotoMove(this.moves.length - 1); |
| 486 | this.emitFenIfAnalyze(); |
| 487 | }, |
| 488 | flip: function() { |
| 489 | this.orientation = V.GetOppCol(this.orientation); |
| 490 | } |
| 491 | } |
| 492 | }; |
| 493 | </script> |
| 494 | |
| 495 | <style lang="sass" scoped> |
| 496 | [type="checkbox"]#modalEog+div .card |
| 497 | min-height: 45px |
| 498 | |
| 499 | #baseGame |
| 500 | width: 100% |
| 501 | &:focus |
| 502 | outline: none |
| 503 | |
| 504 | #gameContainer |
| 505 | margin-left: auto |
| 506 | margin-right: auto |
| 507 | |
| 508 | #downloadDiv |
| 509 | display: inline-block |
| 510 | |
| 511 | #controls |
| 512 | user-select: none |
| 513 | button |
| 514 | border: none |
| 515 | margin: 0 |
| 516 | padding-top: 5px |
| 517 | padding-bottom: 5px |
| 518 | |
| 519 | img.inline |
| 520 | height: 24px |
| 521 | padding-top: 5px |
| 522 | @media screen and (max-width: 767px) |
| 523 | height: 18px |
| 524 | |
| 525 | #turnIndicator |
| 526 | text-align: center |
| 527 | font-weight: bold |
| 528 | |
| 529 | #boardContainer |
| 530 | float: left |
| 531 | // TODO: later, maybe, allow movesList of variable width |
| 532 | // or e.g. between 250 and 350px (but more complicated) |
| 533 | |
| 534 | #movesList |
| 535 | width: 280px |
| 536 | float: left |
| 537 | |
| 538 | @media screen and (max-width: 767px) |
| 539 | #movesList |
| 540 | width: 100% |
| 541 | float: none |
| 542 | clear: both |
| 543 | </style> |