feat(game-engine): port city changes and NPC troop leaders

This commit is contained in:
2026-07-25 19:49:51 +00:00
parent 18a39ec612
commit 09e4a2233c
8 changed files with 1020 additions and 7 deletions
@@ -0,0 +1,79 @@
import { describe, expect, it } from 'vitest';
import type { City, CitySeed } from '@sammo-ts/logic';
import { applyChangeCity, applyInitialChangeCityEvents } from '../src/turn/monthlyChangeCityAction.js';
const buildCity = (id: number, nationId: number, name = `도시${id}`): City => ({
id,
name,
nationId,
level: 4,
state: 0,
population: 1_001,
populationMax: 2_000,
agriculture: 501,
agricultureMax: 1_000,
commerce: 499,
commerceMax: 1_000,
security: 99,
securityMax: 1_000,
supplyState: 1,
frontState: 0,
defence: 101,
defenceMax: 1_000,
wall: 50,
wallMax: 1_000,
meta: { trust: 40, trade: 100 },
});
describe('ChangeCity monthly action', () => {
it('applies legacy target selection, percentage rounding, clamping, and ordered max changes', () => {
const cities = [buildCity(1, 0, '낙양'), buildCity(2, 1, '장안')];
expect(applyChangeCity(cities, 'free', { pop: '50%', trust: '+70', trade: 999 })).toEqual([
expect.objectContaining({
id: 1,
population: 1_000,
meta: expect.objectContaining({ trust: 100, trade: 105 }),
}),
]);
expect(applyChangeCity(cities, 'occupied', { agri: '*2', comm: '-600' })).toEqual([
expect.objectContaining({ id: 2, agriculture: 1_000, commerce: 0 }),
]);
expect(
applyChangeCity(cities, ['cities', 1, '장안'], {
pop_max: '+100',
pop: '100%',
})
).toEqual([
expect.objectContaining({ id: 2, populationMax: 2_100, population: 2_100 }),
]);
});
it('applies unconditional scenario initial events without changing non-target cities', () => {
const cities = [
{ ...buildCity(1, 0), trust: 40, trade: 100 },
{ ...buildCity(2, 1), trust: 40, trade: 100 },
] as CitySeed[];
const result = applyInitialChangeCityEvents(cities, [
[
true,
['ChangeCity', 'free', { pop: '70%', trust: 80 }],
['ChangeCity', 'occupied', { def: '70%', wall: '70%' }],
],
]);
expect(result[0]).toMatchObject({ population: 1_400, trust: 80, defence: 101, wall: 50 });
expect(result[1]).toMatchObject({ population: 1_001, trust: 40, defence: 700, wall: 700 });
});
it('rejects invalid fields and division by zero', () => {
expect(() => applyChangeCity([buildCity(1, 0)], 'all', { unknown: 1 })).toThrow(
'Unsupported ChangeCity key'
);
expect(() => applyChangeCity([buildCity(1, 0)], 'all', { pop: '/0' })).toThrow(
'divide by zero'
);
});
});
@@ -0,0 +1,249 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/infra';
import type { City, Nation } from '@sammo-ts/logic';
import { createDatabaseTurnHooks } from '../src/turn/databaseHooks.js';
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
import { createChangeCityHandler } from '../src/turn/monthlyChangeCityAction.js';
import { createProvideNpcTroopLeaderHandler } from '../src/turn/monthlyProvideNpcTroopLeaderAction.js';
import { InMemoryReservedTurnStore } from '../src/turn/reservedTurnStore.js';
import { buildCommandEnv } from '../src/turn/reservedTurnCommands.js';
import type { TurnEvent, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
const integration = describe.skipIf(!databaseUrl);
const nationId = 990_091;
const cityId = 990_091;
const generalId = 990_091;
const city: City = {
id: cityId,
name: '부대장지원도시',
nationId,
level: 4,
state: 0,
population: 1_001,
populationMax: 2_000,
agriculture: 501,
agricultureMax: 1_000,
commerce: 499,
commerceMax: 1_000,
security: 99,
securityMax: 1_000,
supplyState: 1,
frontState: 0,
defence: 101,
defenceMax: 1_000,
wall: 50,
wallMax: 1_000,
meta: { trust: 40, trade: 100 },
};
const nation: Nation = {
id: nationId,
name: '지원국',
color: '#777777',
capitalCityId: cityId,
chiefGeneralId: null,
gold: 0,
rice: 0,
power: 0,
level: 2,
typeCode: 'che_중립',
meta: {},
};
const event: TurnEvent = {
id: 1,
targetCode: 'month',
priority: 1_000,
condition: true,
action: [['ProvideNPCTroopLeader']],
meta: {},
};
integration('monthly NPC support database persistence', () => {
let db: GamePrismaClient;
let closeDb: (() => Promise<void>) | undefined;
const clean = async () => {
await db.generalTurn.deleteMany({ where: { generalId } });
await db.rankData.deleteMany({ where: { generalId } });
await db.troop.deleteMany({ where: { troopLeaderId: generalId } });
await db.general.deleteMany({ where: { id: generalId } });
await db.nation.deleteMany({ where: { id: nationId } });
await db.city.deleteMany({ where: { id: cityId } });
};
beforeAll(async () => {
const connector = createGamePostgresConnector({ url: databaseUrl! });
await connector.connect();
db = connector.prisma;
closeDb = () => connector.disconnect();
await clean();
});
afterAll(async () => {
await clean();
await closeDb?.();
});
it('commits the changed city, leader, troop, ranks, assembly turns, and world sequence', async () => {
await db.city.create({
data: {
id: city.id,
name: city.name,
nationId: 0,
level: city.level,
supplyState: city.supplyState,
frontState: city.frontState,
population: city.population,
populationMax: city.populationMax,
agriculture: city.agriculture,
agricultureMax: city.agricultureMax,
commerce: city.commerce,
commerceMax: city.commerceMax,
security: city.security,
securityMax: city.securityMax,
trust: 40,
trade: 100,
defence: city.defence,
defenceMax: city.defenceMax,
wall: city.wall,
wallMax: city.wallMax,
region: 1,
conflict: {},
meta: {},
},
});
await db.nation.create({
data: {
id: nation.id,
name: nation.name,
color: nation.color,
capitalCityId: nation.capitalCityId,
chiefGeneralId: null,
gold: nation.gold,
rice: nation.rice,
tech: 0,
level: nation.level,
typeCode: nation.typeCode,
meta: {},
},
});
await db.city.update({ where: { id: cityId }, data: { nationId } });
const stateRow = await db.worldState.create({
data: {
scenarioCode: 'monthly-npc-support-persistence',
currentYear: 200,
currentMonth: 1,
tickSeconds: 600,
config: {},
meta: {
hiddenSeed: 'monthly-npc-support-persistence',
lastGeneralId: generalId - 1,
lastNPCTroopLeaderID: 40,
},
},
});
const state: TurnWorldState = {
id: stateRow.id,
currentYear: 200,
currentMonth: 1,
tickSeconds: 600,
lastTurnTime: new Date('0200-01-01T00:00:00.000Z'),
meta: {
hiddenSeed: 'monthly-npc-support-persistence',
lastGeneralId: generalId - 1,
lastNPCTroopLeaderID: 40,
},
};
const snapshot: TurnWorldSnapshot = {
scenarioConfig: {
stat: { total: 165, min: 15, max: 80, npcTotal: 150, npcMax: 75, npcMin: 10, chiefMin: 65 },
iconPath: '.',
map: {},
const: {},
environment: { mapName: 'test', unitSet: 'default' },
},
map: { id: 'test', name: 'test', cities: [] },
generals: [],
cities: [city],
nations: [nation],
troops: [],
diplomacy: [],
events: [event],
initialEvents: [],
};
const world = new InMemoryTurnWorld(state, snapshot, {
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
});
const reservedTurns = new InMemoryReservedTurnStore(db, {
maxGeneralTurns: 30,
maxNationTurns: 12,
});
const provide = createProvideNpcTroopLeaderHandler({
getWorld: () => world,
reservedTurns,
env: buildCommandEnv(snapshot.scenarioConfig),
});
const changeCity = createChangeCityHandler({ getWorld: () => world });
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world, { reservedTurns });
try {
const environment = {
year: 200,
month: 1,
startyear: 190,
currentEventID: 1,
turnTime: state.lastTurnTime,
};
await provide([], environment, event);
await changeCity(['all', { pop_max: '+100', pop: '50%', trust: 80, trade: 105 }], environment, event);
await dbHooks.hooks.flushChanges?.({
lastTurnTime: state.lastTurnTime.toISOString(),
processedGenerals: 0,
processedTurns: 1,
durationMs: 0,
partial: false,
});
expect(await db.city.findUniqueOrThrow({ where: { id: cityId } })).toMatchObject({
populationMax: 2_100,
population: 1_050,
trust: 80,
trade: 105,
});
expect(await db.general.findUniqueOrThrow({ where: { id: generalId } })).toMatchObject({
name: '㉥부대장 41',
nationId,
cityId,
troopId: generalId,
leadership: 10,
strength: 10,
intel: 10,
experience: 2_000,
dedication: 2_000,
npcState: 5,
affinity: 999,
meta: expect.objectContaining({ killturn: 70 }),
});
expect(
await db.troop.findUniqueOrThrow({ where: { troopLeaderId: generalId } })
).toMatchObject({
nationId,
name: '㉥부대장 41',
});
const turns = await db.generalTurn.findMany({ where: { generalId } });
expect(turns).toHaveLength(30);
expect(new Set(turns.map((turn) => turn.actionCode))).toEqual(new Set(['che_집합']));
expect(await db.rankData.count({ where: { generalId } })).toBe(41);
expect((await db.worldState.findUniqueOrThrow({ where: { id: stateRow.id } })).meta).toMatchObject({
lastNPCTroopLeaderID: 41,
});
} finally {
await dbHooks.close();
await db.worldState.deleteMany({ where: { id: stateRow.id } });
}
});
});
@@ -0,0 +1,206 @@
import { LiteHashDRBG, RandUtil } from '@sammo-ts/common';
import { simpleSerialize } from '@sammo-ts/logic/war/utils.js';
import { describe, expect, it, vi } from 'vitest';
import type { City, Nation } from '@sammo-ts/logic';
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
import { createProvideNpcTroopLeaderHandler } from '../src/turn/monthlyProvideNpcTroopLeaderAction.js';
import { InMemoryReservedTurnStore } from '../src/turn/reservedTurnStore.js';
import { buildCommandEnv } from '../src/turn/reservedTurnCommands.js';
import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
const buildCity = (id: number, nationId: number): City => ({
id,
name: `도시${id}`,
nationId,
level: 4,
state: 0,
population: 1_000,
populationMax: 2_000,
agriculture: 500,
agricultureMax: 1_000,
commerce: 500,
commerceMax: 1_000,
security: 500,
securityMax: 1_000,
supplyState: 1,
frontState: 0,
defence: 500,
defenceMax: 1_000,
wall: 500,
wallMax: 1_000,
meta: {},
});
const buildNation = (id: number, level: number): Nation => ({
id,
name: `국가${id}`,
color: '#777777',
capitalCityId: id,
chiefGeneralId: null,
gold: 0,
rice: 0,
power: 0,
level,
typeCode: 'che_중립',
meta: {},
});
const buildGeneral = (id: number, nationId: number, npcState = 0): TurnGeneral => ({
id,
userId: null,
name: `장수${id}`,
nationId,
cityId: nationId,
troopId: 0,
stats: { leadership: 50, strength: 50, intelligence: 50 },
experience: 0,
dedication: 0,
officerLevel: 1,
role: {
personality: null,
specialDomestic: null,
specialWar: null,
items: { horse: null, weapon: null, book: null, item: null },
},
injury: 0,
gold: 0,
rice: 0,
crew: 0,
crewTypeId: 1100,
train: 0,
atmos: 0,
age: 30,
npcState,
bornYear: 170,
deadYear: 250,
affinity: 1,
picture: 'default.jpg',
triggerState: { flags: {}, counters: {}, modifiers: {}, meta: {} },
lastTurn: { command: '휴식' },
turnTime: new Date('0200-01-01T00:00:00.000Z'),
recentWarTime: null,
meta: { killturn: 1_000 },
});
const scenarioConfig: TurnWorldSnapshot['scenarioConfig'] = {
stat: { total: 300, min: 10, max: 100, npcTotal: 150, npcMax: 75, npcMin: 10, chiefMin: 70 },
iconPath: '.',
map: {},
const: {},
environment: { mapName: 'test', unitSet: 'default' },
};
describe('ProvideNPCTroopLeader monthly action', () => {
it('fills each nation level quota and creates matching troops and 30 assembly turns', async () => {
const state: TurnWorldState = {
id: 1,
currentYear: 200,
currentMonth: 1,
tickSeconds: 600,
lastTurnTime: new Date('0200-01-01T00:00:00.000Z'),
meta: {
hiddenSeed: process.env.REF_HIDDEN_SEED ?? 'troop-leader-fixture',
lastNPCTroopLeaderID: 8,
},
};
const snapshot: TurnWorldSnapshot = {
scenarioConfig,
map: { id: 'test', name: 'test', cities: [] },
generals: [buildGeneral(1, 1), buildGeneral(2, 2, 5)],
cities: [buildCity(1, 1), buildCity(2, 1)],
nations: [buildNation(1, 3), buildNation(2, 2)],
troops: [],
diplomacy: [],
events: [],
initialEvents: [],
};
const world = new InMemoryTurnWorld(state, snapshot, {
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
});
const prisma = {
generalTurn: { findMany: vi.fn(), deleteMany: vi.fn(), createMany: vi.fn() },
nationTurn: { findMany: vi.fn(), deleteMany: vi.fn(), createMany: vi.fn() },
};
const reservedTurns = new InMemoryReservedTurnStore(prisma as never, {
maxGeneralTurns: 30,
maxNationTurns: 12,
});
const handler = createProvideNpcTroopLeaderHandler({
getWorld: () => world,
reservedTurns,
env: buildCommandEnv(scenarioConfig),
});
await handler(
[],
{
year: 200,
month: 1,
startyear: 190,
currentEventID: 1,
turnTime: state.lastTurnTime,
},
{ id: 1, targetCode: 'month', priority: 1, condition: true, action: [], meta: {} }
);
const created = world.peekDirtyState().createdGenerals;
expect(created).toHaveLength(3);
expect(created.map((general) => general.name)).toEqual([
'㉥부대장 9',
'㉥부대장 10',
'㉥부대장 11',
]);
expect(created[0]).toMatchObject({
nationId: 1,
cityId: process.env.REF_HIDDEN_SEED ? 2 : 1,
troopId: 3,
stats: { leadership: 10, strength: 10, intelligence: 10 },
experience: 2_000,
dedication: 2_000,
officerLevel: 1,
role: { personality: 'che_은둔' },
gold: 0,
rice: 0,
age: 20,
npcState: 5,
bornYear: 180,
deadYear: 260,
affinity: 999,
meta: { killturn: 70, specage: 999, specage2: 999 },
});
expect(world.peekDirtyState().createdTroops).toEqual(
created.map((general) => ({
id: general.id,
nationId: general.nationId,
name: general.name,
}))
);
for (const general of created) {
expect(reservedTurns.getGeneralTurns(general.id)).toEqual(
Array.from({ length: 30 }, () => ({ action: 'che_집합', args: {} }))
);
}
expect(world.getState().meta.lastNPCTroopLeaderID).toBe(11);
if (process.env.REF_HIDDEN_SEED) {
const probe = new RandUtil(
new LiteHashDRBG(simpleSerialize(process.env.REF_HIDDEN_SEED, 'troopLeader', 200, 1, 1))
);
expect([
probe.choice([1, 2]),
probe.nextRangeInt(0, 599),
probe.nextRangeInt(0, 999_999),
]).toEqual([2, 567, 821_811]);
expect(
created.map((general) => ({
cityId: general.cityId,
turnTime: general.turnTime.toISOString(),
}))
).toEqual([
{ cityId: 2, turnTime: '0200-01-01T00:09:27.821Z' },
{ cityId: 1, turnTime: '0200-01-01T00:01:59.665Z' },
{ cityId: 2, turnTime: '0200-01-01T00:07:50.470Z' },
]);
}
});
});
+53 -4
View File
@@ -16,6 +16,15 @@ type ScenarioSeederPrismaClient = {
};
city: {
count(): Promise<number>;
findUnique(args: { where: { id: number } }): Promise<{
population: number;
agriculture: number;
commerce: number;
security: number;
trust: number;
defence: number;
wall: number;
} | null>;
};
general: {
count(): Promise<number>;
@@ -26,6 +35,9 @@ type ScenarioSeederPrismaClient = {
where: { srcNationId: number; destNationId: number };
}): Promise<{ stateCode: number; term: number } | null>;
};
event: {
count(): Promise<number>;
};
worldState: {
findFirst(): Promise<{
config: unknown;
@@ -50,7 +62,7 @@ const requiredTables = [
const hasRequiredTables = async (prisma: ScenarioSeederPrismaClient, schemaName: string): Promise<boolean> => {
for (const table of requiredTables) {
const result = (await prisma.$queryRawUnsafe(
`SELECT to_regclass('${schemaName}.${table}') as regclass`
`SELECT to_regclass('${schemaName}.${table}')::text as regclass`
)) as Array<{ regclass: string | null }>;
if (!Array.isArray(result) || result.length === 0 || result[0]?.regclass === null) {
return false;
@@ -90,19 +102,52 @@ describeDb('scenario database seed', () => {
await connector.connect();
try {
const prisma = connector.prisma as unknown as ScenarioSeederPrismaClient;
const [nationCount, cityCount, generalCount, diplomacyCount] = await Promise.all([
const [nationCount, cityCount, generalCount, diplomacyCount, eventCount] = await Promise.all([
prisma.nation.count(),
prisma.city.count(),
prisma.general.count(),
prisma.diplomacy.count(),
prisma.event.count(),
]);
expect(nationCount).toBe(seed.nations.length);
expect(cityCount).toBe(seed.cities.length);
expect(generalCount).toBe(seed.generals.length);
expect(diplomacyCount).toBe(seed.nations.length * Math.max(0, seed.nations.length - 1));
expect(eventCount).toBe(seed.events.length);
expect(generalCount).toBeGreaterThan(0);
const freeCity = seed.cities.find((city) => city.nationId === 0);
const occupiedCity = seed.cities.find((city) => city.nationId !== 0);
expect(freeCity).toMatchObject({
population: freeCity ? Math.round(freeCity.populationMax * 0.7) : undefined,
agriculture: freeCity ? Math.round(freeCity.agricultureMax * 0.7) : undefined,
commerce: freeCity ? Math.round(freeCity.commerceMax * 0.7) : undefined,
security: freeCity ? Math.round(freeCity.securityMax * 0.7) : undefined,
trust: 80,
});
expect(occupiedCity).toMatchObject({
population: occupiedCity ? Math.round(occupiedCity.populationMax * 0.7) : undefined,
defence: occupiedCity ? Math.round(occupiedCity.defenceMax * 0.7) : undefined,
wall: occupiedCity ? Math.round(occupiedCity.wallMax * 0.7) : undefined,
trust: 80,
});
for (const city of [freeCity, occupiedCity]) {
expect(city).toBeDefined();
if (!city) {
continue;
}
expect(await prisma.city.findUnique({ where: { id: city.id } })).toMatchObject({
population: city.population,
agriculture: city.agriculture,
commerce: city.commerce,
security: city.security,
trust: city.trust,
defence: city.defence,
wall: city.wall,
});
}
if (seed.diplomacy.length > 0) {
const sample = seed.diplomacy[0];
const row = await prisma.diplomacy.findFirst({
@@ -158,8 +203,12 @@ describeDb('scenario database seed', () => {
},
});
const expectedGenerals = scenario.generals.length + scenario.generalsNeutral.length;
expect(seed.generals.length).toBe(expectedGenerals);
// Future-born entries are converted to delayed events, so the raw
// scenario array length is not the installed general count.
expect(seed.generals.length).toBeGreaterThan(0);
expect(seed.generals.length).toBeLessThan(
scenario.generals.length + scenario.generalsNeutral.length + scenario.generalsEx.length
);
const connector = createGamePostgresConnector({ url: databaseUrl });
await connector.connect();