Commit | Line | Data |
---|---|---|
24340cae | 1 | <script> |
e2732923 BA |
2 | import { getSquareId, getSquareFromId } from "@/utils/squareId"; |
3 | import { ArrayFun } from "@/utils/array"; | |
dfeb96ea | 4 | import { store } from "@/store"; |
cf2343ce | 5 | export default { |
6808d7a1 | 6 | name: "my-board", |
cf2343ce BA |
7 | // Last move cannot be guessed from here, and is required to highlight squares |
8 | // vr: object to check moves, print board... | |
93d1d7a7 | 9 | // userColor is left undefined for an external observer |
6808d7a1 BA |
10 | props: [ |
11 | "vr", | |
12 | "lastMove", | |
13 | "analyze", | |
20620465 | 14 | "score", |
6808d7a1 BA |
15 | "incheck", |
16 | "orientation", | |
17 | "userColor", | |
18 | "vname" | |
19 | ], | |
20 | data: function() { | |
cf2343ce | 21 | return { |
cafe0166 | 22 | mobileBrowser: ("ontouchstart" in window), |
cf2343ce BA |
23 | possibleMoves: [], //filled after each valid click/dragstart |
24 | choices: [], //promotion pieces, or checkered captures... (as moves) | |
25 | selectedPiece: null, //moving piece (or clicked piece) | |
28b32b4f BA |
26 | start: null, //pixels coordinates + id of starting square (click or drag) |
27 | click: "", | |
6808d7a1 | 28 | settings: store.state.settings |
cf2343ce BA |
29 | }; |
30 | }, | |
31 | render(h) { | |
6808d7a1 | 32 | if (!this.vr) { |
7b3cf1b7 | 33 | // Return empty div of class 'game' to avoid error when setting size |
6808d7a1 BA |
34 | return h("div", { |
35 | class: { | |
36 | game: true | |
37 | } | |
38 | }); | |
7b3cf1b7 | 39 | } |
6808d7a1 | 40 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
cf2343ce | 41 | // Precompute hints squares to facilitate rendering |
e2732923 | 42 | let hintSquares = ArrayFun.init(sizeX, sizeY, false); |
6808d7a1 BA |
43 | this.possibleMoves.forEach(m => { |
44 | hintSquares[m.end.x][m.end.y] = true; | |
45 | }); | |
cf2343ce | 46 | // Also precompute in-check squares |
e2732923 | 47 | let incheckSq = ArrayFun.init(sizeX, sizeY, false); |
6808d7a1 BA |
48 | this.incheck.forEach(sq => { |
49 | incheckSq[sq[0]][sq[1]] = true; | |
50 | }); | |
06e79b07 | 51 | |
cf2343ce | 52 | const lm = this.lastMove; |
57eb158f BA |
53 | const showLight = ( |
54 | this.settings.highlight && | |
55 | ["all","highlight"].includes(V.ShowMoves) | |
56 | ); | |
311cba76 BA |
57 | const orientation = !V.CanFlip ? "w" : this.orientation; |
58 | // Ensure that squares colors do not change when board is flipped | |
59 | const lightSquareMod = (sizeX + sizeY) % 2; | |
60 | const showPiece = (x, y) => { | |
61 | return ( | |
62 | this.vr.board[x][y] != V.EMPTY && | |
63 | (!this.vr.enlightened || this.analyze || this.score != "*" || | |
64 | (!!this.userColor && this.vr.enlightened[this.userColor][x][y])) | |
65 | ); | |
66 | }; | |
67 | const inHighlight = (x, y) => { | |
68 | return showLight && !!lm && ( | |
69 | (lm.end.x == x && lm.end.y == y) || | |
70 | (lm.start.x == x && lm.start.y == y)); | |
71 | }; | |
72 | const inShadow = (x, y) => { | |
73 | return ( | |
74 | !this.analyze && | |
75 | this.score == "*" && | |
76 | this.vr.enlightened && | |
77 | (!this.userColor || !this.vr.enlightened[this.userColor][x][y]) | |
78 | ); | |
79 | }; | |
80 | // Create board element (+ reserves if needed by variant) | |
9d4a0218 | 81 | let elementArray = []; |
cf2343ce | 82 | const gameDiv = h( |
6808d7a1 | 83 | "div", |
cf2343ce | 84 | { |
6808d7a1 BA |
85 | class: { |
86 | game: true, | |
87 | clearer: true | |
88 | } | |
cf2343ce BA |
89 | }, |
90 | [...Array(sizeX).keys()].map(i => { | |
311cba76 | 91 | const ci = orientation == "w" ? i : sizeX - i - 1; |
cf2343ce | 92 | return h( |
6808d7a1 | 93 | "div", |
cf2343ce | 94 | { |
6808d7a1 BA |
95 | class: { |
96 | row: true | |
cf2343ce | 97 | }, |
6808d7a1 | 98 | style: { opacity: this.choices.length > 0 ? "0.5" : "1" } |
cf2343ce BA |
99 | }, |
100 | [...Array(sizeY).keys()].map(j => { | |
311cba76 | 101 | const cj = orientation == "w" ? j : sizeY - j - 1; |
cf2343ce | 102 | let elems = []; |
311cba76 | 103 | if (showPiece(ci, cj)) { |
cf2343ce | 104 | elems.push( |
6808d7a1 BA |
105 | h("img", { |
106 | class: { | |
107 | piece: true, | |
108 | ghost: | |
109 | !!this.selectedPiece && | |
110 | this.selectedPiece.parentNode.id == "sq-" + ci + "-" + cj | |
111 | }, | |
112 | attrs: { | |
113 | src: | |
114 | "/images/pieces/" + | |
241bf8f2 | 115 | this.vr.getPpath(this.vr.board[ci][cj], this.userColor, this.score) + |
6808d7a1 | 116 | ".svg" |
cf2343ce | 117 | } |
6808d7a1 | 118 | }) |
cf2343ce BA |
119 | ); |
120 | } | |
6808d7a1 | 121 | if (this.settings.hints && hintSquares[ci][cj]) { |
cf2343ce | 122 | elems.push( |
6808d7a1 BA |
123 | h("img", { |
124 | class: { | |
125 | "mark-square": true | |
126 | }, | |
127 | attrs: { | |
128 | src: "/images/mark.svg" | |
cf2343ce | 129 | } |
6808d7a1 | 130 | }) |
cf2343ce BA |
131 | ); |
132 | } | |
311cba76 | 133 | const lightSquare = (ci + cj) % 2 == lightSquareMod; |
cf2343ce | 134 | return h( |
6808d7a1 | 135 | "div", |
cf2343ce | 136 | { |
6808d7a1 BA |
137 | class: { |
138 | board: true, | |
139 | ["board" + sizeY]: true, | |
311cba76 BA |
140 | "light-square": lightSquare, |
141 | "dark-square": !lightSquare, | |
dfeb96ea | 142 | [this.settings.bcolor]: true, |
311cba76 BA |
143 | "in-shadow": inShadow(ci, cj), |
144 | "highlight-light": inHighlight(ci, cj) && lightSquare, | |
145 | "highlight-dark": inHighlight(ci, cj) && !lightSquare, | |
146 | "incheck-light": showLight && lightSquare && incheckSq[ci][cj], | |
147 | "incheck-dark": showLight && !lightSquare && incheckSq[ci][cj] | |
cf2343ce BA |
148 | }, |
149 | attrs: { | |
6808d7a1 BA |
150 | id: getSquareId({ x: ci, y: cj }) |
151 | } | |
cf2343ce BA |
152 | }, |
153 | elems | |
154 | ); | |
155 | }) | |
156 | ); | |
24340cae | 157 | }) |
cf2343ce | 158 | ); |
9d4a0218 BA |
159 | if (!!this.vr.reserve) { |
160 | const playingColor = this.userColor || "w"; //default for an observer | |
6808d7a1 | 161 | const shiftIdx = playingColor == "w" ? 0 : 1; |
cf2343ce | 162 | let myReservePiecesArray = []; |
6808d7a1 | 163 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
9d4a0218 | 164 | const qty = this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]; |
6808d7a1 BA |
165 | myReservePiecesArray.push( |
166 | h( | |
167 | "div", | |
168 | { | |
169 | class: { board: true, ["board" + sizeY]: true }, | |
9d4a0218 BA |
170 | attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) }, |
171 | style: { opacity: qty > 0 ? 1 : 0.35 } | |
6808d7a1 BA |
172 | }, |
173 | [ | |
174 | h("img", { | |
175 | class: { piece: true, reserve: true }, | |
176 | attrs: { | |
177 | src: | |
178 | "/images/pieces/" + | |
241bf8f2 | 179 | this.vr.getReservePpath(i, playingColor) + |
6808d7a1 BA |
180 | ".svg" |
181 | } | |
182 | }), | |
9d4a0218 | 183 | h("sup", { class: { "reserve-count": true } }, [ qty ]) |
6808d7a1 | 184 | ] |
cf2343ce | 185 | ) |
6808d7a1 | 186 | ); |
cf2343ce BA |
187 | } |
188 | let oppReservePiecesArray = []; | |
93d1d7a7 | 189 | const oppCol = V.GetOppCol(playingColor); |
6808d7a1 | 190 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
9d4a0218 | 191 | const qty = this.vr.reserve[oppCol][V.RESERVE_PIECES[i]]; |
6808d7a1 BA |
192 | oppReservePiecesArray.push( |
193 | h( | |
194 | "div", | |
195 | { | |
196 | class: { board: true, ["board" + sizeY]: true }, | |
9d4a0218 BA |
197 | attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) }, |
198 | style: { opacity: qty > 0 ? 1 : 0.35 } | |
6808d7a1 BA |
199 | }, |
200 | [ | |
201 | h("img", { | |
202 | class: { piece: true, reserve: true }, | |
203 | attrs: { | |
204 | src: | |
205 | "/images/pieces/" + | |
241bf8f2 | 206 | this.vr.getReservePpath(i, oppCol) + |
6808d7a1 BA |
207 | ".svg" |
208 | } | |
209 | }), | |
9d4a0218 | 210 | h("sup", { class: { "reserve-count": true } }, [ qty ]) |
6808d7a1 | 211 | ] |
cf2343ce | 212 | ) |
6808d7a1 | 213 | ); |
cf2343ce | 214 | } |
9d4a0218 BA |
215 | const myReserveTop = ( |
216 | (playingColor == 'w' && orientation == 'b') || | |
217 | (playingColor == 'b' && orientation == 'w') | |
cf2343ce | 218 | ); |
9d4a0218 BA |
219 | // Center reserves, assuming same number of pieces for each side: |
220 | const nbReservePieces = myReservePiecesArray.length; | |
221 | const marginLeft = ((100 - nbReservePieces * (100 / sizeY)) / 2) + "%"; | |
222 | const reserveTop = | |
223 | h( | |
224 | "div", | |
225 | { | |
226 | class: { | |
227 | game: true, | |
228 | "reserve-div": true | |
229 | }, | |
230 | style: { | |
231 | "margin-left": marginLeft | |
232 | } | |
233 | }, | |
234 | [ | |
235 | h( | |
236 | "div", | |
237 | { | |
238 | class: { | |
239 | row: true, | |
240 | "reserve-row": true | |
241 | } | |
242 | }, | |
243 | myReserveTop ? myReservePiecesArray : oppReservePiecesArray | |
244 | ) | |
245 | ] | |
246 | ); | |
247 | var reserveBottom = | |
248 | h( | |
249 | "div", | |
250 | { | |
251 | class: { | |
252 | game: true, | |
253 | "reserve-div": true | |
254 | }, | |
255 | style: { | |
256 | "margin-left": marginLeft | |
257 | } | |
258 | }, | |
259 | [ | |
260 | h( | |
261 | "div", | |
262 | { | |
263 | class: { | |
264 | row: true, | |
265 | "reserve-row": true | |
266 | } | |
267 | }, | |
268 | myReserveTop ? oppReservePiecesArray : myReservePiecesArray | |
269 | ) | |
270 | ] | |
271 | ); | |
272 | elementArray.push(reserveTop); | |
cf2343ce | 273 | } |
9d4a0218 BA |
274 | elementArray.push(gameDiv); |
275 | if (!!this.vr.reserve) elementArray.push(reserveBottom); | |
aafe9f16 | 276 | const boardElt = document.querySelector(".game"); |
6808d7a1 | 277 | if (this.choices.length > 0 && !!boardElt) { |
afbf3ca7 | 278 | // No choices to show at first drawing |
aafe9f16 BA |
279 | const squareWidth = boardElt.offsetWidth / sizeY; |
280 | const offset = [boardElt.offsetTop, boardElt.offsetLeft]; | |
afbf3ca7 | 281 | // TODO: multi-rows if more than V.size.y pieces (as inEightpieces) |
aafe9f16 | 282 | const choices = h( |
6808d7a1 | 283 | "div", |
aafe9f16 | 284 | { |
6808d7a1 BA |
285 | attrs: { id: "choices" }, |
286 | class: { row: true }, | |
aafe9f16 | 287 | style: { |
6808d7a1 BA |
288 | top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px", |
289 | left: | |
290 | offset[1] + | |
291 | (squareWidth * (sizeY - this.choices.length)) / 2 + | |
292 | "px", | |
293 | width: this.choices.length * squareWidth + "px", | |
294 | height: squareWidth + "px" | |
295 | } | |
aafe9f16 | 296 | }, |
6808d7a1 | 297 | this.choices.map(m => { |
fabb53f2 BA |
298 | // A "choice" is a move |
299 | const applyMove = (e) => { | |
300 | e.stopPropagation(); | |
301 | this.play(m); | |
302 | this.choices = []; | |
303 | }; | |
304 | const onClick = | |
305 | this.mobileBrowser | |
306 | ? { touchend: applyMove } | |
307 | : { mouseup: applyMove }; | |
6808d7a1 BA |
308 | return h( |
309 | "div", | |
aafe9f16 | 310 | { |
6808d7a1 BA |
311 | class: { |
312 | board: true, | |
313 | ["board" + sizeY]: true | |
aafe9f16 BA |
314 | }, |
315 | style: { | |
6808d7a1 BA |
316 | width: 100 / this.choices.length + "%", |
317 | "padding-bottom": 100 / this.choices.length + "%" | |
318 | } | |
aafe9f16 | 319 | }, |
6808d7a1 BA |
320 | [ |
321 | h("img", { | |
322 | attrs: { | |
323 | src: | |
324 | "/images/pieces/" + | |
241bf8f2 | 325 | this.vr.getPpath(m.appear[0].c + m.appear[0].p) + |
6808d7a1 | 326 | ".svg" |
aafe9f16 | 327 | }, |
6808d7a1 | 328 | class: { "choice-piece": true }, |
fabb53f2 | 329 | on: onClick |
aafe9f16 BA |
330 | }) |
331 | ] | |
332 | ); | |
333 | }) | |
334 | ); | |
335 | elementArray.unshift(choices); | |
336 | } | |
4b26ecb8 BA |
337 | let onEvents = {}; |
338 | // NOTE: click = mousedown + mouseup | |
cafe0166 | 339 | if (this.mobileBrowser) { |
4b26ecb8 BA |
340 | onEvents = { |
341 | on: { | |
342 | touchstart: this.mousedown, | |
343 | touchmove: this.mousemove, | |
6808d7a1 BA |
344 | touchend: this.mouseup |
345 | } | |
4b26ecb8 | 346 | }; |
6808d7a1 | 347 | } else { |
4b26ecb8 | 348 | onEvents = { |
cf2343ce | 349 | on: { |
65495c17 BA |
350 | mousedown: this.mousedown, |
351 | mousemove: this.mousemove, | |
6808d7a1 BA |
352 | mouseup: this.mouseup |
353 | } | |
4b26ecb8 BA |
354 | }; |
355 | } | |
6808d7a1 | 356 | return h("div", onEvents, elementArray); |
cf2343ce BA |
357 | }, |
358 | methods: { | |
359 | mousedown: function(e) { | |
28b32b4f BA |
360 | e.preventDefault(); |
361 | if (!this.start) { | |
362 | // Start square must contain a piece. | |
363 | // NOTE: classList[0] is enough: 'piece' is the first assigned class | |
364 | if (e.target.classList[0] != "piece") return; | |
365 | let parent = e.target.parentNode; //surrounding square | |
366 | // Show possible moves if current player allowed to play | |
367 | const startSquare = getSquareFromId(parent.id); | |
368 | this.possibleMoves = []; | |
369 | const color = this.analyze ? this.vr.turn : this.userColor; | |
370 | if (this.vr.canIplay(color, startSquare)) | |
371 | this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare); | |
372 | // For potential drag'n drop, remember start coordinates | |
373 | // (to center the piece on mouse cursor) | |
374 | let rect = parent.getBoundingClientRect(); | |
375 | this.start = { | |
376 | x: rect.x + rect.width / 2, | |
377 | y: rect.y + rect.width / 2, | |
378 | id: parent.id | |
379 | }; | |
380 | // Add the moving piece to the board, just after current image | |
381 | this.selectedPiece = e.target.cloneNode(); | |
382 | Object.assign( | |
383 | this.selectedPiece.style, | |
384 | { | |
385 | position: "absolute", | |
386 | top: 0, | |
387 | display: "inline-block", | |
388 | zIndex: 3000 | |
389 | } | |
390 | ); | |
391 | parent.insertBefore(this.selectedPiece, e.target.nextSibling); | |
392 | } else { | |
393 | this.processMoveAttempt(e); | |
394 | } | |
cf2343ce BA |
395 | }, |
396 | mousemove: function(e) { | |
6808d7a1 | 397 | if (!this.selectedPiece) return; |
28b32b4f | 398 | e.preventDefault(); |
aafe9f16 | 399 | // There is an active element: move it around |
cafe0166 BA |
400 | const [offsetX, offsetY] = |
401 | this.mobileBrowser | |
402 | ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY] | |
403 | : [e.clientX, e.clientY]; | |
28b32b4f BA |
404 | Object.assign( |
405 | this.selectedPiece.style, | |
406 | { | |
407 | left: offsetX - this.start.x + "px", | |
408 | top: offsetY - this.start.y + "px" | |
409 | } | |
410 | ); | |
cf2343ce BA |
411 | }, |
412 | mouseup: function(e) { | |
6808d7a1 | 413 | if (!this.selectedPiece) return; |
28b32b4f BA |
414 | e.preventDefault(); |
415 | // Drag'n drop. Selected piece is no longer needed: | |
416 | this.selectedPiece.parentNode.removeChild(this.selectedPiece); | |
417 | delete this.selectedPiece; | |
418 | this.selectedPiece = null; | |
419 | this.processMoveAttempt(e); | |
420 | }, | |
421 | processMoveAttempt: function(e) { | |
422 | // Obtain the move from start and end squares | |
cafe0166 BA |
423 | const [offsetX, offsetY] = |
424 | this.mobileBrowser | |
425 | ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY] | |
426 | : [e.clientX, e.clientY]; | |
cf2343ce | 427 | let landing = document.elementFromPoint(offsetX, offsetY); |
cf2343ce | 428 | // Next condition: classList.contains(piece) fails because of marks |
6808d7a1 | 429 | while (landing.tagName == "IMG") landing = landing.parentNode; |
28b32b4f BA |
430 | if (this.start.id == landing.id) { |
431 | if (this.click == landing.id) { | |
432 | // Second click on same square: cancel current move | |
433 | this.possibleMoves = []; | |
434 | this.start = null; | |
435 | this.click = ""; | |
436 | } else this.click = landing.id; | |
cf2343ce | 437 | return; |
28b32b4f BA |
438 | } |
439 | this.start = null; | |
bd76b456 | 440 | // OK: process move attempt, landing is a square node |
cf2343ce BA |
441 | let endSquare = getSquareFromId(landing.id); |
442 | let moves = this.findMatchingMoves(endSquare); | |
443 | this.possibleMoves = []; | |
6808d7a1 BA |
444 | if (moves.length > 1) this.choices = moves; |
445 | else if (moves.length == 1) this.play(moves[0]); | |
28b32b4f | 446 | // else: forbidden move attempt |
cf2343ce BA |
447 | }, |
448 | findMatchingMoves: function(endSquare) { | |
449 | // Run through moves list and return the matching set (if promotions...) | |
28b32b4f BA |
450 | return ( |
451 | this.possibleMoves.filter(m => { | |
452 | return (endSquare[0] == m.end.x && endSquare[1] == m.end.y); | |
453 | }) | |
454 | ); | |
cf2343ce BA |
455 | }, |
456 | play: function(move) { | |
6808d7a1 BA |
457 | this.$emit("play-move", move); |
458 | } | |
459 | } | |
cf2343ce BA |
460 | }; |
461 | </script> | |
4473050c | 462 | |
41c80bb6 | 463 | <style lang="sass" scoped> |
50aed5a1 BA |
464 | .game.reserve-div |
465 | margin-bottom: 18px | |
466 | ||
467 | .reserve-count | |
468 | padding-left: 40% | |
469 | ||
9d4a0218 | 470 | .reserve-row |
50aed5a1 BA |
471 | margin-bottom: 15px |
472 | ||
41cb9b94 BA |
473 | // NOTE: no variants with reserve of size != 8 |
474 | ||
50aed5a1 | 475 | .game |
28b32b4f | 476 | user-select: none |
cf94b843 BA |
477 | width: 100% |
478 | margin: 0 | |
50aed5a1 BA |
479 | .board |
480 | cursor: pointer | |
50aed5a1 BA |
481 | |
482 | #choices | |
28b32b4f | 483 | user-select: none |
168a5e4c BA |
484 | margin: 0 |
485 | position: absolute | |
50aed5a1 BA |
486 | z-index: 300 |
487 | overflow-y: inherit | |
488 | background-color: rgba(0,0,0,0) | |
489 | img | |
490 | cursor: pointer | |
491 | background-color: #e6ee9c | |
492 | &:hover | |
493 | background-color: skyblue | |
494 | &.choice-piece | |
495 | width: 100% | |
496 | height: auto | |
497 | display: block | |
498 | ||
50aed5a1 BA |
499 | img.ghost |
500 | position: absolute | |
28b32b4f | 501 | opacity: 0.5 |
50aed5a1 BA |
502 | top: 0 |
503 | ||
311cba76 BA |
504 | .incheck-light |
505 | background-color: rgba(204, 51, 0, 0.7) !important | |
506 | .incheck-dark | |
507 | background-color: rgba(204, 51, 0, 0.9) !important | |
50aed5a1 BA |
508 | |
509 | .light-square.lichess | |
510 | background-color: #f0d9b5; | |
511 | .dark-square.lichess | |
512 | background-color: #b58863; | |
513 | ||
514 | .light-square.chesscom | |
515 | background-color: #e5e5ca; | |
516 | .dark-square.chesscom | |
517 | background-color: #6f8f57; | |
518 | ||
519 | .light-square.chesstempo | |
28b32b4f | 520 | background-color: #dfdfdf; |
50aed5a1 | 521 | .dark-square.chesstempo |
28b32b4f BA |
522 | background-color: #7287b6; |
523 | ||
524 | // TODO: no predefined highlight colors, but layers. How? | |
525 | ||
526 | .light-square.lichess.highlight-light | |
527 | background-color: #cdd26a !important | |
528 | .dark-square.lichess.highlight-dark | |
529 | background-color: #aaa23a !important | |
530 | ||
531 | .light-square.chesscom.highlight-light | |
532 | background-color: #f7f783 !important | |
533 | .dark-square.chesscom.highlight-dark | |
534 | background-color: #bacb44 !important | |
535 | ||
536 | .light-square.chesstempo.highlight-light | |
537 | background-color: #9f9fff !important | |
538 | .dark-square.chesstempo.highlight-dark | |
539 | background-color: #557fff !important | |
540 | ||
4473050c | 541 | </style> |