| 1 | <template lang="pug"> |
| 2 | div#baseGame( |
| 3 | tabindex=-1 |
| 4 | @click="focusBg()" |
| 5 | @keydown="handleKeys($event)" |
| 6 | @wheel="handleScroll($event)" |
| 7 | ) |
| 8 | input#modalEog.modal(type="checkbox") |
| 9 | div#eogDiv( |
| 10 | role="dialog" |
| 11 | data-checkbox="modalEog" |
| 12 | ) |
| 13 | .card.text-center |
| 14 | label.modal-close(for="modalEog") |
| 15 | h3.section {{ endgameMessage }} |
| 16 | input#modalAdjust.modal(type="checkbox") |
| 17 | div#adjuster( |
| 18 | role="dialog" |
| 19 | data-checkbox="modalAdjust" |
| 20 | ) |
| 21 | .card.text-center |
| 22 | label.modal-close(for="modalAdjust") |
| 23 | label(for="boardSize") {{ st.tr["Board size"] }} |
| 24 | input#boardSize.slider( |
| 25 | type="range" |
| 26 | min="0" |
| 27 | max="100" |
| 28 | value="50" |
| 29 | @input="adjustBoard()" |
| 30 | ) |
| 31 | #gameContainer |
| 32 | #boardContainer |
| 33 | Board( |
| 34 | :vr="vr" |
| 35 | :last-move="lastMove" |
| 36 | :analyze="game.mode=='analyze'" |
| 37 | :score="game.score" |
| 38 | :user-color="game.mycolor" |
| 39 | :orientation="orientation" |
| 40 | :vname="game.vname" |
| 41 | :incheck="incheck" |
| 42 | @play-move="play" |
| 43 | ) |
| 44 | #turnIndicator(v-if="showTurn") {{ turn }} |
| 45 | #controls |
| 46 | button(@click="gotoBegin()") << |
| 47 | button(@click="undo()") < |
| 48 | button(@click="flip()") ⇅ |
| 49 | button(@click="play()") > |
| 50 | button(@click="gotoEnd()") >> |
| 51 | #belowControls |
| 52 | #downloadDiv(v-if="allowDownloadPGN") |
| 53 | a#download(href="#") |
| 54 | button(@click="download()") {{ st.tr["Download"] }} PGN |
| 55 | button(onClick="window.doClick('modalAdjust')") ⤢ |
| 56 | button( |
| 57 | v-if="canAnalyze" |
| 58 | @click="analyzePosition()" |
| 59 | ) |
| 60 | | {{ st.tr["Analyse"] }} |
| 61 | // NOTE: variants pages already have a "Rules" link on top |
| 62 | button( |
| 63 | v-if="!$route.path.match('/variants/')" |
| 64 | @click="showRules()" |
| 65 | ) |
| 66 | | {{ st.tr["Rules"] }} |
| 67 | #movesList |
| 68 | MoveList( |
| 69 | v-if="showMoves != 'none'" |
| 70 | :show="showMoves" |
| 71 | :score="game.score" |
| 72 | :message="game.scoreMsg" |
| 73 | :firstNum="firstMoveNumber" |
| 74 | :moves="moves" |
| 75 | :cursor="cursor" |
| 76 | @goto-move="gotoMove" |
| 77 | ) |
| 78 | .clearer |
| 79 | </template> |
| 80 | |
| 81 | <script> |
| 82 | import Board from "@/components/Board.vue"; |
| 83 | import MoveList from "@/components/MoveList.vue"; |
| 84 | import { store } from "@/store"; |
| 85 | import { getSquareId } from "@/utils/squareId"; |
| 86 | import { getDate } from "@/utils/datetime"; |
| 87 | import { processModalClick } from "@/utils/modalClick"; |
| 88 | import { getScoreMessage } from "@/utils/scoring"; |
| 89 | export default { |
| 90 | name: "my-base-game", |
| 91 | components: { |
| 92 | Board, |
| 93 | MoveList |
| 94 | }, |
| 95 | // "vr": VariantRules object, describing the game state + rules |
| 96 | props: ["vr", "game"], |
| 97 | data: function() { |
| 98 | return { |
| 99 | st: store.state, |
| 100 | // NOTE: all following variables must be reset at the beginning of a game |
| 101 | endgameMessage: "", |
| 102 | orientation: "w", |
| 103 | score: "*", //'*' means 'unfinished' |
| 104 | moves: [], |
| 105 | cursor: -1, //index of the move just played |
| 106 | lastMove: null, |
| 107 | firstMoveNumber: 0, //for printing |
| 108 | incheck: [] //for Board |
| 109 | }; |
| 110 | }, |
| 111 | watch: { |
| 112 | // game initial FEN changes when a new game starts |
| 113 | "game.fenStart": function() { |
| 114 | this.re_setVariables(); |
| 115 | }, |
| 116 | }, |
| 117 | computed: { |
| 118 | showMoves: function() { |
| 119 | return this.game.score != "*" |
| 120 | ? "all" |
| 121 | : (this.vr ? this.vr.showMoves : "none"); |
| 122 | }, |
| 123 | showTurn: function() { |
| 124 | return this.game.score == '*' && this.vr && this.vr.showMoves != "all"; |
| 125 | }, |
| 126 | turn: function() { |
| 127 | return this.vr |
| 128 | ? this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"] |
| 129 | : ""; |
| 130 | }, |
| 131 | canAnalyze: function() { |
| 132 | return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze; |
| 133 | }, |
| 134 | allowDownloadPGN: function() { |
| 135 | return this.game.score != "*" || (this.vr && this.vr.showMoves == "all"); |
| 136 | } |
| 137 | }, |
| 138 | created: function() { |
| 139 | if (this.game.fenStart) this.re_setVariables(); |
| 140 | }, |
| 141 | mounted: function() { |
| 142 | [ |
| 143 | document.getElementById("eogDiv"), |
| 144 | document.getElementById("adjuster") |
| 145 | ].forEach(elt => elt.addEventListener("click", processModalClick)); |
| 146 | // Take full width on small screens: |
| 147 | let boardSize = parseInt(localStorage.getItem("boardSize")); |
| 148 | if (!boardSize) { |
| 149 | boardSize = |
| 150 | window.innerWidth >= 768 |
| 151 | ? 0.75 * Math.min(window.innerWidth, window.innerHeight) |
| 152 | : window.innerWidth; |
| 153 | } |
| 154 | const movesWidth = window.innerWidth >= 768 ? 280 : 0; |
| 155 | document.getElementById("boardContainer").style.width = boardSize + "px"; |
| 156 | let gameContainer = document.getElementById("gameContainer"); |
| 157 | gameContainer.style.width = boardSize + movesWidth + "px"; |
| 158 | document.getElementById("boardSize").value = |
| 159 | (boardSize * 100) / (window.innerWidth - movesWidth); |
| 160 | // timeout to avoid calling too many time the adjust method |
| 161 | let timeoutLaunched = false; |
| 162 | window.addEventListener("resize", () => { |
| 163 | if (!timeoutLaunched) { |
| 164 | timeoutLaunched = true; |
| 165 | setTimeout(() => { |
| 166 | this.adjustBoard(); |
| 167 | timeoutLaunched = false; |
| 168 | }, 500); |
| 169 | } |
| 170 | }); |
| 171 | }, |
| 172 | methods: { |
| 173 | focusBg: function() { |
| 174 | document.getElementById("baseGame").focus(); |
| 175 | }, |
| 176 | adjustBoard: function() { |
| 177 | const boardContainer = document.getElementById("boardContainer"); |
| 178 | if (!boardContainer) return; //no board on page |
| 179 | const k = document.getElementById("boardSize").value; |
| 180 | const movesWidth = window.innerWidth >= 768 ? 280 : 0; |
| 181 | const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary... |
| 182 | // Value of 0 is board min size; 100 is window.width [- movesWidth] |
| 183 | const boardSize = |
| 184 | minBoardWidth + |
| 185 | (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100; |
| 186 | localStorage.setItem("boardSize", boardSize); |
| 187 | boardContainer.style.width = boardSize + "px"; |
| 188 | document.getElementById("gameContainer").style.width = |
| 189 | boardSize + movesWidth + "px"; |
| 190 | }, |
| 191 | handleKeys: function(e) { |
| 192 | if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault(); |
| 193 | switch (e.keyCode) { |
| 194 | case 37: |
| 195 | this.undo(); |
| 196 | break; |
| 197 | case 39: |
| 198 | this.play(); |
| 199 | break; |
| 200 | case 38: |
| 201 | this.gotoBegin(); |
| 202 | break; |
| 203 | case 40: |
| 204 | this.gotoEnd(); |
| 205 | break; |
| 206 | case 32: |
| 207 | this.flip(); |
| 208 | break; |
| 209 | } |
| 210 | }, |
| 211 | handleScroll: function(e) { |
| 212 | // NOTE: since game.mode=="analyze" => no score, next condition is enough |
| 213 | if (this.game.score != "*") { |
| 214 | e.preventDefault(); |
| 215 | if (e.deltaY < 0) this.undo(); |
| 216 | else if (e.deltaY > 0) this.play(); |
| 217 | } |
| 218 | }, |
| 219 | showRules: function() { |
| 220 | //this.$router.push("/variants/" + this.game.vname); |
| 221 | window.open("#/variants/" + this.game.vname, "_blank"); //better |
| 222 | }, |
| 223 | re_setVariables: function() { |
| 224 | this.endgameMessage = ""; |
| 225 | // "w": default orientation for observed games |
| 226 | this.orientation = this.game.mycolor || "w"; |
| 227 | this.moves = JSON.parse(JSON.stringify(this.game.moves || [])); |
| 228 | // Post-processing: decorate each move with color, notation and FEN |
| 229 | let vr_tmp = new V(this.game.fenStart); |
| 230 | const parsedFen = V.ParseFen(this.game.fenStart); |
| 231 | const firstMoveColor = parsedFen.turn; |
| 232 | this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2); |
| 233 | this.moves.forEach(move => { |
| 234 | move.color = vr_tmp.turn; |
| 235 | move.notation = vr_tmp.getNotation(move); |
| 236 | vr_tmp.play(move); |
| 237 | move.fen = vr_tmp.getFen(); |
| 238 | }); |
| 239 | if (firstMoveColor == "b") { |
| 240 | // 'end' is required for Board component to check lastMove for e.p. |
| 241 | this.moves.unshift({ |
| 242 | color: "w", |
| 243 | notation: "...", |
| 244 | end: { x: -1, y: -1 } |
| 245 | }); |
| 246 | } |
| 247 | const L = this.moves.length; |
| 248 | this.cursor = L - 1; |
| 249 | this.lastMove = L > 0 ? this.moves[L - 1] : null; |
| 250 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 251 | }, |
| 252 | analyzePosition: function() { |
| 253 | const newUrl = |
| 254 | "/analyse/" + |
| 255 | this.game.vname + |
| 256 | "/?fen=" + |
| 257 | this.vr.getFen().replace(/ /g, "_"); |
| 258 | // Open in same tab in live games (against cheating) |
| 259 | if (this.game.type == "live") this.$router.push(newUrl); |
| 260 | else window.open("#" + newUrl); |
| 261 | }, |
| 262 | download: function() { |
| 263 | const content = this.getPgn(); |
| 264 | // Prepare and trigger download link |
| 265 | let downloadAnchor = document.getElementById("download"); |
| 266 | downloadAnchor.setAttribute("download", "game.pgn"); |
| 267 | downloadAnchor.href = |
| 268 | "data:text/plain;charset=utf-8," + encodeURIComponent(content); |
| 269 | downloadAnchor.click(); |
| 270 | }, |
| 271 | getPgn: function() { |
| 272 | let pgn = ""; |
| 273 | pgn += '[Site "vchess.club"]\n'; |
| 274 | pgn += '[Variant "' + this.game.vname + '"]\n'; |
| 275 | pgn += '[Date "' + getDate(new Date()) + '"]\n'; |
| 276 | pgn += '[White "' + this.game.players[0].name + '"]\n'; |
| 277 | pgn += '[Black "' + this.game.players[1].name + '"]\n'; |
| 278 | pgn += '[Fen "' + this.game.fenStart + '"]\n'; |
| 279 | pgn += '[Result "' + this.game.score + '"]\n\n'; |
| 280 | let counter = 1; |
| 281 | let i = 0; |
| 282 | while (i < this.moves.length) { |
| 283 | pgn += counter++ + "."; |
| 284 | for (let color of ["w", "b"]) { |
| 285 | let move = ""; |
| 286 | while (i < this.moves.length && this.moves[i].color == color) |
| 287 | move += this.moves[i++].notation + ","; |
| 288 | move = move.slice(0, -1); //remove last comma |
| 289 | pgn += move + (i < this.moves.length ? " " : ""); |
| 290 | } |
| 291 | } |
| 292 | return pgn + "\n"; |
| 293 | }, |
| 294 | showEndgameMsg: function(message) { |
| 295 | this.endgameMessage = message; |
| 296 | let modalBox = document.getElementById("modalEog"); |
| 297 | modalBox.checked = true; |
| 298 | setTimeout(() => { |
| 299 | modalBox.checked = false; |
| 300 | }, 2000); |
| 301 | }, |
| 302 | animateMove: function(move, callback) { |
| 303 | let startSquare = document.getElementById(getSquareId(move.start)); |
| 304 | let endSquare = document.getElementById(getSquareId(move.end)); |
| 305 | let rectStart = startSquare.getBoundingClientRect(); |
| 306 | let rectEnd = endSquare.getBoundingClientRect(); |
| 307 | let translation = { |
| 308 | x: rectEnd.x - rectStart.x, |
| 309 | y: rectEnd.y - rectStart.y |
| 310 | }; |
| 311 | let movingPiece = document.querySelector( |
| 312 | "#" + getSquareId(move.start) + " > img.piece" |
| 313 | ); |
| 314 | // HACK for animation (with positive translate, image slides "under background") |
| 315 | // Possible improvement: just alter squares on the piece's way... |
| 316 | const squares = document.getElementsByClassName("board"); |
| 317 | for (let i = 0; i < squares.length; i++) { |
| 318 | let square = squares.item(i); |
| 319 | if (square.id != getSquareId(move.start)) square.style.zIndex = "-1"; |
| 320 | } |
| 321 | movingPiece.style.transform = |
| 322 | "translate(" + translation.x + "px," + translation.y + "px)"; |
| 323 | movingPiece.style.transitionDuration = "0.25s"; |
| 324 | movingPiece.style.zIndex = "3000"; |
| 325 | setTimeout(() => { |
| 326 | for (let i = 0; i < squares.length; i++) |
| 327 | squares.item(i).style.zIndex = "auto"; |
| 328 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap |
| 329 | callback(); |
| 330 | }, 250); |
| 331 | }, |
| 332 | play: function(move, receive) { |
| 333 | // NOTE: navigate and receive are mutually exclusive |
| 334 | const navigate = !move; |
| 335 | // Forbid playing outside analyze mode, except if move is received. |
| 336 | // Sufficient condition because Board already knows which turn it is. |
| 337 | if ( |
| 338 | !navigate && |
| 339 | this.game.mode != "analyze" && |
| 340 | !receive && |
| 341 | (this.game.score != "*" || this.cursor < this.moves.length - 1) |
| 342 | ) { |
| 343 | return; |
| 344 | } |
| 345 | const doPlayMove = () => { |
| 346 | // To play a move, cursor must be at the end of the game: |
| 347 | if (!!receive && this.cursor < this.moves.length - 1) this.gotoEnd(); |
| 348 | if (navigate) { |
| 349 | if (this.cursor == this.moves.length - 1) return; //no more moves |
| 350 | move = this.moves[this.cursor + 1]; |
| 351 | } else { |
| 352 | move.color = this.vr.turn; |
| 353 | move.notation = this.vr.getNotation(move); |
| 354 | } |
| 355 | this.vr.play(move); |
| 356 | this.cursor++; |
| 357 | this.lastMove = move; |
| 358 | if (this.st.settings.sound == 2) |
| 359 | new Audio("/sounds/move.mp3").play().catch(() => {}); |
| 360 | if (!navigate) { |
| 361 | move.fen = this.vr.getFen(); |
| 362 | // Stack move on movesList at current cursor |
| 363 | if (this.cursor == this.moves.length) this.moves.push(move); |
| 364 | else this.moves = this.moves.slice(0, this.cursor).concat([move]); |
| 365 | } |
| 366 | // Is opponent in check? |
| 367 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 368 | const score = this.vr.getCurrentScore(); |
| 369 | if (score != "*") { |
| 370 | const message = getScoreMessage(score); |
| 371 | if (this.game.mode != "analyze") |
| 372 | this.$emit("gameover", score, message); |
| 373 | //just show score on screen (allow undo) |
| 374 | else this.showEndgameMsg(score + " . " + message); |
| 375 | } |
| 376 | if (!navigate && this.game.mode != "analyze") |
| 377 | this.$emit("newmove", move); //post-processing (e.g. computer play) |
| 378 | }; |
| 379 | if (!!receive && V.ShowMoves == "all") |
| 380 | this.animateMove(move, doPlayMove); |
| 381 | else doPlayMove(); |
| 382 | }, |
| 383 | undo: function(move) { |
| 384 | const navigate = !move; |
| 385 | if (navigate) { |
| 386 | if (this.cursor < 0) return; //no more moves |
| 387 | move = this.moves[this.cursor]; |
| 388 | } |
| 389 | this.vr.undo(move); |
| 390 | this.cursor--; |
| 391 | this.lastMove = this.cursor >= 0 ? this.moves[this.cursor] : undefined; |
| 392 | if (this.st.settings.sound == 2) |
| 393 | new Audio("/sounds/undo.mp3").play().catch(() => {}); |
| 394 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 395 | if (!navigate) this.moves.pop(); |
| 396 | }, |
| 397 | gotoMove: function(index) { |
| 398 | this.vr.re_init(this.moves[index].fen); |
| 399 | this.cursor = index; |
| 400 | this.lastMove = this.moves[index]; |
| 401 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 402 | }, |
| 403 | gotoBegin: function() { |
| 404 | if (this.cursor == -1) return; |
| 405 | this.vr.re_init(this.game.fenStart); |
| 406 | if (this.moves.length > 0 && this.moves[0].notation == "...") { |
| 407 | this.cursor = 0; |
| 408 | this.lastMove = this.moves[0]; |
| 409 | } else { |
| 410 | this.cursor = -1; |
| 411 | this.lastMove = null; |
| 412 | } |
| 413 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 414 | }, |
| 415 | gotoEnd: function() { |
| 416 | if (this.cursor == this.moves.length - 1) return; |
| 417 | this.gotoMove(this.moves.length - 1); |
| 418 | }, |
| 419 | flip: function() { |
| 420 | this.orientation = V.GetOppCol(this.orientation); |
| 421 | } |
| 422 | } |
| 423 | }; |
| 424 | </script> |
| 425 | |
| 426 | <style lang="sass" scoped> |
| 427 | [type="checkbox"]#modalEog+div .card |
| 428 | min-height: 45px |
| 429 | |
| 430 | [type="checkbox"]#modalAdjust+div .card |
| 431 | padding: 5px |
| 432 | |
| 433 | #baseGame |
| 434 | width: 100% |
| 435 | &:focus |
| 436 | outline: none |
| 437 | |
| 438 | #gameContainer |
| 439 | margin-left: auto |
| 440 | margin-right: auto |
| 441 | |
| 442 | #downloadDiv |
| 443 | display: inline-block |
| 444 | |
| 445 | #controls |
| 446 | margin: 0 auto |
| 447 | button |
| 448 | display: inline-block |
| 449 | width: 20% |
| 450 | margin: 0 |
| 451 | |
| 452 | #turnIndicator |
| 453 | text-align: center |
| 454 | font-weight: bold |
| 455 | |
| 456 | #belowControls |
| 457 | border-top: 1px solid #2f4f4f |
| 458 | text-align: center |
| 459 | margin: 0 auto |
| 460 | & > #downloadDiv |
| 461 | margin: 0 |
| 462 | & > button |
| 463 | margin: 0 |
| 464 | & > button |
| 465 | border-left: 1px solid #2f4f4f |
| 466 | margin: 0 |
| 467 | |
| 468 | #boardContainer |
| 469 | float: left |
| 470 | // TODO: later, maybe, allow movesList of variable width |
| 471 | // or e.g. between 250 and 350px (but more complicated) |
| 472 | |
| 473 | #movesList |
| 474 | width: 280px |
| 475 | float: left |
| 476 | |
| 477 | @media screen and (max-width: 767px) |
| 478 | #movesList |
| 479 | width: 100% |
| 480 | float: none |
| 481 | clear: both |
| 482 | </style> |