3 input#modalEog.modal(type="checkbox")
6 data-checkbox="modalEog"
9 label.modal-close(for="modalEog")
10 h3.section {{ endgameMessage }}
17 :analyze="game.mode=='analyze'"
19 :user-color="game.mycolor"
20 :orientation="orientation"
25 #turnIndicator(v-if="showTurn") {{ turn }}
26 #controls.button-group
27 button(@click="gotoBegin()")
28 img.inline(src="/images/icons/fast-forward_rev.svg")
29 button(@click="undo()")
30 img.inline(src="/images/icons/play_rev.svg")
31 button(v-if="canFlip" @click="flip()")
32 img.inline(src="/images/icons/flip.svg")
33 button(@click="play()")
34 img.inline(src="/images/icons/play.svg")
35 button(@click="gotoEnd()")
36 img.inline(src="/images/icons/fast-forward.svg")
40 :canAnalyze="canAnalyze"
41 :canDownload="allowDownloadPGN"
43 :message="game.scoreMsg"
44 :firstNum="firstMoveNumber"
48 @showrules="showRules"
49 @analyze="analyzePosition"
56 import Board from "@/components/Board.vue";
57 import MoveList from "@/components/MoveList.vue";
58 import { store } from "@/store";
59 import { getSquareId } from "@/utils/squareId";
60 import { getDate } from "@/utils/datetime";
61 import { processModalClick } from "@/utils/modalClick";
62 import { getScoreMessage } from "@/utils/scoring";
63 import { getFullNotation } from "@/utils/notation";
64 import { undoMove } from "@/utils/playUndo";
75 // NOTE: all following variables must be reset at the beginning of a game
76 vr: null, //VariantRules object, game state
79 score: "*", //'*' means 'unfinished'
81 cursor: -1, //index of the move just played
83 firstMoveNumber: 0, //for printing
84 incheck: [], //for Board
91 showMoves: function() {
92 return this.game.score != "*"
94 : (this.vr ? this.vr.showMoves : "none");
96 showTurn: function() {
98 this.game.score == '*' &&
100 (this.vr.showMoves != "all" || !this.vr.canFlip)
106 if (this.vr.showMoves != "all")
107 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]
108 // Cannot flip: racing king or circular chess
109 return this.vr.movesCount == 0 && this.game.mycolor == "w"
110 ? this.st.tr["It's your turn!"]
113 canAnalyze: function() {
114 return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze;
116 canFlip: function() {
117 return this.vr && this.vr.canFlip;
119 allowDownloadPGN: function() {
120 return this.game.score != "*" || (this.vr && this.vr.showMoves == "all");
123 created: function() {
124 if (!!this.game.fenStart) this.re_setVariables();
126 mounted: function() {
127 if (!("ontouchstart" in window)) {
129 const baseGameDiv = document.getElementById("baseGame");
130 baseGameDiv.tabIndex = 0;
131 baseGameDiv.addEventListener("click", this.focusBg);
132 baseGameDiv.addEventListener("keydown", this.handleKeys);
133 baseGameDiv.addEventListener("wheel", this.handleScroll);
135 document.getElementById("eogDiv")
136 .addEventListener("click", processModalClick);
139 focusBg: function() {
140 document.getElementById("baseGame").focus();
142 handleKeys: function(e) {
143 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
162 handleScroll: function(e) {
164 if (e.deltaY < 0) this.undo();
165 else if (e.deltaY > 0) this.play();
167 showRules: function() {
168 //this.$router.push("/variants/" + this.game.vname);
169 window.open("#/variants/" + this.game.vname, "_blank"); //better
171 re_setVariables: function(game) {
172 if (!game) game = this.game; //in case of...
173 this.endgameMessage = "";
174 // "w": default orientation for observed games
175 this.orientation = game.mycolor || "w";
176 this.moves = JSON.parse(JSON.stringify(game.moves || []));
177 // Post-processing: decorate each move with notation and FEN
178 this.vr = new V(game.fenStart);
179 const parsedFen = V.ParseFen(game.fenStart);
180 const firstMoveColor = parsedFen.turn;
181 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
182 let L = this.moves.length;
183 this.moves.forEach(move => {
184 // Strategy working also for multi-moves:
185 if (!Array.isArray(move)) move = [move];
186 move.forEach((m,idx) => {
187 m.notation = this.vr.getNotation(m);
189 if (idx < L - 1 && this.vr.getCheckSquares(this.vr.turn).length > 0)
193 if (firstMoveColor == "b") {
194 // 'start' & 'end' is required for Board component
197 start: { x: -1, y: -1 },
198 end: { x: -1, y: -1 },
203 this.positionCursorTo(this.moves.length - 1);
204 this.incheck = this.vr.getCheckSquares(this.vr.turn);
205 const score = this.vr.getCurrentScore();
206 if (L > 0 && this.moves[L - 1].notation != "...") {
207 if (["1-0","0-1"].includes(score))
208 this.moves[L - 1].notation += "#";
209 else if (this.vr.getCheckSquares(this.vr.turn).length > 0)
210 this.moves[L - 1].notation += "+";
213 positionCursorTo: function(index) {
215 // Caution: last move in moves array might be a multi-move
217 if (Array.isArray(this.moves[index])) {
218 const L = this.moves[index].length;
219 this.lastMove = this.moves[index][L - 1];
221 this.lastMove = this.moves[index];
223 } else this.lastMove = null;
225 analyzePosition: function() {
230 this.vr.getFen().replace(/ /g, "_");
231 if (this.game.mycolor)
232 newUrl += "&side=" + this.game.mycolor;
233 // Open in same tab in live games (against cheating)
234 if (this.game.type == "live") this.$router.push(newUrl);
235 else window.open("#" + newUrl);
237 download: function() {
238 const content = this.getPgn();
239 // Prepare and trigger download link
240 let downloadAnchor = document.getElementById("download");
241 downloadAnchor.setAttribute("download", "game.pgn");
242 downloadAnchor.href =
243 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
244 downloadAnchor.click();
248 pgn += '[Site "vchess.club"]\n';
249 pgn += '[Variant "' + this.game.vname + '"]\n';
250 pgn += '[Date "' + getDate(new Date()) + '"]\n';
251 pgn += '[White "' + this.game.players[0].name + '"]\n';
252 pgn += '[Black "' + this.game.players[1].name + '"]\n';
253 pgn += '[Fen "' + this.game.fenStart + '"]\n';
254 pgn += '[Result "' + this.game.score + '"]\n\n';
255 for (let i = 0; i < this.moves.length; i += 2) {
256 pgn += (i/2+1) + "." + getFullNotation(this.moves[i]) + " ";
257 if (i+1 < this.moves.length)
258 pgn += getFullNotation(this.moves[i+1]) + " ";
262 showEndgameMsg: function(message) {
263 this.endgameMessage = message;
264 document.getElementById("modalEog").checked = true;
266 // Animate an elementary move
267 animateMove: function(move, callback) {
268 let startSquare = document.getElementById(getSquareId(move.start));
269 if (!startSquare) return; //shouldn't happen but...
270 let endSquare = document.getElementById(getSquareId(move.end));
271 let rectStart = startSquare.getBoundingClientRect();
272 let rectEnd = endSquare.getBoundingClientRect();
274 x: rectEnd.x - rectStart.x,
275 y: rectEnd.y - rectStart.y
277 let movingPiece = document.querySelector(
278 "#" + getSquareId(move.start) + " > img.piece"
280 // For some unknown reasons Opera get "movingPiece == null" error
281 // TOOO: is it calling 'animate()' twice ? One extra time ?
282 if (!movingPiece) return;
283 // HACK for animation (with positive translate, image slides "under background")
284 // Possible improvement: just alter squares on the piece's way...
285 const squares = document.getElementsByClassName("board");
286 for (let i = 0; i < squares.length; i++) {
287 let square = squares.item(i);
288 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
290 movingPiece.style.transform =
291 "translate(" + translation.x + "px," + translation.y + "px)";
292 movingPiece.style.transitionDuration = "0.25s";
293 movingPiece.style.zIndex = "3000";
295 for (let i = 0; i < squares.length; i++)
296 squares.item(i).style.zIndex = "auto";
297 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
302 emitFenIfAnalyze: function() {
303 if (this.game.mode == "analyze") {
306 !!this.lastMove ? this.lastMove.fen : this.game.fenStart
310 // "light": if gotoMove() or gotoEnd()
311 play: function(move, received, light, noemit) {
312 // Freeze while choices are shown:
313 if (this.$refs["board"].choices.length > 0) return;
316 // Received moves in observed games can arrive too fast:
317 this.stackToPlay.unshift(move);
322 const navigate = !move;
323 const playSubmove = (smove) => {
324 smove.notation = this.vr.getNotation(smove);
326 this.lastMove = smove;
327 if (!this.inMultimove) {
328 if (this.cursor < this.moves.length - 1)
329 this.moves = this.moves.slice(0, this.cursor + 1);
330 this.moves.push(smove);
331 this.inMultimove = true; //potentially
334 // Already in the middle of a multi-move
335 const L = this.moves.length;
336 if (!Array.isArray(this.moves[L-1]))
337 this.$set(this.moves, L-1, [this.moves[L-1], smove]);
339 this.$set(this.moves, L-1, this.moves.concat([smove]));
342 const playMove = () => {
343 const animate = (V.ShowMoves == "all" && !!received);
344 if (!Array.isArray(move)) move = [move];
347 const initurn = this.vr.turn;
348 (function executeMove() {
349 const smove = move[moveIdx++];
351 self.animateMove(smove, () => {
353 if (moveIdx < move.length)
354 setTimeout(executeMove, 500);
355 else afterMove(smove, initurn);
359 if (moveIdx < move.length) executeMove();
360 else afterMove(smove, initurn);
364 const computeScore = () => {
365 const score = this.vr.getCurrentScore();
367 if (["1-0","0-1"].includes(score))
368 this.lastMove.notation += "#";
369 else if (this.vr.getCheckSquares(this.vr.turn).length > 0)
370 this.lastMove.notation += "+";
372 if (score != "*" && this.game.mode == "analyze") {
373 const message = getScoreMessage(score);
374 // Just show score on screen (allow undo)
375 this.showEndgameMsg(score + " . " + this.st.tr[message]);
379 const afterMove = (smove, initurn) => {
380 if (this.vr.turn != initurn) {
381 // Turn has changed: move is complete
383 // NOTE: only FEN of last sub-move is required (thus setting it here)
384 smove.fen = this.vr.getFen();
385 // Is opponent in check?
386 this.incheck = this.vr.getCheckSquares(this.vr.turn);
387 this.emitFenIfAnalyze();
388 this.inMultimove = false;
389 this.score = computeScore();
390 if (this.game.mode != "analyze") {
392 // Post-processing (e.g. computer play).
393 const L = this.moves.length;
394 // NOTE: always emit the score, even in unfinished,
395 // to tell Game::processMove() that it's not a received move.
396 this.$emit("newmove", this.moves[L-1], { score: this.score });
399 if (this.stackToPlay.length > 0)
400 // Move(s) arrived in-between
401 this.play(this.stackToPlay.pop(), received, light, noemit);
406 // NOTE: navigate and received are mutually exclusive
408 // The move to navigate to is necessarily full:
409 if (this.cursor == this.moves.length - 1) return; //no more moves
410 move = this.moves[this.cursor + 1];
411 // Just play the move:
412 if (!Array.isArray(move)) move = [move];
413 for (let i=0; i < move.length; i++) this.vr.play(move[i]);
415 this.lastMove = move[move.length-1];
416 this.incheck = this.vr.getCheckSquares(this.vr.turn);
417 this.score = computeScore();
418 this.emitFenIfAnalyze();
423 // Forbid playing outside analyze mode, except if move is received.
424 // Sufficient condition because Board already knows which turn it is.
426 this.game.mode != "analyze" &&
428 (this.game.score != "*" || this.cursor < this.moves.length - 1)
432 // To play a received move, cursor must be at the end of the game:
433 if (received && this.cursor < this.moves.length - 1)
437 cancelCurrentMultimove: function() {
438 const L = this.moves.length;
439 let move = this.moves[L-1];
440 if (!Array.isArray(move)) move = [move];
441 for (let i=move.length -1; i >= 0; i--) this.vr.undo(move[i]);
444 this.inMultimove = false;
446 cancelLastMove: function() {
447 // The last played move was canceled (corr game)
451 // "light": if gotoMove() or gotoBegin()
452 undo: function(move, light) {
453 // Freeze while choices are shown:
454 if (this.$refs["board"].choices.length > 0) return;
455 if (this.inMultimove) {
456 this.cancelCurrentMultimove();
457 this.incheck = this.vr.getCheckSquares(this.vr.turn);
461 this.moves.length > 0 && this.moves[0].notation == "..."
464 if (this.cursor < minCursor) return; //no more moves
465 move = this.moves[this.cursor];
467 undoMove(move, this.vr);
468 if (light) this.cursor--;
470 this.positionCursorTo(this.cursor - 1);
471 this.incheck = this.vr.getCheckSquares(this.vr.turn);
472 this.emitFenIfAnalyze();
476 gotoMove: function(index) {
477 if (this.$refs["board"].choices.length > 0) return;
478 if (this.inMultimove) this.cancelCurrentMultimove();
479 if (index == this.cursor) return;
480 if (index < this.cursor) {
481 while (this.cursor > index)
482 this.undo(null, null, "light");
485 // index > this.cursor)
486 while (this.cursor < index)
487 this.play(null, null, "light");
489 // NOTE: next line also re-assign cursor, but it's very light
490 this.positionCursorTo(index);
491 this.incheck = this.vr.getCheckSquares(this.vr.turn);
492 this.emitFenIfAnalyze();
494 gotoBegin: function() {
495 if (this.$refs["board"].choices.length > 0) return;
496 if (this.inMultimove) this.cancelCurrentMultimove();
498 this.moves.length > 0 && this.moves[0].notation == "..."
501 while (this.cursor >= minCursor) this.undo(null, null, "light");
502 this.lastMove = (minCursor == 1 ? this.moves[0] : null);
503 this.incheck = this.vr.getCheckSquares(this.vr.turn);
504 this.emitFenIfAnalyze();
506 gotoEnd: function() {
507 if (this.$refs["board"].choices.length > 0) return;
508 if (this.cursor == this.moves.length - 1) return;
509 this.gotoMove(this.moves.length - 1);
510 this.emitFenIfAnalyze();
513 if (this.$refs["board"].choices.length > 0) return;
514 this.orientation = V.GetOppCol(this.orientation);
520 <style lang="sass" scoped>
521 [type="checkbox"]#modalEog+div .card
534 display: inline-block
547 @media screen and (max-width: 767px)
556 // TODO: later, maybe, allow movesList of variable width
557 // or e.g. between 250 and 350px (but more complicated)
563 @media screen and (max-width: 767px)