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 .col-sm-12.col-md-9.col-lg-8
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="() => play()") Play
15 button(@click="() => undo()") Undo
16 button(@click="flip") Flip
17 button(@click="gotoBegin") GotoBegin
18 button(@click="gotoEnd") GotoEnd
19 #fenDiv(v-if="showFen && !!vr")
20 p(@click="gotoFenContent") {{ vr.getFen() }}
23 button(@click="download") {{ st.tr["Download PGN"] }}
24 .col-sm-12.col-md-3.col-lg-4
25 MoveList(v-if="showMoves"
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
54 gameHasEnded: false, //to avoid showing end message twice
58 // game initial FEN changes when a new game starts
59 "game.fenStart": function() {
60 this.re_setVariables();
62 // Received a new move to play:
63 "game.moveToPlay": function() {
64 this.play(this.game.moveToPlay, "receive", this.game.vname=="Dark");
66 "game.score": function(score) {
67 if (!this.gameHasEnded && score != "*")
69 // "false" says "don't bubble up": the parent already knows
70 this.endGame(score, this.game.scoreMsg, false);
75 showMoves: function() {
77 //return window.innerWidth >= 768;
80 return this.game.vname != "Dark" || this.score != "*";
83 return this.game.mode == "analyze" || this.score != "*";
87 if (!!this.game.fenStart)
88 this.re_setVariables();
91 re_setVariables: function() {
92 this.endgameMessage = "";
93 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
94 this.score = this.game.score || "*"; //mutable (if initially "*")
95 this.gameHasEnded = (this.score != "*");
96 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
97 // Post-processing: decorate each move with color + current FEN:
98 // (to be able to jump to any position quickly)
99 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
100 this.moves.forEach(move => {
101 // NOTE: this is doing manually what play() function below achieve,
102 // but in a lighter "fast-forward" way
103 move.color = vr_tmp.turn;
104 move.notation = vr_tmp.getNotation(move);
106 move.fen = vr_tmp.getFen();
108 if (this.game.fenStart.indexOf(" b ") >= 0 ||
109 (this.moves.length > 0 && this.moves[0].color == "b"))
111 // 'end' is required for Board component to check lastMove for e.p.
112 this.moves.unshift({color: "w", notation: "...", end: {x:-1,y:-1}});
114 const L = this.moves.length;
116 this.lastMove = (L > 0 ? this.moves[L-1] : null);
118 gotoFenContent: function(event) {
119 this.$router.push("/analyze/" + this.game.vname +
120 "/?fen=" + event.target.innerText.replace(/ /g, "_"));
122 download: function() {
123 const content = this.getPgn();
124 // Prepare and trigger download link
125 let downloadAnchor = document.getElementById("download");
126 downloadAnchor.setAttribute("download", "game.pgn");
127 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
128 downloadAnchor.click();
132 pgn += '[Site "vchess.club"]\n';
133 pgn += '[Variant "' + this.game.vname + '"]\n';
134 pgn += '[Date "' + getDate(new Date()) + '"]\n';
135 pgn += '[White "' + this.game.players[0].name + '"]\n';
136 pgn += '[Black "' + this.game.players[1].name + '"]\n';
137 pgn += '[Fen "' + this.game.fenStart + '"]\n';
138 pgn += '[Result "' + this.score + '"]\n\n';
141 while (i < this.moves.length)
143 pgn += (counter++) + ".";
144 for (let color of ["w","b"])
147 while (i < this.moves.length && this.moves[i].color == color)
148 move += this.moves[i++].notation + ",";
149 move = move.slice(0,-1); //remove last comma
150 pgn += move + (i < this.moves.length ? " " : "");
155 getScoreMessage: function(score) {
156 let eogMessage = "Undefined";
160 eogMessage = this.st.tr["White win"];
163 eogMessage = this.st.tr["Black win"];
166 eogMessage = this.st.tr["Draw"];
169 eogMessage = this.st.tr["Unfinished"];
174 showEndgameMsg: function(message) {
175 this.endgameMessage = message;
176 let modalBox = document.getElementById("modalEog");
177 modalBox.checked = true;
178 setTimeout(() => { modalBox.checked = false; }, 2000);
180 endGame: function(score, message, bubbleUp) {
181 this.gameHasEnded = true;
184 message = this.getScoreMessage(score);
185 this.showEndgameMsg(score + " . " + message);
187 this.$emit("gameover", score);
189 animateMove: function(move) {
190 let startSquare = document.getElementById(getSquareId(move.start));
191 let endSquare = document.getElementById(getSquareId(move.end));
192 let rectStart = startSquare.getBoundingClientRect();
193 let rectEnd = endSquare.getBoundingClientRect();
194 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
196 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
197 // HACK for animation (with positive translate, image slides "under background")
198 // Possible improvement: just alter squares on the piece's way...
199 const squares = document.getElementsByClassName("board");
200 for (let i=0; i<squares.length; i++)
202 let square = squares.item(i);
203 if (square.id != getSquareId(move.start))
204 square.style.zIndex = "-1";
206 movingPiece.style.transform = "translate(" + translation.x + "px," +
207 translation.y + "px)";
208 movingPiece.style.transitionDuration = "0.2s";
209 movingPiece.style.zIndex = "3000";
211 for (let i=0; i<squares.length; i++)
212 squares.item(i).style.zIndex = "auto";
213 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
217 play: function(move, receive, noanimate) {
218 const navigate = !move;
219 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
220 // (except if we receive opponent's move, human or computer)
221 if (!navigate && !this.analyze && !receive
222 && this.cursor < this.moves.length-1)
228 if (this.cursor == this.moves.length-1)
229 return; //no more moves
230 move = this.moves[this.cursor+1];
232 if (!!receive && !noanimate) //opponent move, variant != "Dark"
234 if (this.cursor < this.moves.length-1)
235 this.gotoEnd(); //required to play the move
236 return this.animateMove(move);
240 move.color = this.vr.turn;
241 move.notation = this.vr.getNotation(move);
243 // Not programmatic, or animation is over
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 if (this.score == "*" || this.analyze)
254 // Stack move on movesList at current cursor
255 if (this.cursor == this.moves.length)
256 this.moves.push(move);
258 this.moves = this.moves.slice(0,this.cursor).concat([move]);
262 this.$emit("newmove", move); //post-processing (e.g. computer play)
263 // Is opponent in check?
264 this.incheck = this.vr.getCheckSquares(this.vr.turn);
265 const score = this.vr.getCurrentScore();
269 this.endGame(score, undefined, true);
272 // Just show score on screen (allow undo)
273 const message = this.getScoreMessage(score);
274 this.showEndgameMsg(score + " . " + message);
278 undo: function(move) {
279 const navigate = !move;
283 return; //no more moves
284 move = this.moves[this.cursor];
288 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
289 if (this.st.settings.sound == 2)
290 new Audio("/sounds/undo.mp3").play().catch(err => {});
291 this.incheck = this.vr.getCheckSquares(this.vr.turn);
295 gotoMove: function(index) {
296 this.vr.re_init(this.moves[index].fen);
298 this.lastMove = this.moves[index];
300 gotoBegin: function() {
301 this.vr.re_init(this.game.fenStart);
303 this.lastMove = null;
305 gotoEnd: function() {
306 this.gotoMove(this.moves.length-1);
309 this.orientation = V.GetNextCol(this.orientation);