2 div#baseGame(tabindex=-1 @click="() => focusBg()"
3 @keydown="handleKeys" @wheel="handleScroll")
4 input#modalEog.modal(type="checkbox")
5 div(role="dialog" data-checkbox="modalEog" aria-labelledby="eogMessage")
6 .card.smallpad.small-modal.text-center
7 label.modal-close(for="modalEog")
8 h3#eogMessage.section {{ endgameMessage }}
11 Board(:vr="vr" :last-move="lastMove" :analyze="game.mode=='analyze'"
12 :user-color="game.mycolor" :orientation="orientation"
13 :vname="game.vname" @play-move="play")
15 button(@click="gotoBegin") <<
16 button(@click="() => undo()") <
17 button(@click="flip") ⇅
18 button(@click="() => play()") >
19 button(@click="gotoEnd") >>
22 button(@click="download") {{ st.tr["Download PGN"] }}
23 button(v-if="game.mode!='analyze'" @click="analyzePosition")
24 | {{ st.tr["Analyze"] }}
26 MoveList(v-if="showMoves" :score="game.score" :message="game.scoreMsg"
27 :firstNum="firstMoveNumber" :moves="moves" :cursor="cursor"
28 @goto-move="gotoMove")
29 // TODO: clearer required ?!
34 import Board from "@/components/Board.vue";
35 import MoveList from "@/components/MoveList.vue";
36 import { store } from "@/store";
37 import { getSquareId } from "@/utils/squareId";
38 import { getDate } from "@/utils/datetime";
46 // "vr": VariantRules object, describing the game state + rules
51 // NOTE: all following variables must be reset at the beginning of a game
54 score: "*", //'*' means 'unfinished'
56 cursor: -1, //index of the move just played
58 firstMoveNumber: 0, //for printing
62 // game initial FEN changes when a new game starts
63 "game.fenStart": function() {
64 this.re_setVariables();
66 // Received a new move to play:
67 "game.moveToPlay": function(newMove) {
68 if (!!newMove) //if stop + launch new game, get undefined move
69 this.play(newMove, "receive");
73 showMoves: function() {
74 return this.game.vname != "Dark" || this.game.mode=="analyze";
78 if (!!this.game.fenStart)
79 this.re_setVariables();
82 // Take full width on small screens:
83 let boardSize = parseInt(localStorage.getItem("boardSize"));
86 boardSize = (window.innerWidth >= 768
87 ? Math.min(600, 0.5*window.innerWidth) //heuristic...
90 const movesWidth = (window.innerWidth >= 768 ? 280 : 0);
91 document.getElementById("boardContainer").style.width = boardSize + "px";
92 let gameContainer = document.getElementById("gameContainer");
93 gameContainer.style.width = (boardSize + movesWidth) + "px";
97 // TODO: small blue border appears...
98 document.getElementById("baseGame").focus();
100 handleKeys: function(e) {
101 if ([32,37,38,39,40].includes(e.keyCode))
122 handleScroll: function(e) {
125 else if (e.deltaY > 0)
128 re_setVariables: function() {
129 this.endgameMessage = "";
130 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
131 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
132 // Post-processing: decorate each move with color + current FEN:
133 // (to be able to jump to any position quickly)
134 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
135 this.firstMoveNumber =
136 Math.floor(V.ParseFen(this.game.fenStart).movesCount / 2);
137 this.moves.forEach(move => {
138 // NOTE: this is doing manually what play() function below achieve,
139 // but in a lighter "fast-forward" way
140 move.color = vr_tmp.turn;
141 move.notation = vr_tmp.getNotation(move);
143 move.fen = vr_tmp.getFen();
145 if (this.game.fenStart.indexOf(" b ") >= 0 ||
146 (this.moves.length > 0 && this.moves[0].color == "b"))
148 // 'end' is required for Board component to check lastMove for e.p.
149 this.moves.unshift({color: "w", notation: "...", end: {x:-1,y:-1}});
151 const L = this.moves.length;
153 this.lastMove = (L > 0 ? this.moves[L-1] : null);
155 analyzePosition: function() {
156 const newUrl = "/analyze/" + this.game.vname +
157 "/?fen=" + this.vr.getFen().replace(/ /g, "_");
158 //window.open("#" + newUrl); //to open in a new tab
159 this.$router.push(newUrl); //better
161 download: function() {
162 const content = this.getPgn();
163 // Prepare and trigger download link
164 let downloadAnchor = document.getElementById("download");
165 downloadAnchor.setAttribute("download", "game.pgn");
166 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
167 downloadAnchor.click();
171 pgn += '[Site "vchess.club"]\n';
172 pgn += '[Variant "' + this.game.vname + '"]\n';
173 pgn += '[Date "' + getDate(new Date()) + '"]\n';
174 pgn += '[White "' + this.game.players[0].name + '"]\n';
175 pgn += '[Black "' + this.game.players[1].name + '"]\n';
176 pgn += '[Fen "' + this.game.fenStart + '"]\n';
177 pgn += '[Result "' + this.game.score + '"]\n\n';
180 while (i < this.moves.length)
182 pgn += (counter++) + ".";
183 for (let color of ["w","b"])
186 while (i < this.moves.length && this.moves[i].color == color)
187 move += this.moves[i++].notation + ",";
188 move = move.slice(0,-1); //remove last comma
189 pgn += move + (i < this.moves.length ? " " : "");
194 getScoreMessage: function(score) {
195 let eogMessage = "Undefined";
199 eogMessage = this.st.tr["White win"];
202 eogMessage = this.st.tr["Black win"];
205 eogMessage = this.st.tr["Draw"];
208 eogMessage = this.st.tr["Unfinished"];
213 showEndgameMsg: function(message) {
214 this.endgameMessage = message;
215 let modalBox = document.getElementById("modalEog");
216 modalBox.checked = true;
217 setTimeout(() => { modalBox.checked = false; }, 2000);
219 animateMove: function(move, callback) {
220 let startSquare = document.getElementById(getSquareId(move.start));
221 let endSquare = document.getElementById(getSquareId(move.end));
222 let rectStart = startSquare.getBoundingClientRect();
223 let rectEnd = endSquare.getBoundingClientRect();
224 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
226 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
227 // HACK for animation (with positive translate, image slides "under background")
228 // Possible improvement: just alter squares on the piece's way...
229 const squares = document.getElementsByClassName("board");
230 for (let i=0; i<squares.length; i++)
232 let square = squares.item(i);
233 if (square.id != getSquareId(move.start))
234 square.style.zIndex = "-1";
236 movingPiece.style.transform = "translate(" + translation.x + "px," +
237 translation.y + "px)";
238 movingPiece.style.transitionDuration = "0.2s";
239 movingPiece.style.zIndex = "3000";
241 for (let i=0; i<squares.length; i++)
242 squares.item(i).style.zIndex = "auto";
243 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
247 play: function(move, receive) {
248 // NOTE: navigate and receive are mutually exclusive
249 const navigate = !move;
250 // Forbid playing outside analyze mode, except if move is received.
251 // Sufficient condition because Board already knows which turn it is.
252 if (!navigate && this.game.mode!="analyze" && !receive
253 && this.cursor < this.moves.length-1)
257 const doPlayMove = () => {
258 if (!!receive && this.cursor < this.moves.length-1)
259 this.gotoEnd(); //required to play the move
262 if (this.cursor == this.moves.length-1)
263 return; //no more moves
264 move = this.moves[this.cursor+1];
268 move.color = this.vr.turn;
269 move.notation = this.vr.getNotation(move);
273 this.lastMove = move;
274 if (this.st.settings.sound == 2)
275 new Audio("/sounds/move.mp3").play().catch(err => {});
278 move.fen = this.vr.getFen();
279 // Stack move on movesList at current cursor
280 if (this.cursor == this.moves.length)
281 this.moves.push(move);
283 this.moves = this.moves.slice(0,this.cursor).concat([move]);
285 if (!navigate && this.game.mode != "analyze")
286 this.$emit("newmove", move); //post-processing (e.g. computer play)
287 // Is opponent in check?
288 this.incheck = this.vr.getCheckSquares(this.vr.turn);
289 const score = this.vr.getCurrentScore();
292 const message = this.getScoreMessage(score);
293 if (this.game.mode != "analyze")
294 this.$emit("gameover", score, message);
295 else //just show score on screen (allow undo)
296 this.showEndgameMsg(score + " . " + message);
299 if (!!receive && this.game.vname != "Dark")
300 this.animateMove(move, doPlayMove);
304 undo: function(move) {
305 const navigate = !move;
309 return; //no more moves
310 move = this.moves[this.cursor];
314 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
315 if (this.st.settings.sound == 2)
316 new Audio("/sounds/undo.mp3").play().catch(err => {});
317 this.incheck = this.vr.getCheckSquares(this.vr.turn);
321 gotoMove: function(index) {
322 this.vr.re_init(this.moves[index].fen);
324 this.lastMove = this.moves[index];
326 gotoBegin: function() {
327 if (this.cursor == -1)
329 this.vr.re_init(this.game.fenStart);
330 if (this.moves.length > 0 && this.moves[0].notation == "...")
333 this.lastMove = this.moves[0];
338 this.lastMove = null;
341 gotoEnd: function() {
342 if (this.cursor == this.moves.length - 1)
344 this.gotoMove(this.moves.length-1);
347 this.orientation = V.GetNextCol(this.orientation);
363 @media screen and (min-width: 768px)
373 display: inline-block
385 @media screen and (max-width: 767px)