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