From f9f6716634c2faf722b174a9bab7f704a0e65f24 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Tue, 26 May 2020 14:24:24 +0200
Subject: [PATCH] Fix Koopa promotions with captures, and Balakhlava: pawns
 move forward

---
 client/src/variants/Balaklava.js | 13 ++++++++++++-
 client/src/variants/Koopa.js     | 12 +++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/client/src/variants/Balaklava.js b/client/src/variants/Balaklava.js
index 3a04e1d0..4a720f04 100644
--- a/client/src/variants/Balaklava.js
+++ b/client/src/variants/Balaklava.js
@@ -61,8 +61,19 @@ export class BalaklavaRules extends ChessRules {
         : super.getPotentialMovesFrom([x, y]);
     if (piece != V.KING) {
       // Add non-capturing knight movements
-      const lastRank = (this.turn == 'w' ? 0 : 7);
+      const color = this.turn;
+      const lastRank = (color == 'w' ? 0 : 7);
       V.steps[V.KNIGHT].forEach(step => {
+        // Pawns cannot go backward:
+        if (
+          piece == V.PAWN &&
+          (
+            (color == 'w' && step[0] > 0) ||
+            (color == 'b' && step[0] < 0)
+          )
+        ) {
+          return;
+        }
         const [i, j] = [x + step[0], y + step[1]];
         if (
           V.OnBoard(i, j) &&
diff --git a/client/src/variants/Koopa.js b/client/src/variants/Koopa.js
index 8d4f0e97..57d19b92 100644
--- a/client/src/variants/Koopa.js
+++ b/client/src/variants/Koopa.js
@@ -118,7 +118,17 @@ export class KoopaRules extends ChessRules {
   }
 
   getPotentialMovesFrom([x, y]) {
-    let moves = super.getPotentialMovesFrom([x, y]);
+    let moves = super.getPotentialMovesFrom([x, y]).filter(m => {
+      if (
+        m.vanish[0].p != V.PAWN ||
+        m.appear[0].p == V.PAWN ||
+        m.vanish.length == 1
+      ) {
+        return true;
+      }
+      // Pawn promotion, "capturing": remove duplicates
+      return m.appear[0].p == V.QUEEN;
+    });
     // Complete moves: stuns & kicks
     let promoteAfterStun = [];
     const color = this.turn;
-- 
2.48.1