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