Commit | Line | Data |
---|---|---|
a6088c90 | 1 | <template lang="pug"> |
e71161fb | 2 | div#baseGame |
7e355d68 | 3 | input#modalEog.modal(type="checkbox") |
6808d7a1 BA |
4 | div#eogDiv( |
5 | role="dialog" | |
6 | data-checkbox="modalEog" | |
7 | ) | |
9a3049f3 | 8 | .card.text-center |
7e355d68 | 9 | label.modal-close(for="modalEog") |
9a3049f3 | 10 | h3.section {{ endgameMessage }} |
cf94b843 BA |
11 | #gameContainer |
12 | #boardContainer | |
6808d7a1 | 13 | Board( |
a6836242 | 14 | ref="board" |
6808d7a1 BA |
15 | :vr="vr" |
16 | :last-move="lastMove" | |
20620465 BA |
17 | :analyze="game.mode=='analyze'" |
18 | :score="game.score" | |
6808d7a1 BA |
19 | :user-color="game.mycolor" |
20 | :orientation="orientation" | |
21 | :vname="game.vname" | |
22 | :incheck="incheck" | |
23 | @play-move="play" | |
24 | ) | |
20620465 | 25 | #turnIndicator(v-if="showTurn") {{ turn }} |
b1e46b33 | 26 | #controls.button-group |
b9a5fe01 BA |
27 | button(@click="gotoBegin()") |
28 | img.inline(src="/images/icons/fast-forward_rev.svg") | |
29 | button(@click="undo()") | |
30 | img.inline(src="/images/icons/play_rev.svg") | |
31 | button(v-if="canFlip" @click="flip()") | |
32 | img.inline(src="/images/icons/flip.svg") | |
54ec15eb BA |
33 | button( |
34 | @click="runAutoplay()" | |
35 | :class="{'in-autoplay': autoplay}" | |
36 | ) | |
37 | img.inline(src="/images/icons/autoplay.svg") | |
b9a5fe01 BA |
38 | button(@click="play()") |
39 | img.inline(src="/images/icons/play.svg") | |
40 | button(@click="gotoEnd()") | |
41 | img.inline(src="/images/icons/fast-forward.svg") | |
cf94b843 | 42 | #movesList |
6808d7a1 | 43 | MoveList( |
933fd1f9 | 44 | :show="showMoves" |
feaf1bf7 BA |
45 | :canAnalyze="canAnalyze" |
46 | :canDownload="allowDownloadPGN" | |
6808d7a1 BA |
47 | :score="game.score" |
48 | :message="game.scoreMsg" | |
49 | :firstNum="firstMoveNumber" | |
50 | :moves="moves" | |
51 | :cursor="cursor" | |
feaf1bf7 BA |
52 | @download="download" |
53 | @showrules="showRules" | |
54 | @analyze="analyzePosition" | |
6808d7a1 BA |
55 | @goto-move="gotoMove" |
56 | ) | |
41c80bb6 | 57 | .clearer |
a6088c90 BA |
58 | </template> |
59 | ||
60 | <script> | |
61 | import Board from "@/components/Board.vue"; | |
f21cd6d9 | 62 | import MoveList from "@/components/MoveList.vue"; |
a6088c90 BA |
63 | import { store } from "@/store"; |
64 | import { getSquareId } from "@/utils/squareId"; | |
d4036efe | 65 | import { getDate } from "@/utils/datetime"; |
602d6bef | 66 | import { processModalClick } from "@/utils/modalClick"; |
77c50966 | 67 | import { getScoreMessage } from "@/utils/scoring"; |
e71161fb BA |
68 | import { getFullNotation } from "@/utils/notation"; |
69 | import { undoMove } from "@/utils/playUndo"; | |
a6088c90 | 70 | export default { |
6808d7a1 | 71 | name: "my-base-game", |
a6088c90 BA |
72 | components: { |
73 | Board, | |
6808d7a1 | 74 | MoveList |
a6088c90 | 75 | }, |
e71161fb | 76 | props: ["game"], |
a6088c90 BA |
77 | data: function() { |
78 | return { | |
79 | st: store.state, | |
b7c32f1a | 80 | // NOTE: all following variables must be reset at the beginning of a game |
e71161fb | 81 | vr: null, //VariantRules object, game state |
a6088c90 BA |
82 | endgameMessage: "", |
83 | orientation: "w", | |
84 | score: "*", //'*' means 'unfinished' | |
b7c32f1a | 85 | moves: [], |
a6088c90 BA |
86 | cursor: -1, //index of the move just played |
87 | lastMove: null, | |
5157ce0b | 88 | firstMoveNumber: 0, //for printing |
e71161fb | 89 | incheck: [], //for Board |
57eb158f | 90 | inMultimove: false, |
54ec15eb BA |
91 | autoplay: false, |
92 | autoplayLoop: null, | |
57eb158f BA |
93 | inPlay: false, |
94 | stackToPlay: [] | |
a6088c90 BA |
95 | }; |
96 | }, | |
97 | computed: { | |
98 | showMoves: function() { | |
933fd1f9 BA |
99 | return this.game.score != "*" |
100 | ? "all" | |
101 | : (this.vr ? this.vr.showMoves : "none"); | |
20620465 BA |
102 | }, |
103 | showTurn: function() { | |
71ef1664 BA |
104 | return ( |
105 | this.game.score == '*' && | |
d54f6261 | 106 | !!this.vr && this.vr.showTurn |
71ef1664 | 107 | ); |
a6088c90 | 108 | }, |
58ae6be5 | 109 | turn: function() { |
d54f6261 | 110 | if (!this.vr) return ""; |
71ef1664 | 111 | if (this.vr.showMoves != "all") |
d54f6261 | 112 | return this.st.tr[(this.vr.turn == 'w' ? "White" : "Black") + " to move"]; |
71ef1664 BA |
113 | // Cannot flip: racing king or circular chess |
114 | return this.vr.movesCount == 0 && this.game.mycolor == "w" | |
115 | ? this.st.tr["It's your turn!"] | |
a13cbc0f | 116 | : ""; |
58ae6be5 | 117 | }, |
20620465 | 118 | canAnalyze: function() { |
933fd1f9 | 119 | return this.game.mode != "analyze" && this.vr && this.vr.canAnalyze; |
20620465 | 120 | }, |
71ef1664 BA |
121 | canFlip: function() { |
122 | return this.vr && this.vr.canFlip; | |
123 | }, | |
20620465 | 124 | allowDownloadPGN: function() { |
933fd1f9 | 125 | return this.game.score != "*" || (this.vr && this.vr.showMoves == "all"); |
6808d7a1 | 126 | } |
a6088c90 | 127 | }, |
4b0384fa | 128 | created: function() { |
b1e46b33 | 129 | if (!!this.game.fenStart) this.re_setVariables(); |
4b0384fa | 130 | }, |
cf94b843 | 131 | mounted: function() { |
e71161fb BA |
132 | if (!("ontouchstart" in window)) { |
133 | // Desktop browser: | |
134 | const baseGameDiv = document.getElementById("baseGame"); | |
135 | baseGameDiv.tabIndex = 0; | |
136 | baseGameDiv.addEventListener("click", this.focusBg); | |
137 | baseGameDiv.addEventListener("keydown", this.handleKeys); | |
138 | baseGameDiv.addEventListener("wheel", this.handleScroll); | |
139 | } | |
42a92848 BA |
140 | document.getElementById("eogDiv") |
141 | .addEventListener("click", processModalClick); | |
cf94b843 | 142 | }, |
54ec15eb BA |
143 | beforeDestroy: function() { |
144 | if (!!this.autoplayLoop) clearInterval(this.autoplayLoop); | |
145 | }, | |
a6088c90 | 146 | methods: { |
9ca1e26b | 147 | focusBg: function() { |
9ca1e26b BA |
148 | document.getElementById("baseGame").focus(); |
149 | }, | |
150 | handleKeys: function(e) { | |
6808d7a1 BA |
151 | if ([32, 37, 38, 39, 40].includes(e.keyCode)) e.preventDefault(); |
152 | switch (e.keyCode) { | |
9ca1e26b BA |
153 | case 37: |
154 | this.undo(); | |
155 | break; | |
156 | case 39: | |
157 | this.play(); | |
158 | break; | |
5701c228 | 159 | case 38: |
9ca1e26b BA |
160 | this.gotoBegin(); |
161 | break; | |
162 | case 40: | |
163 | this.gotoEnd(); | |
164 | break; | |
165 | case 32: | |
9ca1e26b BA |
166 | this.flip(); |
167 | break; | |
168 | } | |
169 | }, | |
dcd68c41 | 170 | handleScroll: function(e) { |
e71161fb BA |
171 | e.preventDefault(); |
172 | if (e.deltaY < 0) this.undo(); | |
173 | else if (e.deltaY > 0) this.play(); | |
dcd68c41 | 174 | }, |
0e16cb26 BA |
175 | showRules: function() { |
176 | //this.$router.push("/variants/" + this.game.vname); | |
177 | window.open("#/variants/" + this.game.vname, "_blank"); //better | |
178 | }, | |
b1e46b33 BA |
179 | re_setVariables: function(game) { |
180 | if (!game) game = this.game; //in case of... | |
4b0384fa | 181 | this.endgameMessage = ""; |
8477e53d | 182 | // "w": default orientation for observed games |
b1e46b33 BA |
183 | this.orientation = game.mycolor || "w"; |
184 | this.moves = JSON.parse(JSON.stringify(game.moves || [])); | |
e71161fb | 185 | // Post-processing: decorate each move with notation and FEN |
b1e46b33 BA |
186 | this.vr = new V(game.fenStart); |
187 | const parsedFen = V.ParseFen(game.fenStart); | |
8477e53d BA |
188 | const firstMoveColor = parsedFen.turn; |
189 | this.firstMoveNumber = Math.floor(parsedFen.movesCount / 2); | |
f54f4c26 | 190 | let L = this.moves.length; |
d4036efe | 191 | this.moves.forEach(move => { |
e71161fb BA |
192 | // Strategy working also for multi-moves: |
193 | if (!Array.isArray(move)) move = [move]; | |
f54f4c26 | 194 | move.forEach((m,idx) => { |
e71161fb BA |
195 | m.notation = this.vr.getNotation(m); |
196 | this.vr.play(m); | |
f54f4c26 BA |
197 | if (idx < L - 1 && this.vr.getCheckSquares(this.vr.turn).length > 0) |
198 | m.notation += "+"; | |
e71161fb | 199 | }); |
d4036efe | 200 | }); |
8477e53d | 201 | if (firstMoveColor == "b") { |
311cba76 | 202 | // 'start' & 'end' is required for Board component |
6808d7a1 | 203 | this.moves.unshift({ |
6808d7a1 | 204 | notation: "...", |
311cba76 | 205 | start: { x: -1, y: -1 }, |
3a2a7b5f BA |
206 | end: { x: -1, y: -1 }, |
207 | fen: game.fenStart | |
6808d7a1 | 208 | }); |
f54f4c26 | 209 | L++; |
697ee580 | 210 | } |
e71161fb | 211 | this.positionCursorTo(this.moves.length - 1); |
9ef63965 | 212 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
f54f4c26 | 213 | const score = this.vr.getCurrentScore(); |
3f22c2c3 BA |
214 | if (L > 0 && this.moves[L - 1].notation != "...") { |
215 | if (["1-0","0-1"].includes(score)) | |
216 | this.moves[L - 1].notation += "#"; | |
217 | else if (this.vr.getCheckSquares(this.vr.turn).length > 0) | |
218 | this.moves[L - 1].notation += "+"; | |
219 | } | |
4b0384fa | 220 | }, |
e71161fb BA |
221 | positionCursorTo: function(index) { |
222 | this.cursor = index; | |
223 | // Caution: last move in moves array might be a multi-move | |
224 | if (index >= 0) { | |
225 | if (Array.isArray(this.moves[index])) { | |
226 | const L = this.moves[index].length; | |
227 | this.lastMove = this.moves[index][L - 1]; | |
228 | } else { | |
229 | this.lastMove = this.moves[index]; | |
230 | } | |
3a2a7b5f | 231 | } else this.lastMove = null; |
e71161fb | 232 | }, |
63ca2b89 | 233 | analyzePosition: function() { |
7ba4a5bc | 234 | let newUrl = |
6808d7a1 BA |
235 | "/analyse/" + |
236 | this.game.vname + | |
237 | "/?fen=" + | |
238 | this.vr.getFen().replace(/ /g, "_"); | |
7ba4a5bc BA |
239 | if (this.game.mycolor) |
240 | newUrl += "&side=" + this.game.mycolor; | |
910d631b | 241 | // Open in same tab in live games (against cheating) |
6808d7a1 | 242 | if (this.game.type == "live") this.$router.push(newUrl); |
910d631b | 243 | else window.open("#" + newUrl); |
603b8a8b | 244 | }, |
a6088c90 BA |
245 | download: function() { |
246 | const content = this.getPgn(); | |
247 | // Prepare and trigger download link | |
248 | let downloadAnchor = document.getElementById("download"); | |
249 | downloadAnchor.setAttribute("download", "game.pgn"); | |
6808d7a1 BA |
250 | downloadAnchor.href = |
251 | "data:text/plain;charset=utf-8," + encodeURIComponent(content); | |
a6088c90 BA |
252 | downloadAnchor.click(); |
253 | }, | |
254 | getPgn: function() { | |
255 | let pgn = ""; | |
256 | pgn += '[Site "vchess.club"]\n'; | |
834c202a | 257 | pgn += '[Variant "' + this.game.vname + '"]\n'; |
a6088c90 | 258 | pgn += '[Date "' + getDate(new Date()) + '"]\n'; |
d4036efe BA |
259 | pgn += '[White "' + this.game.players[0].name + '"]\n'; |
260 | pgn += '[Black "' + this.game.players[1].name + '"]\n'; | |
834c202a | 261 | pgn += '[Fen "' + this.game.fenStart + '"]\n'; |
430a2038 | 262 | pgn += '[Result "' + this.game.score + '"]\n\n'; |
e71161fb BA |
263 | for (let i = 0; i < this.moves.length; i += 2) { |
264 | pgn += (i/2+1) + "." + getFullNotation(this.moves[i]) + " "; | |
265 | if (i+1 < this.moves.length) | |
266 | pgn += getFullNotation(this.moves[i+1]) + " "; | |
a6088c90 BA |
267 | } |
268 | return pgn + "\n"; | |
269 | }, | |
b988c726 BA |
270 | showEndgameMsg: function(message) { |
271 | this.endgameMessage = message; | |
aae89b49 | 272 | document.getElementById("modalEog").checked = true; |
a6088c90 | 273 | }, |
54ec15eb BA |
274 | runAutoplay: function() { |
275 | const infinitePlay = () => { | |
276 | if (this.cursor == this.moves.length - 1) { | |
277 | clearInterval(this.autoplayLoop); | |
278 | this.autoplayLoop = null; | |
279 | this.autoplay = false; | |
280 | return; | |
281 | } | |
282 | if (this.inPlay || this.inMultimove) | |
283 | // Wait next tick | |
284 | return; | |
285 | this.play(); | |
286 | }; | |
287 | if (this.autoplay) { | |
288 | this.autoplay = false; | |
289 | clearInterval(this.autoplayLoop); | |
290 | this.autoplayLoop = null; | |
291 | } else { | |
292 | this.autoplay = true; | |
293 | infinitePlay(); | |
294 | this.autoplayLoop = setInterval(infinitePlay, 1500); | |
295 | } | |
296 | }, | |
e71161fb | 297 | // Animate an elementary move |
63ca2b89 | 298 | animateMove: function(move, callback) { |
a6088c90 | 299 | let startSquare = document.getElementById(getSquareId(move.start)); |
f9c36b2d | 300 | if (!startSquare) return; //shouldn't happen but... |
a6088c90 BA |
301 | let endSquare = document.getElementById(getSquareId(move.end)); |
302 | let rectStart = startSquare.getBoundingClientRect(); | |
303 | let rectEnd = endSquare.getBoundingClientRect(); | |
6808d7a1 BA |
304 | let translation = { |
305 | x: rectEnd.x - rectStart.x, | |
306 | y: rectEnd.y - rectStart.y | |
307 | }; | |
308 | let movingPiece = document.querySelector( | |
309 | "#" + getSquareId(move.start) + " > img.piece" | |
310 | ); | |
efdfb4c7 BA |
311 | // For some unknown reasons Opera get "movingPiece == null" error |
312 | // TOOO: is it calling 'animate()' twice ? One extra time ? | |
313 | if (!movingPiece) return; | |
a6088c90 BA |
314 | // HACK for animation (with positive translate, image slides "under background") |
315 | // Possible improvement: just alter squares on the piece's way... | |
316 | const squares = document.getElementsByClassName("board"); | |
6808d7a1 | 317 | for (let i = 0; i < squares.length; i++) { |
a6088c90 | 318 | let square = squares.item(i); |
6808d7a1 | 319 | if (square.id != getSquareId(move.start)) square.style.zIndex = "-1"; |
a6088c90 | 320 | } |
6808d7a1 BA |
321 | movingPiece.style.transform = |
322 | "translate(" + translation.x + "px," + translation.y + "px)"; | |
910d631b | 323 | movingPiece.style.transitionDuration = "0.25s"; |
a6088c90 | 324 | movingPiece.style.zIndex = "3000"; |
6808d7a1 BA |
325 | setTimeout(() => { |
326 | for (let i = 0; i < squares.length; i++) | |
a6088c90 BA |
327 | squares.item(i).style.zIndex = "auto"; |
328 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap | |
63ca2b89 | 329 | callback(); |
a6088c90 BA |
330 | }, 250); |
331 | }, | |
8055eabd BA |
332 | // For Analyse mode: |
333 | emitFenIfAnalyze: function() { | |
334 | if (this.game.mode == "analyze") { | |
335 | this.$emit( | |
336 | "fenchange", | |
f1c9d707 | 337 | !!this.lastMove ? this.lastMove.fen : this.game.fenStart |
8055eabd BA |
338 | ); |
339 | } | |
340 | }, | |
e71161fb | 341 | // "light": if gotoMove() or gotoEnd() |
57eb158f | 342 | play: function(move, received, light, noemit) { |
a6836242 BA |
343 | // Freeze while choices are shown: |
344 | if (this.$refs["board"].choices.length > 0) return; | |
57eb158f BA |
345 | if (!!noemit) { |
346 | if (this.inPlay) { | |
347 | // Received moves in observed games can arrive too fast: | |
348 | this.stackToPlay.unshift(move); | |
349 | return; | |
350 | } | |
351 | this.inPlay = true; | |
352 | } | |
9d54ab89 | 353 | const navigate = !move; |
e71161fb | 354 | const playSubmove = (smove) => { |
fbd68f75 | 355 | smove.notation = this.vr.getNotation(smove); |
e71161fb | 356 | this.vr.play(smove); |
f14572c4 | 357 | this.lastMove = smove; |
fbd68f75 | 358 | if (!this.inMultimove) { |
54ec15eb BA |
359 | // Condition is "!navigate" but we mean "!this.autoplay" |
360 | if (!navigate) { | |
361 | if (this.cursor < this.moves.length - 1) | |
362 | this.moves = this.moves.slice(0, this.cursor + 1); | |
363 | this.moves.push(smove); | |
364 | } | |
fbd68f75 BA |
365 | this.inMultimove = true; //potentially |
366 | this.cursor++; | |
54ec15eb | 367 | } else if (!navigate) { |
fbd68f75 BA |
368 | // Already in the middle of a multi-move |
369 | const L = this.moves.length; | |
370 | if (!Array.isArray(this.moves[L-1])) | |
371 | this.$set(this.moves, L-1, [this.moves[L-1], smove]); | |
372 | else | |
373 | this.$set(this.moves, L-1, this.moves.concat([smove])); | |
e71161fb BA |
374 | } |
375 | }; | |
376 | const playMove = () => { | |
54ec15eb BA |
377 | const animate = ( |
378 | V.ShowMoves == "all" && | |
379 | (this.autoplay || !!received) | |
380 | ); | |
e71161fb BA |
381 | if (!Array.isArray(move)) move = [move]; |
382 | let moveIdx = 0; | |
383 | let self = this; | |
384 | const initurn = this.vr.turn; | |
385 | (function executeMove() { | |
386 | const smove = move[moveIdx++]; | |
387 | if (animate) { | |
388 | self.animateMove(smove, () => { | |
389 | playSubmove(smove); | |
390 | if (moveIdx < move.length) | |
391 | setTimeout(executeMove, 500); | |
392 | else afterMove(smove, initurn); | |
393 | }); | |
394 | } else { | |
395 | playSubmove(smove); | |
396 | if (moveIdx < move.length) executeMove(); | |
397 | else afterMove(smove, initurn); | |
398 | } | |
399 | })(); | |
400 | }; | |
fbd68f75 BA |
401 | const computeScore = () => { |
402 | const score = this.vr.getCurrentScore(); | |
f54f4c26 BA |
403 | if (!navigate) { |
404 | if (["1-0","0-1"].includes(score)) | |
405 | this.lastMove.notation += "#"; | |
406 | else if (this.vr.getCheckSquares(this.vr.turn).length > 0) | |
407 | this.lastMove.notation += "+"; | |
408 | } | |
fbd68f75 BA |
409 | if (score != "*" && this.game.mode == "analyze") { |
410 | const message = getScoreMessage(score); | |
411 | // Just show score on screen (allow undo) | |
412 | this.showEndgameMsg(score + " . " + this.st.tr[message]); | |
413 | } | |
414 | return score; | |
415 | }; | |
e71161fb | 416 | const afterMove = (smove, initurn) => { |
e71161fb BA |
417 | if (this.vr.turn != initurn) { |
418 | // Turn has changed: move is complete | |
3a2a7b5f | 419 | if (!smove.fen) |
cc00b83c BA |
420 | // NOTE: only FEN of last sub-move is required (thus setting it here) |
421 | smove.fen = this.vr.getFen(); | |
b0a0468a BA |
422 | // Is opponent in check? |
423 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
3a2a7b5f | 424 | this.emitFenIfAnalyze(); |
e71161fb | 425 | this.inMultimove = false; |
f54f4c26 | 426 | this.score = computeScore(); |
54ec15eb | 427 | if (this.game.mode != "analyze" && !navigate) { |
f54f4c26 | 428 | if (!noemit) { |
5aa14a21 | 429 | // Post-processing (e.g. computer play). |
f54f4c26 | 430 | const L = this.moves.length; |
5aa14a21 BA |
431 | // NOTE: always emit the score, even in unfinished, |
432 | // to tell Game::processMove() that it's not a received move. | |
f54f4c26 BA |
433 | this.$emit("newmove", this.moves[L-1], { score: this.score }); |
434 | } else { | |
57eb158f BA |
435 | this.inPlay = false; |
436 | if (this.stackToPlay.length > 0) | |
437 | // Move(s) arrived in-between | |
438 | this.play(this.stackToPlay.pop(), received, light, noemit); | |
439 | } | |
e71161fb BA |
440 | } |
441 | } | |
442 | }; | |
443 | // NOTE: navigate and received are mutually exclusive | |
444 | if (navigate) { | |
445 | // The move to navigate to is necessarily full: | |
446 | if (this.cursor == this.moves.length - 1) return; //no more moves | |
447 | move = this.moves[this.cursor + 1]; | |
54ec15eb BA |
448 | if (!this.autoplay) { |
449 | // Just play the move: | |
450 | if (!Array.isArray(move)) move = [move]; | |
451 | for (let i=0; i < move.length; i++) this.vr.play(move[i]); | |
452 | if (!light) { | |
453 | this.lastMove = move[move.length-1]; | |
454 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
455 | this.score = computeScore(); | |
456 | this.emitFenIfAnalyze(); | |
457 | } | |
458 | this.cursor++; | |
459 | return; | |
8055eabd | 460 | } |
e71161fb | 461 | } |
63ca2b89 BA |
462 | // Forbid playing outside analyze mode, except if move is received. |
463 | // Sufficient condition because Board already knows which turn it is. | |
6808d7a1 | 464 | if ( |
6808d7a1 | 465 | this.game.mode != "analyze" && |
dcb3637c | 466 | !navigate && |
e71161fb | 467 | !received && |
6808d7a1 BA |
468 | (this.game.score != "*" || this.cursor < this.moves.length - 1) |
469 | ) { | |
a6088c90 BA |
470 | return; |
471 | } | |
e71161fb BA |
472 | // To play a received move, cursor must be at the end of the game: |
473 | if (received && this.cursor < this.moves.length - 1) | |
474 | this.gotoEnd(); | |
475 | playMove(); | |
476 | }, | |
477 | cancelCurrentMultimove: function() { | |
e71161fb BA |
478 | const L = this.moves.length; |
479 | let move = this.moves[L-1]; | |
480 | if (!Array.isArray(move)) move = [move]; | |
481 | for (let i=move.length -1; i >= 0; i--) this.vr.undo(move[i]); | |
482 | this.moves.pop(); | |
483 | this.cursor--; | |
484 | this.inMultimove = false; | |
485 | }, | |
486 | cancelLastMove: function() { | |
487 | // The last played move was canceled (corr game) | |
488 | this.undo(); | |
489 | this.moves.pop(); | |
490 | }, | |
491 | // "light": if gotoMove() or gotoBegin() | |
492 | undo: function(move, light) { | |
a6836242 BA |
493 | // Freeze while choices are shown: |
494 | if (this.$refs["board"].choices.length > 0) return; | |
e71161fb BA |
495 | if (this.inMultimove) { |
496 | this.cancelCurrentMultimove(); | |
63ca2b89 | 497 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
e71161fb BA |
498 | } else { |
499 | if (!move) { | |
3a2a7b5f BA |
500 | const minCursor = |
501 | this.moves.length > 0 && this.moves[0].notation == "..." | |
502 | ? 1 | |
503 | : 0; | |
504 | if (this.cursor < minCursor) return; //no more moves | |
e71161fb BA |
505 | move = this.moves[this.cursor]; |
506 | } | |
e71161fb BA |
507 | undoMove(move, this.vr); |
508 | if (light) this.cursor--; | |
509 | else { | |
510 | this.positionCursorTo(this.cursor - 1); | |
e71161fb | 511 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
8055eabd | 512 | this.emitFenIfAnalyze(); |
63ca2b89 | 513 | } |
a6088c90 | 514 | } |
a6088c90 BA |
515 | }, |
516 | gotoMove: function(index) { | |
a6836242 | 517 | if (this.$refs["board"].choices.length > 0) return; |
e71161fb BA |
518 | if (this.inMultimove) this.cancelCurrentMultimove(); |
519 | if (index == this.cursor) return; | |
520 | if (index < this.cursor) { | |
521 | while (this.cursor > index) | |
522 | this.undo(null, null, "light"); | |
523 | } | |
524 | else { | |
525 | // index > this.cursor) | |
526 | while (this.cursor < index) | |
527 | this.play(null, null, "light"); | |
528 | } | |
529 | // NOTE: next line also re-assign cursor, but it's very light | |
530 | this.positionCursorTo(index); | |
8b405c81 | 531 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
8055eabd | 532 | this.emitFenIfAnalyze(); |
a6088c90 BA |
533 | }, |
534 | gotoBegin: function() { | |
a6836242 | 535 | if (this.$refs["board"].choices.length > 0) return; |
e71161fb | 536 | if (this.inMultimove) this.cancelCurrentMultimove(); |
3a2a7b5f BA |
537 | const minCursor = |
538 | this.moves.length > 0 && this.moves[0].notation == "..." | |
539 | ? 1 | |
540 | : 0; | |
541 | while (this.cursor >= minCursor) this.undo(null, null, "light"); | |
542 | this.lastMove = (minCursor == 1 ? this.moves[0] : null); | |
543 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
8055eabd | 544 | this.emitFenIfAnalyze(); |
a6088c90 BA |
545 | }, |
546 | gotoEnd: function() { | |
a6836242 | 547 | if (this.$refs["board"].choices.length > 0) return; |
6808d7a1 BA |
548 | if (this.cursor == this.moves.length - 1) return; |
549 | this.gotoMove(this.moves.length - 1); | |
8055eabd | 550 | this.emitFenIfAnalyze(); |
a6088c90 BA |
551 | }, |
552 | flip: function() { | |
a6836242 | 553 | if (this.$refs["board"].choices.length > 0) return; |
0e16cb26 | 554 | this.orientation = V.GetOppCol(this.orientation); |
6808d7a1 BA |
555 | } |
556 | } | |
a6088c90 BA |
557 | }; |
558 | </script> | |
72ccbd67 | 559 | |
41c80bb6 | 560 | <style lang="sass" scoped> |
9a3049f3 BA |
561 | [type="checkbox"]#modalEog+div .card |
562 | min-height: 45px | |
910d631b | 563 | |
cf94b843 BA |
564 | #baseGame |
565 | width: 100% | |
4f518610 BA |
566 | &:focus |
567 | outline: none | |
cf94b843 BA |
568 | |
569 | #gameContainer | |
72ccbd67 BA |
570 | margin-left: auto |
571 | margin-right: auto | |
cf94b843 | 572 | |
ed06d9e9 BA |
573 | #downloadDiv |
574 | display: inline-block | |
575 | ||
72ccbd67 | 576 | #controls |
28b32b4f | 577 | user-select: none |
72ccbd67 | 578 | button |
b1e46b33 | 579 | border: none |
72ccbd67 | 580 | margin: 0 |
feaf1bf7 BA |
581 | padding-top: 5px |
582 | padding-bottom: 5px | |
583 | ||
54ec15eb BA |
584 | .in-autoplay |
585 | background-color: #FACF8C | |
586 | ||
feaf1bf7 | 587 | img.inline |
54ec15eb | 588 | height: 22px |
feaf1bf7 BA |
589 | padding-top: 5px |
590 | @media screen and (max-width: 767px) | |
591 | height: 18px | |
910d631b | 592 | |
0e16cb26 BA |
593 | #turnIndicator |
594 | text-align: center | |
29bc61be | 595 | font-weight: bold |
910d631b | 596 | |
72ccbd67 | 597 | #boardContainer |
cf94b843 | 598 | float: left |
41c80bb6 BA |
599 | // TODO: later, maybe, allow movesList of variable width |
600 | // or e.g. between 250 and 350px (but more complicated) | |
910d631b | 601 | |
cf94b843 BA |
602 | #movesList |
603 | width: 280px | |
604 | float: left | |
910d631b | 605 | |
96e9585a BA |
606 | @media screen and (max-width: 767px) |
607 | #movesList | |
608 | width: 100% | |
609 | float: none | |
610 | clear: both | |
72ccbd67 | 611 | </style> |