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="game.mode=='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") >>
21 button(@click="download") {{ st.tr["Download PGN"] }}
22 button(v-if="game.mode!='analyze'" @click="analyzePosition")
23 | {{ st.tr["Analyze"] }}
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(newMove) {
63 if (!!newMove) //if stop + launch new game, get undefined move
64 this.play(newMove, "receive");
68 showMoves: function() {
69 return this.game.vname != "Dark" || this.game.mode=="analyze";
73 if (!!this.game.fenStart)
74 this.re_setVariables();
78 // TODO: small blue border appears...
79 document.getElementById("baseGame").focus();
81 handleKeys: function(e) {
82 if ([32,37,38,39,40].includes(e.keyCode))
103 re_setVariables: function() {
104 this.endgameMessage = "";
105 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
106 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
107 // Post-processing: decorate each move with color + current FEN:
108 // (to be able to jump to any position quickly)
109 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
110 this.moves.forEach(move => {
111 // NOTE: this is doing manually what play() function below achieve,
112 // but in a lighter "fast-forward" way
113 move.color = vr_tmp.turn;
114 move.notation = vr_tmp.getNotation(move);
116 move.fen = vr_tmp.getFen();
118 if (this.game.fenStart.indexOf(" b ") >= 0 ||
119 (this.moves.length > 0 && this.moves[0].color == "b"))
121 // 'end' is required for Board component to check lastMove for e.p.
122 this.moves.unshift({color: "w", notation: "...", end: {x:-1,y:-1}});
124 const L = this.moves.length;
126 this.lastMove = (L > 0 ? this.moves[L-1] : null);
128 analyzePosition: function() {
129 const newUrl = "/analyze/" + this.game.vname +
130 "/?fen=" + this.vr.getFen().replace(/ /g, "_");
131 //window.open("#" + newUrl); //to open in a new tab
132 this.$router.push(newUrl); //better
134 download: function() {
135 const content = this.getPgn();
136 // Prepare and trigger download link
137 let downloadAnchor = document.getElementById("download");
138 downloadAnchor.setAttribute("download", "game.pgn");
139 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
140 downloadAnchor.click();
144 pgn += '[Site "vchess.club"]\n';
145 pgn += '[Variant "' + this.game.vname + '"]\n';
146 pgn += '[Date "' + getDate(new Date()) + '"]\n';
147 pgn += '[White "' + this.game.players[0].name + '"]\n';
148 pgn += '[Black "' + this.game.players[1].name + '"]\n';
149 pgn += '[Fen "' + this.game.fenStart + '"]\n';
150 pgn += '[Result "' + this.game.score + '"]\n\n';
153 while (i < this.moves.length)
155 pgn += (counter++) + ".";
156 for (let color of ["w","b"])
159 while (i < this.moves.length && this.moves[i].color == color)
160 move += this.moves[i++].notation + ",";
161 move = move.slice(0,-1); //remove last comma
162 pgn += move + (i < this.moves.length ? " " : "");
167 getScoreMessage: function(score) {
168 let eogMessage = "Undefined";
172 eogMessage = this.st.tr["White win"];
175 eogMessage = this.st.tr["Black win"];
178 eogMessage = this.st.tr["Draw"];
181 eogMessage = this.st.tr["Unfinished"];
186 showEndgameMsg: function(message) {
187 this.endgameMessage = message;
188 let modalBox = document.getElementById("modalEog");
189 modalBox.checked = true;
190 setTimeout(() => { modalBox.checked = false; }, 2000);
192 animateMove: function(move, callback) {
193 let startSquare = document.getElementById(getSquareId(move.start));
194 let endSquare = document.getElementById(getSquareId(move.end));
195 let rectStart = startSquare.getBoundingClientRect();
196 let rectEnd = endSquare.getBoundingClientRect();
197 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
199 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
200 // HACK for animation (with positive translate, image slides "under background")
201 // Possible improvement: just alter squares on the piece's way...
202 const squares = document.getElementsByClassName("board");
203 for (let i=0; i<squares.length; i++)
205 let square = squares.item(i);
206 if (square.id != getSquareId(move.start))
207 square.style.zIndex = "-1";
209 movingPiece.style.transform = "translate(" + translation.x + "px," +
210 translation.y + "px)";
211 movingPiece.style.transitionDuration = "0.2s";
212 movingPiece.style.zIndex = "3000";
214 for (let i=0; i<squares.length; i++)
215 squares.item(i).style.zIndex = "auto";
216 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
220 play: function(move, receive) {
221 // NOTE: navigate and receive are mutually exclusive
222 const navigate = !move;
223 // Forbid playing outside analyze mode, except if move is received.
224 // Sufficient condition because Board already knows which turn it is.
225 if (!navigate && this.game.mode!="analyze" && !receive
226 && this.cursor < this.moves.length-1)
230 const doPlayMove = () => {
231 if (!!receive && this.cursor < this.moves.length-1)
232 this.gotoEnd(); //required to play the move
235 if (this.cursor == this.moves.length-1)
236 return; //no more moves
237 move = this.moves[this.cursor+1];
241 move.color = this.vr.turn;
242 move.notation = this.vr.getNotation(move);
246 this.lastMove = move;
247 if (this.st.settings.sound == 2)
248 new Audio("/sounds/move.mp3").play().catch(err => {});
251 move.fen = this.vr.getFen();
252 // Stack move on movesList at current cursor
253 if (this.cursor == this.moves.length)
254 this.moves.push(move);
256 this.moves = this.moves.slice(0,this.cursor).concat([move]);
258 if (this.game.mode != "analyze")
259 this.$emit("newmove", move); //post-processing (e.g. computer play)
260 // Is opponent in check?
261 this.incheck = this.vr.getCheckSquares(this.vr.turn);
262 const score = this.vr.getCurrentScore();
265 const message = this.getScoreMessage(score);
266 if (this.game.mode != "analyze")
267 this.$emit("gameover", score, message);
268 else //just show score on screen (allow undo)
269 this.showEndgameMsg(score + " . " + message);
272 if (!!receive && this.game.vname != "Dark")
273 this.animateMove(move, doPlayMove);
277 undo: function(move) {
278 const navigate = !move;
282 return; //no more moves
283 move = this.moves[this.cursor];
287 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
288 if (this.st.settings.sound == 2)
289 new Audio("/sounds/undo.mp3").play().catch(err => {});
290 this.incheck = this.vr.getCheckSquares(this.vr.turn);
294 gotoMove: function(index) {
295 this.vr.re_init(this.moves[index].fen);
297 this.lastMove = this.moves[index];
299 gotoBegin: function() {
300 this.vr.re_init(this.game.fenStart);
301 if (this.moves.length > 0 && this.moves[0].notation == "...")
304 this.lastMove = this.moves[0];
309 this.lastMove = null;
312 gotoEnd: function() {
313 this.gotoMove(this.moves.length-1);
316 this.orientation = V.GetNextCol(this.orientation);
329 @media screen and (min-width: 768px)
332 @media screen and (max-width: 767px)
338 display: inline-block