Better Ball rules. Buggish but almost OK Synchrone variant
[vchess.git] / client / src / components / Board.vue
index fdeeef3..d89ee40 100644 (file)
@@ -25,6 +25,7 @@ export default {
       selectedPiece: null, //moving piece (or clicked piece)
       start: null, //pixels coordinates + id of starting square (click or drag)
       click: "",
+      clickTime: 0,
       settings: store.state.settings
     };
   },
@@ -54,6 +55,10 @@ export default {
       this.settings.highlight &&
       ["all","highlight"].includes(V.ShowMoves)
     );
+    const showCheck = (
+      this.settings.highlight &&
+      ["all","highlight","byrow"].includes(V.ShowMoves)
+    );
     const orientation = !V.CanFlip ? "w" : this.orientation;
     // Ensure that squares colors do not change when board is flipped
     const lightSquareMod = (sizeX + sizeY) % 2;
@@ -112,8 +117,13 @@ export default {
                   attrs: {
                     src:
                       "/images/pieces/" +
-                      this.vr.getPpath(this.vr.board[ci][cj], this.userColor, this.score) +
-                      ".svg"
+                      this.vr.getPpath(
+                        this.vr.board[ci][cj],
+                        // Extra args useful for some variants:
+                        this.userColor,
+                        this.score,
+                        this.orientation) +
+                      V.IMAGE_EXTENSION
                   }
                 })
               );
@@ -143,8 +153,8 @@ export default {
                   "in-shadow": inShadow(ci, cj),
                   "highlight-light": inHighlight(ci, cj) && lightSquare,
                   "highlight-dark": inHighlight(ci, cj) && !lightSquare,
-                  "incheck-light": showLight && lightSquare && incheckSq[ci][cj],
-                  "incheck-dark": showLight && !lightSquare && incheckSq[ci][cj]
+                  "incheck-light": showCheck && lightSquare && incheckSq[ci][cj],
+                  "incheck-dark": showCheck && !lightSquare && incheckSq[ci][cj]
                 },
                 attrs: {
                   id: getSquareId({ x: ci, y: cj })
@@ -278,59 +288,76 @@ export default {
       // No choices to show at first drawing
       const squareWidth = boardElt.offsetWidth / sizeY;
       const offset = [boardElt.offsetTop, boardElt.offsetLeft];
-      // TODO: multi-rows if more than V.size.y pieces (as inEightpieces)
+      const maxNbeltsPerRow = Math.min(this.choices.length, sizeY);
+      let topOffset = offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2;
+      let choicesHeight = squareWidth;
+      if (this.choices.length >= sizeY) {
+        // A second row is required (Eightpieces variant)
+        topOffset -= squareWidth / 2;
+        choicesHeight *= 2;
+      }
       const choices = h(
         "div",
         {
           attrs: { id: "choices" },
           class: { row: true },
           style: {
-            top: offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2 + "px",
+            top: topOffset + "px",
             left:
               offset[1] +
-              (squareWidth * (sizeY - this.choices.length)) / 2 +
+              (squareWidth * Math.max(sizeY - this.choices.length, 0)) / 2 +
               "px",
-            width: this.choices.length * squareWidth + "px",
-            height: squareWidth + "px"
+            width: (maxNbeltsPerRow * squareWidth) + "px",
+            height: choicesHeight + "px"
           }
         },
-        this.choices.map(m => {
-          // A "choice" is a move
-          const applyMove = (e) => {
-            e.stopPropagation();
-            this.play(m);
-            this.choices = [];
-          };
-          const onClick =
-            this.mobileBrowser
-              ? { touchend: applyMove }
-              : { mouseup: applyMove };
-          return h(
-            "div",
-            {
-              class: {
-                board: true,
-                ["board" + sizeY]: true
-              },
-              style: {
-                width: 100 / this.choices.length + "%",
-                "padding-bottom": 100 / this.choices.length + "%"
-              }
-            },
-            [
-              h("img", {
-                attrs: {
-                  src:
-                    "/images/pieces/" +
-                    this.vr.getPpath(m.appear[0].c + m.appear[0].p) +
-                    ".svg"
+        [ h(
+          "div",
+          { },
+          this.choices.map(m => {
+            // A "choice" is a move
+            const applyMove = (e) => {
+              e.stopPropagation();
+              // Force a delay between move is shown and clicked
+              // (otherwise a "double-click" bug might occur)
+              if (Date.now() - this.clickTime < 200) return;
+              this.choices = [];
+              this.play(m);
+            };
+            const onClick =
+              this.mobileBrowser
+                ? { touchend: applyMove }
+                : { mouseup: applyMove };
+            return h(
+              "div",
+              {
+                class: {
+                  board: true,
+                  ["board" + sizeY]: true
                 },
-                class: { "choice-piece": true },
-                on: onClick
-              })
-            ]
-          );
-        })
+                style: {
+                  width: (100 / maxNbeltsPerRow) + "%",
+                  "padding-bottom": (100 / maxNbeltsPerRow) + "%"
+                }
+              },
+              [
+                h("img", {
+                  attrs: {
+                    src:
+                      "/images/pieces/" +
+                      this.vr.getPPpath(
+                        m.appear[0].c + m.appear[0].p,
+                        // Extra arg useful for some variants:
+                        this.orientation) +
+                      V.IMAGE_EXTENSION
+                  },
+                  class: { "choice-piece": true },
+                  on: onClick
+                })
+              ]
+            );
+          })
+        ) ]
       );
       elementArray.unshift(choices);
     }
@@ -441,8 +468,10 @@ export default {
       let endSquare = getSquareFromId(landing.id);
       let moves = this.findMatchingMoves(endSquare);
       this.possibleMoves = [];
-      if (moves.length > 1) this.choices = moves;
-      else if (moves.length == 1) this.play(moves[0]);
+      if (moves.length > 1) {
+        this.clickTime = Date.now();
+        this.choices = moves;
+      } else if (moves.length == 1) this.play(moves[0]);
       // else: forbidden move attempt
     },
     findMatchingMoves: function(endSquare) {
@@ -524,18 +553,18 @@ img.ghost
 // TODO: no predefined highlight colors, but layers. How?
 
 .light-square.lichess.highlight-light
-  background-color: #cdd26a !important
+  background-color: #cdd26a
 .dark-square.lichess.highlight-dark
-  background-color: #aaa23a !important
+  background-color: #aaa23a
 
 .light-square.chesscom.highlight-light
-  background-color: #f7f783 !important
+  background-color: #f7f783
 .dark-square.chesscom.highlight-dark
-  background-color: #bacb44 !important
+  background-color: #bacb44
 
 .light-square.chesstempo.highlight-light
-  background-color: #9f9fff !important
+  background-color: #9f9fff
 .dark-square.chesstempo.highlight-dark
-  background-color: #557fff !important
+  background-color: #557fff
 
 </style>