From 3ca478329f818efdfe2e03ce72c0effa6a8b0c0b Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Tue, 19 Dec 2023 12:16:44 +0100
Subject: [PATCH] update

---
 base_rules.js             |  8 ++--
 nodemon.json              | 26 -----------
 package.json              |  1 +
 start.sh                  |  2 +-
 utils/setupPieces.js      |  2 +-
 variants.js               |  2 +-
 variants/Coregal/class.js | 93 ++++++++++++++++++++++++++++++++-------
 7 files changed, 86 insertions(+), 48 deletions(-)
 delete mode 100644 nodemon.json

diff --git a/base_rules.js b/base_rules.js
index 8c7d549..ae2ca80 100644
--- a/base_rules.js
+++ b/base_rules.js
@@ -324,7 +324,7 @@ export default class ChessRules {
 
   // Flags part of the FEN string
   getFlagsFen() {
-    return ["w", "b"].map(c => {
+    return ['w', 'b'].map(c => {
       return this.castleFlags[c].map(x => x.toString(36)).join("");
     }).join("");
   }
@@ -340,7 +340,7 @@ export default class ChessRules {
     if (o.init)
       return "000000000000";
     return (
-      ["w","b"].map(c => Object.values(this.reserve[c]).join("")).join("")
+      ['w', 'b'].map(c => Object.values(this.reserve[c]).join("")).join("")
     );
   }
 
@@ -2236,7 +2236,7 @@ export default class ChessRules {
   }
 
   // NOTE: arg "castleFlags" for Coregal or Twokings
-  updateCastleFlags(move, castleFlags) {
+  updateCastleFlags(move, castleFlags, king) {
     castleFlags = castleFlags || this.castleFlags;
     // If flags already off, no need to re-check:
     if (
@@ -2247,7 +2247,7 @@ export default class ChessRules {
     }
     // Update castling flags if start or arrive from/at rook/king locations
     move.appear.concat(move.vanish).forEach(psq => {
-      if (this.isKing(0, 0, psq.p))
+      if ((!!king && psq.p == king) || this.isKing(0, 0, psq.p))
         castleFlags[psq.c] = [this.size.y, this.size.y];
       // NOTE: not "else if" because king can capture enemy rook...
       let c = "";
diff --git a/nodemon.json b/nodemon.json
deleted file mode 100644
index be05234..0000000
--- a/nodemon.json
+++ /dev/null
@@ -1,26 +0,0 @@
-{
-  "ignore": [
-    "app.js",
-    "assets/",
-    "assets.zip",
-    "base_pieces.css",
-    "base_rules.js",
-    "common.css",
-    "favicon.ico",
-    "index.html",
-    "LICENSE",
-    "parameters.js",
-    "parameters.js.dist",
-    "pieces/",
-    "README.md",
-    "start.sh",
-    "stop.sh",
-    "utils/",
-    "TODO",
-    "variants/",
-    "variants.js"
-  ],
-  "watch": [
-    "server.js"
-  ]
-}
diff --git a/package.json b/package.json
index e1c96d5..f76813d 100644
--- a/package.json
+++ b/package.json
@@ -1,4 +1,5 @@
 {
+  "main": "server.js",
   "dependencies": {
     "ws": "^7.5.3"
   },
diff --git a/start.sh b/start.sh
index 35e6b41..53517f8 100755
--- a/start.sh
+++ b/start.sh
@@ -1,5 +1,5 @@
 #!/bin/sh
 
-nodemon ./server.js &
+nodemon -w server.js &
 echo $! > .pid
 php -S localhost:8000 &
diff --git a/utils/setupPieces.js b/utils/setupPieces.js
index 94bbc73..60110cf 100644
--- a/utils/setupPieces.js
+++ b/utils/setupPieces.js
@@ -51,7 +51,7 @@ export const FenUtil = {
             else //p1 > lastP2
               res[lastP2] = b["p1"];
           }
-        }
+        });
       }
     }
     return {fen: res, flags: flags};
diff --git a/variants.js b/variants.js
index dad687f..bc77daf 100644
--- a/variants.js
+++ b/variants.js
@@ -34,7 +34,7 @@ const variants = [
   {name: 'Clorange', desc: 'A Clockwork Orange', disp: 'Clockwork Orange'},
   {name: 'Convert', desc: 'Convert enemy pieces'},
   {name: 'Copycat', desc: 'Borrow powers'},
-//  {name: 'Coregal', desc: 'Two royal pieces'},
+  {name: 'Coregal', desc: 'Two royal pieces'},
 //  {name: 'Coronation', desc: 'Long live the Queen'},
   {name: 'Crazyhouse', desc: 'Captures reborn'},
 //  {name: 'Crossing', desc: 'Cross the river'},
diff --git a/variants/Coregal/class.js b/variants/Coregal/class.js
index d599a1f..15a99e2 100644
--- a/variants/Coregal/class.js
+++ b/variants/Coregal/class.js
@@ -1,4 +1,5 @@
 import ChessRules from "/base_rules.js";
+import {FenUtil} from "/utils/setupPieces.js"
 
 export default class CoregalRules extends ChessRules {
 
@@ -13,12 +14,62 @@ export default class CoregalRules extends ChessRules {
         flags: ['r', 'k', 'l']
       }
     );
+    // Re-arrange flags: king + royal queen positions are only
+    // useful to know ordering, and thus allowed castles.
+    let flags = "";
+    let relPos = { 'w': {}, 'b': {} };
+    for (let c of [0, 1]) {
+      const col = (c == 0 ? 'w' : 'b');
+      let first = "";
+      for (let i=4*c; i<4*(c+1); i++) {
+        const pos = parseInt(flags.charAt(i), 10);
+        const symb = s[col][pos];
+        if (['k', 'l'].includes(symb)) {
+          if (!first) {
+            relPos[col][symb] = '0'; //left
+            first = symb;
+          }
+          else
+            relPos[col][symb] = '1'; //right
+        }
+        else
+          flags += flags.charAt(i);
+      }
+    }
     return {
       fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
            s.w.join("").toUpperCase(),
-      // TODO: re-arrange flags, use another init variable "relPos" (in o)
-      // (maybe after FEN parsing, easier?)
-      o: {flags: s.flags + s.flags} //second set for royal queen
+      o: {
+        flags: flags + flags, //duplicate: one for each royal piece
+        relPos: (
+          relPos['w']['k'] + relPos['w']['l'] +
+          relPos['b']['k'] + relPos['b']['l']
+        )
+      }
+    };
+  }
+
+  getPartFen(o) {
+    return (Object.assign(
+      {"relpos": o.relPos},
+      super.getPartFen(o)
+    ));
+  }
+
+  setOtherVariables(fenParsed, pieceArray) {
+    
+//TODO: issue, relPos is set at init but doesn't persist --> see base_rules.js line 263
+console.log(fenParsed);
+    super.setOtherVariables(fenParsed, pieceArray);
+    this.relPos = {
+      'w': {
+        'k': fenParsed.relpos[0],
+        'l': fenParsed.relpos[1]
+      },
+      'b': {
+        'k': fenParsed.relpos[2],
+        'l': fenParsed.relpos[3]
+      }
     };
   }
 
@@ -30,22 +81,31 @@ export default class CoregalRules extends ChessRules {
     return res;
   }
 
-  // TODO: something like that indeed (+ flags to FEN)
   setFlags(fenflags) {
     this.castleFlags = {
-      'k': { 'w': [...Array(4)], b: [...Array(4)] },
-      'l': { 'w': [...Array(4)], b: [...Array(4)] }
+      k: {
+        w: [0, 1].map(i => parseInt(fenflags.charAt(i), 10)),
+        b: [2, 3].map(i => parseInt(fenflags.charAt(i), 10))
+      },
+      l: {
+        w: [4, 5].map(i => parseInt(fenflags.charAt(i), 10)),
+        b: [6, 7].map(i => parseInt(fenflags.charAt(i), 10))
+      }
     };
-    for (let i = 0; i < 8; i++) {
-      this.castleFlags[i < 4 ? "k" : "l"][i % 4 < 2 ? "w" : "b"] =
-        parseInt(fenflags.charAt(i), 10);
-    }
+  }
+
+  getFlagsFen() {
+    return ['k', 'l'].map(p => {
+      return ['w', 'b'].map(c => {
+        return this.castleFlags[p][c].map(x => x.toString(10)).join("");
+      }).join("")
+    }).join("");
   }
 
   isKing(x, y, p) {
     if (!p)
       p = this.getPiece(x, y);
-    ['k', 'l'].includes(p); //no cannibal mode
+    return ['k', 'l'].includes(p); //no cannibal mode
   }
 
   getCastleMoves([x, y]) {
@@ -55,14 +115,17 @@ export default class CoregalRules extends ChessRules {
     // If left: small castle left, large castle right.
     // If right: usual situation.
     const finalSquares = [
-      this.relPos[c][p] == "left" ? [1, 2] : [2, 3],
-      this.relPos[c][p] == "right" ? [6, 5] : [5, 4]
+      this.relPos[c][p] == '0' ? [1, 2] : [2, 3], //0 == left
+      this.relPos[c][p] == '1' ? [6, 5] : [5, 4] //1 == right
     ];
     const moves =
-      super.getCastleMoves([x, y], finalSquares, null, this.castleFlags[p]);
+      super.getCastleMoves([x, y], finalSquares, null, this.castleFlags[p][c]);
     return moves;
   }
 
-  // TODO: updateFlags (just pass castleFlags arg)
+  updateCastleFlags(move) {
+    super.updateCastleFlags(move, this.castleFlags['k'], 'k');
+    super.updateCastleFlags(move, this.castleFlags['l'], 'l');
+  }
 
 };
-- 
2.44.0