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