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;
68 // Switched to analyze mode: game is over
74 showMoves: function() {
76 //return window.innerWidth >= 768;
79 return this.vname != "Dark" || this.score != "*";
83 setEndgameMessage: function(score) {
84 let eogMessage = "Undefined";
88 eogMessage = translations["White win"];
91 eogMessage = translations["Black win"];
94 eogMessage = translations["Draw"];
97 eogMessage = "Unfinished";
100 this.endgameMessage = eogMessage;
102 download: function() {
103 const content = this.getPgn();
104 // Prepare and trigger download link
105 let downloadAnchor = document.getElementById("download");
106 downloadAnchor.setAttribute("download", "game.pgn");
107 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
108 downloadAnchor.click();
112 pgn += '[Site "vchess.club"]\n';
113 pgn += '[Variant "' + this.vname + '"]\n';
114 pgn += '[Date "' + getDate(new Date()) + '"]\n';
115 pgn += '[White "' + this.players[0] + '"]\n';
116 pgn += '[Black "' + this.players[1] + '"]\n';
117 pgn += '[Fen "' + this.fenStart + '"]\n';
118 pgn += '[Result "' + this.score + '"]\n\n';
121 while (i < this.moves.length)
123 pgn += (counter++) + ".";
124 for (let color of ["w","b"])
127 while (i < this.moves.length && this.moves[i].color == color)
128 move += this.moves[i++].notation[0] + ",";
129 move = move.slice(0,-1); //remove last comma
130 pgn += move + (i < this.moves.length-1 ? " " : "");
135 showScoreMsg: function(score) {
136 this.setEndgameMessage(score);
137 let modalBox = document.getElementById("modalEog");
138 modalBox.checked = true;
139 setTimeout(() => { modalBox.checked = false; }, 2000);
141 endGame: function(score) {
143 this.showScoreMsg(score);
144 this.$emit("game-over");
146 animateMove: function(move) {
147 let startSquare = document.getElementById(getSquareId(move.start));
148 let endSquare = document.getElementById(getSquareId(move.end));
149 let rectStart = startSquare.getBoundingClientRect();
150 let rectEnd = endSquare.getBoundingClientRect();
151 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
153 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
154 // HACK for animation (with positive translate, image slides "under background")
155 // Possible improvement: just alter squares on the piece's way...
156 const squares = document.getElementsByClassName("board");
157 for (let i=0; i<squares.length; i++)
159 let square = squares.item(i);
160 if (square.id != getSquareId(move.start))
161 square.style.zIndex = "-1";
163 movingPiece.style.transform = "translate(" + translation.x + "px," +
164 translation.y + "px)";
165 movingPiece.style.transitionDuration = "0.2s";
166 movingPiece.style.zIndex = "3000";
168 for (let i=0; i<squares.length; i++)
169 squares.item(i).style.zIndex = "auto";
170 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
174 play: function(move, programmatic) {
175 let navigate = !move;
176 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
177 // (except if we receive opponent's move, human or computer)
178 if (!navigate && !this.analyze && !programmatic
179 && this.cursor < this.moves.length-1)
185 if (this.cursor == this.moves.length-1)
186 return; //no more moves
187 move = this.moves[this.cursor+1];
189 if (!!programmatic) //computer or (remote) human opponent
191 if (this.cursor < this.moves.length-1)
192 this.gotoEnd(); //required to play the move
193 return this.animateMove(move);
195 // Not programmatic, or animation is over
197 move.notation = this.vr.getNotation(move);
199 move.color = this.vr.turn;
202 this.lastMove = move;
204 move.fen = this.vr.getFen();
205 if (this.st.settings.sound == 2)
206 new Audio("/sounds/move.mp3").play().catch(err => {});
207 if (!navigate && (this.score == "*" || this.analyze))
209 // Stack move on movesList at current cursor
210 if (this.cursor == this.moves.length)
211 this.moves.push(move);
213 this.moves = this.moves.slice(0,this.cursor).concat([move]);
215 // Is opponent in check?
216 this.incheck = this.vr.getCheckSquares(this.vr.turn);
217 const score = this.vr.getCurrentScore();
222 else //just show score on screen (allow undo)
223 this.showScoreMsg(score);
226 this.$emit("newmove", move); //post-processing (e.g. computer play)
228 undo: function(move) {
229 let navigate = !move;
233 return; //no more moves
234 move = this.moves[this.cursor];
238 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
239 if (this.st.settings.sound == 2)
240 new Audio("/sounds/undo.mp3").play().catch(err => {});
241 this.incheck = this.vr.getCheckSquares(this.vr.turn);
245 gotoMove: function(index) {
246 this.vr.re_init(this.moves[index].fen);
248 this.lastMove = this.moves[index];
250 gotoBegin: function() {
251 this.vr.re_init(this.fenStart);
253 this.lastMove = null;
255 gotoEnd: function() {
256 this.gotoMove(this.moves.length-1);
259 this.orientation = V.GetNextCol(this.orientation);