test(engine): run economic actions at month boundaries
This commit is contained in:
@@ -5,6 +5,7 @@ import type { City, ItemModule, Nation } from '@sammo-ts/logic';
|
|||||||
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 { createUpdateNationLevelHandler } from '../src/turn/monthlyNationLevelAction.js';
|
import { createUpdateNationLevelHandler } from '../src/turn/monthlyNationLevelAction.js';
|
||||||
|
import { createMonthlyEventHandler } from '../src/turn/monthlyEventHandler.js';
|
||||||
import { InMemoryReservedTurnStore } from '../src/turn/reservedTurnStore.js';
|
import { InMemoryReservedTurnStore } from '../src/turn/reservedTurnStore.js';
|
||||||
import type { TurnEvent, TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
import type { TurnEvent, TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||||
|
|
||||||
@@ -216,7 +217,7 @@ integration('monthly nation level database persistence', () => {
|
|||||||
data: {
|
data: {
|
||||||
scenarioCode: 'monthly-nation-level-persistence',
|
scenarioCode: 'monthly-nation-level-persistence',
|
||||||
currentYear: 193,
|
currentYear: 193,
|
||||||
currentMonth: 2,
|
currentMonth: 1,
|
||||||
tickSeconds: 600,
|
tickSeconds: 600,
|
||||||
config: {},
|
config: {},
|
||||||
meta: { hiddenSeed: 'nation-level-persistence', killturn: 1_000 },
|
meta: { hiddenSeed: 'nation-level-persistence', killturn: 1_000 },
|
||||||
@@ -225,7 +226,7 @@ integration('monthly nation level database persistence', () => {
|
|||||||
const state: TurnWorldState = {
|
const state: TurnWorldState = {
|
||||||
id: stateRow.id,
|
id: stateRow.id,
|
||||||
currentYear: 193,
|
currentYear: 193,
|
||||||
currentMonth: 2,
|
currentMonth: 1,
|
||||||
tickSeconds: 600,
|
tickSeconds: 600,
|
||||||
lastTurnTime: new Date('2026-07-25T00:20:00.000Z'),
|
lastTurnTime: new Date('2026-07-25T00:20:00.000Z'),
|
||||||
meta: { hiddenSeed: 'nation-level-persistence', killturn: 1_000 },
|
meta: { hiddenSeed: 'nation-level-persistence', killturn: 1_000 },
|
||||||
@@ -252,24 +253,26 @@ integration('monthly nation level database persistence', () => {
|
|||||||
events: [event],
|
events: [event],
|
||||||
initialEvents: [],
|
initialEvents: [],
|
||||||
};
|
};
|
||||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
|
||||||
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
|
||||||
});
|
|
||||||
const reservedTurns = new InMemoryReservedTurnStore(db, { maxGeneralTurns: 30, maxNationTurns: 12 });
|
const reservedTurns = new InMemoryReservedTurnStore(db, { maxGeneralTurns: 30, maxNationTurns: 12 });
|
||||||
await reservedTurns.loadAll();
|
await reservedTurns.loadAll();
|
||||||
|
let world: InMemoryTurnWorld | null = null;
|
||||||
const handler = createUpdateNationLevelHandler({
|
const handler = createUpdateNationLevelHandler({
|
||||||
getWorld: () => world,
|
getWorld: () => world,
|
||||||
reservedTurns,
|
reservedTurns,
|
||||||
itemModules: [uniqueHorse],
|
itemModules: [uniqueHorse],
|
||||||
});
|
});
|
||||||
|
world = new InMemoryTurnWorld(state, snapshot, {
|
||||||
|
schedule: { entries: [{ startMinute: 0, tickMinutes: 10 }] },
|
||||||
|
calendarHandler: createMonthlyEventHandler({
|
||||||
|
getWorld: () => world,
|
||||||
|
startYear: 190,
|
||||||
|
actions: new Map([['UpdateNationLevel', handler]]),
|
||||||
|
}),
|
||||||
|
});
|
||||||
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world, { reservedTurns });
|
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world, { reservedTurns });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await handler(
|
await world.advanceMonth(new Date('0193-02-01T00:00:00.000Z'));
|
||||||
[],
|
|
||||||
{ year: 193, month: 2, startyear: 190, currentEventID: 1, turnTime: state.lastTurnTime },
|
|
||||||
event
|
|
||||||
);
|
|
||||||
await dbHooks.hooks.flushChanges?.({
|
await dbHooks.hooks.flushChanges?.({
|
||||||
lastTurnTime: state.lastTurnTime.toISOString(),
|
lastTurnTime: state.lastTurnTime.toISOString(),
|
||||||
processedGenerals: 0,
|
processedGenerals: 0,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ 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 { createProcessSemiAnnualHandler } from '../src/turn/monthlySemiAnnualAction.js';
|
import { createProcessSemiAnnualHandler } from '../src/turn/monthlySemiAnnualAction.js';
|
||||||
|
import { createMonthlyEventHandler } from '../src/turn/monthlyEventHandler.js';
|
||||||
import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||||
|
|
||||||
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
||||||
@@ -169,8 +170,8 @@ integration('monthly semi-annual database persistence', () => {
|
|||||||
const row = await db.worldState.create({
|
const row = await db.worldState.create({
|
||||||
data: {
|
data: {
|
||||||
scenarioCode: 'monthly-semi-annual-persistence',
|
scenarioCode: 'monthly-semi-annual-persistence',
|
||||||
currentYear: 193,
|
currentYear: 192,
|
||||||
currentMonth: 1,
|
currentMonth: 12,
|
||||||
tickSeconds: 600,
|
tickSeconds: 600,
|
||||||
config: {},
|
config: {},
|
||||||
meta: {},
|
meta: {},
|
||||||
@@ -178,8 +179,8 @@ integration('monthly semi-annual database persistence', () => {
|
|||||||
});
|
});
|
||||||
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: {},
|
meta: {},
|
||||||
@@ -203,27 +204,32 @@ integration('monthly semi-annual database persistence', () => {
|
|||||||
nations: [nation],
|
nations: [nation],
|
||||||
troops: [],
|
troops: [],
|
||||||
diplomacy: [],
|
diplomacy: [],
|
||||||
events: [],
|
events: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
targetCode: 'month',
|
||||||
|
priority: 0,
|
||||||
|
condition: true,
|
||||||
|
action: [['ProcessSemiAnnual', 'gold']],
|
||||||
|
meta: {},
|
||||||
|
},
|
||||||
|
],
|
||||||
initialEvents: [],
|
initialEvents: [],
|
||||||
};
|
};
|
||||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
let world: InMemoryTurnWorld | null = null;
|
||||||
|
const actions = new Map([['ProcessSemiAnnual', createProcessSemiAnnualHandler({ 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,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const handler = createProcessSemiAnnualHandler({ getWorld: () => world });
|
await world.advanceMonth(new Date('0193-01-01T00:00:00.000Z'));
|
||||||
await handler(
|
|
||||||
['gold'],
|
|
||||||
{
|
|
||||||
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?.({
|
await dbHooks.hooks.flushChanges?.({
|
||||||
lastTurnTime: state.lastTurnTime.toISOString(),
|
lastTurnTime: state.lastTurnTime.toISOString(),
|
||||||
processedGenerals: 0,
|
processedGenerals: 0,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ 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 { createProcessWarIncomeHandler } from '../src/turn/monthlyWarIncomeAction.js';
|
import { createProcessWarIncomeHandler } from '../src/turn/monthlyWarIncomeAction.js';
|
||||||
|
import { createMonthlyEventHandler } from '../src/turn/monthlyEventHandler.js';
|
||||||
import type { TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
import type { TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||||
|
|
||||||
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
|
||||||
@@ -150,27 +151,32 @@ integration('monthly war income database persistence', () => {
|
|||||||
nations: [nation],
|
nations: [nation],
|
||||||
troops: [],
|
troops: [],
|
||||||
diplomacy: [],
|
diplomacy: [],
|
||||||
events: [],
|
events: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
targetCode: 'pre_month',
|
||||||
|
priority: 0,
|
||||||
|
condition: true,
|
||||||
|
action: [['ProcessWarIncome']],
|
||||||
|
meta: {},
|
||||||
|
},
|
||||||
|
],
|
||||||
initialEvents: [],
|
initialEvents: [],
|
||||||
};
|
};
|
||||||
const world = new InMemoryTurnWorld(state, snapshot, {
|
let world: InMemoryTurnWorld | null = null;
|
||||||
|
const actions = new Map([['ProcessWarIncome', createProcessWarIncomeHandler({ 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,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
const dbHooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const handler = createProcessWarIncomeHandler({ getWorld: () => world });
|
await world.advanceMonth(new Date('0193-02-01T00:00:00.000Z'));
|
||||||
await handler(
|
|
||||||
[],
|
|
||||||
{
|
|
||||||
year: 193,
|
|
||||||
month: 1,
|
|
||||||
startyear: 190,
|
|
||||||
currentEventID: 1,
|
|
||||||
turnTime: state.lastTurnTime,
|
|
||||||
},
|
|
||||||
{ id: 1, targetCode: 'pre_month', priority: 0, condition: true, action: [], meta: {} }
|
|
||||||
);
|
|
||||||
await dbHooks.hooks.flushChanges?.({
|
await dbHooks.hooks.flushChanges?.({
|
||||||
lastTurnTime: state.lastTurnTime.toISOString(),
|
lastTurnTime: state.lastTurnTime.toISOString(),
|
||||||
processedGenerals: 0,
|
processedGenerals: 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user