feat: complete general turn lifecycle

This commit is contained in:
2026-07-25 08:42:57 +00:00
parent 88afcb5465
commit 45ba960474
13 changed files with 1412 additions and 30 deletions
+13
View File
@@ -28,6 +28,7 @@ import type { InMemoryTurnWorld } from './inMemoryWorld.js';
import type { InMemoryReservedTurnStore } from './reservedTurnStore.js';
import { buildDiplomacyMeta } from '@sammo-ts/logic';
import { ensureItemInventory, withSerializedItemInventory } from '@sammo-ts/logic/items/index.js';
import { persistGeneralLifecycleEvents } from './generalTurnLifecyclePersistence.js';
export interface DatabaseTurnHooks {
hooks: TurnDaemonHooks;
@@ -119,6 +120,7 @@ const buildRankRows = (
const buildGeneralUpdate = (
general: ReturnType<InMemoryTurnWorld['consumeDirtyState']>['generals'][number]
): TurnEngineGeneralUpdateInput => ({
userId: general.userId ?? null,
name: general.name,
nationId: general.nationId,
cityId: general.cityId,
@@ -236,6 +238,7 @@ const buildNationUpdate = (
name: nation.name,
color: nation.color,
capitalCityId: nation.capitalCityId,
chiefGeneralId: nation.chiefGeneralId,
gold: nation.gold,
rice: nation.rice,
level: nation.level,
@@ -336,6 +339,7 @@ export const createDatabaseTurnHooks = async (
createdNations,
createdTroops,
createdDiplomacy,
lifecycleEvents,
} = changes;
const reservedTurnChanges = options?.reservedTurns?.peekDirtyState();
@@ -354,6 +358,12 @@ export const createDatabaseTurnHooks = async (
const meta = asRecord(state.meta);
const serverId =
typeof meta.serverId === 'string' && meta.serverId.trim() ? meta.serverId.trim() : 'default';
await persistGeneralLifecycleEvents(
prisma,
lifecycleEvents,
meta,
asRecord(world.getScenarioConfig().const)
);
if (deletedNationSnapshots.length > 0) {
const nationIds = deletedNationSnapshots.map((snapshot) => snapshot.nation.id);
@@ -466,6 +476,9 @@ export const createDatabaseTurnHooks = async (
}
if (deletedGenerals.length > 0) {
await prisma.generalTurn.deleteMany({
where: { generalId: { in: deletedGenerals } },
});
await prisma.general.deleteMany({
where: { id: { in: deletedGenerals } },
});