Improve playing experience on smartphones
[xogo.git] / base_rules.js
index 715f4e0..82eec3a 100644 (file)
@@ -876,6 +876,8 @@ export default class ChessRules {
         startPiece, curPiece = null,
         sqSize;
     const mousedown = (e) => {
+      // Disable zoom on smartphones:
+      if (e.touches && e.touches.length > 1) e.preventDefault();
       r = container.getBoundingClientRect();
       sqSize = this.getSquareWidth(r.width);
       const square = this.idToCoords(e.target.id);
@@ -943,9 +945,10 @@ export default class ChessRules {
       document.addEventListener("mouseup", mouseup);
     }
     if ('ontouchstart' in window) {
-      document.addEventListener("touchstart", mousedown);
-      document.addEventListener("touchmove", mousemove);
-      document.addEventListener("touchend", mouseup);
+      // https://stackoverflow.com/a/42509310/12660887
+      document.addEventListener("touchstart", mousedown, {passive: false});
+      document.addEventListener("touchmove", mousemove, {passive: false});
+      document.addEventListener("touchend", mouseup, {passive: false});
     }
   }
 
@@ -1543,11 +1546,19 @@ export default class ChessRules {
   addPawnMoves([x1, y1], [x2, y2], moves, promotions) {
     let finalPieces = ["p"];
     const color = this.getColor(x1, y1);
+    const oppCol = C.GetOppCol(color);
     const lastRank = (color == "w" ? 0 : this.size.x - 1);
     if (x2 == lastRank && (!this.options["rifle"] || this.board[x2][y2] == ""))
     {
       // promotions arg: special override for Hiddenqueen variant
-      if (promotions) finalPieces = promotions;
+      if (
+        this.options["cannibal"] &&
+        this.board[x2][y2] != "" &&
+        this.getColor(x2, y2) == oppCol
+      ) {
+        finalPieces = [this.getPieceType(x2, y2)];
+      }
+      else if (promotions) finalPieces = promotions;
       else if (this.pawnSpecs.promotions)
         finalPieces = this.pawnSpecs.promotions;
     }