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