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