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