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