2 div#baseGame(tabindex=-1 @click="() => focusBg()" @keydown="handleKeys")
3 input#modalEog.modal(type="checkbox")
4 div(role="dialog" aria-labelledby="eogMessage")
5 .card.smallpad.small-modal.text-center
6 label.modal-close(for="modalEog")
7 h3#eogMessage.section {{ endgameMessage }}
9 #boardContainer.col-sm-12.col-md-9
10 Board(:vr="vr" :last-move="lastMove" :analyze="analyze"
11 :user-color="game.mycolor" :orientation="orientation"
12 :vname="game.vname" @play-move="play")
14 button(@click="gotoBegin") <<
15 button(@click="() => undo()") <
16 button(@click="flip") ⇅
17 button(@click="() => play()") >
18 button(@click="gotoEnd") >>
19 #fenDiv(v-if="showFen && !!vr")
20 p(@click="gotoFenContent") {{ vr.getFen() }}
23 button(@click="download") {{ st.tr["Download PGN"] }}
25 MoveList(v-if="showMoves" :score="game.score" :message="game.scoreMsg"
26 :moves="moves" :cursor="cursor" @goto-move="gotoMove")
30 import Board from "@/components/Board.vue";
31 import MoveList from "@/components/MoveList.vue";
32 import { store } from "@/store";
33 import { getSquareId } from "@/utils/squareId";
34 import { getDate } from "@/utils/datetime";
42 // "vr": VariantRules object, describing the game state + rules
47 // NOTE: all following variables must be reset at the beginning of a game
50 score: "*", //'*' means 'unfinished'
52 cursor: -1, //index of the move just played
57 // game initial FEN changes when a new game starts
58 "game.fenStart": function() {
59 this.re_setVariables();
61 // Received a new move to play:
62 "game.moveToPlay": function() {
63 this.play(this.game.moveToPlay, "receive", this.game.vname=="Dark");
67 showMoves: function() {
69 //return window.innerWidth >= 768;
72 return this.game.vname != "Dark" || this.game.score != "*";
75 return this.game.mode == "analyze" || this.game.score != "*";
79 if (!!this.game.fenStart)
80 this.re_setVariables();
84 // TODO: small blue border appears...
85 document.getElementById("baseGame").focus();
87 handleKeys: function(e) {
88 if ([32,37,38,39,40].includes(e.keyCode))
109 re_setVariables: function() {
110 this.endgameMessage = "";
111 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
112 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
113 // Post-processing: decorate each move with color + current FEN:
114 // (to be able to jump to any position quickly)
115 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
116 this.moves.forEach(move => {
117 // NOTE: this is doing manually what play() function below achieve,
118 // but in a lighter "fast-forward" way
119 move.color = vr_tmp.turn;
120 move.notation = vr_tmp.getNotation(move);
122 move.fen = vr_tmp.getFen();
124 if (this.game.fenStart.indexOf(" b ") >= 0 ||
125 (this.moves.length > 0 && this.moves[0].color == "b"))
127 // 'end' is required for Board component to check lastMove for e.p.
128 this.moves.unshift({color: "w", notation: "...", end: {x:-1,y:-1}});
130 const L = this.moves.length;
132 this.lastMove = (L > 0 ? this.moves[L-1] : null);
134 gotoFenContent: function(event) {
135 const newUrl = "#/analyze/" + this.game.vname +
136 "/?fen=" + event.target.innerText.replace(/ /g, "_");
137 window.open(newUrl); //to open in a new tab
139 download: function() {
140 const content = this.getPgn();
141 // Prepare and trigger download link
142 let downloadAnchor = document.getElementById("download");
143 downloadAnchor.setAttribute("download", "game.pgn");
144 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
145 downloadAnchor.click();
149 pgn += '[Site "vchess.club"]\n';
150 pgn += '[Variant "' + this.game.vname + '"]\n';
151 pgn += '[Date "' + getDate(new Date()) + '"]\n';
152 pgn += '[White "' + this.game.players[0].name + '"]\n';
153 pgn += '[Black "' + this.game.players[1].name + '"]\n';
154 pgn += '[Fen "' + this.game.fenStart + '"]\n';
155 pgn += '[Result "' + this.game.score + '"]\n\n';
158 while (i < this.moves.length)
160 pgn += (counter++) + ".";
161 for (let color of ["w","b"])
164 while (i < this.moves.length && this.moves[i].color == color)
165 move += this.moves[i++].notation + ",";
166 move = move.slice(0,-1); //remove last comma
167 pgn += move + (i < this.moves.length ? " " : "");
172 getScoreMessage: function(score) {
173 let eogMessage = "Undefined";
177 eogMessage = this.st.tr["White win"];
180 eogMessage = this.st.tr["Black win"];
183 eogMessage = this.st.tr["Draw"];
186 eogMessage = this.st.tr["Unfinished"];
191 showEndgameMsg: function(message) {
192 this.endgameMessage = message;
193 let modalBox = document.getElementById("modalEog");
194 modalBox.checked = true;
195 setTimeout(() => { modalBox.checked = false; }, 2000);
197 animateMove: function(move) {
198 let startSquare = document.getElementById(getSquareId(move.start));
199 let endSquare = document.getElementById(getSquareId(move.end));
200 let rectStart = startSquare.getBoundingClientRect();
201 let rectEnd = endSquare.getBoundingClientRect();
202 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
204 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
205 // HACK for animation (with positive translate, image slides "under background")
206 // Possible improvement: just alter squares on the piece's way...
207 const squares = document.getElementsByClassName("board");
208 for (let i=0; i<squares.length; i++)
210 let square = squares.item(i);
211 if (square.id != getSquareId(move.start))
212 square.style.zIndex = "-1";
214 movingPiece.style.transform = "translate(" + translation.x + "px," +
215 translation.y + "px)";
216 movingPiece.style.transitionDuration = "0.2s";
217 movingPiece.style.zIndex = "3000";
219 for (let i=0; i<squares.length; i++)
220 squares.item(i).style.zIndex = "auto";
221 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
225 play: function(move, receive, noanimate) {
226 const navigate = !move;
227 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
228 // (except if we receive opponent's move, human or computer)
229 if (!navigate && !this.analyze && !receive
230 && this.cursor < this.moves.length-1)
236 if (this.cursor == this.moves.length-1)
237 return; //no more moves
238 move = this.moves[this.cursor+1];
240 if (!!receive && !noanimate) //opponent move, variant != "Dark"
242 if (this.cursor < this.moves.length-1)
243 this.gotoEnd(); //required to play the move
244 return this.animateMove(move);
248 move.color = this.vr.turn;
249 move.notation = this.vr.getNotation(move);
251 // Not programmatic, or animation is over
254 this.lastMove = move;
255 if (this.st.settings.sound == 2)
256 new Audio("/sounds/move.mp3").play().catch(err => {});
259 move.fen = this.vr.getFen();
260 if (this.game.score == "*" || this.analyze)
262 // Stack move on movesList at current cursor
263 if (this.cursor == this.moves.length)
264 this.moves.push(move);
266 this.moves = this.moves.slice(0,this.cursor).concat([move]);
270 this.$emit("newmove", move); //post-processing (e.g. computer play)
271 // Is opponent in check?
272 this.incheck = this.vr.getCheckSquares(this.vr.turn);
273 const score = this.vr.getCurrentScore();
276 const message = this.getScoreMessage(score);
278 this.$emit("gameover", score, message);
279 else //just show score on screen (allow undo)
280 this.showEndgameMsg(score + " . " + message);
283 undo: function(move) {
284 const navigate = !move;
288 return; //no more moves
289 move = this.moves[this.cursor];
293 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
294 if (this.st.settings.sound == 2)
295 new Audio("/sounds/undo.mp3").play().catch(err => {});
296 this.incheck = this.vr.getCheckSquares(this.vr.turn);
300 gotoMove: function(index) {
301 this.vr.re_init(this.moves[index].fen);
303 this.lastMove = this.moves[index];
305 gotoBegin: function() {
306 this.vr.re_init(this.game.fenStart);
307 if (this.moves.length > 0 && this.moves[0].notation == "...")
310 this.lastMove = this.moves[0];
315 this.lastMove = null;
318 gotoEnd: function() {
319 this.gotoMove(this.moves.length-1);
322 this.orientation = V.GetNextCol(this.orientation);
335 @media screen and (min-width: 768px)
338 @media screen and (max-width: 767px)
344 display: inline-block