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()"
37 :user-color="game.mycolor"
38 :orientation="orientation"
43 #turnIndicator(v-if="game.vname=='Dark' && game.score=='*'") {{ turn }}
45 button(@click="gotoBegin()") <<
46 button(@click="undo()") <
47 button(@click="flip()") ⇅
48 button(@click="play()") >
49 button(@click="gotoEnd()") >>
51 #downloadDiv(v-if="game.vname!='Dark' || game.score!='*'")
53 button(@click="download()") {{ st.tr["Download"] }} PGN
54 button(onClick="window.doClick('modalAdjust')") ⤢
56 v-if="game.vname!='Dark' && game.mode!='analyze'"
57 @click="analyzePosition()"
59 | {{ st.tr["Analyse"] }}
60 // NOTE: rather ugly hack to avoid showing twice "rules" link...
62 v-if="!$route.path.match('/variants/')"
65 | {{ st.tr["Rules"] }}
70 :message="game.scoreMsg"
71 :firstNum="firstMoveNumber"
80 import Board from "@/components/Board.vue";
81 import MoveList from "@/components/MoveList.vue";
82 import { store } from "@/store";
83 import { getSquareId } from "@/utils/squareId";
84 import { getDate } from "@/utils/datetime";
85 import { processModalClick } from "@/utils/modalClick";
86 import { getScoreMessage } from "@/utils/scoring";
93 // "vr": VariantRules object, describing the game state + rules
94 props: ["vr", "game"],
98 // NOTE: all following variables must be reset at the beginning of a game
101 score: "*", //'*' means 'unfinished'
103 cursor: -1, //index of the move just played
105 firstMoveNumber: 0, //for printing
106 incheck: [] //for Board
110 // game initial FEN changes when a new game starts
111 "game.fenStart": function() {
112 this.re_setVariables();
114 // Received a new move to play:
115 "game.moveToPlay": function(move) {
116 if (move) this.play(move, "receive");
118 // ...Or to undo (corr game, move not validated)
119 "game.moveToUndo": function(move) {
120 if (move) this.undo(move);
124 showMoves: function() {
125 return this.game.vname != "Dark" || this.game.score != "*";
128 return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"];
130 analyze: function() {
132 this.game.mode == "analyze" ||
133 // From Board viewpoint, a finished Dark game == analyze (TODO: unclear)
134 (this.game.vname == "Dark" && this.game.score != "*")
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 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
226 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
227 // Post-processing: decorate each move with color + current FEN:
228 // (to be able to jump to any position quickly)
229 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
230 this.firstMoveNumber = Math.floor(
231 V.ParseFen(this.game.fenStart).movesCount / 2
233 this.moves.forEach(move => {
234 // NOTE: this is doing manually what play() function below achieve,
235 // but in a lighter "fast-forward" way
236 move.color = vr_tmp.turn;
237 move.notation = vr_tmp.getNotation(move);
239 move.fen = vr_tmp.getFen();
242 (this.moves.length > 0 && this.moves[0].color == "b") ||
243 (this.moves.length == 0 && vr_tmp.turn == "b")
245 // 'end' is required for Board component to check lastMove for e.p.
249 end: { x: -1, y: -1 }
252 const L = this.moves.length;
254 this.lastMove = L > 0 ? this.moves[L - 1] : null;
255 this.incheck = this.vr.getCheckSquares(this.vr.turn);
257 analyzePosition: function() {
262 this.vr.getFen().replace(/ /g, "_");
263 // Open in same tab in live games (against cheating)
264 if (this.game.type == "live") this.$router.push(newUrl);
265 else window.open("#" + newUrl);
267 download: function() {
268 const content = this.getPgn();
269 // Prepare and trigger download link
270 let downloadAnchor = document.getElementById("download");
271 downloadAnchor.setAttribute("download", "game.pgn");
272 downloadAnchor.href =
273 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
274 downloadAnchor.click();
278 pgn += '[Site "vchess.club"]\n';
279 pgn += '[Variant "' + this.game.vname + '"]\n';
280 pgn += '[Date "' + getDate(new Date()) + '"]\n';
281 pgn += '[White "' + this.game.players[0].name + '"]\n';
282 pgn += '[Black "' + this.game.players[1].name + '"]\n';
283 pgn += '[Fen "' + this.game.fenStart + '"]\n';
284 pgn += '[Result "' + this.game.score + '"]\n\n';
287 while (i < this.moves.length) {
288 pgn += counter++ + ".";
289 for (let color of ["w", "b"]) {
291 while (i < this.moves.length && this.moves[i].color == color)
292 move += this.moves[i++].notation + ",";
293 move = move.slice(0, -1); //remove last comma
294 pgn += move + (i < this.moves.length ? " " : "");
299 showEndgameMsg: function(message) {
300 this.endgameMessage = message;
301 let modalBox = document.getElementById("modalEog");
302 modalBox.checked = true;
304 modalBox.checked = false;
307 animateMove: function(move, callback) {
308 let startSquare = document.getElementById(getSquareId(move.start));
309 let endSquare = document.getElementById(getSquareId(move.end));
310 let rectStart = startSquare.getBoundingClientRect();
311 let rectEnd = endSquare.getBoundingClientRect();
313 x: rectEnd.x - rectStart.x,
314 y: rectEnd.y - rectStart.y
316 let movingPiece = document.querySelector(
317 "#" + getSquareId(move.start) + " > img.piece"
319 // HACK for animation (with positive translate, image slides "under background")
320 // Possible improvement: just alter squares on the piece's way...
321 const squares = document.getElementsByClassName("board");
322 for (let i = 0; i < squares.length; i++) {
323 let square = squares.item(i);
324 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
326 movingPiece.style.transform =
327 "translate(" + translation.x + "px," + translation.y + "px)";
328 movingPiece.style.transitionDuration = "0.25s";
329 movingPiece.style.zIndex = "3000";
331 for (let i = 0; i < squares.length; i++)
332 squares.item(i).style.zIndex = "auto";
333 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
337 play: function(move, receive) {
338 // NOTE: navigate and receive are mutually exclusive
339 const navigate = !move;
340 // Forbid playing outside analyze mode, except if move is received.
341 // Sufficient condition because Board already knows which turn it is.
344 this.game.mode != "analyze" &&
346 (this.game.score != "*" || this.cursor < this.moves.length - 1)
350 const doPlayMove = () => {
351 // To play a move, cursor must be at the end of the game:
352 if (!!receive && this.cursor < this.moves.length - 1) this.gotoEnd();
354 if (this.cursor == this.moves.length - 1) return; //no more moves
355 move = this.moves[this.cursor + 1];
357 move.color = this.vr.turn;
358 move.notation = this.vr.getNotation(move);
362 this.lastMove = move;
363 if (this.st.settings.sound == 2)
364 new Audio("/sounds/move.mp3").play().catch(() => {});
366 move.fen = this.vr.getFen();
367 // Stack move on movesList at current cursor
368 if (this.cursor == this.moves.length) this.moves.push(move);
369 else this.moves = this.moves.slice(0, this.cursor).concat([move]);
371 // Is opponent in check?
372 this.incheck = this.vr.getCheckSquares(this.vr.turn);
373 const score = this.vr.getCurrentScore();
375 const message = getScoreMessage(score);
376 if (this.game.mode != "analyze")
377 this.$emit("gameover", score, message);
378 //just show score on screen (allow undo)
379 else this.showEndgameMsg(score + " . " + message);
381 if (!navigate && this.game.mode != "analyze")
382 this.$emit("newmove", move); //post-processing (e.g. computer play)
384 if (!!receive && this.game.vname != "Dark")
385 this.animateMove(move, doPlayMove);
388 undo: function(move) {
389 const navigate = !move;
391 if (this.cursor < 0) return; //no more moves
392 move = this.moves[this.cursor];
396 this.lastMove = this.cursor >= 0 ? this.moves[this.cursor] : undefined;
397 if (this.st.settings.sound == 2)
398 new Audio("/sounds/undo.mp3").play().catch(() => {});
399 this.incheck = this.vr.getCheckSquares(this.vr.turn);
400 if (!navigate) this.moves.pop();
402 gotoMove: function(index) {
403 this.vr.re_init(this.moves[index].fen);
405 this.lastMove = this.moves[index];
407 gotoBegin: function() {
408 if (this.cursor == -1) return;
409 this.vr.re_init(this.game.fenStart);
410 if (this.moves.length > 0 && this.moves[0].notation == "...") {
412 this.lastMove = this.moves[0];
415 this.lastMove = null;
418 gotoEnd: function() {
419 if (this.cursor == this.moves.length - 1) return;
420 this.gotoMove(this.moves.length - 1);
423 this.orientation = V.GetOppCol(this.orientation);
429 <style lang="sass" scoped>
430 [type="checkbox"]#modalEog+div .card
433 [type="checkbox"]#modalAdjust+div .card
446 display: inline-block
451 display: inline-block
460 border-top: 1px solid #2f4f4f
468 border-left: 1px solid #2f4f4f
473 // TODO: later, maybe, allow movesList of variable width
474 // or e.g. between 250 and 350px (but more complicated)
480 @media screen and (max-width: 767px)