mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-23 21:26:39 +00:00
## Description: Adds a theme system for bot tribe names that maps can customize via their info.json. Instead of all bots using the same global name pool, maps can now specify one or more themed name sets and define custom tribe names with higher priority. Key changes: - New `src/core/execution/utils/tribeNameThemes.json` containing 17 themed name sets (default, north_america, south_america, europe, africa, asia, oceania, space, fantasy, war, western, under_ocean, tournament, funny, scary, weird, vs). The default theme preserves the original `TribeNames.ts` values. **Mappers are encouraged to change that json file, the new themes just serve as an example and are currently not used by any map.** - Maps can specify `"themes": ["europe", "war"]` in info.json to merge prefixes/suffixes from multiple themes into one name pool. - Maps can specify `"custom_tribes": ["Holy Roman Empire", "Kalmar Union"]` for exact tribe names that take priority over theme-generated names. Custom tribes are used first (random selection, no duplicates until exhausted), then theme-based prefix+suffix combos. - `TribeNameResolver` resolves themes per-map at runtime, with fallback to the default theme and a console warning for unknown theme names. - Go codegen (`codegen.go`) updated to propagate `themes` and `custom_tribes` from info.json to Maps.gen.ts. - Germany map now has 404 custom tribes (all German Landkreise and kreisfreie Städte) to showcase the new feature. - Fixed a bug where tribe bots could inherit nation flags if their random name coincidentally matched a nation name (name-based cosmetics lookup now only applies to actual Nations, not Bots). ## 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 ## Please put your Discord username so you can be contacted if a bug or regression is found: FloPinguin
135 lines
5.0 KiB
TypeScript
135 lines
5.0 KiB
TypeScript
import { resolveTribeNameData } from "../src/core/execution/utils/TribeNames";
|
|
import { GameMapType, PlayerType } from "../src/core/game/Game";
|
|
import { PseudoRandom } from "../src/core/PseudoRandom";
|
|
import { createRandomName } from "../src/core/Util";
|
|
|
|
describe("resolveTribeNameData", () => {
|
|
test("returns default theme when called with no arguments", () => {
|
|
const data = resolveTribeNameData();
|
|
expect(data.prefixes.length).toBeGreaterThan(0);
|
|
expect(data.suffixes.length).toBeGreaterThan(0);
|
|
expect(data.customTribes).toBeUndefined();
|
|
// Spot-check that the default theme contains known prefixes.
|
|
expect(data.prefixes).toContain("Roman");
|
|
expect(data.prefixes).toContain("Viking");
|
|
expect(data.suffixes).toContain("Empire");
|
|
expect(data.suffixes).toContain("Kingdom");
|
|
});
|
|
|
|
test("returns default theme when called with undefined", () => {
|
|
const data = resolveTribeNameData(undefined);
|
|
expect(data.prefixes.length).toBeGreaterThan(0);
|
|
expect(data.suffixes.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
test("returns default theme for a map with no theme or customTribes", () => {
|
|
// Most maps in Maps.gen.ts have no theme/customTribes defined.
|
|
const data = resolveTribeNameData(GameMapType.World);
|
|
expect(data.prefixes.length).toBeGreaterThan(0);
|
|
expect(data.suffixes.length).toBeGreaterThan(0);
|
|
expect(data.customTribes).toBeUndefined();
|
|
});
|
|
|
|
test("returns data for every valid GameMapType without throwing", () => {
|
|
for (const mapType of Object.values(GameMapType)) {
|
|
const data = resolveTribeNameData(mapType);
|
|
expect(data.prefixes.length).toBeGreaterThan(0);
|
|
expect(data.suffixes.length).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
test("all default theme prefixes and suffixes are non-empty strings", () => {
|
|
const data = resolveTribeNameData();
|
|
for (const prefix of data.prefixes) {
|
|
expect(prefix.length).toBeGreaterThan(0);
|
|
}
|
|
for (const suffix of data.suffixes) {
|
|
expect(suffix.length).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
test("prefix + suffix combinations produce valid tribe names", () => {
|
|
const data = resolveTribeNameData();
|
|
const random = new PseudoRandom(42);
|
|
for (let i = 0; i < 50; i++) {
|
|
const prefixIndex = random.nextInt(0, data.prefixes.length);
|
|
const suffixIndex = random.nextInt(0, data.suffixes.length);
|
|
const name = `${data.prefixes[prefixIndex]} ${data.suffixes[suffixIndex]}`;
|
|
expect(name.length).toBeGreaterThan(0);
|
|
expect(name).toContain(" ");
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("TribeNameData consistency", () => {
|
|
test("default theme has no duplicate prefixes", () => {
|
|
const data = resolveTribeNameData();
|
|
const unique = new Set(data.prefixes);
|
|
expect(unique.size).toBe(data.prefixes.length);
|
|
});
|
|
|
|
test("default theme has no duplicate suffixes", () => {
|
|
const data = resolveTribeNameData();
|
|
const unique = new Set(data.suffixes);
|
|
expect(unique.size).toBe(data.suffixes.length);
|
|
});
|
|
});
|
|
|
|
describe("tribeNameThemes.json", () => {
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const themes = require("../resources/tribeNameThemes.json");
|
|
|
|
test("default theme exists", () => {
|
|
expect(themes.default).toBeDefined();
|
|
expect(themes.default.prefixes).toBeInstanceOf(Array);
|
|
expect(themes.default.suffixes).toBeInstanceOf(Array);
|
|
});
|
|
|
|
test("all themes have non-empty prefixes and suffixes", () => {
|
|
for (const [, theme] of Object.entries(themes)) {
|
|
const t = theme as { prefixes: string[]; suffixes: string[] };
|
|
expect(t.prefixes.length).toBeGreaterThan(0);
|
|
expect(t.suffixes.length).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
test("all theme entries are non-empty strings", () => {
|
|
for (const [, theme] of Object.entries(themes)) {
|
|
const t = theme as { prefixes: string[]; suffixes: string[] };
|
|
for (const prefix of t.prefixes) {
|
|
expect(prefix.length).toBeGreaterThan(0);
|
|
}
|
|
for (const suffix of t.suffixes) {
|
|
expect(suffix.length).toBeGreaterThan(0);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("at least 3 themes are defined", () => {
|
|
expect(Object.keys(themes).length).toBeGreaterThanOrEqual(3);
|
|
});
|
|
});
|
|
|
|
describe("createRandomName (deterministic)", () => {
|
|
test("returns null for non-human players", () => {
|
|
expect(createRandomName("test", PlayerType.Bot)).toBeNull();
|
|
});
|
|
|
|
test("returns a deterministic name for human players", () => {
|
|
const name1 = createRandomName("Alice", PlayerType.Human);
|
|
const name2 = createRandomName("Alice", PlayerType.Human);
|
|
expect(name1).toBe(name2);
|
|
expect(name1).toBeTruthy();
|
|
expect(name1).toContain("👤");
|
|
});
|
|
|
|
test("different names produce different random names (usually)", () => {
|
|
const name1 = createRandomName("Alice", PlayerType.Human);
|
|
const name2 = createRandomName("Bob", PlayerType.Human);
|
|
// They could theoretically collide, but with 178*66 combinations it's unlikely.
|
|
// We just verify both are valid.
|
|
expect(name1).toBeTruthy();
|
|
expect(name2).toBeTruthy();
|
|
});
|
|
});
|