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