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"] }}
69 v-if="showMoves != 'none'"
72 :message="game.scoreMsg"
73 :firstNum="firstMoveNumber"
82 import Board from "@/components/Board.vue";
83 import MoveList from "@/components/MoveList.vue";
84 import { store } from "@/store";
85 import { getSquareId } from "@/utils/squareId";
86 import { getDate } from "@/utils/datetime";
87 import { processModalClick } from "@/utils/modalClick";
88 import { getScoreMessage } from "@/utils/scoring";
95 // "vr": VariantRules object, describing the game state + rules
96 props: ["vr", "game"],
100 // NOTE: all following variables must be reset at the beginning of a game
103 score: "*", //'*' means 'unfinished'
105 cursor: -1, //index of the move just played
107 firstMoveNumber: 0, //for printing
108 incheck: [] //for Board
112 // game initial FEN changes when a new game starts
113 "game.fenStart": function() {
114 this.re_setVariables();
118 showMoves: function() {
119 return this.game.score != "*"
121 : (this.vr ? this.vr.showMoves : "none");
123 showTurn: function() {
124 return this.game.score == '*' && this.vr && this.vr.showMoves != "all";
128 ? this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]
131 canAnalyze: function() {
132 return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze;
134 allowDownloadPGN: function() {
135 return this.game.score != "*" || (this.vr && this.vr.showMoves == "all");
138 created: function() {
139 if (this.game.fenStart) this.re_setVariables();
141 mounted: function() {
143 document.getElementById("eogDiv"),
144 document.getElementById("adjuster")
145 ].forEach(elt => elt.addEventListener("click", processModalClick));
146 // Take full width on small screens:
147 let boardSize = parseInt(localStorage.getItem("boardSize"));
150 window.innerWidth >= 768
151 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
154 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
155 document.getElementById("boardContainer").style.width = boardSize + "px";
156 let gameContainer = document.getElementById("gameContainer");
157 gameContainer.style.width = boardSize + movesWidth + "px";
158 document.getElementById("boardSize").value =
159 (boardSize * 100) / (window.innerWidth - movesWidth);
160 // timeout to avoid calling too many time the adjust method
161 let timeoutLaunched = false;
162 window.addEventListener("resize", () => {
163 if (!timeoutLaunched) {
164 timeoutLaunched = true;
167 timeoutLaunched = false;
173 focusBg: function() {
174 document.getElementById("baseGame").focus();
176 adjustBoard: function() {
177 const boardContainer = document.getElementById("boardContainer");
178 if (!boardContainer) return; //no board on page
179 const k = document.getElementById("boardSize").value;
180 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
181 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
182 // Value of 0 is board min size; 100 is window.width [- movesWidth]
185 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
186 localStorage.setItem("boardSize", boardSize);
187 boardContainer.style.width = boardSize + "px";
188 document.getElementById("gameContainer").style.width =
189 boardSize + movesWidth + "px";
191 handleKeys: function(e) {
192 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
211 handleScroll: function(e) {
212 // NOTE: since game.mode=="analyze" => no score, next condition is enough
213 if (this.game.score != "*") {
215 if (e.deltaY < 0) this.undo();
216 else if (e.deltaY > 0) this.play();
219 showRules: function() {
220 //this.$router.push("/variants/" + this.game.vname);
221 window.open("#/variants/" + this.game.vname, "_blank"); //better
223 re_setVariables: function() {
224 this.endgameMessage = "";
225 // "w": default orientation for observed games
226 this.orientation = this.game.mycolor || "w";
227 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
228 // Post-processing: decorate each move with color, notation and FEN
229 let vr_tmp = new V(this.game.fenStart);
230 const parsedFen = V.ParseFen(this.game.fenStart);
231 const firstMoveColor = parsedFen.turn;
232 this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2);
233 this.moves.forEach(move => {
234 move.color = vr_tmp.turn;
235 move.notation = vr_tmp.getNotation(move);
237 move.fen = vr_tmp.getFen();
239 if (firstMoveColor == "b") {
240 // 'end' is required for Board component to check lastMove for e.p.
244 end: { x: -1, y: -1 }
247 const L = this.moves.length;
249 this.lastMove = L > 0 ? this.moves[L - 1] : null;
250 this.incheck = this.vr.getCheckSquares(this.vr.turn);
252 analyzePosition: function() {
257 this.vr.getFen().replace(/ /g, "_");
258 // Open in same tab in live games (against cheating)
259 if (this.game.type == "live") this.$router.push(newUrl);
260 else window.open("#" + newUrl);
262 download: function() {
263 const content = this.getPgn();
264 // Prepare and trigger download link
265 let downloadAnchor = document.getElementById("download");
266 downloadAnchor.setAttribute("download", "game.pgn");
267 downloadAnchor.href =
268 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
269 downloadAnchor.click();
273 pgn += '[Site "vchess.club"]\n';
274 pgn += '[Variant "' + this.game.vname + '"]\n';
275 pgn += '[Date "' + getDate(new Date()) + '"]\n';
276 pgn += '[White "' + this.game.players[0].name + '"]\n';
277 pgn += '[Black "' + this.game.players[1].name + '"]\n';
278 pgn += '[Fen "' + this.game.fenStart + '"]\n';
279 pgn += '[Result "' + this.game.score + '"]\n\n';
282 while (i < this.moves.length) {
283 pgn += counter++ + ".";
284 for (let color of ["w", "b"]) {
286 while (i < this.moves.length && this.moves[i].color == color)
287 move += this.moves[i++].notation + ",";
288 move = move.slice(0, -1); //remove last comma
289 pgn += move + (i < this.moves.length ? " " : "");
294 showEndgameMsg: function(message) {
295 this.endgameMessage = message;
296 let modalBox = document.getElementById("modalEog");
297 modalBox.checked = true;
299 modalBox.checked = false;
302 animateMove: function(move, callback) {
303 let startSquare = document.getElementById(getSquareId(move.start));
304 let endSquare = document.getElementById(getSquareId(move.end));
305 let rectStart = startSquare.getBoundingClientRect();
306 let rectEnd = endSquare.getBoundingClientRect();
308 x: rectEnd.x - rectStart.x,
309 y: rectEnd.y - rectStart.y
311 let movingPiece = document.querySelector(
312 "#" + getSquareId(move.start) + " > img.piece"
314 // HACK for animation (with positive translate, image slides "under background")
315 // Possible improvement: just alter squares on the piece's way...
316 const squares = document.getElementsByClassName("board");
317 for (let i = 0; i < squares.length; i++) {
318 let square = squares.item(i);
319 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
321 movingPiece.style.transform =
322 "translate(" + translation.x + "px," + translation.y + "px)";
323 movingPiece.style.transitionDuration = "0.25s";
324 movingPiece.style.zIndex = "3000";
326 for (let i = 0; i < squares.length; i++)
327 squares.item(i).style.zIndex = "auto";
328 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
332 play: function(move, receive) {
333 // NOTE: navigate and receive are mutually exclusive
334 const navigate = !move;
335 // Forbid playing outside analyze mode, except if move is received.
336 // Sufficient condition because Board already knows which turn it is.
339 this.game.mode != "analyze" &&
341 (this.game.score != "*" || this.cursor < this.moves.length - 1)
345 const doPlayMove = () => {
346 // To play a move, cursor must be at the end of the game:
347 if (!!receive && this.cursor < this.moves.length - 1) this.gotoEnd();
349 if (this.cursor == this.moves.length - 1) return; //no more moves
350 move = this.moves[this.cursor + 1];
352 move.color = this.vr.turn;
353 move.notation = this.vr.getNotation(move);
357 this.lastMove = move;
358 if (this.st.settings.sound == 2)
359 new Audio("/sounds/move.mp3").play().catch(() => {});
361 move.fen = this.vr.getFen();
362 // Stack move on movesList at current cursor
363 if (this.cursor == this.moves.length) this.moves.push(move);
364 else this.moves = this.moves.slice(0, this.cursor).concat([move]);
366 // Is opponent in check?
367 this.incheck = this.vr.getCheckSquares(this.vr.turn);
368 const score = this.vr.getCurrentScore();
370 const message = getScoreMessage(score);
371 if (this.game.mode != "analyze")
372 this.$emit("gameover", score, message);
373 //just show score on screen (allow undo)
374 else this.showEndgameMsg(score + " . " + message);
376 if (!navigate && this.game.mode != "analyze")
377 this.$emit("newmove", move); //post-processing (e.g. computer play)
379 if (!!receive && V.ShowMoves == "all")
380 this.animateMove(move, doPlayMove);
383 undo: function(move) {
384 const navigate = !move;
386 if (this.cursor < 0) return; //no more moves
387 move = this.moves[this.cursor];
391 this.lastMove = this.cursor >= 0 ? this.moves[this.cursor] : undefined;
392 if (this.st.settings.sound == 2)
393 new Audio("/sounds/undo.mp3").play().catch(() => {});
394 this.incheck = this.vr.getCheckSquares(this.vr.turn);
395 if (!navigate) this.moves.pop();
397 gotoMove: function(index) {
398 this.vr.re_init(this.moves[index].fen);
400 this.lastMove = this.moves[index];
401 this.incheck = this.vr.getCheckSquares(this.vr.turn);
403 gotoBegin: function() {
404 if (this.cursor == -1) return;
405 this.vr.re_init(this.game.fenStart);
406 if (this.moves.length > 0 && this.moves[0].notation == "...") {
408 this.lastMove = this.moves[0];
411 this.lastMove = null;
413 this.incheck = this.vr.getCheckSquares(this.vr.turn);
415 gotoEnd: function() {
416 if (this.cursor == this.moves.length - 1) return;
417 this.gotoMove(this.moves.length - 1);
420 this.orientation = V.GetOppCol(this.orientation);
426 <style lang="sass" scoped>
427 [type="checkbox"]#modalEog+div .card
430 [type="checkbox"]#modalAdjust+div .card
443 display: inline-block
448 display: inline-block
457 border-top: 1px solid #2f4f4f
465 border-left: 1px solid #2f4f4f
470 // TODO: later, maybe, allow movesList of variable width
471 // or e.g. between 250 and 350px (but more complicated)
477 @media screen and (max-width: 767px)