Skip to content

Commit c17c037

Browse files
committed
Refactor: extract estimateTroopSources helper for estimatedTroopsTerritory/city to remove duplication and improve maintainability
1 parent bbe0457 commit c17c037

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/core/configuration/Config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ export interface Config {
131131
* Maximum troop capacity derived from territory ownership (tiles).
132132
* This represents how many troops the player's territories can hold.
133133
* @param player The player or player view to compute territory capacity for.
134+
* @returns The maximum number of troops capacity derived from the player's territories.
134135
*/
135136
maxTroopsTerritory(player: Player | PlayerView): number;
136137

137138
/**
138139
* Maximum troop capacity derived from cities (unit levels of UnitType.City).
139140
* @param player The player or player view to compute city capacity for.
141+
* @returns The maximum number of troops capacity derived from the player's cities.
140142
*/
141143
maxTroopsCity(player: Player | PlayerView): number;
142144

@@ -145,6 +147,7 @@ export interface Config {
145147
* attributable to territory-derived capacity. These are proportional
146148
* estimates (based on capacity ratios) and do not represent exact tracking.
147149
* @param player The player or player view to compute an estimate for.
150+
* @returns Estimated number of the player's troops attributable to territory capacity as a number.
148151
*/
149152
estimatedTroopsTerritory(player: Player | PlayerView): number;
150153

@@ -153,6 +156,7 @@ export interface Config {
153156
* attributable to city-derived capacity. These are proportional estimates
154157
* (based on capacity ratios) and do not represent exact tracking.
155158
* @param player The player or player view to compute an estimate for.
159+
* @returns Estimated number of player's troops attributable to city capacity as a number.
156160
*/
157161
estimatedTroopsCity(player: Player | PlayerView): number;
158162

src/core/configuration/DefaultConfig.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -846,25 +846,37 @@ export class DefaultConfig implements Config {
846846
}
847847

848848
/**
849-
* Estimate how many of the player's current troops are attributable to
850-
* territory-derived capacity. This is a proportional estimate based on the
851-
* ratio of territory capacity to total capacity, multiplied by current troops.
852-
* Note: this is only an estimate and not precise tracking of troop sources.
849+
* Private helper to estimate troop source breakdowns for a player.
850+
* Returns both territory and city estimates as an object.
853851
* @param player Player or PlayerView used to compute the estimate.
854852
*/
855-
estimatedTroopsTerritory(player: Player | PlayerView): number {
853+
private estimateTroopSources(player: Player | PlayerView): {
854+
territory: number;
855+
city: number;
856+
} {
856857
const maxTroopsTerritory = this.maxTroopsTerritory(player);
857858
const maxTroopsCity = this.maxTroopsCity(player);
858-
859859
const baseCapacity = maxTroopsTerritory + maxTroopsCity;
860-
if (baseCapacity === 0) return 0;
861-
860+
if (baseCapacity === 0) return { territory: 0, city: 0 };
862861
const currentTroops = player.troops();
862+
return {
863+
territory: Math.round(
864+
(maxTroopsTerritory / baseCapacity) * currentTroops,
865+
),
866+
city: Math.round((maxTroopsCity / baseCapacity) * currentTroops),
867+
};
868+
}
863869

864-
// Proportionally attribute current troops based on territory's share of the
865-
// unscaled base capacity (territory + city). This keeps the numerator and
866-
// denominator consistent for all player types.
867-
return Math.round((maxTroopsTerritory / baseCapacity) * currentTroops);
870+
/**
871+
* Estimate how many of the player's current troops are attributable to
872+
* territory-derived capacity. This is a proportional estimate based on the
873+
* ratio of territory capacity to total capacity, multiplied by current troops.
874+
* Note: this is only an estimate and not precise tracking of troop sources.
875+
* @param player Player or PlayerView used to compute the estimate.
876+
* @returns Estimated number of the player's troops attributable to territory-derived capacity (proportional, not exact).
877+
*/
878+
estimatedTroopsTerritory(player: Player | PlayerView): number {
879+
return this.estimateTroopSources(player).territory;
868880
}
869881

870882
/**
@@ -873,20 +885,10 @@ export class DefaultConfig implements Config {
873885
* ratio of city capacity to total capacity, multiplied by current troops.
874886
* Note: this is only an estimate and not precise tracking of troop sources.
875887
* @param player Player or PlayerView used to compute the estimate.
888+
* @returns Estimated number of troops attributable to city capacity as a number.
876889
*/
877890
estimatedTroopsCity(player: Player | PlayerView): number {
878-
const maxTroopsTerritory = this.maxTroopsTerritory(player);
879-
const maxTroopsCity = this.maxTroopsCity(player);
880-
881-
const baseCapacity = maxTroopsTerritory + maxTroopsCity;
882-
if (baseCapacity === 0) return 0;
883-
884-
const currentTroops = player.troops();
885-
886-
// Proportionally attribute current troops based on cities' share of the
887-
// unscaled base capacity (territory + city). This keeps the numerator and
888-
// denominator consistent for all player types.
889-
return Math.round((maxTroopsCity / baseCapacity) * currentTroops);
891+
return this.estimateTroopSources(player).city;
890892
}
891893

892894
/**

0 commit comments

Comments
 (0)