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 Board(:vr="vr" :last-move="lastMove" :analyze="analyze" :user-color="mycolor"
10 :orientation="orientation" :vname="vname" @play-move="play")
12 button(@click="() => play()") Play
13 button(@click="() => undo()") Undo
14 button(@click="flip") Flip
15 button(@click="gotoBegin") GotoBegin
16 button(@click="gotoEnd") GotoEnd
17 #fenDiv.section-content(v-if="showFen && !!vr")
18 p#fenString.text-center {{ vr.getFen() }}
19 #pgnDiv.section-content
22 button#downloadBtn(@click="download") {{ st.tr["Download PGN"] }}
24 //MoveList(v-if="showMoves"
25 :moves="moves" :cursor="cursor" @goto-move="gotoMove")
29 import Board from "@/components/Board.vue";
30 //import MoveList from "@/components/MoveList.vue";
31 import { store } from "@/store";
32 import { getSquareId } from "@/utils/squareId";
40 // "vr": VariantRules object, describing the game state + rules
41 props: ["vname","analyze","vr","fenStart","players","mycolor"],
45 // NOTE: all following variables must be reset at the beginning of a game
48 score: "*", //'*' means 'unfinished'
50 cursor: -1, //index of the move just played
55 // fenStart changes when a new game starts
56 fenStart: function() {
57 // Reset all variables
58 this.endgameMessage = "";
59 this.orientation = this.mycolor;
67 showMoves: function() {
69 //return window.innerWidth >= 768;
72 return this.vname != "Dark" || this.score != "*";
76 setEndgameMessage: function(score) {
77 let eogMessage = "Undefined";
81 eogMessage = translations["White win"];
84 eogMessage = translations["Black win"];
87 eogMessage = translations["Draw"];
90 eogMessage = "Unfinished";
93 this.endgameMessage = eogMessage;
95 download: function() {
96 const content = this.getPgn();
97 // Prepare and trigger download link
98 let downloadAnchor = document.getElementById("download");
99 downloadAnchor.setAttribute("download", "game.pgn");
100 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
101 downloadAnchor.click();
105 pgn += '[Site "vchess.club"]\n';
106 pgn += '[Variant "' + this.vname + '"]\n';
107 pgn += '[Date "' + getDate(new Date()) + '"]\n';
108 pgn += '[White "' + this.players[0] + '"]\n';
109 pgn += '[Black "' + this.players[1] + '"]\n';
110 pgn += '[Fen "' + this.fenStart + '"]\n';
111 pgn += '[Result "' + this.score + '"]\n\n';
114 while (i < this.moves.length)
116 pgn += (counter++) + ".";
117 for (let color of ["w","b"])
120 while (i < this.moves.length && this.moves[i].color == color)
121 move += this.moves[i++].notation[0] + ",";
122 move = move.slice(0,-1); //remove last comma
123 pgn += move + (i < this.moves.length-1 ? " " : "");
128 showScoreMsg: function(score) {
129 this.setEndgameMessage(score);
130 let modalBox = document.getElementById("modal-eog");
131 modalBox.checked = true;
132 setTimeout(() => { modalBox.checked = false; }, 2000);
134 endGame: function(score) {
136 this.showScoreMsg(score);
137 this.$emit("game-over");
139 animateMove: function(move) {
140 let startSquare = document.getElementById(getSquareId(move.start));
141 let endSquare = document.getElementById(getSquareId(move.end));
142 let rectStart = startSquare.getBoundingClientRect();
143 let rectEnd = endSquare.getBoundingClientRect();
144 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
146 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
147 // HACK for animation (with positive translate, image slides "under background")
148 // Possible improvement: just alter squares on the piece's way...
149 const squares = document.getElementsByClassName("board");
150 for (let i=0; i<squares.length; i++)
152 let square = squares.item(i);
153 if (square.id != getSquareId(move.start))
154 square.style.zIndex = "-1";
156 movingPiece.style.transform = "translate(" + translation.x + "px," +
157 translation.y + "px)";
158 movingPiece.style.transitionDuration = "0.2s";
159 movingPiece.style.zIndex = "3000";
161 for (let i=0; i<squares.length; i++)
162 squares.item(i).style.zIndex = "auto";
163 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
167 play: function(move, programmatic) {
168 let navigate = !move;
169 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
170 // (except if we receive opponent's move, human or computer)
171 if (!navigate && !this.analyze && !programmatic
172 && this.cursor < this.moves.length-1)
178 if (this.cursor == this.moves.length-1)
179 return; //no more moves
180 move = this.moves[this.cursor+1];
182 if (!!programmatic) //computer or (remote) human opponent
184 if (this.cursor < this.moves.length-1)
185 this.gotoEnd(); //required to play the move
186 return this.animateMove(move);
188 // Not programmatic, or animation is over
190 move.notation = this.vr.getNotation(move);
192 move.color = this.vr.turn;
195 this.lastMove = move;
197 move.fen = this.vr.getFen();
198 if (this.st.settings.sound == 2)
199 new Audio("/sounds/move.mp3").play().catch(err => {});
200 if (!navigate && (this.score == "*" || this.analyze))
202 // Stack move on movesList at current cursor
203 if (this.cursor == this.moves.length)
204 this.moves.push(move);
206 this.moves = this.moves.slice(0,this.cursor).concat([move]);
208 // Is opponent in check?
209 this.incheck = this.vr.getCheckSquares(this.vr.turn);
210 const score = this.vr.getCurrentScore();
215 else //just show score on screen (allow undo)
216 this.showScoreMsg(score);
218 this.$emit("newmove", move); //post-processing (e.g. computer play)
220 undo: function(move) {
221 let navigate = !move;
225 return; //no more moves
226 move = this.moves[this.cursor];
230 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
231 if (this.st.settings.sound == 2)
232 new Audio("/sounds/undo.mp3").play().catch(err => {});
233 this.incheck = this.vr.getCheckSquares(this.vr.turn);
234 if (this.analyze) //TODO: can this happen?
237 gotoMove: function(index) {
238 this.vr.re_init(this.moves[index].fen);
240 this.lastMove = this.moves[index];
242 gotoBegin: function() {
243 this.vr.re_init(this.fenStart);
245 this.lastMove = null;
247 gotoEnd: function() {
248 this.gotoMove(this.moves.length-1);
251 this.orientation = V.GetNextCol(this.orientation);