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