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();
115 // Received a new move to play:
116 "game.moveToPlay": function(move) {
117 if (move) this.play(move, "receive");
119 // ...Or to undo (corr game, move not validated)
120 "game.moveToUndo": function(move) {
121 if (move) this.undo(move);
125 showMoves: function() {
126 return this.game.score != "*" || (window.V && V.ShowMoves == "all");
128 showTurn: function() {
129 return this.game.score == '*' && window.V && V.ShowMoves != "all";
132 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"];
134 canAnalyze: function() {
135 return this.game.mode != "analyze" && window.V && V.CanAnalyze;
137 allowDownloadPGN: function() {
138 return this.game.score != "*" || (window.V && V.ShowMoves == "all");
141 created: function() {
142 if (this.game.fenStart) this.re_setVariables();
144 mounted: function() {
146 document.getElementById("eogDiv"),
147 document.getElementById("adjuster")
148 ].forEach(elt => elt.addEventListener("click", processModalClick));
149 // Take full width on small screens:
150 let boardSize = parseInt(localStorage.getItem("boardSize"));
153 window.innerWidth >= 768
154 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
157 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
158 document.getElementById("boardContainer").style.width = boardSize + "px";
159 let gameContainer = document.getElementById("gameContainer");
160 gameContainer.style.width = boardSize + movesWidth + "px";
161 document.getElementById("boardSize").value =
162 (boardSize * 100) / (window.innerWidth - movesWidth);
163 // timeout to avoid calling too many time the adjust method
164 let timeoutLaunched = false;
165 window.addEventListener("resize", () => {
166 if (!timeoutLaunched) {
167 timeoutLaunched = true;
170 timeoutLaunched = false;
176 focusBg: function() {
177 document.getElementById("baseGame").focus();
179 adjustBoard: function() {
180 const boardContainer = document.getElementById("boardContainer");
181 if (!boardContainer) return; //no board on page
182 const k = document.getElementById("boardSize").value;
183 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
184 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
185 // Value of 0 is board min size; 100 is window.width [- movesWidth]
188 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
189 localStorage.setItem("boardSize", boardSize);
190 boardContainer.style.width = boardSize + "px";
191 document.getElementById("gameContainer").style.width =
192 boardSize + movesWidth + "px";
194 handleKeys: function(e) {
195 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
214 handleScroll: function(e) {
215 // NOTE: since game.mode=="analyze" => no score, next condition is enough
216 if (this.game.score != "*") {
218 if (e.deltaY < 0) this.undo();
219 else if (e.deltaY > 0) this.play();
222 showRules: function() {
223 //this.$router.push("/variants/" + this.game.vname);
224 window.open("#/variants/" + this.game.vname, "_blank"); //better
226 re_setVariables: function() {
227 this.endgameMessage = "";
228 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
229 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
230 // Post-processing: decorate each move with color + current FEN:
231 // (to be able to jump to any position quickly)
232 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
233 this.firstMoveNumber = Math.floor(
234 V.ParseFen(this.game.fenStart).movesCount / 2
236 this.moves.forEach(move => {
237 // NOTE: this is doing manually what play() function below achieve,
238 // but in a lighter "fast-forward" way
239 move.color = vr_tmp.turn;
240 move.notation = vr_tmp.getNotation(move);
242 move.fen = vr_tmp.getFen();
245 (this.moves.length > 0 && this.moves[0].color == "b") ||
246 (this.moves.length == 0 && vr_tmp.turn == "b")
248 // 'end' is required for Board component to check lastMove for e.p.
252 end: { x: -1, y: -1 }
255 const L = this.moves.length;
257 this.lastMove = L > 0 ? this.moves[L - 1] : null;
258 this.incheck = this.vr.getCheckSquares(this.vr.turn);
260 analyzePosition: function() {
265 this.vr.getFen().replace(/ /g, "_");
266 // Open in same tab in live games (against cheating)
267 if (this.game.type == "live") this.$router.push(newUrl);
268 else window.open("#" + newUrl);
270 download: function() {
271 const content = this.getPgn();
272 // Prepare and trigger download link
273 let downloadAnchor = document.getElementById("download");
274 downloadAnchor.setAttribute("download", "game.pgn");
275 downloadAnchor.href =
276 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
277 downloadAnchor.click();
281 pgn += '[Site "vchess.club"]\n';
282 pgn += '[Variant "' + this.game.vname + '"]\n';
283 pgn += '[Date "' + getDate(new Date()) + '"]\n';
284 pgn += '[White "' + this.game.players[0].name + '"]\n';
285 pgn += '[Black "' + this.game.players[1].name + '"]\n';
286 pgn += '[Fen "' + this.game.fenStart + '"]\n';
287 pgn += '[Result "' + this.game.score + '"]\n\n';
290 while (i < this.moves.length) {
291 pgn += counter++ + ".";
292 for (let color of ["w", "b"]) {
294 while (i < this.moves.length && this.moves[i].color == color)
295 move += this.moves[i++].notation + ",";
296 move = move.slice(0, -1); //remove last comma
297 pgn += move + (i < this.moves.length ? " " : "");
302 showEndgameMsg: function(message) {
303 this.endgameMessage = message;
304 let modalBox = document.getElementById("modalEog");
305 modalBox.checked = true;
307 modalBox.checked = false;
310 animateMove: function(move, callback) {
311 let startSquare = document.getElementById(getSquareId(move.start));
312 let endSquare = document.getElementById(getSquareId(move.end));
313 let rectStart = startSquare.getBoundingClientRect();
314 let rectEnd = endSquare.getBoundingClientRect();
316 x: rectEnd.x - rectStart.x,
317 y: rectEnd.y - rectStart.y
319 let movingPiece = document.querySelector(
320 "#" + getSquareId(move.start) + " > img.piece"
322 // HACK for animation (with positive translate, image slides "under background")
323 // Possible improvement: just alter squares on the piece's way...
324 const squares = document.getElementsByClassName("board");
325 for (let i = 0; i < squares.length; i++) {
326 let square = squares.item(i);
327 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
329 movingPiece.style.transform =
330 "translate(" + translation.x + "px," + translation.y + "px)";
331 movingPiece.style.transitionDuration = "0.25s";
332 movingPiece.style.zIndex = "3000";
334 for (let i = 0; i < squares.length; i++)
335 squares.item(i).style.zIndex = "auto";
336 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
340 play: function(move, receive) {
341 // NOTE: navigate and receive are mutually exclusive
342 const navigate = !move;
343 // Forbid playing outside analyze mode, except if move is received.
344 // Sufficient condition because Board already knows which turn it is.
347 this.game.mode != "analyze" &&
349 (this.game.score != "*" || this.cursor < this.moves.length - 1)
353 const doPlayMove = () => {
354 // To play a move, cursor must be at the end of the game:
355 if (!!receive && this.cursor < this.moves.length - 1) this.gotoEnd();
357 if (this.cursor == this.moves.length - 1) return; //no more moves
358 move = this.moves[this.cursor + 1];
360 move.color = this.vr.turn;
361 move.notation = this.vr.getNotation(move);
365 this.lastMove = move;
366 if (this.st.settings.sound == 2)
367 new Audio("/sounds/move.mp3").play().catch(() => {});
369 move.fen = this.vr.getFen();
370 // Stack move on movesList at current cursor
371 if (this.cursor == this.moves.length) this.moves.push(move);
372 else this.moves = this.moves.slice(0, this.cursor).concat([move]);
374 // Is opponent in check?
375 this.incheck = this.vr.getCheckSquares(this.vr.turn);
376 const score = this.vr.getCurrentScore();
378 const message = getScoreMessage(score);
379 if (this.game.mode != "analyze")
380 this.$emit("gameover", score, message);
381 //just show score on screen (allow undo)
382 else this.showEndgameMsg(score + " . " + message);
384 if (!navigate && this.game.mode != "analyze")
385 this.$emit("newmove", move); //post-processing (e.g. computer play)
387 if (!!receive && V.ShowMoves == "all")
388 this.animateMove(move, doPlayMove);
391 undo: function(move) {
392 const navigate = !move;
394 if (this.cursor < 0) return; //no more moves
395 move = this.moves[this.cursor];
399 this.lastMove = this.cursor >= 0 ? this.moves[this.cursor] : undefined;
400 if (this.st.settings.sound == 2)
401 new Audio("/sounds/undo.mp3").play().catch(() => {});
402 this.incheck = this.vr.getCheckSquares(this.vr.turn);
403 if (!navigate) this.moves.pop();
405 gotoMove: function(index) {
406 this.vr.re_init(this.moves[index].fen);
408 this.lastMove = this.moves[index];
410 gotoBegin: function() {
411 if (this.cursor == -1) return;
412 this.vr.re_init(this.game.fenStart);
413 if (this.moves.length > 0 && this.moves[0].notation == "...") {
415 this.lastMove = this.moves[0];
418 this.lastMove = null;
421 gotoEnd: function() {
422 if (this.cursor == this.moves.length - 1) return;
423 this.gotoMove(this.moves.length - 1);
426 this.orientation = V.GetOppCol(this.orientation);
432 <style lang="sass" scoped>
433 [type="checkbox"]#modalEog+div .card
436 [type="checkbox"]#modalAdjust+div .card
449 display: inline-block
454 display: inline-block
463 border-top: 1px solid #2f4f4f
471 border-left: 1px solid #2f4f4f
476 // TODO: later, maybe, allow movesList of variable width
477 // or e.g. between 250 and 350px (but more complicated)
483 @media screen and (max-width: 767px)