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 // TODO: later, use subCursor to navigate intra-multimoves?
91 cursor: -1, //index of the move just played
93 firstMoveNumber: 0, //for printing
94 incheck: [], //for Board
99 // game initial FEN changes when a new game starts
100 "game.fenStart": function() {
101 this.re_setVariables();
105 showMoves: function() {
106 return this.game.score != "*"
108 : (this.vr ? this.vr.showMoves : "none");
110 showTurn: function() {
112 this.game.score == '*' &&
114 (this.vr.showMoves != "all" || !this.vr.canFlip)
120 if (this.vr.showMoves != "all")
121 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]
122 // Cannot flip: racing king or circular chess
123 return this.vr.movesCount == 0 && this.game.mycolor == "w"
124 ? this.st.tr["It's your turn!"]
127 canAnalyze: function() {
128 return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze;
130 canFlip: function() {
131 return this.vr && this.vr.canFlip;
133 allowDownloadPGN: function() {
134 return this.game.score != "*" || (this.vr && this.vr.showMoves == "all");
137 created: function() {
138 if (this.game.fenStart) this.re_setVariables();
140 mounted: function() {
141 if (!("ontouchstart" in window)) {
143 const baseGameDiv = document.getElementById("baseGame");
144 baseGameDiv.tabIndex = 0;
145 baseGameDiv.addEventListener("click", this.focusBg);
146 baseGameDiv.addEventListener("keydown", this.handleKeys);
147 baseGameDiv.addEventListener("wheel", this.handleScroll);
149 document.getElementById("eogDiv").addEventListener(
154 focusBg: function() {
155 document.getElementById("baseGame").focus();
157 handleKeys: function(e) {
158 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
177 handleScroll: function(e) {
179 if (e.deltaY < 0) this.undo();
180 else if (e.deltaY > 0) this.play();
182 showRules: function() {
183 //this.$router.push("/variants/" + this.game.vname);
184 window.open("#/variants/" + this.game.vname, "_blank"); //better
186 re_setVariables: function() {
187 this.endgameMessage = "";
188 // "w": default orientation for observed games
189 this.orientation = this.game.mycolor || "w";
190 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
191 // Post-processing: decorate each move with notation and FEN
192 this.vr = new V(this.game.fenStart);
193 const parsedFen = V.ParseFen(this.game.fenStart);
194 const firstMoveColor = parsedFen.turn;
195 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
196 this.moves.forEach(move => {
197 // Strategy working also for multi-moves:
198 if (!Array.isArray(move)) move = [move];
200 m.notation = this.vr.getNotation(m);
204 if (firstMoveColor == "b") {
205 // 'start' & 'end' is required for Board component
208 start: { x: -1, y: -1 },
209 end: { x: -1, y: -1 }
212 this.positionCursorTo(this.moves.length - 1);
213 this.incheck = this.vr.getCheckSquares(this.vr.turn);
215 positionCursorTo: function(index) {
217 // Caution: last move in moves array might be a multi-move
219 if (Array.isArray(this.moves[index])) {
220 const L = this.moves[index].length;
221 this.lastMove = this.moves[index][L - 1];
223 this.lastMove = this.moves[index];
227 this.lastMove = null;
229 analyzePosition: function() {
234 this.vr.getFen().replace(/ /g, "_");
235 if (this.game.mycolor)
236 newUrl += "&side=" + this.game.mycolor;
237 // Open in same tab in live games (against cheating)
238 if (this.game.type == "live") this.$router.push(newUrl);
239 else window.open("#" + newUrl);
241 download: function() {
242 const content = this.getPgn();
243 // Prepare and trigger download link
244 let downloadAnchor = document.getElementById("download");
245 downloadAnchor.setAttribute("download", "game.pgn");
246 downloadAnchor.href =
247 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
248 downloadAnchor.click();
252 pgn += '[Site "vchess.club"]\n';
253 pgn += '[Variant "' + this.game.vname + '"]\n';
254 pgn += '[Date "' + getDate(new Date()) + '"]\n';
255 pgn += '[White "' + this.game.players[0].name + '"]\n';
256 pgn += '[Black "' + this.game.players[1].name + '"]\n';
257 pgn += '[Fen "' + this.game.fenStart + '"]\n';
258 pgn += '[Result "' + this.game.score + '"]\n\n';
259 for (let i = 0; i < this.moves.length; i += 2) {
260 pgn += (i/2+1) + "." + getFullNotation(this.moves[i]) + " ";
261 if (i+1 < this.moves.length)
262 pgn += getFullNotation(this.moves[i+1]) + " ";
266 showEndgameMsg: function(message) {
267 this.endgameMessage = message;
268 let modalBox = document.getElementById("modalEog");
269 modalBox.checked = true;
271 modalBox.checked = false;
274 // Animate an elementary move
275 animateMove: function(move, callback) {
276 let startSquare = document.getElementById(getSquareId(move.start));
277 let endSquare = document.getElementById(getSquareId(move.end));
278 let rectStart = startSquare.getBoundingClientRect();
279 let rectEnd = endSquare.getBoundingClientRect();
281 x: rectEnd.x - rectStart.x,
282 y: rectEnd.y - rectStart.y
284 let movingPiece = document.querySelector(
285 "#" + getSquareId(move.start) + " > img.piece"
287 // For some unknown reasons Opera get "movingPiece == null" error
288 // TOOO: is it calling 'animate()' twice ? One extra time ?
289 if (!movingPiece) return;
290 // HACK for animation (with positive translate, image slides "under background")
291 // Possible improvement: just alter squares on the piece's way...
292 const squares = document.getElementsByClassName("board");
293 for (let i = 0; i < squares.length; i++) {
294 let square = squares.item(i);
295 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
297 movingPiece.style.transform =
298 "translate(" + translation.x + "px," + translation.y + "px)";
299 movingPiece.style.transitionDuration = "0.25s";
300 movingPiece.style.zIndex = "3000";
302 for (let i = 0; i < squares.length; i++)
303 squares.item(i).style.zIndex = "auto";
304 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
309 emitFenIfAnalyze: function() {
310 if (this.game.mode == "analyze") {
313 this.lastMove ? this.lastMove.fen : this.game.fenStart
317 // "light": if gotoMove() or gotoEnd()
318 // data: some custom data (addTime) to be re-emitted
319 play: function(move, received, light, data) {
320 const navigate = !move;
321 const playSubmove = (smove) => {
322 if (!navigate) smove.notation = this.vr.getNotation(smove);
324 this.lastMove = smove;
325 // Is opponent in check?
326 this.incheck = this.vr.getCheckSquares(this.vr.turn);
328 if (!this.inMultimove) {
329 if (this.cursor < this.moves.length - 1)
330 this.moves = this.moves.slice(0, this.cursor + 1);
331 this.moves.push(smove);
332 this.inMultimove = true; //potentially
335 // Already in the middle of a multi-move
336 const L = this.moves.length;
337 if (!Array.isArray(this.moves[L-1]))
338 this.$set(this.moves, L-1, [this.moves[L-1], smove]);
340 this.$set(this.moves, L-1, this.moves.concat([smove]));
344 const playMove = () => {
345 const animate = V.ShowMoves == "all" && (received || navigate);
346 if (!Array.isArray(move)) move = [move];
349 const initurn = this.vr.turn;
350 (function executeMove() {
351 const smove = move[moveIdx++];
353 self.animateMove(smove, () => {
355 if (moveIdx < move.length)
356 setTimeout(executeMove, 500);
357 else afterMove(smove, initurn);
361 if (moveIdx < move.length) executeMove();
362 else afterMove(smove, initurn);
366 const afterMove = (smove, initurn) => {
367 if (this.vr.turn != initurn) {
368 // Turn has changed: move is complete
370 // NOTE: only FEN of last sub-move is required (thus setting it here)
371 smove.fen = this.vr.getFen();
372 this.emitFenIfAnalyze();
374 this.inMultimove = false;
375 const score = this.vr.getCurrentScore();
377 const message = getScoreMessage(score);
378 if (!navigate && this.game.mode != "analyze")
379 this.$emit("gameover", score, message);
380 else if (this.game.mode == "analyze")
381 // Just show score on screen (allow undo)
382 this.showEndgameMsg(score + " . " + this.st.tr[message]);
384 if (!navigate && this.game.mode != "analyze") {
385 const L = this.moves.length;
386 // Post-processing (e.g. computer play)
387 this.$emit("newmove", this.moves[L-1], data);
391 // NOTE: navigate and received are mutually exclusive
393 // The move to navigate to is necessarily full:
394 if (this.cursor == this.moves.length - 1) return; //no more moves
395 move = this.moves[this.cursor + 1];
397 // Just play the move, nothing else:
398 if (!Array.isArray(move)) move = [move];
399 for (let i=0; i < move.length; i++) this.vr.play(move[i]);
403 this.emitFenIfAnalyze();
408 // Forbid playing outside analyze mode, except if move is received.
409 // Sufficient condition because Board already knows which turn it is.
411 this.game.mode != "analyze" &&
413 (this.game.score != "*" || this.cursor < this.moves.length - 1)
417 // To play a received move, cursor must be at the end of the game:
418 if (received && this.cursor < this.moves.length - 1)
422 cancelCurrentMultimove: function() {
423 const L = this.moves.length;
424 let move = this.moves[L-1];
425 if (!Array.isArray(move)) move = [move];
426 for (let i=move.length -1; i >= 0; i--) this.vr.undo(move[i]);
429 this.inMultimove = false;
431 cancelLastMove: function() {
432 // The last played move was canceled (corr game)
436 // "light": if gotoMove() or gotoBegin()
437 undo: function(move, light) {
438 if (this.inMultimove) {
439 this.cancelCurrentMultimove();
440 this.incheck = this.vr.getCheckSquares(this.vr.turn);
443 if (this.cursor < 0) return; //no more moves
444 move = this.moves[this.cursor];
446 // Caution; if multi-move, undo all submoves from last to first
447 undoMove(move, this.vr);
448 if (light) this.cursor--;
450 this.positionCursorTo(this.cursor - 1);
451 this.incheck = this.vr.getCheckSquares(this.vr.turn);
452 this.emitFenIfAnalyze();
456 gotoMove: function(index) {
457 if (this.inMultimove) this.cancelCurrentMultimove();
458 if (index == this.cursor) return;
459 if (index < this.cursor) {
460 while (this.cursor > index)
461 this.undo(null, null, "light");
464 // index > this.cursor)
465 while (this.cursor < index)
466 this.play(null, null, "light");
468 // NOTE: next line also re-assign cursor, but it's very light
469 this.positionCursorTo(index);
470 this.incheck = this.vr.getCheckSquares(this.vr.turn);
471 this.emitFenIfAnalyze();
473 gotoBegin: function() {
474 if (this.inMultimove) this.cancelCurrentMultimove();
475 while (this.cursor >= 0)
476 this.undo(null, null, "light");
477 if (this.moves.length > 0 && this.moves[0].notation == "...") {
479 this.lastMove = this.moves[0];
481 this.lastMove = null;
484 this.emitFenIfAnalyze();
486 gotoEnd: function() {
487 if (this.cursor == this.moves.length - 1) return;
488 this.gotoMove(this.moves.length - 1);
489 this.emitFenIfAnalyze();
492 this.orientation = V.GetOppCol(this.orientation);
498 <style lang="sass" scoped>
499 [type="checkbox"]#modalEog+div .card
512 display: inline-block
519 display: inline-block
525 @media screen and (max-width: 767px)
534 border-top: 1px solid #2f4f4f
542 border-left: 1px solid #2f4f4f
547 // TODO: later, maybe, allow movesList of variable width
548 // or e.g. between 250 and 350px (but more complicated)
554 @media screen and (max-width: 767px)