feat: migrate monthly city trade rates
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { MapDefinition } from '@sammo-ts/logic';
|
||||
import type { City, MapDefinition } from '@sammo-ts/logic';
|
||||
|
||||
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
||||
import { createMonthlyEventHandler, type MonthlyEventActionHandler } from '../src/turn/monthlyEventHandler.js';
|
||||
import {
|
||||
createMonthlyEventHandler,
|
||||
createRandomizeCityTradeRateHandler,
|
||||
type MonthlyEventActionHandler,
|
||||
} from '../src/turn/monthlyEventHandler.js';
|
||||
import type { TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||
|
||||
const map: MapDefinition = {
|
||||
@@ -14,7 +18,8 @@ const map: MapDefinition = {
|
||||
|
||||
const buildWorld = (
|
||||
events: TurnWorldSnapshot['events'],
|
||||
actions: Map<string, MonthlyEventActionHandler>
|
||||
actions: Map<string, MonthlyEventActionHandler>,
|
||||
cities: City[] = []
|
||||
): InMemoryTurnWorld => {
|
||||
const state: TurnWorldState = {
|
||||
id: 1,
|
||||
@@ -22,7 +27,7 @@ const buildWorld = (
|
||||
currentMonth: 12,
|
||||
tickSeconds: 600,
|
||||
lastTurnTime: new Date('0189-12-01T00:00:00.000Z'),
|
||||
meta: {},
|
||||
meta: { hiddenSeed: 'monthly-event-test-seed' },
|
||||
};
|
||||
const snapshot: TurnWorldSnapshot = {
|
||||
scenarioConfig: {
|
||||
@@ -45,7 +50,7 @@ const buildWorld = (
|
||||
events,
|
||||
initialEvents: [],
|
||||
generals: [],
|
||||
cities: [],
|
||||
cities,
|
||||
nations: [],
|
||||
troops: [],
|
||||
};
|
||||
@@ -62,6 +67,30 @@ const buildWorld = (
|
||||
return world;
|
||||
};
|
||||
|
||||
const buildCity = (id: number, level: number): City => ({
|
||||
id,
|
||||
name: `도시${id}`,
|
||||
nationId: 0,
|
||||
level,
|
||||
state: 0,
|
||||
population: 1_000,
|
||||
populationMax: 2_000,
|
||||
agriculture: 100,
|
||||
agricultureMax: 200,
|
||||
commerce: 100,
|
||||
commerceMax: 200,
|
||||
security: 100,
|
||||
securityMax: 200,
|
||||
supplyState: 1,
|
||||
frontState: 0,
|
||||
defence: 100,
|
||||
defenceMax: 200,
|
||||
wall: 100,
|
||||
wallMax: 200,
|
||||
conflict: {},
|
||||
meta: { trust: 50, trade: 100, marker: id },
|
||||
});
|
||||
|
||||
describe('monthly event pipeline', () => {
|
||||
it('runs PRE_MONTH before the date change and MONTH after it in priority/id order', async () => {
|
||||
const trace: string[] = [];
|
||||
@@ -150,4 +179,69 @@ describe('monthly event pipeline', () => {
|
||||
'Unsupported monthly event action: RaiseInvader (eventId=9)'
|
||||
);
|
||||
});
|
||||
|
||||
it('randomizes city trade rates with the legacy seed domain and nullable no-trader state', async () => {
|
||||
const actions = new Map<string, MonthlyEventActionHandler>();
|
||||
const world = buildWorld(
|
||||
[
|
||||
{
|
||||
id: 10,
|
||||
targetCode: 'month',
|
||||
priority: 0,
|
||||
condition: true,
|
||||
action: [['RandomizeCityTradeRate']],
|
||||
meta: {},
|
||||
},
|
||||
],
|
||||
actions,
|
||||
[buildCity(1, 1), buildCity(2, 4), buildCity(3, 5), buildCity(4, 6), buildCity(5, 7), buildCity(6, 8)]
|
||||
);
|
||||
actions.set(
|
||||
'RandomizeCityTradeRate',
|
||||
createRandomizeCityTradeRateHandler({
|
||||
getWorld: () => world,
|
||||
})
|
||||
);
|
||||
|
||||
await world.advanceMonth(new Date('0190-01-01T00:00:00.000Z'));
|
||||
|
||||
expect(
|
||||
world.listCities().map((city) => ({
|
||||
id: city.id,
|
||||
trade: city.meta.trade ?? null,
|
||||
marker: city.meta.marker,
|
||||
}))
|
||||
).toEqual([
|
||||
{ id: 1, trade: null, marker: 1 },
|
||||
{ id: 2, trade: null, marker: 2 },
|
||||
{ id: 3, trade: 101, marker: 3 },
|
||||
{ id: 4, trade: 100, marker: 4 },
|
||||
{ id: 5, trade: 105, marker: 5 },
|
||||
{ id: 6, trade: 102, marker: 6 },
|
||||
]);
|
||||
expect(world.peekDirtyState().cities.map((city) => city.id)).toEqual([1, 2, 3, 4, 5, 6]);
|
||||
});
|
||||
|
||||
it('rejects an unsupported city level instead of changing RNG consumption silently', async () => {
|
||||
const actions = new Map<string, MonthlyEventActionHandler>();
|
||||
const world = buildWorld(
|
||||
[
|
||||
{
|
||||
id: 11,
|
||||
targetCode: 'month',
|
||||
priority: 0,
|
||||
condition: true,
|
||||
action: [['RandomizeCityTradeRate']],
|
||||
meta: {},
|
||||
},
|
||||
],
|
||||
actions,
|
||||
[buildCity(99, 9)]
|
||||
);
|
||||
actions.set('RandomizeCityTradeRate', createRandomizeCityTradeRateHandler({ getWorld: () => world }));
|
||||
|
||||
await expect(world.advanceMonth(new Date('0190-01-01T00:00:00.000Z'))).rejects.toThrow(
|
||||
'Unsupported city level for RandomizeCityTradeRate: 9 (cityId=99)'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import type { City, MapDefinition } from '@sammo-ts/logic';
|
||||
import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/infra';
|
||||
|
||||
import { createDatabaseTurnHooks } from '../src/turn/databaseHooks.js';
|
||||
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
||||
import type { TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||
|
||||
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
||||
const integration = describe.skipIf(!databaseUrl);
|
||||
const cityId = 990_041;
|
||||
|
||||
const map: MapDefinition = {
|
||||
id: 'monthly-trade-persistence',
|
||||
name: 'monthly-trade-persistence',
|
||||
cities: [],
|
||||
defaults: { trust: 50, trade: 100, supplyState: 1, frontState: 0 },
|
||||
};
|
||||
|
||||
const city: City = {
|
||||
id: cityId,
|
||||
name: '시세검증도시',
|
||||
nationId: 0,
|
||||
level: 1,
|
||||
state: 0,
|
||||
population: 1_000,
|
||||
populationMax: 2_000,
|
||||
agriculture: 100,
|
||||
agricultureMax: 200,
|
||||
commerce: 100,
|
||||
commerceMax: 200,
|
||||
security: 100,
|
||||
securityMax: 200,
|
||||
supplyState: 1,
|
||||
frontState: 0,
|
||||
defence: 100,
|
||||
defenceMax: 200,
|
||||
wall: 100,
|
||||
wallMax: 200,
|
||||
conflict: {},
|
||||
meta: { trust: 50, trade: 100, region: 1 },
|
||||
};
|
||||
|
||||
integration('monthly city trade persistence', () => {
|
||||
let db: GamePrismaClient;
|
||||
let closeDb: (() => Promise<void>) | undefined;
|
||||
|
||||
beforeAll(async () => {
|
||||
const connector = createGamePostgresConnector({ url: databaseUrl! });
|
||||
await connector.connect();
|
||||
db = connector.prisma;
|
||||
closeDb = () => connector.disconnect();
|
||||
await db.city.deleteMany({ where: { id: cityId } });
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await db.city.deleteMany({ where: { id: cityId } });
|
||||
await closeDb?.();
|
||||
});
|
||||
|
||||
it('writes both the legacy null state and a randomized numeric rate', async () => {
|
||||
await db.city.create({
|
||||
data: {
|
||||
id: city.id,
|
||||
name: city.name,
|
||||
level: city.level,
|
||||
nationId: city.nationId,
|
||||
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: 50,
|
||||
trade: 100,
|
||||
defence: city.defence,
|
||||
defenceMax: city.defenceMax,
|
||||
wall: city.wall,
|
||||
wallMax: city.wallMax,
|
||||
region: 1,
|
||||
conflict: {},
|
||||
meta: {},
|
||||
},
|
||||
});
|
||||
const row = await db.worldState.create({
|
||||
data: {
|
||||
scenarioCode: 'monthly-trade-persistence',
|
||||
currentYear: 190,
|
||||
currentMonth: 1,
|
||||
tickSeconds: 600,
|
||||
config: {},
|
||||
meta: {},
|
||||
},
|
||||
});
|
||||
const state: TurnWorldState = {
|
||||
id: row.id,
|
||||
currentYear: 190,
|
||||
currentMonth: 1,
|
||||
tickSeconds: 600,
|
||||
lastTurnTime: new Date('2026-07-25T00:10:00.000Z'),
|
||||
meta: {},
|
||||
};
|
||||
const snapshot: TurnWorldSnapshot = {
|
||||
scenarioConfig: {
|
||||
stat: { total: 300, min: 10, max: 100, npcTotal: 150, npcMax: 50, npcMin: 10, chiefMin: 70 },
|
||||
iconPath: '',
|
||||
map: {},
|
||||
const: {},
|
||||
environment: { mapName: map.id, unitSet: 'default' },
|
||||
},
|
||||
map,
|
||||
generals: [],
|
||||
cities: [city],
|
||||
nations: [],
|
||||
troops: [],
|
||||
diplomacy: [],
|
||||
events: [],
|
||||
initialEvents: [],
|
||||
};
|
||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
||||
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
||||
});
|
||||
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
||||
const checkpoint = {
|
||||
lastTurnTime: state.lastTurnTime.toISOString(),
|
||||
processedGenerals: 0,
|
||||
processedTurns: 1,
|
||||
durationMs: 0,
|
||||
partial: false,
|
||||
};
|
||||
|
||||
try {
|
||||
const { trade: _trade, ...metaWithoutTrade } = city.meta;
|
||||
world.updateCity(city.id, { meta: metaWithoutTrade });
|
||||
await dbHooks.hooks.flushChanges?.(checkpoint);
|
||||
expect((await db.city.findUniqueOrThrow({ where: { id: cityId } })).trade).toBeNull();
|
||||
|
||||
world.updateCity(city.id, { meta: { ...metaWithoutTrade, trade: 103 } });
|
||||
await dbHooks.hooks.flushChanges?.(checkpoint);
|
||||
expect((await db.city.findUniqueOrThrow({ where: { id: cityId } })).trade).toBe(103);
|
||||
} finally {
|
||||
await dbHooks.close();
|
||||
await db.worldState.delete({ where: { id: row.id } });
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user