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 != "*" || (window.V && V.ShowMoves == "all");
120 showTurn: function() {
121 return this.game.score == '*' && window.V && V.ShowMoves != "all";
124 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"];
126 canAnalyze: function() {
127 return this.game.mode != "analyze" && window.V && V.CanAnalyze;
129 allowDownloadPGN: function() {
130 return this.game.score != "*" || (window.V && V.ShowMoves == "all");
133 created: function() {
134 if (this.game.fenStart) this.re_setVariables();
136 mounted: function() {
138 document.getElementById("eogDiv"),
139 document.getElementById("adjuster")
140 ].forEach(elt => elt.addEventListener("click", processModalClick));
141 // Take full width on small screens:
142 let boardSize = parseInt(localStorage.getItem("boardSize"));
145 window.innerWidth >= 768
146 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
149 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
150 document.getElementById("boardContainer").style.width = boardSize + "px";
151 let gameContainer = document.getElementById("gameContainer");
152 gameContainer.style.width = boardSize + movesWidth + "px";
153 document.getElementById("boardSize").value =
154 (boardSize * 100) / (window.innerWidth - movesWidth);
155 // timeout to avoid calling too many time the adjust method
156 let timeoutLaunched = false;
157 window.addEventListener("resize", () => {
158 if (!timeoutLaunched) {
159 timeoutLaunched = true;
162 timeoutLaunched = false;
168 focusBg: function() {
169 document.getElementById("baseGame").focus();
171 adjustBoard: function() {
172 const boardContainer = document.getElementById("boardContainer");
173 if (!boardContainer) return; //no board on page
174 const k = document.getElementById("boardSize").value;
175 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
176 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
177 // Value of 0 is board min size; 100 is window.width [- movesWidth]
180 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
181 localStorage.setItem("boardSize", boardSize);
182 boardContainer.style.width = boardSize + "px";
183 document.getElementById("gameContainer").style.width =
184 boardSize + movesWidth + "px";
186 handleKeys: function(e) {
187 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
206 handleScroll: function(e) {
207 // NOTE: since game.mode=="analyze" => no score, next condition is enough
208 if (this.game.score != "*") {
210 if (e.deltaY < 0) this.undo();
211 else if (e.deltaY > 0) this.play();
214 showRules: function() {
215 //this.$router.push("/variants/" + this.game.vname);
216 window.open("#/variants/" + this.game.vname, "_blank"); //better
218 re_setVariables: function() {
219 this.endgameMessage = "";
220 // "w": default orientation for observed games
221 this.orientation = this.game.mycolor || "w";
222 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
223 // Post-processing: decorate each move with color, notation and FEN
224 let vr_tmp = new V(this.game.fenStart);
225 const parsedFen = V.ParseFen(this.game.fenStart);
226 const firstMoveColor = parsedFen.turn;
227 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
228 this.moves.forEach(move => {
229 move.color = vr_tmp.turn;
230 move.notation = vr_tmp.getNotation(move);
232 move.fen = vr_tmp.getFen();
234 if (firstMoveColor == "b") {
235 // 'end' is required for Board component to check lastMove for e.p.
239 end: { x: -1, y: -1 }
242 const L = this.moves.length;
244 this.lastMove = L > 0 ? this.moves[L - 1] : null;
245 this.incheck = this.vr.getCheckSquares(this.vr.turn);
247 analyzePosition: function() {
252 this.vr.getFen().replace(/ /g, "_");
253 // Open in same tab in live games (against cheating)
254 if (this.game.type == "live") this.$router.push(newUrl);
255 else window.open("#" + newUrl);
257 download: function() {
258 const content = this.getPgn();
259 // Prepare and trigger download link
260 let downloadAnchor = document.getElementById("download");
261 downloadAnchor.setAttribute("download", "game.pgn");
262 downloadAnchor.href =
263 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
264 downloadAnchor.click();
268 pgn += '[Site "vchess.club"]\n';
269 pgn += '[Variant "' + this.game.vname + '"]\n';
270 pgn += '[Date "' + getDate(new Date()) + '"]\n';
271 pgn += '[White "' + this.game.players[0].name + '"]\n';
272 pgn += '[Black "' + this.game.players[1].name + '"]\n';
273 pgn += '[Fen "' + this.game.fenStart + '"]\n';
274 pgn += '[Result "' + this.game.score + '"]\n\n';
277 while (i < this.moves.length) {
278 pgn += counter++ + ".";
279 for (let color of ["w", "b"]) {
281 while (i < this.moves.length && this.moves[i].color == color)
282 move += this.moves[i++].notation + ",";
283 move = move.slice(0, -1); //remove last comma
284 pgn += move + (i < this.moves.length ? " " : "");
289 showEndgameMsg: function(message) {
290 this.endgameMessage = message;
291 let modalBox = document.getElementById("modalEog");
292 modalBox.checked = true;
294 modalBox.checked = false;
297 animateMove: function(move, callback) {
298 let startSquare = document.getElementById(getSquareId(move.start));
299 let endSquare = document.getElementById(getSquareId(move.end));
300 let rectStart = startSquare.getBoundingClientRect();
301 let rectEnd = endSquare.getBoundingClientRect();
303 x: rectEnd.x - rectStart.x,
304 y: rectEnd.y - rectStart.y
306 let movingPiece = document.querySelector(
307 "#" + getSquareId(move.start) + " > img.piece"
309 // HACK for animation (with positive translate, image slides "under background")
310 // Possible improvement: just alter squares on the piece's way...
311 const squares = document.getElementsByClassName("board");
312 for (let i = 0; i < squares.length; i++) {
313 let square = squares.item(i);
314 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
316 movingPiece.style.transform =
317 "translate(" + translation.x + "px," + translation.y + "px)";
318 movingPiece.style.transitionDuration = "0.25s";
319 movingPiece.style.zIndex = "3000";
321 for (let i = 0; i < squares.length; i++)
322 squares.item(i).style.zIndex = "auto";
323 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
327 play: function(move, receive) {
328 // NOTE: navigate and receive are mutually exclusive
329 const navigate = !move;
330 // Forbid playing outside analyze mode, except if move is received.
331 // Sufficient condition because Board already knows which turn it is.
334 this.game.mode != "analyze" &&
336 (this.game.score != "*" || this.cursor < this.moves.length - 1)
340 const doPlayMove = () => {
341 // To play a move, cursor must be at the end of the game:
342 if (!!receive && this.cursor < this.moves.length - 1) this.gotoEnd();
344 if (this.cursor == this.moves.length - 1) return; //no more moves
345 move = this.moves[this.cursor + 1];
347 move.color = this.vr.turn;
348 move.notation = this.vr.getNotation(move);
352 this.lastMove = move;
353 if (this.st.settings.sound == 2)
354 new Audio("/sounds/move.mp3").play().catch(() => {});
356 move.fen = this.vr.getFen();
357 // Stack move on movesList at current cursor
358 if (this.cursor == this.moves.length) this.moves.push(move);
359 else this.moves = this.moves.slice(0, this.cursor).concat([move]);
361 // Is opponent in check?
362 this.incheck = this.vr.getCheckSquares(this.vr.turn);
363 const score = this.vr.getCurrentScore();
365 const message = getScoreMessage(score);
366 if (this.game.mode != "analyze")
367 this.$emit("gameover", score, message);
368 //just show score on screen (allow undo)
369 else this.showEndgameMsg(score + " . " + message);
371 if (!navigate && this.game.mode != "analyze")
372 this.$emit("newmove", move); //post-processing (e.g. computer play)
374 if (!!receive && V.ShowMoves == "all")
375 this.animateMove(move, doPlayMove);
378 undo: function(move) {
379 const navigate = !move;
381 if (this.cursor < 0) return; //no more moves
382 move = this.moves[this.cursor];
386 this.lastMove = this.cursor >= 0 ? this.moves[this.cursor] : undefined;
387 if (this.st.settings.sound == 2)
388 new Audio("/sounds/undo.mp3").play().catch(() => {});
389 this.incheck = this.vr.getCheckSquares(this.vr.turn);
390 if (!navigate) this.moves.pop();
392 gotoMove: function(index) {
393 this.vr.re_init(this.moves[index].fen);
395 this.lastMove = this.moves[index];
397 gotoBegin: function() {
398 if (this.cursor == -1) return;
399 this.vr.re_init(this.game.fenStart);
400 if (this.moves.length > 0 && this.moves[0].notation == "...") {
402 this.lastMove = this.moves[0];
405 this.lastMove = null;
408 gotoEnd: function() {
409 if (this.cursor == this.moves.length - 1) return;
410 this.gotoMove(this.moves.length - 1);
413 this.orientation = V.GetOppCol(this.orientation);
419 <style lang="sass" scoped>
420 [type="checkbox"]#modalEog+div .card
423 [type="checkbox"]#modalAdjust+div .card
436 display: inline-block
441 display: inline-block
450 border-top: 1px solid #2f4f4f
458 border-left: 1px solid #2f4f4f
463 // TODO: later, maybe, allow movesList of variable width
464 // or e.g. between 250 and 350px (but more complicated)
470 @media screen and (max-width: 767px)