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")
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
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");
65 "game.score": function(score) {
67 this.endGame(score, this.game.scoreMsg);
71 showMoves: function() {
73 //return window.innerWidth >= 768;
76 return this.game.vname != "Dark" || this.score != "*";
79 return this.game.mode == "analyze" || this.score != "*";
83 if (!!this.game.fenStart)
84 this.re_setVariables();
87 re_setVariables: function() {
88 this.endgameMessage = "";
89 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
90 this.score = this.game.score || "*"; //mutable (if initially "*")
91 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
92 // Post-processing: decorate each move with color + current FEN:
93 // (to be able to jump to any position quickly)
94 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
95 this.moves.forEach(move => {
96 // NOTE: this is doing manually what play() function below achieve,
97 // but in a lighter "fast-forward" way
98 move.color = vr_tmp.turn;
99 move.notation = vr_tmp.getNotation(move);
101 move.fen = vr_tmp.getFen();
103 const L = this.moves.length;
105 this.lastMove = (L > 0 ? this.moves[L-1] : null);
107 download: function() {
108 const content = this.getPgn();
109 // Prepare and trigger download link
110 let downloadAnchor = document.getElementById("download");
111 downloadAnchor.setAttribute("download", "game.pgn");
112 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
113 downloadAnchor.click();
117 pgn += '[Site "vchess.club"]\n';
118 pgn += '[Variant "' + this.game.vname + '"]\n';
119 pgn += '[Date "' + getDate(new Date()) + '"]\n';
120 pgn += '[White "' + this.game.players[0].name + '"]\n';
121 pgn += '[Black "' + this.game.players[1].name + '"]\n';
122 pgn += '[Fen "' + this.game.fenStart + '"]\n';
123 pgn += '[Result "' + this.score + '"]\n\n';
126 while (i < this.moves.length)
128 pgn += (counter++) + ".";
129 for (let color of ["w","b"])
132 while (i < this.moves.length && this.moves[i].color == color)
133 move += this.moves[i++].notation + ",";
134 move = move.slice(0,-1); //remove last comma
135 pgn += move + (i < this.moves.length ? " " : "");
140 getScoreMessage: function(score) {
141 let eogMessage = "Undefined";
145 eogMessage = this.st.tr["White win"];
148 eogMessage = this.st.tr["Black win"];
151 eogMessage = this.st.tr["Draw"];
154 eogMessage = this.st.tr["Unfinished"];
159 showEndgameMsg: function(message) {
160 this.endgameMessage = message;
161 let modalBox = document.getElementById("modalEog");
162 modalBox.checked = true;
163 setTimeout(() => { modalBox.checked = false; }, 2000);
165 endGame: function(score, message) {
168 message = this.getScoreMessage(score);
169 this.showEndgameMsg(score + " . " + message);
170 this.$emit("gameover", score);
172 animateMove: function(move) {
173 let startSquare = document.getElementById(getSquareId(move.start));
174 let endSquare = document.getElementById(getSquareId(move.end));
175 let rectStart = startSquare.getBoundingClientRect();
176 let rectEnd = endSquare.getBoundingClientRect();
177 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
179 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
180 // HACK for animation (with positive translate, image slides "under background")
181 // Possible improvement: just alter squares on the piece's way...
182 const squares = document.getElementsByClassName("board");
183 for (let i=0; i<squares.length; i++)
185 let square = squares.item(i);
186 if (square.id != getSquareId(move.start))
187 square.style.zIndex = "-1";
189 movingPiece.style.transform = "translate(" + translation.x + "px," +
190 translation.y + "px)";
191 movingPiece.style.transitionDuration = "0.2s";
192 movingPiece.style.zIndex = "3000";
194 for (let i=0; i<squares.length; i++)
195 squares.item(i).style.zIndex = "auto";
196 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
200 play: function(move, receive, noanimate) {
201 const navigate = !move;
202 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
203 // (except if we receive opponent's move, human or computer)
204 if (!navigate && !this.analyze && !receive
205 && this.cursor < this.moves.length-1)
211 if (this.cursor == this.moves.length-1)
212 return; //no more moves
213 move = this.moves[this.cursor+1];
215 if (!!receive && !noanimate) //opponent move, variant != "Dark"
217 if (this.cursor < this.moves.length-1)
218 this.gotoEnd(); //required to play the move
219 return this.animateMove(move);
223 move.color = this.vr.turn;
224 move.notation = this.vr.getNotation(move);
226 // Not programmatic, or animation is over
229 this.lastMove = move;
230 if (this.st.settings.sound == 2)
231 new Audio("/sounds/move.mp3").play().catch(err => {});
234 move.fen = this.vr.getFen();
235 if (this.score == "*" || this.analyze)
237 // Stack move on movesList at current cursor
238 if (this.cursor == this.moves.length)
239 this.moves.push(move);
241 this.moves = this.moves.slice(0,this.cursor).concat([move]);
245 this.$emit("newmove", move); //post-processing (e.g. computer play)
246 // Is opponent in check?
247 this.incheck = this.vr.getCheckSquares(this.vr.turn);
248 const score = this.vr.getCurrentScore();
255 // Just show score on screen (allow undo)
256 const message = this.getScoreMessage(score);
257 this.showEndgameMsg(score + " . " + message);
261 undo: function(move) {
262 const navigate = !move;
266 return; //no more moves
267 move = this.moves[this.cursor];
271 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
272 if (this.st.settings.sound == 2)
273 new Audio("/sounds/undo.mp3").play().catch(err => {});
274 this.incheck = this.vr.getCheckSquares(this.vr.turn);
278 gotoMove: function(index) {
279 this.vr.re_init(this.moves[index].fen);
281 this.lastMove = this.moves[index];
283 gotoBegin: function() {
284 this.vr.re_init(this.game.fenStart);
286 this.lastMove = null;
288 gotoEnd: function() {
289 this.gotoMove(this.moves.length-1);
292 this.orientation = V.GetNextCol(this.orientation);