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