3 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
4 input#modalEog.modal(type="checkbox")
5 div(role="dialog" aria-labelledby="eogMessage")
6 .card.smallpad.small-modal.text-center
7 label.modal-close(for="modalEog")
8 h3#eogMessage.section {{ endgameMessage }}
9 // TODO: or "BoardHex" if this.game.vname in "Hexagonal..."
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 #messageDiv.section-content(v-if="game.type=='corr'") {{ curMoveMessage() }}
20 #fenDiv.section-content(v-if="showFen && !!vr")
21 p#fenString.text-center {{ vr.getFen() }}
22 #pgnDiv.section-content
24 button#downloadBtn(@click="download") {{ st.tr["Download PGN"] }}
26 MoveList(v-if="showMoves"
27 :moves="moves" :cursor="cursor" @goto-move="gotoMove")
31 import Board from "@/components/Board.vue";
32 import MoveList from "@/components/MoveList.vue";
33 import { store } from "@/store";
34 import { getSquareId } from "@/utils/squareId";
35 import { getDate } from "@/utils/datetime";
43 // "vr": VariantRules object, describing the game state + rules
48 // NOTE: all following variables must be reset at the beginning of a game
51 score: "*", //'*' means 'unfinished'
53 cursor: -1, //index of the move just played
58 // game initial FEN changes when a new game starts
59 "game.fenStart": function() {
60 this.re_setVariables();
64 showMoves: function() {
66 //return window.innerWidth >= 768;
69 return this.game.vname != "Dark" || this.score != "*";
72 return this.game.mode == "analyze" || this.game.score != "*";
76 if (!!this.game.fenStart)
77 this.re_setVariables();
80 re_setVariables: function() {
81 this.endgameMessage = "";
82 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
83 this.score = this.game.score || "*"; //mutable (if initially "*")
84 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
85 // Post-processing: decorate each move with color + current FEN:
86 // (to be able to jump to any position quickly)
87 let vr_tmp = new V(this.game.fenStart); //vr is already at end of game
88 this.moves.forEach(move => {
89 // NOTE: this is doing manually what play() function below achieve,
90 // but in a lighter "fast-forward" way
91 move.color = vr_tmp.turn;
92 move.notation = vr_tmp.getNotation(move);
94 move.fen = vr_tmp.getFen();
96 const L = this.moves.length;
98 this.lastMove = (L > 0 ? this.moves[L-1] : null);
100 // For corr games, potential message with each move sent
101 curMoveMessage: function() {
104 return this.game.moves[this.cursor].message || "";
106 setCurrentMessage: function(message) {
107 this.game.moves[this.game.moves.length-1].message = message;
109 download: function() {
110 const content = this.getPgn();
111 // Prepare and trigger download link
112 let downloadAnchor = document.getElementById("download");
113 downloadAnchor.setAttribute("download", "game.pgn");
114 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
115 downloadAnchor.click();
119 pgn += '[Site "vchess.club"]\n';
120 pgn += '[Variant "' + this.game.vname + '"]\n';
121 pgn += '[Date "' + getDate(new Date()) + '"]\n';
122 pgn += '[White "' + this.game.players[0].name + '"]\n';
123 pgn += '[Black "' + this.game.players[1].name + '"]\n';
124 pgn += '[Fen "' + this.game.fenStart + '"]\n';
125 pgn += '[Result "' + this.score + '"]\n\n';
128 while (i < this.moves.length)
130 pgn += (counter++) + ".";
131 for (let color of ["w","b"])
134 while (i < this.moves.length && this.moves[i].color == color)
135 move += this.moves[i++].notation + ",";
136 move = move.slice(0,-1); //remove last comma
137 pgn += move + (i < this.moves.length ? " " : "");
142 getScoreMessage: function(score) {
143 let eogMessage = "Undefined";
147 eogMessage = this.st.tr["White win"];
150 eogMessage = this.st.tr["Black win"];
153 eogMessage = this.st.tr["Draw"];
156 eogMessage = this.st.tr["Unfinished"];
161 showEndgameMsg: function(message) {
162 this.endgameMessage = message;
163 let modalBox = document.getElementById("modalEog");
164 modalBox.checked = true;
165 setTimeout(() => { modalBox.checked = false; }, 2000);
167 endGame: function(score, message) {
170 message = this.getScoreMessage(score);
171 this.showEndgameMsg(score + " . " + message);
172 this.$emit("gameover", score);
174 animateMove: function(move) {
175 let startSquare = document.getElementById(getSquareId(move.start));
176 let endSquare = document.getElementById(getSquareId(move.end));
177 let rectStart = startSquare.getBoundingClientRect();
178 let rectEnd = endSquare.getBoundingClientRect();
179 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
181 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
182 // HACK for animation (with positive translate, image slides "under background")
183 // Possible improvement: just alter squares on the piece's way...
184 const squares = document.getElementsByClassName("board");
185 for (let i=0; i<squares.length; i++)
187 let square = squares.item(i);
188 if (square.id != getSquareId(move.start))
189 square.style.zIndex = "-1";
191 movingPiece.style.transform = "translate(" + translation.x + "px," +
192 translation.y + "px)";
193 movingPiece.style.transitionDuration = "0.2s";
194 movingPiece.style.zIndex = "3000";
196 for (let i=0; i<squares.length; i++)
197 squares.item(i).style.zIndex = "auto";
198 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
202 play: function(move, receive, noanimate) {
203 const navigate = !move;
204 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
205 // (except if we receive opponent's move, human or computer)
206 if (!navigate && !this.analyze && !receive
207 && this.cursor < this.moves.length-1)
213 if (this.cursor == this.moves.length-1)
214 return; //no more moves
215 move = this.moves[this.cursor+1];
217 if (!!receive && !noanimate) //opponent move, variant != "Dark"
219 if (this.cursor < this.moves.length-1)
220 this.gotoEnd(); //required to play the move
221 return this.animateMove(move);
225 move.color = this.vr.turn;
226 move.notation = this.vr.getNotation(move);
228 // Not programmatic, or animation is over
231 this.lastMove = move;
232 if (this.st.settings.sound == 2)
233 new Audio("/sounds/move.mp3").play().catch(err => {});
236 move.fen = this.vr.getFen();
237 if (this.score == "*" || this.analyze)
239 // Stack move on movesList at current cursor
240 if (this.cursor == this.moves.length)
241 this.moves.push(move);
243 this.moves = this.moves.slice(0,this.cursor).concat([move]);
247 this.$emit("newmove", move); //post-processing (e.g. computer play)
248 // Is opponent in check?
249 this.incheck = this.vr.getCheckSquares(this.vr.turn);
250 const score = this.vr.getCurrentScore();
257 // Just show score on screen (allow undo)
258 const message = this.getScoreMessage(score);
259 this.showEndgameMsg(score + " . " + message);
263 undo: function(move) {
264 const navigate = !move;
268 return; //no more moves
269 move = this.moves[this.cursor];
273 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
274 if (this.st.settings.sound == 2)
275 new Audio("/sounds/undo.mp3").play().catch(err => {});
276 this.incheck = this.vr.getCheckSquares(this.vr.turn);
280 gotoMove: function(index) {
281 this.vr.re_init(this.moves[index].fen);
283 this.lastMove = this.moves[index];
285 gotoBegin: function() {
286 this.vr.re_init(this.game.fenStart);
288 this.lastMove = null;
290 gotoEnd: function() {
291 this.gotoMove(this.moves.length-1);
294 this.orientation = V.GetNextCol(this.orientation);