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="analyze"
12 :user-color="game.mycolor" :orientation="orientation"
13 :vname="game.vname" @play-move="play")
14 #turnIndicator(v-if="game.vname=='Dark' && game.score=='*'")
17 button(@click="gotoBegin") <<
18 button(@click="() => undo()") <
19 button(@click="flip") ⇅
20 button(@click="() => play()") >
21 button(@click="gotoEnd") >>
23 #downloadDiv(v-if="game.vname!='Dark' || game.score!='*'")
25 button(@click="download") {{ st.tr["Download PGN"] }}
26 button(v-if="game.vname!='Dark' && game.mode!='analyze'"
27 @click="analyzePosition")
28 | {{ st.tr["Analyze"] }}
29 // NOTE: rather ugly hack to avoid showing twice "rules" link...
30 button(v-if="!$route.path.match('/variants/')" @click="showRules")
31 | {{ st.tr["Rules"] }}
33 MoveList(v-if="showMoves" :score="game.score" :message="game.scoreMsg"
34 :firstNum="firstMoveNumber" :moves="moves" :cursor="cursor"
35 @goto-move="gotoMove")
40 import Board from "@/components/Board.vue";
41 import MoveList from "@/components/MoveList.vue";
42 import { store } from "@/store";
43 import { getSquareId } from "@/utils/squareId";
44 import { getDate } from "@/utils/datetime";
52 // "vr": VariantRules object, describing the game state + rules
57 // NOTE: all following variables must be reset at the beginning of a game
60 score: "*", //'*' means 'unfinished'
62 cursor: -1, //index of the move just played
64 firstMoveNumber: 0, //for printing
68 // game initial FEN changes when a new game starts
69 "game.fenStart": function() {
70 this.re_setVariables();
72 // Received a new move to play:
73 "game.moveToPlay": function(newMove) {
74 if (!!newMove) //if stop + launch new game, get undefined move
75 this.play(newMove, "receive");
79 showMoves: function() {
80 return this.game.vname != "Dark" || this.game.score != "*";
84 const L = this.moves.length;
85 if (L == 0 || this.moves[L-1].color == "b")
87 else //if (this.moves[L-1].color == "w")
89 return color + " turn";
92 return this.game.mode=="analyze" ||
93 // From Board viewpoint, a finished Dark game == analyze (TODO: unclear)
94 (this.game.vname == "Dark" && this.game.score != "*");
98 if (!!this.game.fenStart)
99 this.re_setVariables();
101 mounted: function() {
102 // Take full width on small screens:
103 let boardSize = parseInt(localStorage.getItem("boardSize"));
106 boardSize = (window.innerWidth >= 768
107 ? Math.min(600, 0.5*window.innerWidth) //heuristic...
108 : window.innerWidth);
110 const movesWidth = (window.innerWidth >= 768 ? 280 : 0);
111 document.getElementById("boardContainer").style.width = boardSize + "px";
112 let gameContainer = document.getElementById("gameContainer");
113 gameContainer.style.width = (boardSize + movesWidth) + "px";
116 focusBg: function() {
117 // NOTE: small blue border appears...
118 document.getElementById("baseGame").focus();
120 handleKeys: function(e) {
121 if ([32,37,38,39,40].includes(e.keyCode))
142 handleScroll: function(e) {
143 // NOTE: since game.mode=="analyze" => no score, next condition is enough
144 if (this.game.score != "*")
149 else if (e.deltaY > 0)
153 showRules: function() {
154 //this.$router.push("/variants/" + this.game.vname);
155 window.open("#/variants/" + this.game.vname, "_blank"); //better
157 re_setVariables: function() {
158 this.endgameMessage = "";
159 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
160 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
161 // Post-processing: decorate each move with color + current FEN:
162 // (to be able to jump to any position quickly)
163 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
164 this.firstMoveNumber =
165 Math.floor(V.ParseFen(this.game.fenStart).movesCount / 2);
166 this.moves.forEach(move => {
167 // NOTE: this is doing manually what play() function below achieve,
168 // but in a lighter "fast-forward" way
169 move.color = vr_tmp.turn;
170 move.notation = vr_tmp.getNotation(move);
172 move.fen = vr_tmp.getFen();
174 if (this.game.fenStart.indexOf(" b ") >= 0 ||
175 (this.moves.length > 0 && this.moves[0].color == "b"))
177 // 'end' is required for Board component to check lastMove for e.p.
178 this.moves.unshift({color: "w", notation: "...", end: {x:-1,y:-1}});
180 const L = this.moves.length;
182 this.lastMove = (L > 0 ? this.moves[L-1] : null);
184 analyzePosition: function() {
185 const newUrl = "/analyze/" + this.game.vname +
186 "/?fen=" + this.vr.getFen().replace(/ /g, "_");
187 if (this.game.type == "live")
188 this.$router.push(newUrl); //open in same tab: against cheating...
190 window.open("#" + newUrl); //open in a new tab: more comfortable
192 download: function() {
193 const content = this.getPgn();
194 // Prepare and trigger download link
195 let downloadAnchor = document.getElementById("download");
196 downloadAnchor.setAttribute("download", "game.pgn");
197 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
198 downloadAnchor.click();
202 pgn += '[Site "vchess.club"]\n';
203 pgn += '[Variant "' + this.game.vname + '"]\n';
204 pgn += '[Date "' + getDate(new Date()) + '"]\n';
205 pgn += '[White "' + this.game.players[0].name + '"]\n';
206 pgn += '[Black "' + this.game.players[1].name + '"]\n';
207 pgn += '[Fen "' + this.game.fenStart + '"]\n';
208 pgn += '[Result "' + this.game.score + '"]\n\n';
211 while (i < this.moves.length)
213 pgn += (counter++) + ".";
214 for (let color of ["w","b"])
217 while (i < this.moves.length && this.moves[i].color == color)
218 move += this.moves[i++].notation + ",";
219 move = move.slice(0,-1); //remove last comma
220 pgn += move + (i < this.moves.length ? " " : "");
225 getScoreMessage: function(score) {
226 let eogMessage = "Undefined";
230 eogMessage = this.st.tr["White win"];
233 eogMessage = this.st.tr["Black win"];
236 eogMessage = this.st.tr["Draw"];
239 eogMessage = this.st.tr["Unfinished"];
244 showEndgameMsg: function(message) {
245 this.endgameMessage = message;
246 let modalBox = document.getElementById("modalEog");
247 modalBox.checked = true;
248 setTimeout(() => { modalBox.checked = false; }, 2000);
250 animateMove: function(move, callback) {
251 let startSquare = document.getElementById(getSquareId(move.start));
252 let endSquare = document.getElementById(getSquareId(move.end));
253 let rectStart = startSquare.getBoundingClientRect();
254 let rectEnd = endSquare.getBoundingClientRect();
255 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
257 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
258 // HACK for animation (with positive translate, image slides "under background")
259 // Possible improvement: just alter squares on the piece's way...
260 const squares = document.getElementsByClassName("board");
261 for (let i=0; i<squares.length; i++)
263 let square = squares.item(i);
264 if (square.id != getSquareId(move.start))
265 square.style.zIndex = "-1";
267 movingPiece.style.transform = "translate(" + translation.x + "px," +
268 translation.y + "px)";
269 movingPiece.style.transitionDuration = "0.2s";
270 movingPiece.style.zIndex = "3000";
272 for (let i=0; i<squares.length; i++)
273 squares.item(i).style.zIndex = "auto";
274 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
278 play: function(move, receive) {
279 // NOTE: navigate and receive are mutually exclusive
280 const navigate = !move;
281 // Forbid playing outside analyze mode, except if move is received.
282 // Sufficient condition because Board already knows which turn it is.
283 if (!navigate && this.game.mode!="analyze" && !receive
284 && (this.game.score != "*" || this.cursor < this.moves.length-1))
288 const doPlayMove = () => {
289 if (!!receive && this.cursor < this.moves.length-1)
290 this.gotoEnd(); //required to play the move
293 if (this.cursor == this.moves.length-1)
294 return; //no more moves
295 move = this.moves[this.cursor+1];
299 move.color = this.vr.turn;
300 move.notation = this.vr.getNotation(move);
304 this.lastMove = move;
305 if (this.st.settings.sound == 2)
306 new Audio("/sounds/move.mp3").play().catch(err => {});
309 move.fen = this.vr.getFen();
310 // Stack move on movesList at current cursor
311 if (this.cursor == this.moves.length)
312 this.moves.push(move);
314 this.moves = this.moves.slice(0,this.cursor).concat([move]);
316 // Is opponent in check?
317 this.incheck = this.vr.getCheckSquares(this.vr.turn);
318 const score = this.vr.getCurrentScore();
321 const message = this.getScoreMessage(score);
322 if (this.game.mode != "analyze")
323 this.$emit("gameover", score, message);
324 else //just show score on screen (allow undo)
325 this.showEndgameMsg(score + " . " + message);
327 if (!navigate && this.game.mode!="analyze")
328 this.$emit("newmove", move); //post-processing (e.g. computer play)
330 if (!!receive && this.game.vname != "Dark")
331 this.animateMove(move, doPlayMove);
335 undo: function(move) {
336 const navigate = !move;
340 return; //no more moves
341 move = this.moves[this.cursor];
345 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
346 if (this.st.settings.sound == 2)
347 new Audio("/sounds/undo.mp3").play().catch(err => {});
348 this.incheck = this.vr.getCheckSquares(this.vr.turn);
352 gotoMove: function(index) {
353 this.vr.re_init(this.moves[index].fen);
355 this.lastMove = this.moves[index];
357 gotoBegin: function() {
358 if (this.cursor == -1)
360 this.vr.re_init(this.game.fenStart);
361 if (this.moves.length > 0 && this.moves[0].notation == "...")
364 this.lastMove = this.moves[0];
369 this.lastMove = null;
372 gotoEnd: function() {
373 if (this.cursor == this.moves.length - 1)
375 this.gotoMove(this.moves.length-1);
378 this.orientation = V.GetOppCol(this.orientation);
384 <style lang="sass" scoped>
395 display: inline-block
404 display: inline-block
407 @media screen and (min-width: 768px)
418 // TODO: later, maybe, allow movesList of variable width
419 // or e.g. between 250 and 350px (but more complicated)
423 @media screen and (max-width: 767px)