From ad43e7881da6202aecf68b22ea5a7db469df1b8e Mon Sep 17 00:00:00 2001
From: Tor Andersson <tor@ccxvii.net>
Date: Thu, 20 Mar 2025 01:22:43 +0100
Subject: fix get_fronts_closest_to

---
 rules.ts | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

(limited to 'rules.ts')

diff --git a/rules.ts b/rules.ts
index ca1f191..4057e40 100644
--- a/rules.ts
+++ b/rules.ts
@@ -3419,27 +3419,27 @@ function lose_hero_points(faction: FactionId, value: number) {
 // #region FRONTS
 
 function get_fronts_closest_to(target: ClosestToDefeat | ClosestToVictory): FrontId[] {
-  const values = Object.values(game.fronts).reduce(
-    (accrued: number[], current: Front) => {
-      if (current.status === null) {
-        accrued.push(current.value);
-      }
-      return accrued;
-    },
-    []
-  );
+  let minValue = 100;
+  let maxValue = -100;
+  for (let front of game.fronts) {
+    if (front.status === null) {
+      minValue = Math.min(minValue, front.value);
+      maxValue = Math.max(maxValue, front.value);
+    }
+  }
 
   // Possible if all fronts have either been
   // defeated or are victorious
-  if (values.length === 0) {
+  if (minValue === 100)
     return [];
-  }
 
-  const targetValue =
-    target === CLOSEST_TO_DEFEAT ? Math.min(...values) : Math.max(...values);
-  return game.fronts.findIndex(
-    (front) => front.value === targetValue
-  ) as unknown as FrontId[];
+  const targetValue = target === CLOSEST_TO_DEFEAT ? minValue : maxValue;
+
+  const closest = []
+  for (let i = 0; i < game.fronts.length; ++i)
+    if (game.fronts[i].value === targetValue)
+      closest.push(i)
+  return closest
 }
 
 // #endregion
-- 
cgit v1.2.3