| 1 | <template lang="pug"> |
| 2 | div#baseGame(tabindex=-1 @click="() => focusBg()" |
| 3 | @keydown="handleKeys" @wheel="handleScroll") |
| 4 | input#modalEog.modal(type="checkbox") |
| 5 | div(role="dialog" data-checkbox="modalEog" aria-labelledby="eogMessage") |
| 6 | .card.smallpad.small-modal.text-center |
| 7 | label.modal-close(for="modalEog") |
| 8 | h3#eogMessage.section {{ endgameMessage }} |
| 9 | #gameContainer |
| 10 | #boardContainer |
| 11 | Board(:vr="vr" :last-move="lastMove" :analyze="game.mode=='analyze'" |
| 12 | :user-color="game.mycolor" :orientation="orientation" |
| 13 | :vname="game.vname" @play-move="play") |
| 14 | #controls |
| 15 | button(@click="gotoBegin") << |
| 16 | button(@click="() => undo()") < |
| 17 | button(@click="flip") ⇅ |
| 18 | button(@click="() => play()") > |
| 19 | button(@click="gotoEnd") >> |
| 20 | #pgnDiv |
| 21 | a#download(href="#") |
| 22 | button(@click="download") {{ st.tr["Download PGN"] }} |
| 23 | button(v-if="game.mode!='analyze'" @click="analyzePosition") |
| 24 | | {{ st.tr["Analyze"] }} |
| 25 | #movesList |
| 26 | MoveList(v-if="showMoves" :score="game.score" :message="game.scoreMsg" |
| 27 | :firstNum="firstMoveNumber" :moves="moves" :cursor="cursor" |
| 28 | @goto-move="gotoMove") |
| 29 | // TODO: clearer required ?! |
| 30 | .clearer |
| 31 | </template> |
| 32 | |
| 33 | <script> |
| 34 | import Board from "@/components/Board.vue"; |
| 35 | import MoveList from "@/components/MoveList.vue"; |
| 36 | import { store } from "@/store"; |
| 37 | import { getSquareId } from "@/utils/squareId"; |
| 38 | import { getDate } from "@/utils/datetime"; |
| 39 | |
| 40 | export default { |
| 41 | name: 'my-base-game', |
| 42 | components: { |
| 43 | Board, |
| 44 | MoveList, |
| 45 | }, |
| 46 | // "vr": VariantRules object, describing the game state + rules |
| 47 | props: ["vr","game"], |
| 48 | data: function() { |
| 49 | return { |
| 50 | st: store.state, |
| 51 | // NOTE: all following variables must be reset at the beginning of a game |
| 52 | endgameMessage: "", |
| 53 | orientation: "w", |
| 54 | score: "*", //'*' means 'unfinished' |
| 55 | moves: [], |
| 56 | cursor: -1, //index of the move just played |
| 57 | lastMove: null, |
| 58 | firstMoveNumber: 0, //for printing |
| 59 | }; |
| 60 | }, |
| 61 | watch: { |
| 62 | // game initial FEN changes when a new game starts |
| 63 | "game.fenStart": function() { |
| 64 | this.re_setVariables(); |
| 65 | }, |
| 66 | // Received a new move to play: |
| 67 | "game.moveToPlay": function(newMove) { |
| 68 | if (!!newMove) //if stop + launch new game, get undefined move |
| 69 | this.play(newMove, "receive"); |
| 70 | }, |
| 71 | }, |
| 72 | computed: { |
| 73 | showMoves: function() { |
| 74 | return this.game.vname != "Dark" || this.game.mode=="analyze"; |
| 75 | }, |
| 76 | }, |
| 77 | created: function() { |
| 78 | if (!!this.game.fenStart) |
| 79 | this.re_setVariables(); |
| 80 | }, |
| 81 | mounted: function() { |
| 82 | // Take full width on small screens: |
| 83 | let boardSize = parseInt(localStorage.getItem("boardSize")); |
| 84 | if (!boardSize) |
| 85 | { |
| 86 | boardSize = (window.innerWidth >= 768 |
| 87 | ? Math.min(600, 0.5*window.innerWidth) //heuristic... |
| 88 | : window.innerWidth); |
| 89 | } |
| 90 | const movesWidth = (window.innerWidth >= 768 ? 280 : 0); |
| 91 | document.getElementById("boardContainer").style.width = boardSize + "px"; |
| 92 | let gameContainer = document.getElementById("gameContainer"); |
| 93 | gameContainer.style.width = (boardSize + movesWidth) + "px"; |
| 94 | }, |
| 95 | methods: { |
| 96 | focusBg: function() { |
| 97 | // TODO: small blue border appears... |
| 98 | document.getElementById("baseGame").focus(); |
| 99 | }, |
| 100 | handleKeys: function(e) { |
| 101 | if ([32,37,38,39,40].includes(e.keyCode)) |
| 102 | e.preventDefault(); |
| 103 | switch (e.keyCode) |
| 104 | { |
| 105 | case 37: |
| 106 | this.undo(); |
| 107 | break; |
| 108 | case 39: |
| 109 | this.play(); |
| 110 | break; |
| 111 | case 38: |
| 112 | this.gotoBegin(); |
| 113 | break; |
| 114 | case 40: |
| 115 | this.gotoEnd(); |
| 116 | break; |
| 117 | case 32: |
| 118 | this.flip(); |
| 119 | break; |
| 120 | } |
| 121 | }, |
| 122 | handleScroll: function(e) { |
| 123 | if (e.deltaY < 0) |
| 124 | this.undo(); |
| 125 | else if (e.deltaY > 0) |
| 126 | this.play(); |
| 127 | }, |
| 128 | re_setVariables: function() { |
| 129 | this.endgameMessage = ""; |
| 130 | this.orientation = this.game.mycolor || "w"; //default orientation for observed games |
| 131 | this.moves = JSON.parse(JSON.stringify(this.game.moves || [])); |
| 132 | // Post-processing: decorate each move with color + current FEN: |
| 133 | // (to be able to jump to any position quickly) |
| 134 | let vr_tmp = new V(this.game.fenStart); //vr is already at end of game |
| 135 | this.firstMoveNumber = |
| 136 | Math.floor(V.ParseFen(this.game.fenStart).movesCount / 2); |
| 137 | this.moves.forEach(move => { |
| 138 | // NOTE: this is doing manually what play() function below achieve, |
| 139 | // but in a lighter "fast-forward" way |
| 140 | move.color = vr_tmp.turn; |
| 141 | move.notation = vr_tmp.getNotation(move); |
| 142 | vr_tmp.play(move); |
| 143 | move.fen = vr_tmp.getFen(); |
| 144 | }); |
| 145 | if (this.game.fenStart.indexOf(" b ") >= 0 || |
| 146 | (this.moves.length > 0 && this.moves[0].color == "b")) |
| 147 | { |
| 148 | // 'end' is required for Board component to check lastMove for e.p. |
| 149 | this.moves.unshift({color: "w", notation: "...", end: {x:-1,y:-1}}); |
| 150 | } |
| 151 | const L = this.moves.length; |
| 152 | this.cursor = L-1; |
| 153 | this.lastMove = (L > 0 ? this.moves[L-1] : null); |
| 154 | }, |
| 155 | analyzePosition: function() { |
| 156 | const newUrl = "/analyze/" + this.game.vname + |
| 157 | "/?fen=" + this.vr.getFen().replace(/ /g, "_"); |
| 158 | //window.open("#" + newUrl); //to open in a new tab |
| 159 | this.$router.push(newUrl); //better |
| 160 | }, |
| 161 | download: function() { |
| 162 | const content = this.getPgn(); |
| 163 | // Prepare and trigger download link |
| 164 | let downloadAnchor = document.getElementById("download"); |
| 165 | downloadAnchor.setAttribute("download", "game.pgn"); |
| 166 | downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content); |
| 167 | downloadAnchor.click(); |
| 168 | }, |
| 169 | getPgn: function() { |
| 170 | let pgn = ""; |
| 171 | pgn += '[Site "vchess.club"]\n'; |
| 172 | pgn += '[Variant "' + this.game.vname + '"]\n'; |
| 173 | pgn += '[Date "' + getDate(new Date()) + '"]\n'; |
| 174 | pgn += '[White "' + this.game.players[0].name + '"]\n'; |
| 175 | pgn += '[Black "' + this.game.players[1].name + '"]\n'; |
| 176 | pgn += '[Fen "' + this.game.fenStart + '"]\n'; |
| 177 | pgn += '[Result "' + this.game.score + '"]\n\n'; |
| 178 | let counter = 1; |
| 179 | let i = 0; |
| 180 | while (i < this.moves.length) |
| 181 | { |
| 182 | pgn += (counter++) + "."; |
| 183 | for (let color of ["w","b"]) |
| 184 | { |
| 185 | let move = ""; |
| 186 | while (i < this.moves.length && this.moves[i].color == color) |
| 187 | move += this.moves[i++].notation + ","; |
| 188 | move = move.slice(0,-1); //remove last comma |
| 189 | pgn += move + (i < this.moves.length ? " " : ""); |
| 190 | } |
| 191 | } |
| 192 | return pgn + "\n"; |
| 193 | }, |
| 194 | getScoreMessage: function(score) { |
| 195 | let eogMessage = "Undefined"; |
| 196 | switch (score) |
| 197 | { |
| 198 | case "1-0": |
| 199 | eogMessage = this.st.tr["White win"]; |
| 200 | break; |
| 201 | case "0-1": |
| 202 | eogMessage = this.st.tr["Black win"]; |
| 203 | break; |
| 204 | case "1/2": |
| 205 | eogMessage = this.st.tr["Draw"]; |
| 206 | break; |
| 207 | case "?": |
| 208 | eogMessage = this.st.tr["Unfinished"]; |
| 209 | break; |
| 210 | } |
| 211 | return eogMessage; |
| 212 | }, |
| 213 | showEndgameMsg: function(message) { |
| 214 | this.endgameMessage = message; |
| 215 | let modalBox = document.getElementById("modalEog"); |
| 216 | modalBox.checked = true; |
| 217 | setTimeout(() => { modalBox.checked = false; }, 2000); |
| 218 | }, |
| 219 | animateMove: function(move, callback) { |
| 220 | let startSquare = document.getElementById(getSquareId(move.start)); |
| 221 | let endSquare = document.getElementById(getSquareId(move.end)); |
| 222 | let rectStart = startSquare.getBoundingClientRect(); |
| 223 | let rectEnd = endSquare.getBoundingClientRect(); |
| 224 | let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y}; |
| 225 | let movingPiece = |
| 226 | document.querySelector("#" + getSquareId(move.start) + " > img.piece"); |
| 227 | // HACK for animation (with positive translate, image slides "under background") |
| 228 | // Possible improvement: just alter squares on the piece's way... |
| 229 | const squares = document.getElementsByClassName("board"); |
| 230 | for (let i=0; i<squares.length; i++) |
| 231 | { |
| 232 | let square = squares.item(i); |
| 233 | if (square.id != getSquareId(move.start)) |
| 234 | square.style.zIndex = "-1"; |
| 235 | } |
| 236 | movingPiece.style.transform = "translate(" + translation.x + "px," + |
| 237 | translation.y + "px)"; |
| 238 | movingPiece.style.transitionDuration = "0.2s"; |
| 239 | movingPiece.style.zIndex = "3000"; |
| 240 | setTimeout( () => { |
| 241 | for (let i=0; i<squares.length; i++) |
| 242 | squares.item(i).style.zIndex = "auto"; |
| 243 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap |
| 244 | callback(); |
| 245 | }, 250); |
| 246 | }, |
| 247 | play: function(move, receive) { |
| 248 | // NOTE: navigate and receive are mutually exclusive |
| 249 | const navigate = !move; |
| 250 | // Forbid playing outside analyze mode, except if move is received. |
| 251 | // Sufficient condition because Board already knows which turn it is. |
| 252 | if (!navigate && this.game.mode!="analyze" && !receive |
| 253 | && this.cursor < this.moves.length-1) |
| 254 | { |
| 255 | return; |
| 256 | } |
| 257 | const doPlayMove = () => { |
| 258 | if (!!receive && this.cursor < this.moves.length-1) |
| 259 | this.gotoEnd(); //required to play the move |
| 260 | if (navigate) |
| 261 | { |
| 262 | if (this.cursor == this.moves.length-1) |
| 263 | return; //no more moves |
| 264 | move = this.moves[this.cursor+1]; |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | move.color = this.vr.turn; |
| 269 | move.notation = this.vr.getNotation(move); |
| 270 | } |
| 271 | this.vr.play(move); |
| 272 | this.cursor++; |
| 273 | this.lastMove = move; |
| 274 | if (this.st.settings.sound == 2) |
| 275 | new Audio("/sounds/move.mp3").play().catch(err => {}); |
| 276 | if (!navigate) |
| 277 | { |
| 278 | move.fen = this.vr.getFen(); |
| 279 | // Stack move on movesList at current cursor |
| 280 | if (this.cursor == this.moves.length) |
| 281 | this.moves.push(move); |
| 282 | else |
| 283 | this.moves = this.moves.slice(0,this.cursor).concat([move]); |
| 284 | } |
| 285 | if (!navigate && this.game.mode != "analyze") |
| 286 | this.$emit("newmove", move); //post-processing (e.g. computer play) |
| 287 | // Is opponent in check? |
| 288 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 289 | const score = this.vr.getCurrentScore(); |
| 290 | if (score != "*") |
| 291 | { |
| 292 | const message = this.getScoreMessage(score); |
| 293 | if (this.game.mode != "analyze") |
| 294 | this.$emit("gameover", score, message); |
| 295 | else //just show score on screen (allow undo) |
| 296 | this.showEndgameMsg(score + " . " + message); |
| 297 | } |
| 298 | }; |
| 299 | if (!!receive && this.game.vname != "Dark") |
| 300 | this.animateMove(move, doPlayMove); |
| 301 | else |
| 302 | doPlayMove(); |
| 303 | }, |
| 304 | undo: function(move) { |
| 305 | const navigate = !move; |
| 306 | if (navigate) |
| 307 | { |
| 308 | if (this.cursor < 0) |
| 309 | return; //no more moves |
| 310 | move = this.moves[this.cursor]; |
| 311 | } |
| 312 | this.vr.undo(move); |
| 313 | this.cursor--; |
| 314 | this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined); |
| 315 | if (this.st.settings.sound == 2) |
| 316 | new Audio("/sounds/undo.mp3").play().catch(err => {}); |
| 317 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
| 318 | if (!navigate) |
| 319 | this.moves.pop(); |
| 320 | }, |
| 321 | gotoMove: function(index) { |
| 322 | this.vr.re_init(this.moves[index].fen); |
| 323 | this.cursor = index; |
| 324 | this.lastMove = this.moves[index]; |
| 325 | }, |
| 326 | gotoBegin: function() { |
| 327 | if (this.cursor == -1) |
| 328 | return; |
| 329 | this.vr.re_init(this.game.fenStart); |
| 330 | if (this.moves.length > 0 && this.moves[0].notation == "...") |
| 331 | { |
| 332 | this.cursor = 0; |
| 333 | this.lastMove = this.moves[0]; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | this.cursor = -1; |
| 338 | this.lastMove = null; |
| 339 | } |
| 340 | }, |
| 341 | gotoEnd: function() { |
| 342 | if (this.cursor == this.moves.length - 1) |
| 343 | return; |
| 344 | this.gotoMove(this.moves.length-1); |
| 345 | }, |
| 346 | flip: function() { |
| 347 | this.orientation = V.GetNextCol(this.orientation); |
| 348 | }, |
| 349 | }, |
| 350 | }; |
| 351 | </script> |
| 352 | |
| 353 | <style lang="sass"> |
| 354 | #baseGame |
| 355 | width: 100% |
| 356 | |
| 357 | #gameContainer |
| 358 | margin-left: auto |
| 359 | margin-right: auto |
| 360 | |
| 361 | #modal-eog+div .card |
| 362 | overflow: hidden |
| 363 | @media screen and (min-width: 768px) |
| 364 | #controls |
| 365 | width: 400px |
| 366 | margin-left: auto |
| 367 | margin-right: auto |
| 368 | #controls |
| 369 | margin-top: 10px |
| 370 | margin-left: auto |
| 371 | margin-right: auto |
| 372 | button |
| 373 | display: inline-block |
| 374 | width: 20% |
| 375 | margin: 0 |
| 376 | #pgnDiv |
| 377 | text-align: center |
| 378 | margin-left: auto |
| 379 | margin-right: auto |
| 380 | #boardContainer |
| 381 | float: left |
| 382 | #movesList |
| 383 | width: 280px |
| 384 | float: left |
| 385 | @media screen and (max-width: 767px) |
| 386 | #movesList |
| 387 | width: 100% |
| 388 | float: none |
| 389 | clear: both |
| 390 | table |
| 391 | tr |
| 392 | display: flex |
| 393 | margin: 0 |
| 394 | padding: 0 |
| 395 | td |
| 396 | text-align: left |
| 397 | </style> |