Files
OpenFrontIO/src/client/utilities/GameConfigHelpers.ts
T
FloPinguin 0b9d43cb46 Configurable nation count 🤖 (#3338)
## Description:

I hope we can get this into v30?
The nation count is configurable now, just like the bot count.
Replaced the "Disable Nations" toggle with a nations slider (0–400) in
SinglePlayer and Host Lobby modals.

<img width="710" height="121" alt="Screenshot 2026-03-03 021952"
src="https://github.com/user-attachments/assets/c8d0f0c3-db51-4303-95fa-dbc770460ec2"
/>


Public games are staying exactly the same, this is just for singleplayer
and private lobby fun.
Youtubers could play HvN against 400 nations, for example.
Singleplayer enjoyers no longer have to play against 1 nation in HvN,
they can freely choose.

`GameConfig.disableNations: boolean` got replaced by `nations: number
(0-400, optional)`
`undefined` = map default, 
`0` = disabled, 
number = custom count

Nations slider defaults to the map's nation count, shows "(MAP DEFAULT)"
label when unchanged
Compact map toggle reduces nations to 25% when at default, restores when
toggled off (just like we already do with bots)
The nation count for HvN no longer automatically matches the human count
in singleplayer and private games, only in public games.

**What if there aren't enough nations configured for the map?**
We just use the HvN logic (Generate random nations)

### Warning

**This infra PR also needs to get merged:
https://github.com/openfrontio/infra/pull/263
Otherwise players can set 0 nations and get achievements.**

## Please complete the following:

- [X] I have added screenshots for all UI updates
- [X] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [X] I have added relevant tests to the test directory
- [X] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

FloPinguin
2026-03-03 14:07:06 -08:00

141 lines
3.3 KiB
TypeScript

import { GameMapType, UnitType } from "../../core/game/Game";
import { GameConfig } from "../../core/Schemas";
/**
* Maps a slider value (0-400) to the nations config value.
* 0 → "disabled", value === defaultNationCount → "default", otherwise → number.
*/
export function sliderToNationsConfig(
sliderValue: number,
defaultNationCount: number,
): GameConfig["nations"] {
if (sliderValue === 0) return "disabled";
if (sliderValue === defaultNationCount) return "default";
return sliderValue;
}
/**
* Maps a nations config value to a slider-friendly number.
* "disabled" → 0, "default" → defaultNationCount, number → number.
*/
export function nationsConfigToSlider(
nations: GameConfig["nations"],
defaultNationCount: number,
): number {
if (nations === "disabled") return 0;
if (nations === "default") return defaultNationCount;
return nations;
}
export function toOptionalNumber(
value: number | string | undefined,
): number | undefined {
if (typeof value === "number") {
return Number.isFinite(value) ? value : undefined;
}
if (typeof value === "string") {
const trimmed = value.trim();
if (!trimmed) return undefined;
const numeric = Number(trimmed);
return Number.isFinite(numeric) ? numeric : undefined;
}
return undefined;
}
export function preventDisallowedKeys(
e: KeyboardEvent,
disallowedKeys: string[],
): void {
if (disallowedKeys.includes(e.key)) {
e.preventDefault();
}
}
export function parseBoundedIntegerFromInput(
input: HTMLInputElement,
{
min,
max,
stripPattern = /[eE+-]/g,
radix = 10,
}: {
min: number;
max: number;
stripPattern?: RegExp;
radix?: number;
},
): number | undefined {
input.value = input.value.replace(stripPattern, "");
const value = parseInt(input.value, radix);
if (isNaN(value) || value < min || value > max) {
return undefined;
}
return value;
}
export function parseBoundedFloatFromInput(
input: HTMLInputElement,
{ min, max }: { min: number; max: number },
): number | undefined {
const value = parseFloat(input.value);
if (isNaN(value) || value < min || value > max) {
return undefined;
}
return value;
}
export function getBotsForCompactMap(
bots: number,
compactMapEnabled: boolean,
): number {
if (compactMapEnabled && bots === 400) {
return 100;
}
if (!compactMapEnabled && bots === 100) {
return 400;
}
return bots;
}
export function getNationsForCompactMap(
nations: number,
defaultNationCount: number,
compactMapEnabled: boolean,
): number {
const compactCount = Math.max(0, Math.floor(defaultNationCount * 0.25));
if (compactMapEnabled) {
// Only reduce if at the full default
if (nations === defaultNationCount) {
return compactCount;
}
return nations;
}
// Restoring from compact: if at the compact default, go back to full default
if (nations === compactCount) {
return defaultNationCount;
}
return nations;
}
export function getRandomMapType(): GameMapType {
const maps = Object.values(GameMapType);
const randIdx = Math.floor(Math.random() * maps.length);
return maps[randIdx] as GameMapType;
}
export function getUpdatedDisabledUnits(
disabledUnits: UnitType[],
unit: UnitType,
checked: boolean,
): UnitType[] {
return checked
? [...disabledUnits, unit]
: disabledUnits.filter((u) => u !== unit);
}