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