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