3 input#modalEog.modal(type="checkbox")
6 data-checkbox="modalEog"
9 label.modal-close(for="modalEog")
10 h3.section {{ endgameMessage }}
16 :analyze="game.mode=='analyze'"
18 :user-color="game.mycolor"
19 :orientation="orientation"
24 #turnIndicator(v-if="showTurn") {{ turn }}
25 #controls.button-group
26 button(@click="gotoBegin()")
27 img.inline(src="/images/icons/fast-forward_rev.svg")
28 button(@click="undo()")
29 img.inline(src="/images/icons/play_rev.svg")
30 button(v-if="canFlip" @click="flip()")
31 img.inline(src="/images/icons/flip.svg")
32 button(@click="play()")
33 img.inline(src="/images/icons/play.svg")
34 button(@click="gotoEnd()")
35 img.inline(src="/images/icons/fast-forward.svg")
39 :canAnalyze="canAnalyze"
40 :canDownload="allowDownloadPGN"
42 :message="game.scoreMsg"
43 :firstNum="firstMoveNumber"
47 @showrules="showRules"
48 @analyze="analyzePosition"
55 import Board from "@/components/Board.vue";
56 import MoveList from "@/components/MoveList.vue";
57 import { store } from "@/store";
58 import { getSquareId } from "@/utils/squareId";
59 import { getDate } from "@/utils/datetime";
60 import { processModalClick } from "@/utils/modalClick";
61 import { getScoreMessage } from "@/utils/scoring";
62 import { getFullNotation } from "@/utils/notation";
63 import { undoMove } from "@/utils/playUndo";
74 // NOTE: all following variables must be reset at the beginning of a game
75 vr: null, //VariantRules object, game state
78 score: "*", //'*' means 'unfinished'
80 cursor: -1, //index of the move just played
82 firstMoveNumber: 0, //for printing
83 incheck: [], //for Board
90 showMoves: function() {
91 return this.game.score != "*"
93 : (this.vr ? this.vr.showMoves : "none");
95 showTurn: function() {
97 this.game.score == '*' &&
99 (this.vr.showMoves != "all" || !this.vr.canFlip)
105 if (this.vr.showMoves != "all")
106 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]
107 // Cannot flip: racing king or circular chess
108 return this.vr.movesCount == 0 && this.game.mycolor == "w"
109 ? this.st.tr["It's your turn!"]
112 canAnalyze: function() {
113 return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze;
115 canFlip: function() {
116 return this.vr && this.vr.canFlip;
118 allowDownloadPGN: function() {
119 return this.game.score != "*" || (this.vr && this.vr.showMoves == "all");
122 created: function() {
123 if (!!this.game.fenStart) this.re_setVariables();
125 mounted: function() {
126 if (!("ontouchstart" in window)) {
128 const baseGameDiv = document.getElementById("baseGame");
129 baseGameDiv.tabIndex = 0;
130 baseGameDiv.addEventListener("click", this.focusBg);
131 baseGameDiv.addEventListener("keydown", this.handleKeys);
132 baseGameDiv.addEventListener("wheel", this.handleScroll);
134 document.getElementById("eogDiv")
135 .addEventListener("click", processModalClick);
138 focusBg: function() {
139 document.getElementById("baseGame").focus();
141 handleKeys: function(e) {
142 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
161 handleScroll: function(e) {
163 if (e.deltaY < 0) this.undo();
164 else if (e.deltaY > 0) this.play();
166 showRules: function() {
167 //this.$router.push("/variants/" + this.game.vname);
168 window.open("#/variants/" + this.game.vname, "_blank"); //better
170 re_setVariables: function(game) {
171 if (!game) game = this.game; //in case of...
172 this.endgameMessage = "";
173 // "w": default orientation for observed games
174 this.orientation = game.mycolor || "w";
175 this.moves = JSON.parse(JSON.stringify(game.moves || []));
176 // Post-processing: decorate each move with notation and FEN
177 this.vr = new V(game.fenStart);
178 const parsedFen = V.ParseFen(game.fenStart);
179 const firstMoveColor = parsedFen.turn;
180 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
181 let L = this.moves.length;
182 this.moves.forEach(move => {
183 // Strategy working also for multi-moves:
184 if (!Array.isArray(move)) move = [move];
185 move.forEach((m,idx) => {
186 m.notation = this.vr.getNotation(m);
188 if (idx < L - 1 && this.vr.getCheckSquares(this.vr.turn).length > 0)
192 if (firstMoveColor == "b") {
193 // 'start' & 'end' is required for Board component
196 start: { x: -1, y: -1 },
197 end: { x: -1, y: -1 },
202 this.positionCursorTo(this.moves.length - 1);
203 this.incheck = this.vr.getCheckSquares(this.vr.turn);
204 const score = this.vr.getCurrentScore();
205 if (["1-0","0-1"].includes(score))
206 this.moves[L - 1].notation += "#";
207 else if (this.vr.getCheckSquares(this.vr.turn).length > 0)
208 this.moves[L - 1].notation += "+";
210 positionCursorTo: function(index) {
212 // Caution: last move in moves array might be a multi-move
214 if (Array.isArray(this.moves[index])) {
215 const L = this.moves[index].length;
216 this.lastMove = this.moves[index][L - 1];
218 this.lastMove = this.moves[index];
220 } else this.lastMove = null;
222 analyzePosition: function() {
227 this.vr.getFen().replace(/ /g, "_");
228 if (this.game.mycolor)
229 newUrl += "&side=" + this.game.mycolor;
230 // Open in same tab in live games (against cheating)
231 if (this.game.type == "live") this.$router.push(newUrl);
232 else window.open("#" + newUrl);
234 download: function() {
235 const content = this.getPgn();
236 // Prepare and trigger download link
237 let downloadAnchor = document.getElementById("download");
238 downloadAnchor.setAttribute("download", "game.pgn");
239 downloadAnchor.href =
240 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
241 downloadAnchor.click();
245 pgn += '[Site "vchess.club"]\n';
246 pgn += '[Variant "' + this.game.vname + '"]\n';
247 pgn += '[Date "' + getDate(new Date()) + '"]\n';
248 pgn += '[White "' + this.game.players[0].name + '"]\n';
249 pgn += '[Black "' + this.game.players[1].name + '"]\n';
250 pgn += '[Fen "' + this.game.fenStart + '"]\n';
251 pgn += '[Result "' + this.game.score + '"]\n\n';
252 for (let i = 0; i < this.moves.length; i += 2) {
253 pgn += (i/2+1) + "." + getFullNotation(this.moves[i]) + " ";
254 if (i+1 < this.moves.length)
255 pgn += getFullNotation(this.moves[i+1]) + " ";
259 showEndgameMsg: function(message) {
260 this.endgameMessage = message;
261 document.getElementById("modalEog").checked = true;
263 // Animate an elementary move
264 animateMove: function(move, callback) {
265 let startSquare = document.getElementById(getSquareId(move.start));
266 if (!startSquare) return; //shouldn't happen but...
267 let endSquare = document.getElementById(getSquareId(move.end));
268 let rectStart = startSquare.getBoundingClientRect();
269 let rectEnd = endSquare.getBoundingClientRect();
271 x: rectEnd.x - rectStart.x,
272 y: rectEnd.y - rectStart.y
274 let movingPiece = document.querySelector(
275 "#" + getSquareId(move.start) + " > img.piece"
277 // For some unknown reasons Opera get "movingPiece == null" error
278 // TOOO: is it calling 'animate()' twice ? One extra time ?
279 if (!movingPiece) return;
280 // HACK for animation (with positive translate, image slides "under background")
281 // Possible improvement: just alter squares on the piece's way...
282 const squares = document.getElementsByClassName("board");
283 for (let i = 0; i < squares.length; i++) {
284 let square = squares.item(i);
285 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
287 movingPiece.style.transform =
288 "translate(" + translation.x + "px," + translation.y + "px)";
289 movingPiece.style.transitionDuration = "0.25s";
290 movingPiece.style.zIndex = "3000";
292 for (let i = 0; i < squares.length; i++)
293 squares.item(i).style.zIndex = "auto";
294 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
299 emitFenIfAnalyze: function() {
300 if (this.game.mode == "analyze") {
303 !!this.lastMove ? this.lastMove.fen : this.game.fenStart
307 // "light": if gotoMove() or gotoEnd()
308 play: function(move, received, light, noemit) {
311 // Received moves in observed games can arrive too fast:
312 this.stackToPlay.unshift(move);
317 const navigate = !move;
318 const playSubmove = (smove) => {
319 smove.notation = this.vr.getNotation(smove);
321 this.lastMove = smove;
322 if (!this.inMultimove) {
323 if (this.cursor < this.moves.length - 1)
324 this.moves = this.moves.slice(0, this.cursor + 1);
325 this.moves.push(smove);
326 this.inMultimove = true; //potentially
329 // Already in the middle of a multi-move
330 const L = this.moves.length;
331 if (!Array.isArray(this.moves[L-1]))
332 this.$set(this.moves, L-1, [this.moves[L-1], smove]);
334 this.$set(this.moves, L-1, this.moves.concat([smove]));
337 const playMove = () => {
338 const animate = (V.ShowMoves == "all" && !!received);
339 if (!Array.isArray(move)) move = [move];
342 const initurn = this.vr.turn;
343 (function executeMove() {
344 const smove = move[moveIdx++];
346 self.animateMove(smove, () => {
348 if (moveIdx < move.length)
349 setTimeout(executeMove, 500);
350 else afterMove(smove, initurn);
354 if (moveIdx < move.length) executeMove();
355 else afterMove(smove, initurn);
359 const computeScore = () => {
360 const score = this.vr.getCurrentScore();
362 if (["1-0","0-1"].includes(score))
363 this.lastMove.notation += "#";
364 else if (this.vr.getCheckSquares(this.vr.turn).length > 0)
365 this.lastMove.notation += "+";
367 if (score != "*" && this.game.mode == "analyze") {
368 const message = getScoreMessage(score);
369 // Just show score on screen (allow undo)
370 this.showEndgameMsg(score + " . " + this.st.tr[message]);
374 const afterMove = (smove, initurn) => {
375 if (this.vr.turn != initurn) {
376 // Turn has changed: move is complete
378 // NOTE: only FEN of last sub-move is required (thus setting it here)
379 smove.fen = this.vr.getFen();
380 // Is opponent in check?
381 this.incheck = this.vr.getCheckSquares(this.vr.turn);
382 this.emitFenIfAnalyze();
383 this.inMultimove = false;
384 this.score = computeScore();
385 if (this.game.mode != "analyze") {
387 // Post-processing (e.g. computer play).
388 const L = this.moves.length;
389 // NOTE: always emit the score, even in unfinished,
390 // to tell Game::processMove() that it's not a received move.
391 this.$emit("newmove", this.moves[L-1], { score: this.score });
394 if (this.stackToPlay.length > 0)
395 // Move(s) arrived in-between
396 this.play(this.stackToPlay.pop(), received, light, noemit);
401 // NOTE: navigate and received are mutually exclusive
403 // The move to navigate to is necessarily full:
404 if (this.cursor == this.moves.length - 1) return; //no more moves
405 move = this.moves[this.cursor + 1];
406 // Just play the move:
407 if (!Array.isArray(move)) move = [move];
408 for (let i=0; i < move.length; i++) this.vr.play(move[i]);
410 this.lastMove = move[move.length-1];
411 this.incheck = this.vr.getCheckSquares(this.vr.turn);
412 this.score = computeScore();
413 this.emitFenIfAnalyze();
418 // Forbid playing outside analyze mode, except if move is received.
419 // Sufficient condition because Board already knows which turn it is.
421 this.game.mode != "analyze" &&
423 (this.game.score != "*" || this.cursor < this.moves.length - 1)
427 // To play a received move, cursor must be at the end of the game:
428 if (received && this.cursor < this.moves.length - 1)
432 cancelCurrentMultimove: function() {
433 const L = this.moves.length;
434 let move = this.moves[L-1];
435 if (!Array.isArray(move)) move = [move];
436 for (let i=move.length -1; i >= 0; i--) this.vr.undo(move[i]);
439 this.inMultimove = false;
441 cancelLastMove: function() {
442 // The last played move was canceled (corr game)
446 // "light": if gotoMove() or gotoBegin()
447 undo: function(move, light) {
448 if (this.inMultimove) {
449 this.cancelCurrentMultimove();
450 this.incheck = this.vr.getCheckSquares(this.vr.turn);
454 this.moves.length > 0 && this.moves[0].notation == "..."
457 if (this.cursor < minCursor) return; //no more moves
458 move = this.moves[this.cursor];
460 undoMove(move, this.vr);
461 if (light) this.cursor--;
463 this.positionCursorTo(this.cursor - 1);
464 this.incheck = this.vr.getCheckSquares(this.vr.turn);
465 this.emitFenIfAnalyze();
469 gotoMove: function(index) {
470 if (this.inMultimove) this.cancelCurrentMultimove();
471 if (index == this.cursor) return;
472 if (index < this.cursor) {
473 while (this.cursor > index)
474 this.undo(null, null, "light");
477 // index > this.cursor)
478 while (this.cursor < index)
479 this.play(null, null, "light");
481 // NOTE: next line also re-assign cursor, but it's very light
482 this.positionCursorTo(index);
483 this.incheck = this.vr.getCheckSquares(this.vr.turn);
484 this.emitFenIfAnalyze();
486 gotoBegin: function() {
487 if (this.inMultimove) this.cancelCurrentMultimove();
489 this.moves.length > 0 && this.moves[0].notation == "..."
492 while (this.cursor >= minCursor) this.undo(null, null, "light");
493 this.lastMove = (minCursor == 1 ? this.moves[0] : null);
494 this.incheck = this.vr.getCheckSquares(this.vr.turn);
495 this.emitFenIfAnalyze();
497 gotoEnd: function() {
498 if (this.cursor == this.moves.length - 1) return;
499 this.gotoMove(this.moves.length - 1);
500 this.emitFenIfAnalyze();
503 this.orientation = V.GetOppCol(this.orientation);
509 <style lang="sass" scoped>
510 [type="checkbox"]#modalEog+div .card
523 display: inline-block
536 @media screen and (max-width: 767px)
545 // TODO: later, maybe, allow movesList of variable width
546 // or e.g. between 250 and 350px (but more complicated)
552 @media screen and (max-width: 767px)