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="game.mode=='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.section-content(v-if="showFen && !!vr")
20 p#fenString.text-center {{ vr.getFen() }}
21 #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";
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();
63 showMoves: function() {
65 //return window.innerWidth >= 768;
68 return this.game.vname != "Dark" || this.score != "*";
71 return this.game.mode == "analyze";
75 if (!!this.game.fenStart)
76 this.re_setVariables();
79 re_setVariables: function() {
80 this.endgameMessage = "";
81 this.orientation = this.game.mycolor || "w"; //default orientation for observed games
82 this.score = this.game.score || "*"; //mutable (if initially "*")
83 this.moves = JSON.parse(JSON.stringify(this.game.moves || []));
84 const L = this.moves.length;
86 this.lastMove = (L > 0 ? this.moves[L-1] : null);
88 download: function() {
89 const content = this.getPgn();
90 // Prepare and trigger download link
91 let downloadAnchor = document.getElementById("download");
92 downloadAnchor.setAttribute("download", "game.pgn");
93 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
94 downloadAnchor.click();
98 pgn += '[Site "vchess.club"]\n';
99 pgn += '[Variant "' + this.game.vname + '"]\n';
100 pgn += '[Date "' + getDate(new Date()) + '"]\n';
101 pgn += '[White "' + this.game.players[0] + '"]\n';
102 pgn += '[Black "' + this.game.players[1] + '"]\n';
103 pgn += '[Fen "' + this.game.fenStart + '"]\n';
104 pgn += '[Result "' + this.score + '"]\n\n';
107 while (i < this.moves.length)
109 pgn += (counter++) + ".";
110 for (let color of ["w","b"])
113 while (i < this.moves.length && this.moves[i].color == color)
114 move += this.moves[i++].notation[0] + ",";
115 move = move.slice(0,-1); //remove last comma
116 pgn += move + (i < this.moves.length-1 ? " " : "");
121 getScoreMessage: function(score) {
122 let eogMessage = "Undefined";
126 eogMessage = this.st.tr["White win"];
129 eogMessage = this.st.tr["Black win"];
132 eogMessage = this.st.tr["Draw"];
135 eogMessage = this.st.tr["Unfinished"];
140 showEndgameMsg: function(message) {
141 this.endgameMessage = message;
142 let modalBox = document.getElementById("modalEog");
143 modalBox.checked = true;
144 setTimeout(() => { modalBox.checked = false; }, 2000);
146 endGame: function(score, message) {
149 message = this.getScoreMessage(score);
150 this.showEndgameMsg(score + " . " + message);
151 this.$emit("gameover", score);
153 animateMove: function(move) {
154 let startSquare = document.getElementById(getSquareId(move.start));
155 let endSquare = document.getElementById(getSquareId(move.end));
156 let rectStart = startSquare.getBoundingClientRect();
157 let rectEnd = endSquare.getBoundingClientRect();
158 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
160 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
161 // HACK for animation (with positive translate, image slides "under background")
162 // Possible improvement: just alter squares on the piece's way...
163 const squares = document.getElementsByClassName("board");
164 for (let i=0; i<squares.length; i++)
166 let square = squares.item(i);
167 if (square.id != getSquareId(move.start))
168 square.style.zIndex = "-1";
170 movingPiece.style.transform = "translate(" + translation.x + "px," +
171 translation.y + "px)";
172 movingPiece.style.transitionDuration = "0.2s";
173 movingPiece.style.zIndex = "3000";
175 for (let i=0; i<squares.length; i++)
176 squares.item(i).style.zIndex = "auto";
177 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
181 play: function(move, receive, noanimate) {
182 const navigate = !move;
183 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
184 // (except if we receive opponent's move, human or computer)
185 if (!navigate && !this.analyze && !receive
186 && this.cursor < this.moves.length-1)
192 if (this.cursor == this.moves.length-1)
193 return; //no more moves
194 move = this.moves[this.cursor+1];
196 if (!!receive && !noanimate) //opponent move, variant != "Dark"
198 if (this.cursor < this.moves.length-1)
199 this.gotoEnd(); //required to play the move
200 return this.animateMove(move);
204 move.color = this.vr.turn;
205 move.notation = this.vr.getNotation(move);
207 // Not programmatic, or animation is over
210 this.lastMove = move;
211 if (this.st.settings.sound == 2)
212 new Audio("/sounds/move.mp3").play().catch(err => {});
215 move.fen = this.vr.getFen();
216 if (this.score == "*" || this.analyze)
218 // Stack move on movesList at current cursor
219 if (this.cursor == this.moves.length)
220 this.moves.push(move);
222 this.moves = this.moves.slice(0,this.cursor).concat([move]);
225 // Is opponent in check? (TODO: generalize, find all check squares)
226 this.incheck = this.vr.getCheckSquares(this.vr.turn);
227 const score = this.vr.getCurrentScore();
228 if (score != "*") //TODO: generalize score for 3 or 4 players
234 // Just show score on screen (allow undo)
235 const message = this.getScoreMessage(score);
236 this.showEndgameMsg(score + " . " + message);
240 this.$emit("newmove", move); //post-processing (e.g. computer play)
242 undo: function(move) {
243 const navigate = !move;
247 return; //no more moves
248 move = this.moves[this.cursor];
252 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
253 if (this.st.settings.sound == 2)
254 new Audio("/sounds/undo.mp3").play().catch(err => {});
255 this.incheck = this.vr.getCheckSquares(this.vr.turn);
259 gotoMove: function(index) {
260 this.vr.re_init(this.moves[index].fen);
262 this.lastMove = this.moves[index];
264 gotoBegin: function() {
265 this.vr.re_init(this.game.fenStart);
267 this.lastMove = null;
269 gotoEnd: function() {
270 this.gotoMove(this.moves.length-1);
273 this.orientation = V.GetNextCol(this.orientation);