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=='*'")
44 | {{ st.tr[vr.turn + " to move"] }}
46 button(@click="gotoBegin()") <<
47 button(@click="undo()") <
48 button(@click="flip()") ⇅
49 button(@click="play()") >
50 button(@click="gotoEnd()") >>
52 #downloadDiv(v-if="game.vname!='Dark' || game.score!='*'")
54 button(@click="download()") {{ st.tr["Download"] }} PGN
55 button(onClick="window.doClick('modalAdjust')") ⤢
57 v-if="game.vname!='Dark' && game.mode!='analyze'"
58 @click="analyzePosition()"
60 | {{ st.tr["Analyse"] }}
61 // NOTE: rather ugly hack to avoid showing twice "rules" link...
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.vname != "Dark" || this.game.score != "*";
128 analyze: function() {
130 this.game.mode == "analyze" ||
131 // From Board viewpoint, a finished Dark game == analyze (TODO: unclear)
132 (this.game.vname == "Dark" && this.game.score != "*")
136 created: function() {
137 if (this.game.fenStart) this.re_setVariables();
139 mounted: function() {
141 document.getElementById("eogDiv"),
142 document.getElementById("adjuster")
143 ].forEach(elt => elt.addEventListener("click", processModalClick));
144 // Take full width on small screens:
145 let boardSize = parseInt(localStorage.getItem("boardSize"));
148 window.innerWidth >= 768
149 ? 0.75 * Math.min(window.innerWidth, window.innerHeight)
152 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
153 document.getElementById("boardContainer").style.width = boardSize + "px";
154 let gameContainer = document.getElementById("gameContainer");
155 gameContainer.style.width = boardSize + movesWidth + "px";
156 document.getElementById("boardSize").value =
157 (boardSize * 100) / (window.innerWidth - movesWidth);
158 // timeout to avoid calling too many time the adjust method
159 let timeoutLaunched = false;
160 window.addEventListener("resize", () => {
161 if (!timeoutLaunched) {
162 timeoutLaunched = true;
165 timeoutLaunched = false;
171 focusBg: function() {
172 document.getElementById("baseGame").focus();
174 adjustBoard: function() {
175 const boardContainer = document.getElementById("boardContainer");
176 if (!boardContainer) return; //no board on page
177 const k = document.getElementById("boardSize").value;
178 const movesWidth = window.innerWidth >= 768 ? 280 : 0;
179 const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary...
180 // Value of 0 is board min size; 100 is window.width [- movesWidth]
183 (k * (window.innerWidth - (movesWidth + minBoardWidth))) / 100;
184 localStorage.setItem("boardSize", boardSize);
185 boardContainer.style.width = boardSize + "px";
186 document.getElementById("gameContainer").style.width =
187 boardSize + movesWidth + "px";
189 handleKeys: function(e) {
190 if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault();
209 handleScroll: function(e) {
210 // NOTE: since game.mode=="analyze" => no score, next condition is enough
211 if (this.game.score != "*") {
213 if (e.deltaY < 0) this.undo();
214 else if (e.deltaY > 0) this.play();
217 showRules: function() {
218 //this.$router.push("/variants/" + this.game.vname);
219 window.open("#/variants/" + this.game.vname, "_blank"); //better
221 re_setVariables: function() {
222 this.endgameMessage = "";
223 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
224 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
225 // Post-processing: decorate each move with color + current FEN:
226 // (to be able to jump to any position quickly)
227 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
228 this.firstMoveNumber = Math.floor(
229 V.ParseFen(this.game.fenStart).movesCount / 2
231 this.moves.forEach(move => {
232 // NOTE: this is doing manually what play() function below achieve,
233 // but in a lighter "fast-forward" way
234 move.color = vr_tmp.turn;
235 move.notation = vr_tmp.getNotation(move);
237 move.fen = vr_tmp.getFen();
240 (this.moves.length > 0 && this.moves[0].color == "b") ||
241 (this.moves.length == 0 && vr_tmp.turn == "b")
243 // 'end' is required for Board component to check lastMove for e.p.
247 end: { x: -1, y: -1 }
250 const L = this.moves.length;
252 this.lastMove = L > 0 ? this.moves[L - 1] : null;
253 this.incheck = this.vr.getCheckSquares(this.vr.turn);
255 analyzePosition: function() {
260 this.vr.getFen().replace(/ /g, "_");
261 // Open in same tab in live games (against cheating)
262 if (this.game.type == "live") this.$router.push(newUrl);
263 else window.open("#" + newUrl);
265 download: function() {
266 const content = this.getPgn();
267 // Prepare and trigger download link
268 let downloadAnchor = document.getElementById("download");
269 downloadAnchor.setAttribute("download", "game.pgn");
270 downloadAnchor.href =
271 "data:text/plain;charset=utf-8," + encodeURIComponent(content);
272 downloadAnchor.click();
276 pgn += '[Site "vchess.club"]\n';
277 pgn += '[Variant "' + this.game.vname + '"]\n';
278 pgn += '[Date "' + getDate(new Date()) + '"]\n';
279 pgn += '[White "' + this.game.players[0].name + '"]\n';
280 pgn += '[Black "' + this.game.players[1].name + '"]\n';
281 pgn += '[Fen "' + this.game.fenStart + '"]\n';
282 pgn += '[Result "' + this.game.score + '"]\n\n';
285 while (i < this.moves.length) {
286 pgn += counter++ + ".";
287 for (let color of ["w", "b"]) {
289 while (i < this.moves.length && this.moves[i].color == color)
290 move += this.moves[i++].notation + ",";
291 move = move.slice(0, -1); //remove last comma
292 pgn += move + (i < this.moves.length ? " " : "");
297 showEndgameMsg: function(message) {
298 this.endgameMessage = message;
299 let modalBox = document.getElementById("modalEog");
300 modalBox.checked = true;
302 modalBox.checked = false;
305 animateMove: function(move, callback) {
306 let startSquare = document.getElementById(getSquareId(move.start));
307 let endSquare = document.getElementById(getSquareId(move.end));
308 let rectStart = startSquare.getBoundingClientRect();
309 let rectEnd = endSquare.getBoundingClientRect();
311 x: rectEnd.x - rectStart.x,
312 y: rectEnd.y - rectStart.y
314 let movingPiece = document.querySelector(
315 "#" + getSquareId(move.start) + " > img.piece"
317 // HACK for animation (with positive translate, image slides "under background")
318 // Possible improvement: just alter squares on the piece's way...
319 const squares = document.getElementsByClassName("board");
320 for (let i = 0; i < squares.length; i++) {
321 let square = squares.item(i);
322 if (square.id != getSquareId(move.start)) square.style.zIndex = "-1";
324 movingPiece.style.transform =
325 "translate(" + translation.x + "px," + translation.y + "px)";
326 movingPiece.style.transitionDuration = "0.25s";
327 movingPiece.style.zIndex = "3000";
329 for (let i = 0; i < squares.length; i++)
330 squares.item(i).style.zIndex = "auto";
331 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
335 play: function(move, receive) {
336 // NOTE: navigate and receive are mutually exclusive
337 const navigate = !move;
338 // Forbid playing outside analyze mode, except if move is received.
339 // Sufficient condition because Board already knows which turn it is.
342 this.game.mode != "analyze" &&
344 (this.game.score != "*" || this.cursor < this.moves.length - 1)
348 const doPlayMove = () => {
349 // To play a move, cursor must be at the end of the game:
350 if (!!receive && this.cursor < this.moves.length - 1) this.gotoEnd();
352 if (this.cursor == this.moves.length - 1) return; //no more moves
353 move = this.moves[this.cursor + 1];
355 move.color = this.vr.turn;
356 move.notation = this.vr.getNotation(move);
360 this.lastMove = move;
361 if (this.st.settings.sound == 2)
362 new Audio("/sounds/move.mp3").play().catch(() => {});
364 move.fen = this.vr.getFen();
365 // Stack move on movesList at current cursor
366 if (this.cursor == this.moves.length) this.moves.push(move);
367 else this.moves = this.moves.slice(0, this.cursor).concat([move]);
369 // Is opponent in check?
370 this.incheck = this.vr.getCheckSquares(this.vr.turn);
371 const score = this.vr.getCurrentScore();
373 const message = getScoreMessage(score);
374 if (this.game.mode != "analyze")
375 this.$emit("gameover", score, message);
376 //just show score on screen (allow undo)
377 else this.showEndgameMsg(score + " . " + message);
379 if (!navigate && this.game.mode != "analyze")
380 this.$emit("newmove", move); //post-processing (e.g. computer play)
382 if (!!receive && this.game.vname != "Dark")
383 this.animateMove(move, doPlayMove);
386 undo: function(move) {
387 const navigate = !move;
389 if (this.cursor < 0) return; //no more moves
390 move = this.moves[this.cursor];
394 this.lastMove = this.cursor >= 0 ? this.moves[this.cursor] : undefined;
395 if (this.st.settings.sound == 2)
396 new Audio("/sounds/undo.mp3").play().catch(() => {});
397 this.incheck = this.vr.getCheckSquares(this.vr.turn);
398 if (!navigate) this.moves.pop();
400 gotoMove: function(index) {
401 this.vr.re_init(this.moves[index].fen);
403 this.lastMove = this.moves[index];
405 gotoBegin: function() {
406 if (this.cursor == -1) return;
407 this.vr.re_init(this.game.fenStart);
408 if (this.moves.length > 0 && this.moves[0].notation == "...") {
410 this.lastMove = this.moves[0];
413 this.lastMove = null;
416 gotoEnd: function() {
417 if (this.cursor == this.moves.length - 1) return;
418 this.gotoMove(this.moves.length - 1);
421 this.orientation = V.GetOppCol(this.orientation);
427 <style lang="sass" scoped>
428 [type="checkbox"]#modalEog+div .card
431 [type="checkbox"]#modalAdjust+div .card
444 display: inline-block
449 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)