Add Koth (experimental)
[vchess.git] / client / src / components / Board.vue
CommitLineData
24340cae 1<script>
e2732923
BA
2import { getSquareId, getSquareFromId } from "@/utils/squareId";
3import { ArrayFun } from "@/utils/array";
dfeb96ea 4import { store } from "@/store";
cf2343ce 5export default {
6808d7a1 6 name: "my-board",
2c5d7b20 7 // Last move cannot be guessed from here, and is required for highlights.
cf2343ce 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 26 start: null, //pixels coordinates + id of starting square (click or drag)
49dad261
BA
27 startArrow: null,
28 movingArrow: { x: -1, y: -1 },
29 arrows: [], //object of {start: x,y / end: x,y}
30 circles: {}, //object of squares' ID --> true (TODO: use a set?)
28b32b4f 31 click: "",
3a2a7b5f 32 clickTime: 0,
6808d7a1 33 settings: store.state.settings
cf2343ce
BA
34 };
35 },
36 render(h) {
6808d7a1 37 if (!this.vr) {
7b3cf1b7 38 // Return empty div of class 'game' to avoid error when setting size
6808d7a1
BA
39 return h("div", {
40 class: {
41 game: true
42 }
43 });
7b3cf1b7 44 }
6808d7a1 45 const [sizeX, sizeY] = [V.size.x, V.size.y];
cf2343ce 46 // Precompute hints squares to facilitate rendering
e2732923 47 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
6808d7a1
BA
48 this.possibleMoves.forEach(m => {
49 hintSquares[m.end.x][m.end.y] = true;
50 });
cf2343ce 51 // Also precompute in-check squares
e2732923 52 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
6808d7a1
BA
53 this.incheck.forEach(sq => {
54 incheckSq[sq[0]][sq[1]] = true;
55 });
06e79b07 56
af34341d
BA
57 let lm = this.lastMove;
58 // Precompute lastMove highlighting squares
59 const lmHighlights = {};
60 if (!!lm) {
61 if (!Array.isArray(lm)) lm = [lm];
62 lm.forEach(m => {
b406466b
BA
63 if (V.OnBoard(m.start.x, m.start.y))
64 lmHighlights[m.start.x + sizeX * m.start.y] = true;
65 if (V.OnBoard(m.end.x, m.end.y))
66 lmHighlights[m.end.x + sizeX * m.end.y] = true;
af34341d
BA
67 });
68 }
57eb158f
BA
69 const showLight = (
70 this.settings.highlight &&
71 ["all","highlight"].includes(V.ShowMoves)
72 );
d54f6261
BA
73 const showCheck = (
74 this.settings.highlight &&
75 ["all","highlight","byrow"].includes(V.ShowMoves)
76 );
311cba76
BA
77 const orientation = !V.CanFlip ? "w" : this.orientation;
78 // Ensure that squares colors do not change when board is flipped
79 const lightSquareMod = (sizeX + sizeY) % 2;
80 const showPiece = (x, y) => {
81 return (
82 this.vr.board[x][y] != V.EMPTY &&
83 (!this.vr.enlightened || this.analyze || this.score != "*" ||
84 (!!this.userColor && this.vr.enlightened[this.userColor][x][y]))
85 );
86 };
87 const inHighlight = (x, y) => {
af34341d 88 return showLight && !!lmHighlights[x + sizeX * y];
311cba76
BA
89 };
90 const inShadow = (x, y) => {
91 return (
92 !this.analyze &&
93 this.score == "*" &&
94 this.vr.enlightened &&
95 (!this.userColor || !this.vr.enlightened[this.userColor][x][y])
96 );
97 };
98 // Create board element (+ reserves if needed by variant)
9d4a0218 99 let elementArray = [];
cf2343ce 100 const gameDiv = h(
6808d7a1 101 "div",
cf2343ce 102 {
6ec2feb2 103 "class": {
6808d7a1
BA
104 game: true,
105 clearer: true
106 }
cf2343ce
BA
107 },
108 [...Array(sizeX).keys()].map(i => {
311cba76 109 const ci = orientation == "w" ? i : sizeX - i - 1;
cf2343ce 110 return h(
6808d7a1 111 "div",
cf2343ce 112 {
6ec2feb2 113 "class": {
6808d7a1 114 row: true
cf2343ce 115 },
6808d7a1 116 style: { opacity: this.choices.length > 0 ? "0.5" : "1" }
cf2343ce
BA
117 },
118 [...Array(sizeY).keys()].map(j => {
311cba76 119 const cj = orientation == "w" ? j : sizeY - j - 1;
49dad261 120 const squareId = "sq-" + ci + "-" + cj;
cf2343ce 121 let elems = [];
311cba76 122 if (showPiece(ci, cj)) {
cf2343ce 123 elems.push(
6808d7a1 124 h("img", {
6ec2feb2 125 "class": {
6808d7a1
BA
126 piece: true,
127 ghost:
128 !!this.selectedPiece &&
49dad261 129 this.selectedPiece.parentNode.id == squareId
6808d7a1
BA
130 },
131 attrs: {
132 src:
133 "/images/pieces/" +
3a2a7b5f
BA
134 this.vr.getPpath(
135 this.vr.board[ci][cj],
136 // Extra args useful for some variants:
137 this.userColor,
138 this.score,
139 this.orientation) +
14edde72 140 V.IMAGE_EXTENSION
cf2343ce 141 }
6808d7a1 142 })
cf2343ce
BA
143 );
144 }
6808d7a1 145 if (this.settings.hints && hintSquares[ci][cj]) {
cf2343ce 146 elems.push(
6808d7a1 147 h("img", {
6ec2feb2 148 "class": {
6808d7a1
BA
149 "mark-square": true
150 },
151 attrs: {
152 src: "/images/mark.svg"
cf2343ce 153 }
6808d7a1 154 })
cf2343ce
BA
155 );
156 }
49dad261
BA
157 if (!!this.circles[squareId]) {
158 elems.push(
159 h("img", {
160 "class": {
161 "circle-square": true
162 },
163 attrs: {
164 src: "/images/circle.svg"
165 }
166 })
167 );
168 }
311cba76 169 const lightSquare = (ci + cj) % 2 == lightSquareMod;
cf2343ce 170 return h(
6808d7a1 171 "div",
cf2343ce 172 {
6ec2feb2 173 "class": {
6808d7a1
BA
174 board: true,
175 ["board" + sizeY]: true,
311cba76
BA
176 "light-square": lightSquare,
177 "dark-square": !lightSquare,
dfeb96ea 178 [this.settings.bcolor]: true,
311cba76
BA
179 "in-shadow": inShadow(ci, cj),
180 "highlight-light": inHighlight(ci, cj) && lightSquare,
181 "highlight-dark": inHighlight(ci, cj) && !lightSquare,
2c5d7b20
BA
182 "incheck-light":
183 showCheck && lightSquare && incheckSq[ci][cj],
184 "incheck-dark":
185 showCheck && !lightSquare && incheckSq[ci][cj]
cf2343ce
BA
186 },
187 attrs: {
6808d7a1
BA
188 id: getSquareId({ x: ci, y: cj })
189 }
cf2343ce
BA
190 },
191 elems
192 );
193 })
194 );
24340cae 195 })
cf2343ce 196 );
9d4a0218
BA
197 if (!!this.vr.reserve) {
198 const playingColor = this.userColor || "w"; //default for an observer
6808d7a1 199 const shiftIdx = playingColor == "w" ? 0 : 1;
cf2343ce 200 let myReservePiecesArray = [];
6808d7a1 201 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
9d4a0218 202 const qty = this.vr.reserve[playingColor][V.RESERVE_PIECES[i]];
6808d7a1
BA
203 myReservePiecesArray.push(
204 h(
205 "div",
206 {
6ec2feb2 207 "class": { board: true, ["board" + sizeY]: true },
9d4a0218
BA
208 attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) },
209 style: { opacity: qty > 0 ? 1 : 0.35 }
6808d7a1
BA
210 },
211 [
212 h("img", {
6ec2feb2 213 "class": { piece: true, reserve: true },
6808d7a1
BA
214 attrs: {
215 src:
216 "/images/pieces/" +
cd49e617 217 this.vr.getReservePpath(i, playingColor, orientation) +
6808d7a1
BA
218 ".svg"
219 }
220 }),
6ec2feb2 221 h("sup", { "class": { "reserve-count": true } }, [ qty ])
6808d7a1 222 ]
cf2343ce 223 )
6808d7a1 224 );
cf2343ce
BA
225 }
226 let oppReservePiecesArray = [];
93d1d7a7 227 const oppCol = V.GetOppCol(playingColor);
6808d7a1 228 for (let i = 0; i < V.RESERVE_PIECES.length; i++) {
9d4a0218 229 const qty = this.vr.reserve[oppCol][V.RESERVE_PIECES[i]];
6808d7a1
BA
230 oppReservePiecesArray.push(
231 h(
232 "div",
233 {
6ec2feb2 234 "class": { board: true, ["board" + sizeY]: true },
9d4a0218
BA
235 attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) },
236 style: { opacity: qty > 0 ? 1 : 0.35 }
6808d7a1
BA
237 },
238 [
239 h("img", {
6ec2feb2 240 "class": { piece: true, reserve: true },
6808d7a1
BA
241 attrs: {
242 src:
243 "/images/pieces/" +
cd49e617 244 this.vr.getReservePpath(i, oppCol, orientation) +
6808d7a1
BA
245 ".svg"
246 }
247 }),
6ec2feb2 248 h("sup", { "class": { "reserve-count": true } }, [ qty ])
6808d7a1 249 ]
cf2343ce 250 )
6808d7a1 251 );
cf2343ce 252 }
9d4a0218
BA
253 const myReserveTop = (
254 (playingColor == 'w' && orientation == 'b') ||
255 (playingColor == 'b' && orientation == 'w')
cf2343ce 256 );
9d4a0218
BA
257 // Center reserves, assuming same number of pieces for each side:
258 const nbReservePieces = myReservePiecesArray.length;
259 const marginLeft = ((100 - nbReservePieces * (100 / sizeY)) / 2) + "%";
260 const reserveTop =
261 h(
262 "div",
263 {
6ec2feb2 264 "class": {
9d4a0218
BA
265 game: true,
266 "reserve-div": true
267 },
268 style: {
269 "margin-left": marginLeft
270 }
271 },
272 [
273 h(
274 "div",
275 {
6ec2feb2 276 "class": {
9d4a0218
BA
277 row: true,
278 "reserve-row": true
279 }
280 },
281 myReserveTop ? myReservePiecesArray : oppReservePiecesArray
282 )
283 ]
284 );
285 var reserveBottom =
286 h(
287 "div",
288 {
6ec2feb2 289 "class": {
9d4a0218
BA
290 game: true,
291 "reserve-div": true
292 },
293 style: {
294 "margin-left": marginLeft
295 }
296 },
297 [
298 h(
299 "div",
300 {
6ec2feb2 301 "class": {
9d4a0218
BA
302 row: true,
303 "reserve-row": true
304 }
305 },
306 myReserveTop ? oppReservePiecesArray : myReservePiecesArray
307 )
308 ]
309 );
310 elementArray.push(reserveTop);
cf2343ce 311 }
9d4a0218
BA
312 elementArray.push(gameDiv);
313 if (!!this.vr.reserve) elementArray.push(reserveBottom);
aafe9f16 314 const boardElt = document.querySelector(".game");
5f918a27 315 // boardElt might be undefine (at first drawing),
cd049aa1 316 // but it won't be used in this case.
5f918a27 317 const squareWidth = (!!boardElt ? boardElt.offsetWidth / sizeY : 42);
6808d7a1 318 if (this.choices.length > 0 && !!boardElt) {
afbf3ca7 319 // No choices to show at first drawing
aafe9f16 320 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
14edde72
BA
321 const maxNbeltsPerRow = Math.min(this.choices.length, sizeY);
322 let topOffset = offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2;
323 let choicesHeight = squareWidth;
324 if (this.choices.length >= sizeY) {
325 // A second row is required (Eightpieces variant)
326 topOffset -= squareWidth / 2;
327 choicesHeight *= 2;
328 }
aafe9f16 329 const choices = h(
6808d7a1 330 "div",
aafe9f16 331 {
6808d7a1 332 attrs: { id: "choices" },
6ec2feb2 333 "class": { row: true },
aafe9f16 334 style: {
14edde72 335 top: topOffset + "px",
6808d7a1
BA
336 left:
337 offset[1] +
14edde72 338 (squareWidth * Math.max(sizeY - this.choices.length, 0)) / 2 +
6808d7a1 339 "px",
14edde72
BA
340 width: (maxNbeltsPerRow * squareWidth) + "px",
341 height: choicesHeight + "px"
6808d7a1 342 }
aafe9f16 343 },
14edde72
BA
344 [ h(
345 "div",
6ec2feb2
BA
346 {
347 "class": { "full-width": true }
348 },
14edde72
BA
349 this.choices.map(m => {
350 // A "choice" is a move
351 const applyMove = (e) => {
352 e.stopPropagation();
353 // Force a delay between move is shown and clicked
354 // (otherwise a "double-click" bug might occur)
355 if (Date.now() - this.clickTime < 200) return;
14edde72 356 this.choices = [];
78c23cd6 357 this.play(m);
14edde72
BA
358 };
359 const onClick =
360 this.mobileBrowser
361 ? { touchend: applyMove }
362 : { mouseup: applyMove };
363 return h(
364 "div",
365 {
6ec2feb2 366 "class": {
14edde72
BA
367 board: true,
368 ["board" + sizeY]: true
aafe9f16 369 },
14edde72
BA
370 style: {
371 width: (100 / maxNbeltsPerRow) + "%",
372 "padding-bottom": (100 / maxNbeltsPerRow) + "%"
373 }
374 },
375 [
376 h("img", {
377 attrs: {
378 src:
379 "/images/pieces/" +
c7550017
BA
380 // orientation: extra arg useful for some variants:
381 this.vr.getPPpath(m, this.orientation) +
14edde72
BA
382 V.IMAGE_EXTENSION
383 },
6ec2feb2 384 "class": { "choice-piece": true },
14edde72
BA
385 on: onClick
386 })
387 ]
388 );
389 })
390 ) ]
aafe9f16
BA
391 );
392 elementArray.unshift(choices);
393 }
49dad261
BA
394 if (
395 !this.mobileBrowser &&
396 (this.arrows.length > 0 || this.movingArrow.x >= 0)
397 ) {
398 let svgArrows = [];
cd049aa1 399 const arrowWidth = squareWidth / 4;
49dad261 400 this.arrows.forEach(a => {
cd049aa1 401 const endPoint = this.adjustEndArrow(a.start, a.end, squareWidth);
49dad261
BA
402 svgArrows.push(
403 h(
404 "path",
405 {
406 "class": { "svg-arrow": true },
407 attrs: {
408 d: (
409 "M" + a.start.x + "," + a.start.y + " " +
cd049aa1
BA
410 "L" + endPoint.x + "," + endPoint.y
411 ),
412 style: "stroke-width:" + arrowWidth + "px"
49dad261
BA
413 }
414 }
415 )
416 );
417 });
418 if (this.movingArrow.x >= 0) {
cd049aa1
BA
419 const endPoint =
420 this.adjustEndArrow(this.startArrow, this.movingArrow, squareWidth);
49dad261
BA
421 svgArrows.push(
422 h(
423 "path",
424 {
425 "class": { "svg-arrow": true },
426 attrs: {
427 d: (
428 "M" + this.startArrow.x + "," + this.startArrow.y + " " +
cd049aa1
BA
429 "L" + endPoint.x + "," + endPoint.y
430 ),
431 style: "stroke-width:" + arrowWidth + "px"
49dad261
BA
432 }
433 }
434 )
435 );
436 }
437 // Add SVG element for drawing arrows
438 elementArray.push(
439 h(
440 "svg",
441 {
442 attrs: {
443 id: "arrowCanvas",
444 stroke: "none"
445 }
446 },
447 [
448 h(
449 "defs",
450 {},
451 [
452 h(
453 "marker",
454 {
455 attrs: {
456 id: "arrow",
cd049aa1 457 markerWidth: (2 * arrowWidth) + "px",
5f918a27 458 markerHeight: (3 * arrowWidth) + "px",
cd049aa1 459 markerUnits: "userSpaceOnUse",
49dad261 460 refX: "0",
5f918a27 461 refY: (1.5 * arrowWidth) + "px",
49dad261
BA
462 orient: "auto"
463 }
464 },
465 [
466 h(
467 "path",
468 {
cd049aa1 469 "class": { "arrow-head": true },
49dad261 470 attrs: {
cd049aa1 471 d: (
5f918a27
BA
472 "M0,0 L0," + (3 * arrowWidth) + " L" +
473 (2 * arrowWidth) + "," + (1.5 * arrowWidth) + " z"
cd049aa1 474 )
49dad261
BA
475 }
476 }
477 )
478 ]
479 )
480 ]
481 )
482 ].concat(svgArrows)
483 )
484 );
485 }
4b26ecb8
BA
486 let onEvents = {};
487 // NOTE: click = mousedown + mouseup
cafe0166 488 if (this.mobileBrowser) {
4b26ecb8
BA
489 onEvents = {
490 on: {
491 touchstart: this.mousedown,
492 touchmove: this.mousemove,
6808d7a1
BA
493 touchend: this.mouseup
494 }
4b26ecb8 495 };
6808d7a1 496 } else {
4b26ecb8 497 onEvents = {
cf2343ce 498 on: {
65495c17
BA
499 mousedown: this.mousedown,
500 mousemove: this.mousemove,
49dad261
BA
501 mouseup: this.mouseup,
502 contextmenu: this.blockContextMenu
6808d7a1 503 }
4b26ecb8
BA
504 };
505 }
6808d7a1 506 return h("div", onEvents, elementArray);
cf2343ce
BA
507 },
508 methods: {
49dad261
BA
509 blockContextMenu: function(e) {
510 e.preventDefault();
511 e.stopPropagation();
512 return false;
513 },
514 cancelResetArrows: function() {
515 this.startArrow = null;
516 this.arrows = [];
517 this.circles = {};
518 },
cd049aa1
BA
519 adjustEndArrow: function(start, end, squareWidth) {
520 // Simple heuristic for now, just remove 1/3 square.
521 // TODO: should depend on the orientation.
522 const delta = [end.x - start.x, end.y - start.y];
523 const dist = Math.sqrt(delta[0] * delta[0] + delta[1] * delta[1]);
524 const fracSqWidth = squareWidth / 3;
525 return {
526 x: end.x - delta[0] * fracSqWidth / dist,
527 y: end.y - delta[1] * fracSqWidth / dist
528 };
529 },
cf2343ce 530 mousedown: function(e) {
28b32b4f 531 e.preventDefault();
1ef65040 532 if (!this.mobileBrowser && e.which != 3)
49dad261
BA
533 // Cancel current drawing and circles, if any
534 this.cancelResetArrows();
1ef65040 535 if (this.mobileBrowser || e.which == 1) {
49dad261
BA
536 // Mouse left button
537 if (!this.start) {
538 // NOTE: classList[0] is enough: 'piece' is the first assigned class
539 const withPiece = (e.target.classList[0] == "piece");
540 // Emit the click event which could be used by some variants
541 this.$emit(
542 "click-square",
543 getSquareFromId(withPiece ? e.target.parentNode.id : e.target.id)
544 );
545 // Start square must contain a piece.
546 if (!withPiece) return;
547 let parent = e.target.parentNode; //surrounding square
548 // Show possible moves if current player allowed to play
549 const startSquare = getSquareFromId(parent.id);
550 this.possibleMoves = [];
551 const color = this.analyze ? this.vr.turn : this.userColor;
552 if (this.vr.canIplay(color, startSquare))
553 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
554 // For potential drag'n drop, remember start coordinates
555 // (to center the piece on mouse cursor)
556 const rect = parent.getBoundingClientRect();
557 this.start = {
558 x: rect.x + rect.width / 2,
559 y: rect.y + rect.width / 2,
560 id: parent.id
561 };
562 // Add the moving piece to the board, just after current image
563 this.selectedPiece = e.target.cloneNode();
564 Object.assign(
565 this.selectedPiece.style,
566 {
567 position: "absolute",
568 top: 0,
569 display: "inline-block",
570 zIndex: 3000
571 }
572 );
573 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
574 } else {
575 this.processMoveAttempt(e);
576 }
1ef65040
BA
577 } else if (e.which == 3) {
578 // Mouse right button
49dad261
BA
579 let elem = e.target;
580 // Next loop because of potential marks
581 while (elem.tagName == "IMG") elem = elem.parentNode;
582 // To center the arrow in square:
583 const rect = elem.getBoundingClientRect();
584 this.startArrow = {
28b32b4f
BA
585 x: rect.x + rect.width / 2,
586 y: rect.y + rect.width / 2,
49dad261 587 id: elem.id
28b32b4f 588 };
49dad261
BA
589 }
590 },
591 mousemove: function(e) {
592 if (!this.selectedPiece && !this.startArrow) return;
593 e.preventDefault();
594 if (!!this.selectedPiece) {
595 // There is an active element: move it around
596 const [offsetX, offsetY] =
597 this.mobileBrowser
598 ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY]
599 : [e.clientX, e.clientY];
28b32b4f
BA
600 Object.assign(
601 this.selectedPiece.style,
602 {
49dad261
BA
603 left: offsetX - this.start.x + "px",
604 top: offsetY - this.start.y + "px"
28b32b4f
BA
605 }
606 );
28b32b4f 607 }
49dad261
BA
608 else {
609 let elem = e.target;
610 // Next loop because of potential marks
611 while (elem.tagName == "IMG") elem = elem.parentNode;
612 // To center the arrow in square:
613 if (elem.id != this.startArrow.id) {
614 const rect = elem.getBoundingClientRect();
615 this.movingArrow = {
616 x: rect.x + rect.width / 2,
617 y: rect.y + rect.width / 2
618 };
28b32b4f 619 }
49dad261 620 }
cf2343ce
BA
621 },
622 mouseup: function(e) {
28b32b4f 623 e.preventDefault();
1ef65040 624 if (this.mobileBrowser || e.which == 1) {
49dad261
BA
625 if (!this.selectedPiece) return;
626 // Drag'n drop. Selected piece is no longer needed:
627 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
628 delete this.selectedPiece;
629 this.selectedPiece = null;
630 this.processMoveAttempt(e);
1ef65040
BA
631 } else if (e.which == 3) {
632 // Mouse right button
49dad261
BA
633 this.movingArrow = { x: -1, y: -1 };
634 this.processArrowAttempt(e);
635 }
28b32b4f 636 },
e90bafa8
BA
637 // Called by BaseGame after partially undoing multi-moves:
638 resetCurrentAttempt: function() {
639 this.possibleMoves = [];
640 this.start = null;
641 this.click = "";
642 this.selectedPiece = null;
643 },
28b32b4f
BA
644 processMoveAttempt: function(e) {
645 // Obtain the move from start and end squares
cafe0166
BA
646 const [offsetX, offsetY] =
647 this.mobileBrowser
648 ? [e.changedTouches[0].pageX, e.changedTouches[0].pageY]
649 : [e.clientX, e.clientY];
cf2343ce 650 let landing = document.elementFromPoint(offsetX, offsetY);
cf2343ce 651 // Next condition: classList.contains(piece) fails because of marks
6808d7a1 652 while (landing.tagName == "IMG") landing = landing.parentNode;
28b32b4f
BA
653 if (this.start.id == landing.id) {
654 if (this.click == landing.id) {
655 // Second click on same square: cancel current move
656 this.possibleMoves = [];
657 this.start = null;
658 this.click = "";
659 } else this.click = landing.id;
cf2343ce 660 return;
28b32b4f
BA
661 }
662 this.start = null;
bd76b456 663 // OK: process move attempt, landing is a square node
cf2343ce
BA
664 let endSquare = getSquareFromId(landing.id);
665 let moves = this.findMatchingMoves(endSquare);
666 this.possibleMoves = [];
3a2a7b5f
BA
667 if (moves.length > 1) {
668 this.clickTime = Date.now();
669 this.choices = moves;
670 } else if (moves.length == 1) this.play(moves[0]);
28b32b4f 671 // else: forbidden move attempt
cf2343ce 672 },
49dad261
BA
673 processArrowAttempt: function(e) {
674 // Obtain the arrow from start and end squares
675 const [offsetX, offsetY] = [e.clientX, e.clientY];
676 let landing = document.elementFromPoint(offsetX, offsetY);
677 // Next condition: classList.contains(piece) fails because of marks
678 while (landing.tagName == "IMG") landing = landing.parentNode;
679 if (this.startArrow.id == landing.id)
680 // Draw (or erase) a circle
681 this.$set(this.circles, landing.id, !this.circles[landing.id]);
682 else {
683 // OK: add arrow, landing is a new square
684 const rect = landing.getBoundingClientRect();
685 this.arrows.push({
686 start: {
687 x: this.startArrow.x,
688 y: this.startArrow.y
689 },
690 end: {
691 x: rect.x + rect.width / 2,
692 y: rect.y + rect.width / 2
693 }
694 });
695 }
696 this.startArrow = null;
697 },
cf2343ce
BA
698 findMatchingMoves: function(endSquare) {
699 // Run through moves list and return the matching set (if promotions...)
28b32b4f
BA
700 return (
701 this.possibleMoves.filter(m => {
702 return (endSquare[0] == m.end.x && endSquare[1] == m.end.y);
703 })
704 );
cf2343ce
BA
705 },
706 play: function(move) {
6808d7a1
BA
707 this.$emit("play-move", move);
708 }
709 }
cf2343ce
BA
710};
711</script>
4473050c 712
41c80bb6 713<style lang="sass" scoped>
26d8a01a
BA
714@import "@/styles/_board_squares_img.sass";
715
6ec2feb2 716// NOTE: no variants with reserve of size != 8
50aed5a1
BA
717.game.reserve-div
718 margin-bottom: 18px
50aed5a1
BA
719.reserve-count
720 padding-left: 40%
9d4a0218 721.reserve-row
50aed5a1
BA
722 margin-bottom: 15px
723
6ec2feb2
BA
724.full-width
725 width: 100%
41cb9b94 726
50aed5a1 727.game
28b32b4f 728 user-select: none
cf94b843
BA
729 width: 100%
730 margin: 0
50aed5a1
BA
731 .board
732 cursor: pointer
50aed5a1
BA
733
734#choices
28b32b4f 735 user-select: none
168a5e4c
BA
736 margin: 0
737 position: absolute
50aed5a1
BA
738 z-index: 300
739 overflow-y: inherit
740 background-color: rgba(0,0,0,0)
741 img
742 cursor: pointer
743 background-color: #e6ee9c
744 &:hover
745 background-color: skyblue
746 &.choice-piece
747 width: 100%
748 height: auto
749 display: block
750
50aed5a1
BA
751img.ghost
752 position: absolute
28b32b4f 753 opacity: 0.5
50aed5a1
BA
754 top: 0
755
49dad261
BA
756#arrowCanvas
757 pointer-events: none
758 position: absolute
759 top: 0
760 left: 0
761 width: 100%
762 height: 100%
763
764.svg-arrow
765 opacity: 0.65
766 stroke: #5f0e78
49dad261
BA
767 fill: none
768 marker-end: url(#arrow)
769
cd049aa1
BA
770.arrow-head
771 fill: #5f0e78
772
311cba76
BA
773.incheck-light
774 background-color: rgba(204, 51, 0, 0.7) !important
775.incheck-dark
776 background-color: rgba(204, 51, 0, 0.9) !important
50aed5a1
BA
777
778.light-square.lichess
779 background-color: #f0d9b5;
780.dark-square.lichess
781 background-color: #b58863;
782
783.light-square.chesscom
784 background-color: #e5e5ca;
785.dark-square.chesscom
786 background-color: #6f8f57;
787
788.light-square.chesstempo
28b32b4f 789 background-color: #dfdfdf;
50aed5a1 790.dark-square.chesstempo
28b32b4f
BA
791 background-color: #7287b6;
792
793// TODO: no predefined highlight colors, but layers. How?
794
795.light-square.lichess.highlight-light
b0a0468a 796 background-color: #cdd26a
28b32b4f 797.dark-square.lichess.highlight-dark
b0a0468a 798 background-color: #aaa23a
28b32b4f
BA
799
800.light-square.chesscom.highlight-light
b0a0468a 801 background-color: #f7f783
28b32b4f 802.dark-square.chesscom.highlight-dark
b0a0468a 803 background-color: #bacb44
28b32b4f
BA
804
805.light-square.chesstempo.highlight-light
b0a0468a 806 background-color: #9f9fff
28b32b4f 807.dark-square.chesstempo.highlight-dark
b0a0468a 808 background-color: #557fff
4473050c 809</style>