From a2bb7e0621f143f912ca505fed914fe0a5e6d611 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 20 Jun 2022 21:46:20 +0200
Subject: [PATCH] Fix Chakart promotions after effects

---
 base_rules.js             |  2 ++
 variants/Chakart/CREDITS  |  1 +
 variants/Chakart/class.js | 28 ++++++++++++++++++++++------
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/base_rules.js b/base_rules.js
index 8ad0894..8b07059 100644
--- a/base_rules.js
+++ b/base_rules.js
@@ -1007,6 +1007,8 @@ export default class ChessRules {
     let chessboard = container.querySelector(".chessboard");
     let choices = document.createElement("div");
     choices.id = "choices";
+    if (!r)
+      r = chessboard.getBoundingClientRect();
     choices.style.width = r.width + "px";
     choices.style.height = r.height + "px";
     choices.style.left = r.x + "px";
diff --git a/variants/Chakart/CREDITS b/variants/Chakart/CREDITS
index e15c948..53a9181 100644
--- a/variants/Chakart/CREDITS
+++ b/variants/Chakart/CREDITS
@@ -5,3 +5,4 @@ https://commons.wikimedia.org/wiki/File:Tux_Paint_banana.svg
 https://www.onlinewebfonts.com/icon/425540
 https://www.svgrepo.com/svg/264673/easter-egg-easter
 https://www.svgrepo.com/svg/321648/turtle-shell
+https://svgsilh.com/image/40876.html
diff --git a/variants/Chakart/class.js b/variants/Chakart/class.js
index c987649..a0c854b 100644
--- a/variants/Chakart/class.js
+++ b/variants/Chakart/class.js
@@ -306,7 +306,7 @@ export default class ChakartRules extends ChessRules {
         moves.push(this.getBasicMove([x, y], [x + shiftX, y + shiftY]));
       }
     }
-    this.pawnPostProcess(moves, color, oppCol);
+    super.pawnPostProcess(moves, color, oppCol);
     // Add mushroom on before-last square
     moves.forEach(m => {
       let revStep = [m.start.x - m.end.x, m.start.y - m.end.y];
@@ -389,6 +389,22 @@ export default class ChakartRules extends ChessRules {
   }
 
   play(move) {
+    const color = this.turn;
+    const oppCol = C.GetOppCol(color);
+    if (
+      move.appear.length > 0 &&
+      move.appear[0].p == 'p' &&
+      (
+        (color == 'w' && move.end.x == 0) ||
+        (color == 'b' && move.end.x == this.size.x - 1)
+      )
+    ) {
+      // "Forgotten" promotion, which occurred after some effect
+      let moves = [move];
+      super.pawnPostProcess(moves, color, oppCol);
+      super.showChoices(moves);
+      return false;
+    }
     if (!move.nextComputed) {
       // Set potential random effects, so that play() is deterministic
       // from opponent viewpoint:
@@ -441,8 +457,6 @@ export default class ChakartRules extends ChessRules {
       move.nextComputed = true;
     }
     this.egg = move.egg;
-    const color = this.turn;
-    const oppCol = C.GetOppCol(color);
     if (move.egg == "toadette") {
       this.reserve = { w: {}, b: {} };
       // Randomly select a piece in pawnPromotions
@@ -510,6 +524,7 @@ export default class ChakartRules extends ChessRules {
       this.displayBonus(move);
     this.playOnBoard(move);
     this.nextMove = move.next;
+    return true;
   }
 
   // Helper to set and apply banana/bomb effect
@@ -687,9 +702,10 @@ export default class ChakartRules extends ChessRules {
   }
 
   playPlusVisual(move, r) {
-    this.moveStack.push(move);
     const nextLines = () => {
-      this.play(move);
+      if (!this.play(move))
+        return;
+      this.moveStack.push(move);
       this.playVisual(move, r);
       if (this.nextMove)
         this.playPlusVisual(this.nextMove, r);
@@ -698,7 +714,7 @@ export default class ChakartRules extends ChessRules {
         this.moveStack = [];
       }
     };
-    if (this.moveStack.length == 1)
+    if (this.moveStack.length == 0)
       nextLines();
     else
       this.animate(move, nextLines);
-- 
2.44.0