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