From c0b036aaf5a2a8e477c955623df2a8e5ce826151 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sun, 29 Mar 2020 16:29:03 +0200
Subject: [PATCH] Fix Omega and Wormhole FEN parsing

---
 client/src/variants/Omega.js    | 22 ++++++++++++++++++++++
 client/src/variants/Wormhole.js | 22 ++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/client/src/variants/Omega.js b/client/src/variants/Omega.js
index e6826718..3310dd5e 100644
--- a/client/src/variants/Omega.js
+++ b/client/src/variants/Omega.js
@@ -36,6 +36,28 @@ export class OmegaRules extends ChessRules {
     return ([V.CHAMPION, V.WIZARD].includes(b[1]) ? "Omega/" : "") + b;
   }
 
+  static IsGoodPosition(position) {
+    if (position.length == 0) return false;
+    const rows = position.split("/");
+    if (rows.length != V.size.x) return false;
+    let kings = { "k": 0, "K": 0 };
+    for (let row of rows) {
+      let sumElts = 0;
+      for (let i = 0; i < row.length; i++) {
+        if (['K','k'].includes(row[i])) kings[row[i]]++;
+        if (['x'].concat(V.PIECES).includes(row[i].toLowerCase())) sumElts++;
+        else {
+          const num = parseInt(row[i]);
+          if (isNaN(num)) return false;
+          sumElts += num;
+        }
+      }
+      if (sumElts != V.size.y) return false;
+    }
+    if (Object.values(kings).some(v => v != 1)) return false;
+    return true;
+  }
+
   // NOTE: keep this extensive check because the board has holes
   static IsGoodEnpassant(enpassant) {
     if (enpassant != "-") {
diff --git a/client/src/variants/Wormhole.js b/client/src/variants/Wormhole.js
index 20c9d119..cc9fd497 100644
--- a/client/src/variants/Wormhole.js
+++ b/client/src/variants/Wormhole.js
@@ -28,6 +28,28 @@ export class WormholeRules extends ChessRules {
     return b;
   }
 
+  static IsGoodPosition(position) {
+    if (position.length == 0) return false;
+    const rows = position.split("/");
+    if (rows.length != V.size.x) return false;
+    let kings = { "k": 0, "K": 0 };
+    for (let row of rows) {
+      let sumElts = 0;
+      for (let i = 0; i < row.length; i++) {
+        if (['K','k'].includes(row[i])) kings[row[i]]++;
+        if (['x'].concat(V.PIECES).includes(row[i].toLowerCase())) sumElts++;
+        else {
+          const num = parseInt(row[i]);
+          if (isNaN(num)) return false;
+          sumElts += num;
+        }
+      }
+      if (sumElts != V.size.y) return false;
+    }
+    if (Object.values(kings).some(v => v != 1)) return false;
+    return true;
+  }
+
   getSquareAfter(square, movement) {
     let shift1, shift2;
     if (Array.isArray(movement[0])) {
-- 
2.44.0