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