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