5 @keydown="handleKeys($event)"
6 @wheel="handleScroll($event)"
8 input#modalEog.modal(type="checkbox")
11 data-checkbox="modalEog"
14 label.modal-close(for="modalEog")
15 h3.section {{ endgameMessage }}
16 input#modalAdjust.modal(type="checkbox")
19 data-checkbox="modalAdjust"
22 label.modal-close(for="modalAdjust")
23 label(for="boardSize") {{ st.tr["Board size"] }}
24 input#boardSize.slider(
29 @input="adjustBoard()"
36 :analyze="game.mode=='analyze'"
38 :user-color="game.mycolor"
39 :orientation="orientation"
44 #turnIndicator(v-if="showTurn") {{ turn }}
46 button(@click="gotoBegin()") <<
47 button(@click="undo()") <
48 button(@click="flip()") ⇅
49 button(@click="play()") >
50 button(@click="gotoEnd()") >>
52 #downloadDiv(v-if="allowDownloadPGN")
54 button(@click="download()") {{ st.tr["Download"] }} PGN
55 button(onClick="window.doClick('modalAdjust')") ⤢
58 @click="analyzePosition()"
60 | {{ st.tr["Analyse"] }}
61 // NOTE: variants pages already have a "Rules" link on top
63 v-if="!$route.path.match('/variants/')"
66 | {{ st.tr["Rules"] }}
71 :message="game.scoreMsg"
72 :firstNum="firstMoveNumber"
81 import Board from "@/components/Board.vue";
82 import MoveList from "@/components/MoveList.vue";
83 import { store } from "@/store";
84 import { getSquareId } from "@/utils/squareId";
85 import { getDate } from "@/utils/datetime";
86 import { processModalClick } from "@/utils/modalClick";
87 import { getScoreMessage } from "@/utils/scoring";
94 // "vr": VariantRules object, describing the game state + rules
95 props: ["vr", "game"],
99 // NOTE: all following variables must be reset at the beginning of a game
102 score: "*", //'*' means 'unfinished'
104 cursor: -1, //index of the move just played
106 firstMoveNumber: 0, //for printing
107 incheck: [] //for Board
111 // game initial FEN changes when a new game starts
112 "game.fenStart": function() {
113 this.re_setVariables();
117 showMoves: function() {
118 return this.game.score != "*" || (this.vr && this.vr.ShowMoves == "all");
120 showTurn: function() {
121 return this.game.score == '*' && this.vr && this.vr.ShowMoves != "all";
125 ? this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]
128 canAnalyze: function() {
129 return this.game.mode != "analyze" && this.vr && this.vr.CanAnalyze;
131 allowDownloadPGN: function() {
132 return this.game.score != "*" || (this.vr && this.vr.ShowMoves == "all");
135 created: function() {
136 if (this.game.fenStart) this.re_setVariables();
138 mounted: function() {
140 document.getElementById("eogDiv"),
141 document.getElementById("adjuster")
142 ].forEach(elt => elt.addEventListener("click", processModalClick));
143 // Take full width on small screens:
144 let boardSize = parseInt(localStorage.getItem("boardSize"));
147 window.innerWidth >= 768
148 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
151 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
152 document.getElementById("boardContainer").style.width = boardSize + "px";
153 let gameContainer = document.getElementById("gameContainer");
154 gameContainer.style.width = boardSize + movesWidth + "px";
155 document.getElementById("boardSize").value =
156 (boardSize * 100) / (window.innerWidth - movesWidth);
157 // timeout to avoid calling too many time the adjust method
158 let timeoutLaunched = false;
159 window.addEventListener("resize", () => {
160 if (!timeoutLaunched) {
161 timeoutLaunched = true;
164 timeoutLaunched = false;
170 focusBg: function() {
171 document.getElementById("baseGame").focus();
173 adjustBoard: function() {
174 const boardContainer = document.getElementById("boardContainer");
175 if (!boardContainer) return; //no board on page
176 const k = document.getElementById("boardSize").value;
177 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
178 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
179 // Value of 0 is board min size; 100 is window.width [- movesWidth]
182 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
183 localStorage.setItem("boardSize", boardSize);
184 boardContainer.style.width = boardSize + "px";
185 document.getElementById("gameContainer").style.width =
186 boardSize + movesWidth + "px";
188 handleKeys: function(e) {
189 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
208 handleScroll: function(e) {
209 // NOTE: since game.mode=="analyze" => no score, next condition is enough
210 if (this.game.score != "*") {
212 if (e.deltaY < 0) this.undo();
213 else if (e.deltaY > 0) this.play();
216 showRules: function() {
217 //this.$router.push("/variants/" + this.game.vname);
218 window.open("#/variants/" + this.game.vname, "_blank"); //better
220 re_setVariables: function() {
221 this.endgameMessage = "";
222 // "w": default orientation for observed games
223 this.orientation = this.game.mycolor || "w";
224 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
225 // Post-processing: decorate each move with color, notation and FEN
226 let vr_tmp = new V(this.game.fenStart);
227 const parsedFen = V.ParseFen(this.game.fenStart);
228 const firstMoveColor = parsedFen.turn;
229 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
230 this.moves.forEach(move => {
231 move.color = vr_tmp.turn;
232 move.notation = vr_tmp.getNotation(move);
234 move.fen = vr_tmp.getFen();
236 if (firstMoveColor == "b") {
237 // 'end' is required for Board component to check lastMove for e.p.
241 end: { x: -1, y: -1 }
244 const L = this.moves.length;
246 this.lastMove = L > 0 ? this.moves[L - 1] : null;
247 this.incheck = this.vr.getCheckSquares(this.vr.turn);
249 analyzePosition: function() {
254 this.vr.getFen().replace(/ /g, "_");
255 // Open in same tab in live games (against cheating)
256 if (this.game.type == "live") this.$router.push(newUrl);
257 else window.open("#" + newUrl);
259 download: function() {
260 const content = this.getPgn();
261 // Prepare and trigger download link
262 let downloadAnchor = document.getElementById("download");
263 downloadAnchor.setAttribute("download", "game.pgn");
264 downloadAnchor.href =
265 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
266 downloadAnchor.click();
270 pgn += '[Site "vchess.club"]\n';
271 pgn += '[Variant "' + this.game.vname + '"]\n';
272 pgn += '[Date "' + getDate(new Date()) + '"]\n';
273 pgn += '[White "' + this.game.players[0].name + '"]\n';
274 pgn += '[Black "' + this.game.players[1].name + '"]\n';
275 pgn += '[Fen "' + this.game.fenStart + '"]\n';
276 pgn += '[Result "' + this.game.score + '"]\n\n';
279 while (i < this.moves.length) {
280 pgn += counter++ + ".";
281 for (let color of ["w", "b"]) {
283 while (i < this.moves.length && this.moves[i].color == color)
284 move += this.moves[i++].notation + ",";
285 move = move.slice(0, -1); //remove last comma
286 pgn += move + (i < this.moves.length ? " " : "");
291 showEndgameMsg: function(message) {
292 this.endgameMessage = message;
293 let modalBox = document.getElementById("modalEog");
294 modalBox.checked = true;
296 modalBox.checked = false;
299 animateMove: function(move, callback) {
300 let startSquare = document.getElementById(getSquareId(move.start));
301 let endSquare = document.getElementById(getSquareId(move.end));
302 let rectStart = startSquare.getBoundingClientRect();
303 let rectEnd = endSquare.getBoundingClientRect();
305 x: rectEnd.x - rectStart.x,
306 y: rectEnd.y - rectStart.y
308 let movingPiece = document.querySelector(
309 "#" + getSquareId(move.start) + " > img.piece"
311 // HACK for animation (with positive translate, image slides "under background")
312 // Possible improvement: just alter squares on the piece's way...
313 const squares = document.getElementsByClassName("board");
314 for (let i = 0; i < squares.length; i++) {
315 let square = squares.item(i);
316 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
318 movingPiece.style.transform =
319 "translate(" + translation.x + "px," + translation.y + "px)";
320 movingPiece.style.transitionDuration = "0.25s";
321 movingPiece.style.zIndex = "3000";
323 for (let i = 0; i < squares.length; i++)
324 squares.item(i).style.zIndex = "auto";
325 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
329 play: function(move, receive) {
330 // NOTE: navigate and receive are mutually exclusive
331 const navigate = !move;
332 // Forbid playing outside analyze mode, except if move is received.
333 // Sufficient condition because Board already knows which turn it is.
336 this.game.mode != "analyze" &&
338 (this.game.score != "*" || this.cursor < this.moves.length - 1)
342 const doPlayMove = () => {
343 // To play a move, cursor must be at the end of the game:
344 if (!!receive && this.cursor < this.moves.length - 1) this.gotoEnd();
346 if (this.cursor == this.moves.length - 1) return; //no more moves
347 move = this.moves[this.cursor + 1];
349 move.color = this.vr.turn;
350 move.notation = this.vr.getNotation(move);
354 this.lastMove = move;
355 if (this.st.settings.sound == 2)
356 new Audio("/sounds/move.mp3").play().catch(() => {});
358 move.fen = this.vr.getFen();
359 // Stack move on movesList at current cursor
360 if (this.cursor == this.moves.length) this.moves.push(move);
361 else this.moves = this.moves.slice(0, this.cursor).concat([move]);
363 // Is opponent in check?
364 this.incheck = this.vr.getCheckSquares(this.vr.turn);
365 const score = this.vr.getCurrentScore();
367 const message = getScoreMessage(score);
368 if (this.game.mode != "analyze")
369 this.$emit("gameover", score, message);
370 //just show score on screen (allow undo)
371 else this.showEndgameMsg(score + " . " + message);
373 if (!navigate && this.game.mode != "analyze")
374 this.$emit("newmove", move); //post-processing (e.g. computer play)
376 if (!!receive && V.ShowMoves == "all")
377 this.animateMove(move, doPlayMove);
380 undo: function(move) {
381 const navigate = !move;
383 if (this.cursor < 0) return; //no more moves
384 move = this.moves[this.cursor];
388 this.lastMove = this.cursor >= 0 ? this.moves[this.cursor] : undefined;
389 if (this.st.settings.sound == 2)
390 new Audio("/sounds/undo.mp3").play().catch(() => {});
391 this.incheck = this.vr.getCheckSquares(this.vr.turn);
392 if (!navigate) this.moves.pop();
394 gotoMove: function(index) {
395 this.vr.re_init(this.moves[index].fen);
397 this.lastMove = this.moves[index];
398 this.incheck = this.vr.getCheckSquares(this.vr.turn);
400 gotoBegin: function() {
401 if (this.cursor == -1) return;
402 this.vr.re_init(this.game.fenStart);
403 if (this.moves.length > 0 && this.moves[0].notation == "...") {
405 this.lastMove = this.moves[0];
408 this.lastMove = null;
410 this.incheck = this.vr.getCheckSquares(this.vr.turn);
412 gotoEnd: function() {
413 if (this.cursor == this.moves.length - 1) return;
414 this.gotoMove(this.moves.length - 1);
417 this.orientation = V.GetOppCol(this.orientation);
423 <style lang="sass" scoped>
424 [type="checkbox"]#modalEog+div .card
427 [type="checkbox"]#modalAdjust+div .card
440 display: inline-block
445 display: inline-block
454 border-top: 1px solid #2f4f4f
462 border-left: 1px solid #2f4f4f
467 // TODO: later, maybe, allow movesList of variable width
468 // or e.g. between 250 and 350px (but more complicated)
474 @media screen and (max-width: 767px)