Finalize Shinobi Chess
[vchess.git] / client / src / components / Board.vue
index b176bd7..799185e 100644 (file)
@@ -194,7 +194,9 @@ export default {
                     showCheck && lightSquare && incheckSq[ci][cj],
                   "incheck-dark":
                     showCheck && !lightSquare && incheckSq[ci][cj],
-                  "hover-highlight": this.vr.hoverHighlight(ci, cj)
+                  "hover-highlight":
+                    this.vr.hoverHighlight(
+                      [ci, cj], !this.analyze ? this.userColor : null)
                 },
                 attrs: {
                   id: getSquareId({ x: ci, y: cj })
@@ -297,7 +299,8 @@ export default {
         (!myReserveTop && !!this.vr.reserve[playingColor])
       );
       // Center reserves, assuming same number of pieces for each side:
-      const nbReservePieces = myReservePiecesArray.length;
+      const nbReservePieces =
+        Math.max(myReservePiecesArray.length, oppReservePiecesArray.length);
       const marginLeft =
         ((100 - nbReservePieces * (100 / reserveSquareNb)) / 2) + "%";
       if (hasReserveTop) {
@@ -365,7 +368,7 @@ export default {
       const squareWidth = boardElt.offsetWidth / sizeY;
       const offset = [boardElt.offsetTop, boardElt.offsetLeft];
       const maxNbeltsPerRow = Math.min(this.choices.length, sizeY);
-      let topOffset = offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2;
+      let topOffset = offset[0] + ((sizeX - 1) / 2) * squareWidth;
       let choicesHeight = squareWidth;
       if (this.choices.length >= sizeY) {
         // A second row is required (Eightpieces variant)
@@ -544,15 +547,20 @@ export default {
       }
     },
     addArrow: function(arrow) {
-      this.arrows.push(arrow);
-      // Also add to DOM:
-      const boardElt = document.getElementById("gamePosition");
-      const squareWidth = boardElt.offsetWidth / V.size.y;
-      const bPos = boardElt.getBoundingClientRect();
-      const newArrow =
-        this.getSvgArrow(arrow, bPos.top, bPos.left, squareWidth);
-      document.getElementById("arrowCanvas")
-        .insertAdjacentElement("beforeend", newArrow);
+      const arrowIdx = this.arrows.findIndex(a => {
+        return (
+          a.start[0] == arrow.start[0] && a.start[1] == arrow.start[1] &&
+          a.end[0] == arrow.end[0] && a.end[1] == arrow.end[1]
+        );
+      });
+      if (arrowIdx >= 0)
+        // Erase the arrow
+        this.arrows.splice(arrowIdx, 1);
+      else
+        // Add to arrows vector:
+        this.arrows.push(arrow);
+      // NOTE: no need to draw here, will be re-draw
+      // by updated() hook callong re_setDrawings()
     },
     getSvgArrow: function(arrow, top, left, squareWidth) {
       const aStart =
@@ -575,11 +583,12 @@ export default {
       return path;
     },
     re_setDrawings: function() {
+      // Add some drawing on board (for some variants + arrows and circles)
+      const boardElt = document.getElementById("gamePosition");
+      if (!boardElt) return;
       // Remove current canvas, if any
       const curCanvas = document.getElementById("arrowCanvas");
       if (!!curCanvas) curCanvas.parentNode.removeChild(curCanvas);
-      // Add some drawing on board (for some variants + arrows and circles)
-      const boardElt = document.getElementById("gamePosition");
       const squareWidth = boardElt.offsetWidth / V.size.y;
       const bPos = boardElt.getBoundingClientRect();
       let svgArrows = [];