Commit | Line | Data |
---|---|---|
a6088c90 | 1 | <template lang="pug"> |
9ddaf8da | 2 | div#baseGame(tabindex=-1 @click="focusBg()" |
9a3049f3 | 3 | @keydown="handleKeys($event)" @wheel="handleScroll($event)") |
7e355d68 | 4 | input#modalEog.modal(type="checkbox") |
9a3049f3 BA |
5 | div#eogDiv(role="dialog" data-checkbox="modalEog") |
6 | .card.text-center | |
7e355d68 | 7 | label.modal-close(for="modalEog") |
9a3049f3 | 8 | h3.section {{ endgameMessage }} |
602d6bef | 9 | input#modalAdjust.modal(type="checkbox") |
9a3049f3 BA |
10 | div#adjuster(role="dialog" data-checkbox="modalAdjust") |
11 | .card.text-center | |
602d6bef | 12 | label.modal-close(for="modalAdjust") |
9a3049f3 | 13 | label(for="boardSize") {{ st.tr["Board size"] }} |
602d6bef | 14 | input#boardSize.slider(type="range" min="0" max="100" value="50" |
9a3049f3 | 15 | @input="adjustBoard()") |
cf94b843 BA |
16 | #gameContainer |
17 | #boardContainer | |
4f518610 | 18 | Board(:vr="vr" :last-move="lastMove" :analyze="analyze" |
cf94b843 | 19 | :user-color="game.mycolor" :orientation="orientation" |
f51c37cf | 20 | :vname="game.vname" :incheck="incheck" @play-move="play") |
4f518610 | 21 | #turnIndicator(v-if="game.vname=='Dark' && game.score=='*'") |
0e16cb26 | 22 | | {{ turn }} |
cf94b843 | 23 | #controls |
9ddaf8da BA |
24 | button(@click="gotoBegin()") << |
25 | button(@click="undo()") < | |
26 | button(@click="flip()") ⇅ | |
27 | button(@click="play()") > | |
28 | button(@click="gotoEnd()") >> | |
cf94b843 | 29 | #pgnDiv |
ed06d9e9 | 30 | #downloadDiv(v-if="game.vname!='Dark' || game.score!='*'") |
4f518610 | 31 | a#download(href="#") |
9ddaf8da | 32 | button(@click="download()") {{ st.tr["Download"] }} PGN |
602d6bef | 33 | button(onClick="doClick('modalAdjust')") ⤢ |
0e16cb26 | 34 | button(v-if="game.vname!='Dark' && game.mode!='analyze'" |
9ddaf8da | 35 | @click="analyzePosition()") |
677fe285 | 36 | | {{ st.tr["Analyse"] }} |
4f518610 | 37 | // NOTE: rather ugly hack to avoid showing twice "rules" link... |
9ddaf8da | 38 | button(v-if="!$route.path.match('/variants/')" @click="showRules()") |
4f518610 | 39 | | {{ st.tr["Rules"] }} |
cf94b843 | 40 | #movesList |
430a2038 | 41 | MoveList(v-if="showMoves" :score="game.score" :message="game.scoreMsg" |
5157ce0b BA |
42 | :firstNum="firstMoveNumber" :moves="moves" :cursor="cursor" |
43 | @goto-move="gotoMove") | |
41c80bb6 | 44 | .clearer |
a6088c90 BA |
45 | </template> |
46 | ||
47 | <script> | |
48 | import Board from "@/components/Board.vue"; | |
f21cd6d9 | 49 | import MoveList from "@/components/MoveList.vue"; |
a6088c90 BA |
50 | import { store } from "@/store"; |
51 | import { getSquareId } from "@/utils/squareId"; | |
d4036efe | 52 | import { getDate } from "@/utils/datetime"; |
602d6bef | 53 | import { processModalClick } from "@/utils/modalClick"; |
77c50966 | 54 | import { getScoreMessage } from "@/utils/scoring"; |
a6088c90 BA |
55 | export default { |
56 | name: 'my-base-game', | |
57 | components: { | |
58 | Board, | |
f21cd6d9 | 59 | MoveList, |
a6088c90 | 60 | }, |
bc8734ba | 61 | // "vr": VariantRules object, describing the game state + rules |
834c202a | 62 | props: ["vr","game"], |
a6088c90 BA |
63 | data: function() { |
64 | return { | |
65 | st: store.state, | |
b7c32f1a | 66 | // NOTE: all following variables must be reset at the beginning of a game |
a6088c90 BA |
67 | endgameMessage: "", |
68 | orientation: "w", | |
69 | score: "*", //'*' means 'unfinished' | |
b7c32f1a | 70 | moves: [], |
a6088c90 BA |
71 | cursor: -1, //index of the move just played |
72 | lastMove: null, | |
5157ce0b | 73 | firstMoveNumber: 0, //for printing |
f51c37cf | 74 | incheck: [], //for Board |
a6088c90 BA |
75 | }; |
76 | }, | |
37cdcbf3 | 77 | watch: { |
834c202a BA |
78 | // game initial FEN changes when a new game starts |
79 | "game.fenStart": function() { | |
4b0384fa | 80 | this.re_setVariables(); |
37cdcbf3 | 81 | }, |
7e355d68 | 82 | // Received a new move to play: |
63ca2b89 BA |
83 | "game.moveToPlay": function(newMove) { |
84 | if (!!newMove) //if stop + launch new game, get undefined move | |
85 | this.play(newMove, "receive"); | |
7e355d68 | 86 | }, |
37cdcbf3 | 87 | }, |
a6088c90 BA |
88 | computed: { |
89 | showMoves: function() { | |
4f518610 | 90 | return this.game.vname != "Dark" || this.game.score != "*"; |
a6088c90 | 91 | }, |
0e16cb26 BA |
92 | turn: function() { |
93 | let color = ""; | |
94 | const L = this.moves.length; | |
95 | if (L == 0 || this.moves[L-1].color == "b") | |
96 | color = "White"; | |
97 | else //if (this.moves[L-1].color == "w") | |
98 | color = "Black"; | |
602d6bef | 99 | return this.st.tr[color + " to move"]; |
0e16cb26 | 100 | }, |
4f518610 BA |
101 | analyze: function() { |
102 | return this.game.mode=="analyze" || | |
103 | // From Board viewpoint, a finished Dark game == analyze (TODO: unclear) | |
104 | (this.game.vname == "Dark" && this.game.score != "*"); | |
105 | }, | |
a6088c90 | 106 | }, |
4b0384fa BA |
107 | created: function() { |
108 | if (!!this.game.fenStart) | |
109 | this.re_setVariables(); | |
110 | }, | |
cf94b843 | 111 | mounted: function() { |
602d6bef BA |
112 | [document.getElementById("eogDiv"),document.getElementById("adjuster")] |
113 | .forEach(elt => elt.addEventListener("click", processModalClick)); | |
96e9585a BA |
114 | // Take full width on small screens: |
115 | let boardSize = parseInt(localStorage.getItem("boardSize")); | |
116 | if (!boardSize) | |
117 | { | |
118 | boardSize = (window.innerWidth >= 768 | |
119 | ? Math.min(600, 0.5*window.innerWidth) //heuristic... | |
120 | : window.innerWidth); | |
121 | } | |
122 | const movesWidth = (window.innerWidth >= 768 ? 280 : 0); | |
123 | document.getElementById("boardContainer").style.width = boardSize + "px"; | |
124 | let gameContainer = document.getElementById("gameContainer"); | |
125 | gameContainer.style.width = (boardSize + movesWidth) + "px"; | |
602d6bef BA |
126 | // TODO: find the right formula here: |
127 | //document.getElementById("boardSize").value = Math.floor(boardSize / 10); | |
128 | // timeout to avoid calling too many time the adjust method | |
129 | let timeoutLaunched = false; | |
130 | window.addEventListener("resize", (e) => { | |
131 | if (!timeoutLaunched) | |
132 | { | |
133 | timeoutLaunched = true; | |
134 | setTimeout( () => { | |
135 | this.adjustBoard(); | |
136 | timeoutLaunched = false; | |
137 | }, 500); | |
138 | } | |
139 | }); | |
cf94b843 | 140 | }, |
a6088c90 | 141 | methods: { |
9ca1e26b | 142 | focusBg: function() { |
41c80bb6 | 143 | // NOTE: small blue border appears... |
9ca1e26b BA |
144 | document.getElementById("baseGame").focus(); |
145 | }, | |
602d6bef BA |
146 | adjustBoard: function() { |
147 | const boardContainer = document.getElementById("boardContainer"); | |
148 | if (!boardContainer) | |
149 | return; //no board on page | |
150 | const k = document.getElementById("boardSize").value; | |
151 | const movesWidth = (window.innerWidth >= 768 ? 280 : 0); | |
152 | const minBoardWidth = 240; //TODO: these 240 and 280 are arbitrary... | |
153 | // Value of 0 is board min size; 100 is window.width [- movesWidth] | |
154 | const boardSize = minBoardWidth + | |
155 | k * (window.innerWidth - (movesWidth+minBoardWidth)) / 100; | |
156 | localStorage.setItem("boardSize", boardSize); | |
157 | boardContainer.style.width = boardSize + "px"; | |
158 | document.getElementById("gameContainer").style.width = | |
159 | (boardSize + movesWidth) + "px"; | |
160 | }, | |
9ca1e26b | 161 | handleKeys: function(e) { |
5701c228 BA |
162 | if ([32,37,38,39,40].includes(e.keyCode)) |
163 | e.preventDefault(); | |
9ca1e26b BA |
164 | switch (e.keyCode) |
165 | { | |
166 | case 37: | |
167 | this.undo(); | |
168 | break; | |
169 | case 39: | |
170 | this.play(); | |
171 | break; | |
5701c228 | 172 | case 38: |
9ca1e26b BA |
173 | this.gotoBegin(); |
174 | break; | |
175 | case 40: | |
176 | this.gotoEnd(); | |
177 | break; | |
178 | case 32: | |
9ca1e26b BA |
179 | this.flip(); |
180 | break; | |
181 | } | |
182 | }, | |
dcd68c41 | 183 | handleScroll: function(e) { |
4f518610 BA |
184 | // NOTE: since game.mode=="analyze" => no score, next condition is enough |
185 | if (this.game.score != "*") | |
41c80bb6 BA |
186 | { |
187 | e.preventDefault(); | |
188 | if (e.deltaY < 0) | |
189 | this.undo(); | |
190 | else if (e.deltaY > 0) | |
191 | this.play(); | |
192 | } | |
dcd68c41 | 193 | }, |
0e16cb26 BA |
194 | showRules: function() { |
195 | //this.$router.push("/variants/" + this.game.vname); | |
196 | window.open("#/variants/" + this.game.vname, "_blank"); //better | |
197 | }, | |
4b0384fa BA |
198 | re_setVariables: function() { |
199 | this.endgameMessage = ""; | |
200 | this.orientation = this.game.mycolor || "w"; //default orientation for observed games | |
4b0384fa | 201 | this.moves = JSON.parse(JSON.stringify(this.game.moves || [])); |
d4036efe BA |
202 | // Post-processing: decorate each move with color + current FEN: |
203 | // (to be able to jump to any position quickly) | |
27d18a24 | 204 | let vr_tmp = new V(this.game.fenStart); //vr is already at end of game |
5157ce0b BA |
205 | this.firstMoveNumber = |
206 | Math.floor(V.ParseFen(this.game.fenStart).movesCount / 2); | |
d4036efe BA |
207 | this.moves.forEach(move => { |
208 | // NOTE: this is doing manually what play() function below achieve, | |
209 | // but in a lighter "fast-forward" way | |
27d18a24 BA |
210 | move.color = vr_tmp.turn; |
211 | move.notation = vr_tmp.getNotation(move); | |
212 | vr_tmp.play(move); | |
213 | move.fen = vr_tmp.getFen(); | |
d4036efe | 214 | }); |
697ee580 BA |
215 | if (this.game.fenStart.indexOf(" b ") >= 0 || |
216 | (this.moves.length > 0 && this.moves[0].color == "b")) | |
217 | { | |
218 | // 'end' is required for Board component to check lastMove for e.p. | |
219 | this.moves.unshift({color: "w", notation: "...", end: {x:-1,y:-1}}); | |
220 | } | |
4b0384fa BA |
221 | const L = this.moves.length; |
222 | this.cursor = L-1; | |
223 | this.lastMove = (L > 0 ? this.moves[L-1] : null); | |
e01dff1a | 224 | this.incheck = []; |
4b0384fa | 225 | }, |
63ca2b89 | 226 | analyzePosition: function() { |
677fe285 | 227 | const newUrl = "/analyse/" + this.game.vname + |
63ca2b89 | 228 | "/?fen=" + this.vr.getFen().replace(/ /g, "_"); |
0e16cb26 BA |
229 | if (this.game.type == "live") |
230 | this.$router.push(newUrl); //open in same tab: against cheating... | |
231 | else | |
232 | window.open("#" + newUrl); //open in a new tab: more comfortable | |
603b8a8b | 233 | }, |
a6088c90 BA |
234 | download: function() { |
235 | const content = this.getPgn(); | |
236 | // Prepare and trigger download link | |
237 | let downloadAnchor = document.getElementById("download"); | |
238 | downloadAnchor.setAttribute("download", "game.pgn"); | |
239 | downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content); | |
240 | downloadAnchor.click(); | |
241 | }, | |
242 | getPgn: function() { | |
243 | let pgn = ""; | |
244 | pgn += '[Site "vchess.club"]\n'; | |
834c202a | 245 | pgn += '[Variant "' + this.game.vname + '"]\n'; |
a6088c90 | 246 | pgn += '[Date "' + getDate(new Date()) + '"]\n'; |
d4036efe BA |
247 | pgn += '[White "' + this.game.players[0].name + '"]\n'; |
248 | pgn += '[Black "' + this.game.players[1].name + '"]\n'; | |
834c202a | 249 | pgn += '[Fen "' + this.game.fenStart + '"]\n'; |
430a2038 | 250 | pgn += '[Result "' + this.game.score + '"]\n\n'; |
a6088c90 BA |
251 | let counter = 1; |
252 | let i = 0; | |
253 | while (i < this.moves.length) | |
254 | { | |
255 | pgn += (counter++) + "."; | |
256 | for (let color of ["w","b"]) | |
257 | { | |
258 | let move = ""; | |
259 | while (i < this.moves.length && this.moves[i].color == color) | |
d4036efe | 260 | move += this.moves[i++].notation + ","; |
a6088c90 | 261 | move = move.slice(0,-1); //remove last comma |
d4036efe | 262 | pgn += move + (i < this.moves.length ? " " : ""); |
a6088c90 BA |
263 | } |
264 | } | |
265 | return pgn + "\n"; | |
266 | }, | |
b988c726 BA |
267 | showEndgameMsg: function(message) { |
268 | this.endgameMessage = message; | |
4494c17c | 269 | let modalBox = document.getElementById("modalEog"); |
a6088c90 BA |
270 | modalBox.checked = true; |
271 | setTimeout(() => { modalBox.checked = false; }, 2000); | |
272 | }, | |
63ca2b89 | 273 | animateMove: function(move, callback) { |
a6088c90 | 274 | let startSquare = document.getElementById(getSquareId(move.start)); |
a0c41e7e BA |
275 | // TODO: error "flush nextTick callbacks" when observer reloads page: |
276 | // this late check is not a fix! | |
277 | if (!startSquare) | |
278 | return; | |
a6088c90 BA |
279 | let endSquare = document.getElementById(getSquareId(move.end)); |
280 | let rectStart = startSquare.getBoundingClientRect(); | |
281 | let rectEnd = endSquare.getBoundingClientRect(); | |
282 | let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y}; | |
283 | let movingPiece = | |
284 | document.querySelector("#" + getSquareId(move.start) + " > img.piece"); | |
a0c41e7e BA |
285 | if (!movingPiece) //TODO: shouldn't happen |
286 | return; | |
a6088c90 BA |
287 | // HACK for animation (with positive translate, image slides "under background") |
288 | // Possible improvement: just alter squares on the piece's way... | |
289 | const squares = document.getElementsByClassName("board"); | |
290 | for (let i=0; i<squares.length; i++) | |
291 | { | |
292 | let square = squares.item(i); | |
293 | if (square.id != getSquareId(move.start)) | |
294 | square.style.zIndex = "-1"; | |
295 | } | |
296 | movingPiece.style.transform = "translate(" + translation.x + "px," + | |
297 | translation.y + "px)"; | |
298 | movingPiece.style.transitionDuration = "0.2s"; | |
299 | movingPiece.style.zIndex = "3000"; | |
300 | setTimeout( () => { | |
301 | for (let i=0; i<squares.length; i++) | |
302 | squares.item(i).style.zIndex = "auto"; | |
303 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap | |
63ca2b89 | 304 | callback(); |
a6088c90 BA |
305 | }, 250); |
306 | }, | |
63ca2b89 BA |
307 | play: function(move, receive) { |
308 | // NOTE: navigate and receive are mutually exclusive | |
9d54ab89 | 309 | const navigate = !move; |
63ca2b89 BA |
310 | // Forbid playing outside analyze mode, except if move is received. |
311 | // Sufficient condition because Board already knows which turn it is. | |
312 | if (!navigate && this.game.mode!="analyze" && !receive | |
4f518610 | 313 | && (this.game.score != "*" || this.cursor < this.moves.length-1)) |
a6088c90 BA |
314 | { |
315 | return; | |
316 | } | |
63ca2b89 BA |
317 | const doPlayMove = () => { |
318 | if (!!receive && this.cursor < this.moves.length-1) | |
a6088c90 | 319 | this.gotoEnd(); //required to play the move |
63ca2b89 BA |
320 | if (navigate) |
321 | { | |
322 | if (this.cursor == this.moves.length-1) | |
323 | return; //no more moves | |
324 | move = this.moves[this.cursor+1]; | |
325 | } | |
326 | else | |
9d54ab89 | 327 | { |
63ca2b89 BA |
328 | move.color = this.vr.turn; |
329 | move.notation = this.vr.getNotation(move); | |
330 | } | |
331 | this.vr.play(move); | |
332 | this.cursor++; | |
333 | this.lastMove = move; | |
334 | if (this.st.settings.sound == 2) | |
335 | new Audio("/sounds/move.mp3").play().catch(err => {}); | |
336 | if (!navigate) | |
337 | { | |
338 | move.fen = this.vr.getFen(); | |
9d54ab89 BA |
339 | // Stack move on movesList at current cursor |
340 | if (this.cursor == this.moves.length) | |
341 | this.moves.push(move); | |
342 | else | |
343 | this.moves = this.moves.slice(0,this.cursor).concat([move]); | |
344 | } | |
63ca2b89 BA |
345 | // Is opponent in check? |
346 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
347 | const score = this.vr.getCurrentScore(); | |
348 | if (score != "*") | |
349 | { | |
77c50966 | 350 | const message = getScoreMessage(score); |
63ca2b89 BA |
351 | if (this.game.mode != "analyze") |
352 | this.$emit("gameover", score, message); | |
353 | else //just show score on screen (allow undo) | |
354 | this.showEndgameMsg(score + " . " + message); | |
355 | } | |
4f518610 BA |
356 | if (!navigate && this.game.mode!="analyze") |
357 | this.$emit("newmove", move); //post-processing (e.g. computer play) | |
63ca2b89 BA |
358 | }; |
359 | if (!!receive && this.game.vname != "Dark") | |
360 | this.animateMove(move, doPlayMove); | |
361 | else | |
362 | doPlayMove(); | |
a6088c90 BA |
363 | }, |
364 | undo: function(move) { | |
9d54ab89 | 365 | const navigate = !move; |
a6088c90 BA |
366 | if (navigate) |
367 | { | |
368 | if (this.cursor < 0) | |
369 | return; //no more moves | |
370 | move = this.moves[this.cursor]; | |
371 | } | |
372 | this.vr.undo(move); | |
373 | this.cursor--; | |
374 | this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined); | |
375 | if (this.st.settings.sound == 2) | |
376 | new Audio("/sounds/undo.mp3").play().catch(err => {}); | |
377 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
4494c17c | 378 | if (!navigate) |
a6088c90 BA |
379 | this.moves.pop(); |
380 | }, | |
381 | gotoMove: function(index) { | |
37cdcbf3 | 382 | this.vr.re_init(this.moves[index].fen); |
a6088c90 BA |
383 | this.cursor = index; |
384 | this.lastMove = this.moves[index]; | |
385 | }, | |
386 | gotoBegin: function() { | |
5157ce0b BA |
387 | if (this.cursor == -1) |
388 | return; | |
834c202a | 389 | this.vr.re_init(this.game.fenStart); |
5701c228 BA |
390 | if (this.moves.length > 0 && this.moves[0].notation == "...") |
391 | { | |
392 | this.cursor = 0; | |
393 | this.lastMove = this.moves[0]; | |
394 | } | |
395 | else | |
396 | { | |
397 | this.cursor = -1; | |
398 | this.lastMove = null; | |
399 | } | |
a6088c90 BA |
400 | }, |
401 | gotoEnd: function() { | |
5157ce0b BA |
402 | if (this.cursor == this.moves.length - 1) |
403 | return; | |
a6088c90 BA |
404 | this.gotoMove(this.moves.length-1); |
405 | }, | |
406 | flip: function() { | |
0e16cb26 | 407 | this.orientation = V.GetOppCol(this.orientation); |
a6088c90 BA |
408 | }, |
409 | }, | |
410 | }; | |
411 | </script> | |
72ccbd67 | 412 | |
41c80bb6 | 413 | <style lang="sass" scoped> |
9a3049f3 BA |
414 | [type="checkbox"]#modalEog+div .card |
415 | min-height: 45px | |
416 | [type="checkbox"]#modalAdjust+div .card | |
417 | padding: 5px | |
418 | ||
cf94b843 BA |
419 | #baseGame |
420 | width: 100% | |
4f518610 BA |
421 | &:focus |
422 | outline: none | |
cf94b843 BA |
423 | |
424 | #gameContainer | |
72ccbd67 BA |
425 | margin-left: auto |
426 | margin-right: auto | |
cf94b843 | 427 | |
ed06d9e9 BA |
428 | #downloadDiv |
429 | display: inline-block | |
430 | ||
72ccbd67 BA |
431 | #controls |
432 | margin-top: 10px | |
cf94b843 BA |
433 | margin-left: auto |
434 | margin-right: auto | |
72ccbd67 BA |
435 | button |
436 | display: inline-block | |
437 | width: 20% | |
438 | margin: 0 | |
41c80bb6 BA |
439 | @media screen and (min-width: 768px) |
440 | #controls | |
441 | max-width: 400px | |
0e16cb26 BA |
442 | #turnIndicator |
443 | text-align: center | |
cf94b843 BA |
444 | #pgnDiv |
445 | text-align: center | |
446 | margin-left: auto | |
447 | margin-right: auto | |
72ccbd67 | 448 | #boardContainer |
cf94b843 | 449 | float: left |
41c80bb6 BA |
450 | // TODO: later, maybe, allow movesList of variable width |
451 | // or e.g. between 250 and 350px (but more complicated) | |
cf94b843 BA |
452 | #movesList |
453 | width: 280px | |
454 | float: left | |
96e9585a BA |
455 | @media screen and (max-width: 767px) |
456 | #movesList | |
457 | width: 100% | |
458 | float: none | |
459 | clear: both | |
72ccbd67 | 460 | </style> |