summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2025-03-20 01:22:43 +0100
committerTor Andersson <tor@ccxvii.net>2025-03-20 01:23:42 +0100
commitad43e7881da6202aecf68b22ea5a7db469df1b8e (patch)
tree2d01bbe6f23b380d08b7c77757d9d6f114072d4c
parent3da77df81e8aed49a3c2935b7cc3ff1a735e7924 (diff)
downloadland-and-freedom-ad43e7881da6202aecf68b22ea5a7db469df1b8e.tar.gz
fix get_fronts_closest_to
-rw-r--r--rules.js23
-rw-r--r--rules.ts32
2 files changed, 30 insertions, 25 deletions
diff --git a/rules.js b/rules.js
index 30c59f9..9dd728d 100644
--- a/rules.js
+++ b/rules.js
@@ -2726,17 +2726,22 @@ function lose_hero_points(faction, value) {
logi(`${get_player(faction)} -${points_lost} HP`);
}
function get_fronts_closest_to(target) {
- const values = Object.values(game.fronts).reduce((accrued, current) => {
- if (current.status === null) {
- accrued.push(current.value);
+ 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);
}
- return accrued;
- }, []);
- if (values.length === 0) {
- return [];
}
- const targetValue = target === data_1.CLOSEST_TO_DEFEAT ? Math.min(...values) : Math.max(...values);
- return game.fronts.findIndex((front) => front.value === targetValue);
+ if (minValue === 100)
+ return [];
+ const targetValue = target === data_1.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;
}
function log_br() {
if (game.log.length > 0 && game.log[game.log.length - 1] !== '')
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