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"
10 :user-color="gameInfo.mycolor" :orientation="orientation"
11 :vname="vname" @play-move="play")
13 button(@click="() => play()") Play
14 button(@click="() => undo()") Undo
15 button(@click="flip") Flip
16 button(@click="gotoBegin") GotoBegin
17 button(@click="gotoEnd") GotoEnd
18 #fenDiv.section-content(v-if="showFen && !!vr")
19 p#fenString.text-center {{ vr.getFen() }}
20 #pgnDiv.section-content
23 button#downloadBtn(@click="download") {{ st.tr["Download PGN"] }}
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";
41 // "vr": VariantRules object, describing the game state + rules
42 // fenStart, players, mycolor
43 props: ["vname","analyze","vr","gameInfo"],
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 // gameInfo (immutable once set) changes when a new game starts
58 "gameInfo": function() {
59 // Reset all variables
60 this.endgameMessage = "";
61 this.orientation = this.gameInfo.mycolor || "w"; //default orientation for observed games
62 this.score = this.gameInfo.score; //mutable (if initially "*")
63 this.moves = this.gameInfo.moves; //TODO: this is mutable; make a copy instead
64 const L = this.moves.length;
66 this.lastMove = (L > 0 ? this.moves[L-1] : null);
71 // Switched to analyze mode: game is over
77 showMoves: function() {
79 //return window.innerWidth >= 768;
82 return this.vname != "Dark" || this.score != "*";
86 setEndgameMessage: function(score) {
87 let eogMessage = "Undefined";
91 eogMessage = translations["White win"];
94 eogMessage = translations["Black win"];
97 eogMessage = translations["Draw"];
100 eogMessage = "Unfinished";
103 this.endgameMessage = eogMessage;
105 download: function() {
106 const content = this.getPgn();
107 // Prepare and trigger download link
108 let downloadAnchor = document.getElementById("download");
109 downloadAnchor.setAttribute("download", "game.pgn");
110 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
111 downloadAnchor.click();
115 pgn += '[Site "vchess.club"]\n';
116 pgn += '[Variant "' + this.vname + '"]\n';
117 pgn += '[Date "' + getDate(new Date()) + '"]\n';
118 pgn += '[White "' + this.gameInfo.players[0] + '"]\n';
119 pgn += '[Black "' + this.gameInfo.players[1] + '"]\n';
120 pgn += '[Fen "' + this.gameInfo.fenStart + '"]\n';
121 pgn += '[Result "' + this.score + '"]\n\n';
124 while (i < this.moves.length)
126 pgn += (counter++) + ".";
127 for (let color of ["w","b"])
130 while (i < this.moves.length && this.moves[i].color == color)
131 move += this.moves[i++].notation[0] + ",";
132 move = move.slice(0,-1); //remove last comma
133 pgn += move + (i < this.moves.length-1 ? " " : "");
138 showScoreMsg: function(score) {
139 this.setEndgameMessage(score);
140 let modalBox = document.getElementById("modalEog");
141 modalBox.checked = true;
142 setTimeout(() => { modalBox.checked = false; }, 2000);
144 endGame: function(score) {
146 this.showScoreMsg(score);
147 this.$emit("game-over");
149 animateMove: function(move) {
150 let startSquare = document.getElementById(getSquareId(move.start));
151 let endSquare = document.getElementById(getSquareId(move.end));
152 let rectStart = startSquare.getBoundingClientRect();
153 let rectEnd = endSquare.getBoundingClientRect();
154 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
156 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
157 // HACK for animation (with positive translate, image slides "under background")
158 // Possible improvement: just alter squares on the piece's way...
159 const squares = document.getElementsByClassName("board");
160 for (let i=0; i<squares.length; i++)
162 let square = squares.item(i);
163 if (square.id != getSquareId(move.start))
164 square.style.zIndex = "-1";
166 movingPiece.style.transform = "translate(" + translation.x + "px," +
167 translation.y + "px)";
168 movingPiece.style.transitionDuration = "0.2s";
169 movingPiece.style.zIndex = "3000";
171 for (let i=0; i<squares.length; i++)
172 squares.item(i).style.zIndex = "auto";
173 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
177 play: function(move, programmatic) {
178 let navigate = !move;
179 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
180 // (except if we receive opponent's move, human or computer)
181 if (!navigate && !this.analyze && !programmatic
182 && this.cursor < this.moves.length-1)
188 if (this.cursor == this.moves.length-1)
189 return; //no more moves
190 move = this.moves[this.cursor+1];
192 if (!!programmatic) //computer or (remote) human opponent
194 if (this.cursor < this.moves.length-1)
195 this.gotoEnd(); //required to play the move
196 return this.animateMove(move);
198 // Not programmatic, or animation is over
200 move.notation = this.vr.getNotation(move);
202 move.color = this.vr.turn;
205 this.lastMove = move;
207 move.fen = this.vr.getFen();
208 if (this.st.settings.sound == 2)
209 new Audio("/sounds/move.mp3").play().catch(err => {});
210 if (!navigate && (this.score == "*" || this.analyze))
212 // Stack move on movesList at current cursor
213 if (this.cursor == this.moves.length)
214 this.moves.push(move);
216 this.moves = this.moves.slice(0,this.cursor).concat([move]);
218 // Is opponent in check?
219 this.incheck = this.vr.getCheckSquares(this.vr.turn);
220 const score = this.vr.getCurrentScore();
225 else //just show score on screen (allow undo)
226 this.showScoreMsg(score);
229 this.$emit("newmove", move); //post-processing (e.g. computer play)
231 undo: function(move) {
232 let navigate = !move;
236 return; //no more moves
237 move = this.moves[this.cursor];
241 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
242 if (this.st.settings.sound == 2)
243 new Audio("/sounds/undo.mp3").play().catch(err => {});
244 this.incheck = this.vr.getCheckSquares(this.vr.turn);
248 gotoMove: function(index) {
249 this.vr.re_init(this.moves[index].fen);
251 this.lastMove = this.moves[index];
253 gotoBegin: function() {
254 this.vr.re_init(this.fenStart);
256 this.lastMove = null;
258 gotoEnd: function() {
259 this.gotoMove(this.moves.length-1);
262 this.orientation = V.GetNextCol(this.orientation);