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="variant.name" @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 props: ["variant","analyze","players"],
44 vr: null, //VariantRules object, describing the game state + rules
47 score: "*", //'*' means 'unfinished'
48 // userColor: given by gameId, or fen in problems mode (if no game Id)...
51 moves: [], //all moves played in current game
52 cursor: -1, //index of the move just played
57 showMoves: function() {
59 //return window.innerWidth >= 768;
62 return this.variant.name != "Dark" || this.score != "*";
66 setEndgameMessage: function(score) {
67 let eogMessage = "Undefined";
71 eogMessage = translations["White win"];
74 eogMessage = translations["Black win"];
77 eogMessage = translations["Draw"];
80 eogMessage = "Unfinished";
83 this.endgameMessage = eogMessage;
85 download: function() {
86 const content = this.getPgn();
87 // Prepare and trigger download link
88 let downloadAnchor = document.getElementById("download");
89 downloadAnchor.setAttribute("download", "game.pgn");
90 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
91 downloadAnchor.click();
95 pgn += '[Site "vchess.club"]\n';
96 pgn += '[Variant "' + this.variant.name + '"]\n';
97 pgn += '[Date "' + getDate(new Date()) + '"]\n';
98 pgn += '[White "' + this.players[0] + '"]\n';
99 pgn += '[Black "' + this.players[1] + '"]\n';
100 pgn += '[Fen "' + this.fenStart + '"]\n';
101 pgn += '[Result "' + this.score + '"]\n\n';
104 while (i < this.moves.length)
106 pgn += (counter++) + ".";
107 for (let color of ["w","b"])
110 while (i < this.moves.length && this.moves[i].color == color)
111 move += this.moves[i++].notation[0] + ",";
112 move = move.slice(0,-1); //remove last comma
113 pgn += move + (i < this.moves.length-1 ? " " : "");
118 showScoreMsg: function(score) {
119 this.setEndgameMessage(score);
120 let modalBox = document.getElementById("modal-eog");
121 modalBox.checked = true;
122 setTimeout(() => { modalBox.checked = false; }, 2000);
124 endGame: function(score) {
126 this.showScoreMsg(score);
127 this.$emit("game-over");
129 animateMove: function(move) {
130 let startSquare = document.getElementById(getSquareId(move.start));
131 let endSquare = document.getElementById(getSquareId(move.end));
132 let rectStart = startSquare.getBoundingClientRect();
133 let rectEnd = endSquare.getBoundingClientRect();
134 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
136 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
137 // HACK for animation (with positive translate, image slides "under background")
138 // Possible improvement: just alter squares on the piece's way...
139 const squares = document.getElementsByClassName("board");
140 for (let i=0; i<squares.length; i++)
142 let square = squares.item(i);
143 if (square.id != getSquareId(move.start))
144 square.style.zIndex = "-1";
146 movingPiece.style.transform = "translate(" + translation.x + "px," +
147 translation.y + "px)";
148 movingPiece.style.transitionDuration = "0.2s";
149 movingPiece.style.zIndex = "3000";
151 for (let i=0; i<squares.length; i++)
152 squares.item(i).style.zIndex = "auto";
153 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
157 play: function(move, programmatic) {
158 let navigate = !move;
159 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
160 // (except if we receive opponent's move, human or computer)
161 if (!navigate && !this.analyze && !programmatic
162 && this.cursor < this.moves.length-1)
168 if (this.cursor == this.moves.length-1)
169 return; //no more moves
170 move = this.moves[this.cursor+1];
172 if (!!programmatic) //computer or (remote) human opponent
174 if (this.cursor < this.moves.length-1)
175 this.gotoEnd(); //required to play the move
176 return this.animateMove(move);
178 // Not programmatic, or animation is over
180 move.notation = this.vr.getNotation(move);
182 move.color = this.vr.turn;
185 this.lastMove = move;
187 move.fen = this.vr.getFen();
188 if (this.st.settings.sound == 2)
189 new Audio("/sounds/move.mp3").play().catch(err => {});
190 // Send the move to web worker (including his own moves)
191 this.compWorker.postMessage(["newmove",move]);
192 if (!navigate && (this.score == "*" || this.analyze))
194 // Stack move on movesList at current cursor
195 if (this.cursor == this.moves.length)
196 this.moves.push(move);
198 this.moves = this.moves.slice(0,this.cursor).concat([move]);
200 // Is opponent in check?
201 this.incheck = this.vr.getCheckSquares(this.vr.turn);
202 const score = this.vr.getCurrentScore();
207 else //just show score on screen (allow undo)
208 this.showScoreMsg(score);
210 // subTurn condition for Marseille (and Avalanche) rules
211 else if ((this.mode == "computer" && (!this.vr.subTurn || this.vr.subTurn <= 1))
212 && (this.subMode == "auto" || this.vr.turn != this.mycolor))
214 this.playComputerMove();
216 // https://vuejs.org/v2/guide/list.html#Caveats (also for undo)
218 // this.$children[0].$forceUpdate(); //TODO!?
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);
235 this.$children[0].$forceUpdate(); //TODO!?
236 else if (this.analyze) //TODO: can this happen?
239 gotoMove: function(index) {
240 this.vr = new V(this.moves[index].fen);
242 this.lastMove = this.moves[index];
244 gotoBegin: function() {
245 this.vr = new V(this.fenStart);
247 this.lastMove = null;
249 gotoEnd: function() {
250 this.gotoMove(this.moves.length-1);
253 this.orientation = V.GetNextCol(this.orientation);