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 }}
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 // game initial FEN changes when a new game starts.
91 // NOTE: when game ID change on Game page, fenStart may be temporarily undefined
92 "game.fenStart": function(fenStart) {
93 if (!!fenStart) this.re_setVariables();
97 showMoves: function() {
98 return this.game.score != "*"
100 : (this.vr ? this.vr.showMoves : "none");
102 showTurn: function() {
104 this.game.score == '*' &&
106 (this.vr.showMoves != "all" || !this.vr.canFlip)
112 if (this.vr.showMoves != "all")
113 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]
114 // Cannot flip: racing king or circular chess
115 return this.vr.movesCount == 0 && this.game.mycolor == "w"
116 ? this.st.tr["It's your turn!"]
119 canAnalyze: function() {
120 return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze;
122 canFlip: function() {
123 return this.vr && this.vr.canFlip;
125 allowDownloadPGN: function() {
126 return this.game.score != "*" || (this.vr && this.vr.showMoves == "all");
129 created: function() {
130 if (this.game.fenStart) this.re_setVariables();
132 mounted: function() {
133 if (!("ontouchstart" in window)) {
135 const baseGameDiv = document.getElementById("baseGame");
136 baseGameDiv.tabIndex = 0;
137 baseGameDiv.addEventListener("click", this.focusBg);
138 baseGameDiv.addEventListener("keydown", this.handleKeys);
139 baseGameDiv.addEventListener("wheel", this.handleScroll);
141 document.getElementById("eogDiv").addEventListener(
146 focusBg: function() {
147 document.getElementById("baseGame").focus();
149 handleKeys: function(e) {
150 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
169 handleScroll: function(e) {
171 if (e.deltaY < 0) this.undo();
172 else if (e.deltaY > 0) this.play();
174 showRules: function() {
175 //this.$router.push("/variants/" + this.game.vname);
176 window.open("#/variants/" + this.game.vname, "_blank"); //better
178 re_setVariables: function() {
179 this.endgameMessage = "";
180 // "w": default orientation for observed games
181 this.orientation = this.game.mycolor || "w";
182 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
183 // Post-processing: decorate each move with notation and FEN
184 this.vr = new V(this.game.fenStart);
185 const parsedFen = V.ParseFen(this.game.fenStart);
186 const firstMoveColor = parsedFen.turn;
187 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
188 this.moves.forEach(move => {
189 // Strategy working also for multi-moves:
190 if (!Array.isArray(move)) move = [move];
192 m.notation = this.vr.getNotation(m);
196 if (firstMoveColor == "b") {
197 // 'start' & 'end' is required for Board component
200 start: { x: -1, y: -1 },
201 end: { x: -1, y: -1 }
204 this.positionCursorTo(this.moves.length - 1);
205 this.incheck = this.vr.getCheckSquares(this.vr.turn);
207 positionCursorTo: function(index) {
209 // Caution: last move in moves array might be a multi-move
211 if (Array.isArray(this.moves[index])) {
212 const L = this.moves[index].length;
213 this.lastMove = this.moves[index][L - 1];
215 this.lastMove = this.moves[index];
219 this.lastMove = null;
221 analyzePosition: function() {
226 this.vr.getFen().replace(/ /g, "_");
227 if (this.game.mycolor)
228 newUrl += "&side=" + this.game.mycolor;
229 // Open in same tab in live games (against cheating)
230 if (this.game.type == "live") this.$router.push(newUrl);
231 else window.open("#" + newUrl);
233 download: function() {
234 const content = this.getPgn();
235 // Prepare and trigger download link
236 let downloadAnchor = document.getElementById("download");
237 downloadAnchor.setAttribute("download", "game.pgn");
238 downloadAnchor.href =
239 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
240 downloadAnchor.click();
244 pgn += '[Site "vchess.club"]\n';
245 pgn += '[Variant "' + this.game.vname + '"]\n';
246 pgn += '[Date "' + getDate(new Date()) + '"]\n';
247 pgn += '[White "' + this.game.players[0].name + '"]\n';
248 pgn += '[Black "' + this.game.players[1].name + '"]\n';
249 pgn += '[Fen "' + this.game.fenStart + '"]\n';
250 pgn += '[Result "' + this.game.score + '"]\n\n';
251 for (let i = 0; i < this.moves.length; i += 2) {
252 pgn += (i/2+1) + "." + getFullNotation(this.moves[i]) + " ";
253 if (i+1 < this.moves.length)
254 pgn += getFullNotation(this.moves[i+1]) + " ";
258 showEndgameMsg: function(message) {
259 this.endgameMessage = message;
260 document.getElementById("modalEog").checked = true;
262 // Animate an elementary move
263 animateMove: function(move, callback) {
264 let startSquare = document.getElementById(getSquareId(move.start));
265 if (!startSquare) return; //shouldn't happen but...
266 let endSquare = document.getElementById(getSquareId(move.end));
267 let rectStart = startSquare.getBoundingClientRect();
268 let rectEnd = endSquare.getBoundingClientRect();
270 x: rectEnd.x - rectStart.x,
271 y: rectEnd.y - rectStart.y
273 let movingPiece = document.querySelector(
274 "#" + getSquareId(move.start) + " > img.piece"
276 // For some unknown reasons Opera get "movingPiece == null" error
277 // TOOO: is it calling 'animate()' twice ? One extra time ?
278 if (!movingPiece) return;
279 // HACK for animation (with positive translate, image slides "under background")
280 // Possible improvement: just alter squares on the piece's way...
281 const squares = document.getElementsByClassName("board");
282 for (let i = 0; i < squares.length; i++) {
283 let square = squares.item(i);
284 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
286 movingPiece.style.transform =
287 "translate(" + translation.x + "px," + translation.y + "px)";
288 movingPiece.style.transitionDuration = "0.25s";
289 movingPiece.style.zIndex = "3000";
291 for (let i = 0; i < squares.length; i++)
292 squares.item(i).style.zIndex = "auto";
293 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
298 emitFenIfAnalyze: function() {
299 if (this.game.mode == "analyze") {
302 this.lastMove ? this.lastMove.fen : this.game.fenStart
306 // "light": if gotoMove() or gotoEnd()
307 play: function(move, received, light, noemit) {
310 // Received moves in observed games can arrive too fast:
311 this.stackToPlay.unshift(move);
316 const navigate = !move;
317 const playSubmove = (smove) => {
318 if (!navigate) smove.notation = this.vr.getNotation(smove);
320 this.lastMove = smove;
321 // Is opponent in check?
322 this.incheck = this.vr.getCheckSquares(this.vr.turn);
324 if (!this.inMultimove) {
325 if (this.cursor < this.moves.length - 1)
326 this.moves = this.moves.slice(0, this.cursor + 1);
327 this.moves.push(smove);
328 this.inMultimove = true; //potentially
331 // Already in the middle of a multi-move
332 const L = this.moves.length;
333 if (!Array.isArray(this.moves[L-1]))
334 this.$set(this.moves, L-1, [this.moves[L-1], smove]);
336 this.$set(this.moves, L-1, this.moves.concat([smove]));
340 const playMove = () => {
341 const animate = V.ShowMoves == "all" && (received || navigate);
342 if (!Array.isArray(move)) move = [move];
345 const initurn = this.vr.turn;
346 (function executeMove() {
347 const smove = move[moveIdx++];
349 self.animateMove(smove, () => {
351 if (moveIdx < move.length)
352 setTimeout(executeMove, 500);
353 else afterMove(smove, initurn);
357 if (moveIdx < move.length) executeMove();
358 else afterMove(smove, initurn);
362 const afterMove = (smove, initurn) => {
363 if (this.vr.turn != initurn) {
364 // Turn has changed: move is complete
366 // NOTE: only FEN of last sub-move is required (thus setting it here)
367 smove.fen = this.vr.getFen();
368 this.emitFenIfAnalyze();
370 this.inMultimove = false;
372 var score = this.vr.getCurrentScore();
373 if (score != "*" && this.game.mode == "analyze") {
374 const message = getScoreMessage(score);
375 // Just show score on screen (allow undo)
376 this.showEndgameMsg(score + " . " + this.st.tr[message]);
379 if (!navigate && this.game.mode != "analyze") {
380 const L = this.moves.length;
382 // Post-processing (e.g. computer play).
383 // NOTE: always emit the score, even in unfinished,
384 // to tell Game::processMove() that it's not a received move.
385 this.$emit("newmove", this.moves[L-1], { score: score });
388 if (this.stackToPlay.length > 0)
389 // Move(s) arrived in-between
390 this.play(this.stackToPlay.pop(), received, light, noemit);
395 // NOTE: navigate and received are mutually exclusive
397 // The move to navigate to is necessarily full:
398 if (this.cursor == this.moves.length - 1) return; //no more moves
399 move = this.moves[this.cursor + 1];
401 // Just play the move, nothing else:
402 if (!Array.isArray(move)) move = [move];
403 for (let i=0; i < move.length; i++) this.vr.play(move[i]);
407 this.emitFenIfAnalyze();
412 // Forbid playing outside analyze mode, except if move is received.
413 // Sufficient condition because Board already knows which turn it is.
415 this.game.mode != "analyze" &&
417 (this.game.score != "*" || this.cursor < this.moves.length - 1)
421 // To play a received move, cursor must be at the end of the game:
422 if (received && this.cursor < this.moves.length - 1)
426 cancelCurrentMultimove: function() {
427 const L = this.moves.length;
428 let move = this.moves[L-1];
429 if (!Array.isArray(move)) move = [move];
430 for (let i=move.length -1; i >= 0; i--) this.vr.undo(move[i]);
433 this.inMultimove = false;
435 cancelLastMove: function() {
436 // The last played move was canceled (corr game)
440 // "light": if gotoMove() or gotoBegin()
441 undo: function(move, light) {
442 if (this.inMultimove) {
443 this.cancelCurrentMultimove();
444 this.incheck = this.vr.getCheckSquares(this.vr.turn);
447 if (this.cursor < 0) return; //no more moves
448 move = this.moves[this.cursor];
450 // Caution; if multi-move, undo all submoves from last to first
451 undoMove(move, this.vr);
452 if (light) this.cursor--;
454 this.positionCursorTo(this.cursor - 1);
455 this.incheck = this.vr.getCheckSquares(this.vr.turn);
456 this.emitFenIfAnalyze();
460 gotoMove: function(index) {
461 if (this.inMultimove) this.cancelCurrentMultimove();
462 if (index == this.cursor) return;
463 if (index < this.cursor) {
464 while (this.cursor > index)
465 this.undo(null, null, "light");
468 // index > this.cursor)
469 while (this.cursor < index)
470 this.play(null, null, "light");
472 // NOTE: next line also re-assign cursor, but it's very light
473 this.positionCursorTo(index);
474 this.incheck = this.vr.getCheckSquares(this.vr.turn);
475 this.emitFenIfAnalyze();
477 gotoBegin: function() {
478 if (this.inMultimove) this.cancelCurrentMultimove();
479 while (this.cursor >= 0)
480 this.undo(null, null, "light");
481 if (this.moves.length > 0 && this.moves[0].notation == "...") {
483 this.lastMove = this.moves[0];
485 this.lastMove = null;
488 this.emitFenIfAnalyze();
490 gotoEnd: function() {
491 if (this.cursor == this.moves.length - 1) return;
492 this.gotoMove(this.moves.length - 1);
493 this.emitFenIfAnalyze();
496 this.orientation = V.GetOppCol(this.orientation);
502 <style lang="sass" scoped>
503 [type="checkbox"]#modalEog+div .card
516 display: inline-block
523 display: inline-block
532 @media screen and (max-width: 767px)
541 // TODO: later, maybe, allow movesList of variable width
542 // or e.g. between 250 and 350px (but more complicated)
548 @media screen and (max-width: 767px)