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")
37 #downloadDiv(v-if="allowDownloadPGN")
39 button(@click="download()") {{ st.tr["Download"] }} PGN
42 @click="analyzePosition()"
44 | {{ st.tr["Analyse"] }}
45 // NOTE: variants pages already have a "Rules" link on top
47 v-if="!$route.path.match('/variants/')"
50 | {{ st.tr["Rules"] }}
55 :message="game.scoreMsg"
56 :firstNum="firstMoveNumber"
65 import Board from "@/components/Board.vue";
66 import MoveList from "@/components/MoveList.vue";
67 import { store } from "@/store";
68 import { getSquareId } from "@/utils/squareId";
69 import { getDate } from "@/utils/datetime";
70 import { processModalClick } from "@/utils/modalClick";
71 import { getScoreMessage } from "@/utils/scoring";
72 import { getFullNotation } from "@/utils/notation";
73 import { undoMove } from "@/utils/playUndo";
84 // NOTE: all following variables must be reset at the beginning of a game
85 vr: null, //VariantRules object, game state
88 score: "*", //'*' means 'unfinished'
90 cursor: -1, //index of the move just played
92 firstMoveNumber: 0, //for printing
93 incheck: [], //for Board
100 // game initial FEN changes when a new game starts
101 "game.fenStart": function() {
102 this.re_setVariables();
106 showMoves: function() {
107 return this.game.score != "*"
109 : (this.vr ? this.vr.showMoves : "none");
111 showTurn: function() {
113 this.game.score == '*' &&
115 (this.vr.showMoves != "all" || !this.vr.canFlip)
121 if (this.vr.showMoves != "all")
122 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]
123 // Cannot flip: racing king or circular chess
124 return this.vr.movesCount == 0 && this.game.mycolor == "w"
125 ? this.st.tr["It's your turn!"]
128 canAnalyze: function() {
129 return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze;
131 canFlip: function() {
132 return this.vr && this.vr.canFlip;
134 allowDownloadPGN: function() {
135 return this.game.score != "*" || (this.vr && this.vr.showMoves == "all");
138 created: function() {
139 if (this.game.fenStart) this.re_setVariables();
141 mounted: function() {
142 if (!("ontouchstart" in window)) {
144 const baseGameDiv = document.getElementById("baseGame");
145 baseGameDiv.tabIndex = 0;
146 baseGameDiv.addEventListener("click", this.focusBg);
147 baseGameDiv.addEventListener("keydown", this.handleKeys);
148 baseGameDiv.addEventListener("wheel", this.handleScroll);
150 document.getElementById("eogDiv").addEventListener(
155 focusBg: function() {
156 document.getElementById("baseGame").focus();
158 handleKeys: function(e) {
159 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
178 handleScroll: function(e) {
180 if (e.deltaY < 0) this.undo();
181 else if (e.deltaY > 0) this.play();
183 showRules: function() {
184 //this.$router.push("/variants/" + this.game.vname);
185 window.open("#/variants/" + this.game.vname, "_blank"); //better
187 re_setVariables: function() {
188 this.endgameMessage = "";
189 // "w": default orientation for observed games
190 this.orientation = this.game.mycolor || "w";
191 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
192 // Post-processing: decorate each move with notation and FEN
193 this.vr = new V(this.game.fenStart);
194 const parsedFen = V.ParseFen(this.game.fenStart);
195 const firstMoveColor = parsedFen.turn;
196 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
197 this.moves.forEach(move => {
198 // Strategy working also for multi-moves:
199 if (!Array.isArray(move)) move = [move];
201 m.notation = this.vr.getNotation(m);
205 if (firstMoveColor == "b") {
206 // 'start' & 'end' is required for Board component
209 start: { x: -1, y: -1 },
210 end: { x: -1, y: -1 }
213 this.positionCursorTo(this.moves.length - 1);
214 this.incheck = this.vr.getCheckSquares(this.vr.turn);
216 positionCursorTo: function(index) {
218 // Caution: last move in moves array might be a multi-move
220 if (Array.isArray(this.moves[index])) {
221 const L = this.moves[index].length;
222 this.lastMove = this.moves[index][L - 1];
224 this.lastMove = this.moves[index];
228 this.lastMove = null;
230 analyzePosition: function() {
235 this.vr.getFen().replace(/ /g, "_");
236 if (this.game.mycolor)
237 newUrl += "&side=" + this.game.mycolor;
238 // Open in same tab in live games (against cheating)
239 if (this.game.type == "live") this.$router.push(newUrl);
240 else window.open("#" + newUrl);
242 download: function() {
243 const content = this.getPgn();
244 // Prepare and trigger download link
245 let downloadAnchor = document.getElementById("download");
246 downloadAnchor.setAttribute("download", "game.pgn");
247 downloadAnchor.href =
248 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
249 downloadAnchor.click();
253 pgn += '[Site "vchess.club"]\n';
254 pgn += '[Variant "' + this.game.vname + '"]\n';
255 pgn += '[Date "' + getDate(new Date()) + '"]\n';
256 pgn += '[White "' + this.game.players[0].name + '"]\n';
257 pgn += '[Black "' + this.game.players[1].name + '"]\n';
258 pgn += '[Fen "' + this.game.fenStart + '"]\n';
259 pgn += '[Result "' + this.game.score + '"]\n\n';
260 for (let i = 0; i < this.moves.length; i += 2) {
261 pgn += (i/2+1) + "." + getFullNotation(this.moves[i]) + " ";
262 if (i+1 < this.moves.length)
263 pgn += getFullNotation(this.moves[i+1]) + " ";
267 showEndgameMsg: function(message) {
268 this.endgameMessage = message;
269 let modalBox = document.getElementById("modalEog");
270 modalBox.checked = true;
272 modalBox.checked = false;
275 // Animate an elementary move
276 animateMove: function(move, callback) {
277 let startSquare = document.getElementById(getSquareId(move.start));
278 if (!startSquare) return; //shouldn't happen but...
279 let endSquare = document.getElementById(getSquareId(move.end));
280 let rectStart = startSquare.getBoundingClientRect();
281 let rectEnd = endSquare.getBoundingClientRect();
283 x: rectEnd.x - rectStart.x,
284 y: rectEnd.y - rectStart.y
286 let movingPiece = document.querySelector(
287 "#" + getSquareId(move.start) + " > img.piece"
289 // For some unknown reasons Opera get "movingPiece == null" error
290 // TOOO: is it calling 'animate()' twice ? One extra time ?
291 if (!movingPiece) return;
292 // HACK for animation (with positive translate, image slides "under background")
293 // Possible improvement: just alter squares on the piece's way...
294 const squares = document.getElementsByClassName("board");
295 for (let i = 0; i < squares.length; i++) {
296 let square = squares.item(i);
297 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
299 movingPiece.style.transform =
300 "translate(" + translation.x + "px," + translation.y + "px)";
301 movingPiece.style.transitionDuration = "0.25s";
302 movingPiece.style.zIndex = "3000";
304 for (let i = 0; i < squares.length; i++)
305 squares.item(i).style.zIndex = "auto";
306 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
311 emitFenIfAnalyze: function() {
312 if (this.game.mode == "analyze") {
315 this.lastMove ? this.lastMove.fen : this.game.fenStart
319 // "light": if gotoMove() or gotoEnd()
320 play: function(move, received, light, noemit) {
323 // Received moves in observed games can arrive too fast:
324 this.stackToPlay.unshift(move);
329 const navigate = !move;
330 const playSubmove = (smove) => {
331 if (!navigate) smove.notation = this.vr.getNotation(smove);
333 this.lastMove = smove;
334 // Is opponent in check?
335 this.incheck = this.vr.getCheckSquares(this.vr.turn);
337 if (!this.inMultimove) {
338 if (this.cursor < this.moves.length - 1)
339 this.moves = this.moves.slice(0, this.cursor + 1);
340 this.moves.push(smove);
341 this.inMultimove = true; //potentially
344 // Already in the middle of a multi-move
345 const L = this.moves.length;
346 if (!Array.isArray(this.moves[L-1]))
347 this.$set(this.moves, L-1, [this.moves[L-1], smove]);
349 this.$set(this.moves, L-1, this.moves.concat([smove]));
353 const playMove = () => {
354 const animate = V.ShowMoves == "all" && (received || navigate);
355 if (!Array.isArray(move)) move = [move];
358 const initurn = this.vr.turn;
359 (function executeMove() {
360 const smove = move[moveIdx++];
362 self.animateMove(smove, () => {
364 if (moveIdx < move.length)
365 setTimeout(executeMove, 500);
366 else afterMove(smove, initurn);
370 if (moveIdx < move.length) executeMove();
371 else afterMove(smove, initurn);
375 const afterMove = (smove, initurn) => {
376 if (this.vr.turn != initurn) {
377 // Turn has changed: move is complete
379 // NOTE: only FEN of last sub-move is required (thus setting it here)
380 smove.fen = this.vr.getFen();
381 this.emitFenIfAnalyze();
383 this.inMultimove = false;
384 const score = this.vr.getCurrentScore();
386 const message = getScoreMessage(score);
387 if (!navigate && this.game.mode != "analyze")
388 this.$emit("gameover", score, message);
389 else if (this.game.mode == "analyze")
390 // Just show score on screen (allow undo)
391 this.showEndgameMsg(score + " . " + this.st.tr[message]);
393 if (!navigate && this.game.mode != "analyze") {
394 const L = this.moves.length;
396 // Post-processing (e.g. computer play)
397 this.$emit("newmove", this.moves[L-1]);
400 if (this.stackToPlay.length > 0)
401 // Move(s) arrived in-between
402 this.play(this.stackToPlay.pop(), received, light, noemit);
407 // NOTE: navigate and received are mutually exclusive
409 // The move to navigate to is necessarily full:
410 if (this.cursor == this.moves.length - 1) return; //no more moves
411 move = this.moves[this.cursor + 1];
413 // Just play the move, nothing else:
414 if (!Array.isArray(move)) move = [move];
415 for (let i=0; i < move.length; i++) this.vr.play(move[i]);
419 this.emitFenIfAnalyze();
424 // Forbid playing outside analyze mode, except if move is received.
425 // Sufficient condition because Board already knows which turn it is.
427 this.game.mode != "analyze" &&
429 (this.game.score != "*" || this.cursor < this.moves.length - 1)
433 // To play a received move, cursor must be at the end of the game:
434 if (received && this.cursor < this.moves.length - 1)
438 cancelCurrentMultimove: function() {
439 const L = this.moves.length;
440 let move = this.moves[L-1];
441 if (!Array.isArray(move)) move = [move];
442 for (let i=move.length -1; i >= 0; i--) this.vr.undo(move[i]);
445 this.inMultimove = false;
447 cancelLastMove: function() {
448 // The last played move was canceled (corr game)
452 // "light": if gotoMove() or gotoBegin()
453 undo: function(move, light) {
454 if (this.inMultimove) {
455 this.cancelCurrentMultimove();
456 this.incheck = this.vr.getCheckSquares(this.vr.turn);
459 if (this.cursor < 0) return; //no more moves
460 move = this.moves[this.cursor];
462 // Caution; if multi-move, undo all submoves from last to first
463 undoMove(move, this.vr);
464 if (light) this.cursor--;
466 this.positionCursorTo(this.cursor - 1);
467 this.incheck = this.vr.getCheckSquares(this.vr.turn);
468 this.emitFenIfAnalyze();
472 gotoMove: function(index) {
473 if (this.inMultimove) this.cancelCurrentMultimove();
474 if (index == this.cursor) return;
475 if (index < this.cursor) {
476 while (this.cursor > index)
477 this.undo(null, null, "light");
480 // index > this.cursor)
481 while (this.cursor < index)
482 this.play(null, null, "light");
484 // NOTE: next line also re-assign cursor, but it's very light
485 this.positionCursorTo(index);
486 this.incheck = this.vr.getCheckSquares(this.vr.turn);
487 this.emitFenIfAnalyze();
489 gotoBegin: function() {
490 if (this.inMultimove) this.cancelCurrentMultimove();
491 while (this.cursor >= 0)
492 this.undo(null, null, "light");
493 if (this.moves.length > 0 && this.moves[0].notation == "...") {
495 this.lastMove = this.moves[0];
497 this.lastMove = null;
500 this.emitFenIfAnalyze();
502 gotoEnd: function() {
503 if (this.cursor == this.moves.length - 1) return;
504 this.gotoMove(this.moves.length - 1);
505 this.emitFenIfAnalyze();
508 this.orientation = V.GetOppCol(this.orientation);
514 <style lang="sass" scoped>
515 [type="checkbox"]#modalEog+div .card
528 display: inline-block
535 display: inline-block
541 @media screen and (max-width: 767px)
550 border-top: 1px solid #2f4f4f
558 border-left: 1px solid #2f4f4f
563 // TODO: later, maybe, allow movesList of variable width
564 // or e.g. between 250 and 350px (but more complicated)
570 @media screen and (max-width: 767px)