test(game-engine): persist world actions through month boundary
This commit is contained in:
@@ -5,7 +5,8 @@ import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/in
|
|||||||
import { createDatabaseTurnHooks } from '../src/turn/databaseHooks.js';
|
import { createDatabaseTurnHooks } from '../src/turn/databaseHooks.js';
|
||||||
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
||||||
import { createRaiseDisasterHandler } from '../src/turn/monthlyDisasterAction.js';
|
import { createRaiseDisasterHandler } from '../src/turn/monthlyDisasterAction.js';
|
||||||
import type { TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
import { createMonthlyEventHandler } from '../src/turn/monthlyEventHandler.js';
|
||||||
|
import type { TurnEvent, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||||
|
|
||||||
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
||||||
const integration = describe.skipIf(!databaseUrl);
|
const integration = describe.skipIf(!databaseUrl);
|
||||||
@@ -41,6 +42,14 @@ const city: City = {
|
|||||||
conflict: {},
|
conflict: {},
|
||||||
meta: { trust: 99.5, trade: 100, region: 1 },
|
meta: { trust: 99.5, trade: 100, region: 1 },
|
||||||
};
|
};
|
||||||
|
const event: TurnEvent = {
|
||||||
|
id: 1,
|
||||||
|
targetCode: 'month',
|
||||||
|
priority: 1_000,
|
||||||
|
condition: true,
|
||||||
|
action: [['RaiseDisaster']],
|
||||||
|
meta: {},
|
||||||
|
};
|
||||||
|
|
||||||
integration('monthly disaster database persistence', () => {
|
integration('monthly disaster database persistence', () => {
|
||||||
let db: GamePrismaClient;
|
let db: GamePrismaClient;
|
||||||
@@ -90,17 +99,17 @@ integration('monthly disaster database persistence', () => {
|
|||||||
const row = await db.worldState.create({
|
const row = await db.worldState.create({
|
||||||
data: {
|
data: {
|
||||||
scenarioCode: 'monthly-disaster-persistence',
|
scenarioCode: 'monthly-disaster-persistence',
|
||||||
currentYear: 193,
|
currentYear: 192,
|
||||||
currentMonth: 1,
|
currentMonth: 12,
|
||||||
tickSeconds: 600,
|
tickSeconds: 600,
|
||||||
config: {},
|
config: {},
|
||||||
meta: {},
|
meta: { hiddenSeed: 'disaster-test-6' },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const state: TurnWorldState = {
|
const state: TurnWorldState = {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
currentYear: 193,
|
currentYear: 192,
|
||||||
currentMonth: 1,
|
currentMonth: 12,
|
||||||
tickSeconds: 600,
|
tickSeconds: 600,
|
||||||
lastTurnTime: new Date('2026-07-25T00:10:00.000Z'),
|
lastTurnTime: new Date('2026-07-25T00:10:00.000Z'),
|
||||||
meta: { hiddenSeed: 'disaster-test-6' },
|
meta: { hiddenSeed: 'disaster-test-6' },
|
||||||
@@ -119,11 +128,18 @@ integration('monthly disaster database persistence', () => {
|
|||||||
nations: [],
|
nations: [],
|
||||||
troops: [],
|
troops: [],
|
||||||
diplomacy: [],
|
diplomacy: [],
|
||||||
events: [],
|
events: [event],
|
||||||
initialEvents: [],
|
initialEvents: [],
|
||||||
};
|
};
|
||||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
let world: InMemoryTurnWorld | null = null;
|
||||||
|
const raiseDisaster = createRaiseDisasterHandler({ getWorld: () => world });
|
||||||
|
world = new InMemoryTurnWorld(state, snapshot, {
|
||||||
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
||||||
|
calendarHandler: createMonthlyEventHandler({
|
||||||
|
getWorld: () => world,
|
||||||
|
startYear: 190,
|
||||||
|
actions: new Map([['RaiseDisaster', raiseDisaster]]),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
||||||
const checkpoint = {
|
const checkpoint = {
|
||||||
@@ -135,18 +151,7 @@ integration('monthly disaster database persistence', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const raiseDisaster = createRaiseDisasterHandler({ getWorld: () => world });
|
await world.advanceMonth(new Date('0193-01-01T00:00:00.000Z'));
|
||||||
raiseDisaster(
|
|
||||||
[],
|
|
||||||
{
|
|
||||||
year: 193,
|
|
||||||
month: 1,
|
|
||||||
startyear: 190,
|
|
||||||
currentEventID: 1,
|
|
||||||
turnTime: state.lastTurnTime,
|
|
||||||
},
|
|
||||||
{ id: 1, targetCode: 'month', priority: 0, condition: true, action: [], meta: {} }
|
|
||||||
);
|
|
||||||
await dbHooks.hooks.flushChanges?.(checkpoint);
|
await dbHooks.hooks.flushChanges?.(checkpoint);
|
||||||
|
|
||||||
const persisted = await db.city.findUniqueOrThrow({ where: { id: cityId } });
|
const persisted = await db.city.findUniqueOrThrow({ where: { id: cityId } });
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/in
|
|||||||
|
|
||||||
import { createDatabaseTurnHooks } from '../src/turn/databaseHooks.js';
|
import { createDatabaseTurnHooks } from '../src/turn/databaseHooks.js';
|
||||||
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
||||||
import type { TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
import { createMonthlyEventHandler, createRandomizeCityTradeRateHandler } from '../src/turn/monthlyEventHandler.js';
|
||||||
|
import type { TurnEvent, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||||
|
|
||||||
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
||||||
const integration = describe.skipIf(!databaseUrl);
|
const integration = describe.skipIf(!databaseUrl);
|
||||||
const cityId = 990_041;
|
const cityId = 990_041;
|
||||||
|
const cityIds = Array.from({ length: 6 }, (_, index) => cityId + index);
|
||||||
|
|
||||||
const map: MapDefinition = {
|
const map: MapDefinition = {
|
||||||
id: 'monthly-trade-persistence',
|
id: 'monthly-trade-persistence',
|
||||||
@@ -40,6 +42,21 @@ const city: City = {
|
|||||||
conflict: {},
|
conflict: {},
|
||||||
meta: { trust: 50, trade: 100, region: 1 },
|
meta: { trust: 50, trade: 100, region: 1 },
|
||||||
};
|
};
|
||||||
|
const cities = [1, 4, 5, 6, 7, 8].map((level, index): City => ({
|
||||||
|
...city,
|
||||||
|
id: cityIds[index]!,
|
||||||
|
name: `시세검증도시${level}`,
|
||||||
|
level,
|
||||||
|
meta: { ...city.meta },
|
||||||
|
}));
|
||||||
|
const event: TurnEvent = {
|
||||||
|
id: 1,
|
||||||
|
targetCode: 'month',
|
||||||
|
priority: 1_000,
|
||||||
|
condition: true,
|
||||||
|
action: [['RandomizeCityTradeRate']],
|
||||||
|
meta: {},
|
||||||
|
};
|
||||||
|
|
||||||
integration('monthly city trade persistence', () => {
|
integration('monthly city trade persistence', () => {
|
||||||
let db: GamePrismaClient;
|
let db: GamePrismaClient;
|
||||||
@@ -50,59 +67,59 @@ integration('monthly city trade persistence', () => {
|
|||||||
await connector.connect();
|
await connector.connect();
|
||||||
db = connector.prisma;
|
db = connector.prisma;
|
||||||
closeDb = () => connector.disconnect();
|
closeDb = () => connector.disconnect();
|
||||||
await db.city.deleteMany({ where: { id: cityId } });
|
await db.city.deleteMany({ where: { id: { in: cityIds } } });
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await db.city.deleteMany({ where: { id: cityId } });
|
await db.city.deleteMany({ where: { id: { in: cityIds } } });
|
||||||
await closeDb?.();
|
await closeDb?.();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('writes both the legacy null state and a randomized numeric rate', async () => {
|
it('writes both the legacy null state and a randomized numeric rate', async () => {
|
||||||
await db.city.create({
|
await db.city.createMany({
|
||||||
data: {
|
data: cities.map((entry) => ({
|
||||||
id: city.id,
|
id: entry.id,
|
||||||
name: city.name,
|
name: entry.name,
|
||||||
level: city.level,
|
level: entry.level,
|
||||||
nationId: city.nationId,
|
nationId: entry.nationId,
|
||||||
supplyState: city.supplyState,
|
supplyState: entry.supplyState,
|
||||||
frontState: city.frontState,
|
frontState: entry.frontState,
|
||||||
population: city.population,
|
population: entry.population,
|
||||||
populationMax: city.populationMax,
|
populationMax: entry.populationMax,
|
||||||
agriculture: city.agriculture,
|
agriculture: entry.agriculture,
|
||||||
agricultureMax: city.agricultureMax,
|
agricultureMax: entry.agricultureMax,
|
||||||
commerce: city.commerce,
|
commerce: entry.commerce,
|
||||||
commerceMax: city.commerceMax,
|
commerceMax: entry.commerceMax,
|
||||||
security: city.security,
|
security: entry.security,
|
||||||
securityMax: city.securityMax,
|
securityMax: entry.securityMax,
|
||||||
trust: 50,
|
trust: 50,
|
||||||
trade: 100,
|
trade: 100,
|
||||||
defence: city.defence,
|
defence: entry.defence,
|
||||||
defenceMax: city.defenceMax,
|
defenceMax: entry.defenceMax,
|
||||||
wall: city.wall,
|
wall: entry.wall,
|
||||||
wallMax: city.wallMax,
|
wallMax: entry.wallMax,
|
||||||
region: 1,
|
region: 1,
|
||||||
conflict: {},
|
conflict: {},
|
||||||
meta: {},
|
meta: {},
|
||||||
},
|
})),
|
||||||
});
|
});
|
||||||
const row = await db.worldState.create({
|
const row = await db.worldState.create({
|
||||||
data: {
|
data: {
|
||||||
scenarioCode: 'monthly-trade-persistence',
|
scenarioCode: 'monthly-trade-persistence',
|
||||||
currentYear: 190,
|
currentYear: 189,
|
||||||
currentMonth: 1,
|
currentMonth: 12,
|
||||||
tickSeconds: 600,
|
tickSeconds: 600,
|
||||||
config: {},
|
config: {},
|
||||||
meta: {},
|
meta: { hiddenSeed: 'monthly-event-test-seed' },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const state: TurnWorldState = {
|
const state: TurnWorldState = {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
currentYear: 190,
|
currentYear: 189,
|
||||||
currentMonth: 1,
|
currentMonth: 12,
|
||||||
tickSeconds: 600,
|
tickSeconds: 600,
|
||||||
lastTurnTime: new Date('2026-07-25T00:10:00.000Z'),
|
lastTurnTime: new Date('2026-07-25T00:10:00.000Z'),
|
||||||
meta: {},
|
meta: { hiddenSeed: 'monthly-event-test-seed' },
|
||||||
};
|
};
|
||||||
const snapshot: TurnWorldSnapshot = {
|
const snapshot: TurnWorldSnapshot = {
|
||||||
scenarioConfig: {
|
scenarioConfig: {
|
||||||
@@ -114,15 +131,22 @@ integration('monthly city trade persistence', () => {
|
|||||||
},
|
},
|
||||||
map,
|
map,
|
||||||
generals: [],
|
generals: [],
|
||||||
cities: [city],
|
cities,
|
||||||
nations: [],
|
nations: [],
|
||||||
troops: [],
|
troops: [],
|
||||||
diplomacy: [],
|
diplomacy: [],
|
||||||
events: [],
|
events: [event],
|
||||||
initialEvents: [],
|
initialEvents: [],
|
||||||
};
|
};
|
||||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
let world: InMemoryTurnWorld | null = null;
|
||||||
|
const randomize = createRandomizeCityTradeRateHandler({ getWorld: () => world });
|
||||||
|
world = new InMemoryTurnWorld(state, snapshot, {
|
||||||
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
||||||
|
calendarHandler: createMonthlyEventHandler({
|
||||||
|
getWorld: () => world,
|
||||||
|
startYear: 189,
|
||||||
|
actions: new Map([['RandomizeCityTradeRate', randomize]]),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
||||||
const checkpoint = {
|
const checkpoint = {
|
||||||
@@ -134,14 +158,17 @@ integration('monthly city trade persistence', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { trade: _trade, ...metaWithoutTrade } = city.meta;
|
await world.advanceMonth(new Date('0190-01-01T00:00:00.000Z'));
|
||||||
world.updateCity(city.id, { meta: metaWithoutTrade });
|
|
||||||
await dbHooks.hooks.flushChanges?.(checkpoint);
|
await dbHooks.hooks.flushChanges?.(checkpoint);
|
||||||
expect((await db.city.findUniqueOrThrow({ where: { id: cityId } })).trade).toBeNull();
|
expect(
|
||||||
|
(
|
||||||
world.updateCity(city.id, { meta: { ...metaWithoutTrade, trade: 103 } });
|
await db.city.findMany({
|
||||||
await dbHooks.hooks.flushChanges?.(checkpoint);
|
where: { id: { in: cityIds } },
|
||||||
expect((await db.city.findUniqueOrThrow({ where: { id: cityId } })).trade).toBe(103);
|
orderBy: { id: 'asc' },
|
||||||
|
select: { trade: true },
|
||||||
|
})
|
||||||
|
).map((entry) => entry.trade)
|
||||||
|
).toEqual([null, null, 101, 100, 105, 102]);
|
||||||
} finally {
|
} finally {
|
||||||
await dbHooks.close();
|
await dbHooks.close();
|
||||||
await db.worldState.delete({ where: { id: row.id } });
|
await db.worldState.delete({ where: { id: row.id } });
|
||||||
|
|||||||
Reference in New Issue
Block a user