플레이 감사에 외교 관계 최초 관측을 기록
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { asRecord } from '@sammo-ts/common';
|
||||
import type { InMemoryTurnWorld } from '../turn/inMemoryWorld.js';
|
||||
import type { TurnDiplomacy } from '../turn/types.js';
|
||||
|
||||
@@ -99,3 +100,81 @@ export const recordTurnAuditDiplomacy = (
|
||||
after: nextState,
|
||||
});
|
||||
};
|
||||
|
||||
/** 현재 로드된 관계를 도입 시 한 번만 고정한다. 과거 발생 원인/주체는 추정하지 않는다. */
|
||||
export const initializeAuditDiplomacy = (world: InMemoryTurnWorld, observedAt = new Date()): boolean => {
|
||||
const state = world.getState();
|
||||
const serverId = state.meta.serverId;
|
||||
if (typeof serverId !== 'string' || !serverId.trim()) return false;
|
||||
const previous = asRecord(state.meta.playAuditDiplomacy);
|
||||
if (previous.serverId === serverId) {
|
||||
if (
|
||||
previous.schemaVersion !== 1 ||
|
||||
typeof previous.year !== 'number' ||
|
||||
previous.year < 0 ||
|
||||
!Number.isInteger(previous.year) ||
|
||||
!Number.isInteger(previous.month) ||
|
||||
typeof previous.month !== 'number' ||
|
||||
previous.month < 1 ||
|
||||
previous.month > 12 ||
|
||||
typeof previous.tick !== 'number' ||
|
||||
!Number.isSafeInteger(previous.tick) ||
|
||||
previous.tick < 0 ||
|
||||
typeof previous.clockRevision !== 'number' ||
|
||||
!Number.isSafeInteger(previous.clockRevision) ||
|
||||
previous.clockRevision < 0 ||
|
||||
typeof previous.relationCount !== 'number' ||
|
||||
!Number.isInteger(previous.relationCount) ||
|
||||
previous.relationCount < 0 ||
|
||||
previous.year * 12 + previous.month > state.currentYear * 12 + state.currentMonth ||
|
||||
typeof previous.observedAt !== 'string' ||
|
||||
!Number.isFinite(Date.parse(previous.observedAt))
|
||||
)
|
||||
throw new Error('Invalid play audit diplomacy boundary');
|
||||
return false;
|
||||
}
|
||||
const clock = world.getGameClockState();
|
||||
const observedAtIso = observedAt.toISOString();
|
||||
const relations = world
|
||||
.listDiplomacy()
|
||||
.filter((entry) => entry.fromNationId > 0 && entry.toNationId > 0)
|
||||
.sort((left, right) => left.fromNationId - right.fromNationId || left.toNationId - right.toNationId);
|
||||
for (const [index, entry] of relations.entries()) {
|
||||
world.queueAuditDiplomacy({
|
||||
schemaVersion: 1,
|
||||
serverId,
|
||||
srcNationId: entry.fromNationId,
|
||||
destNationId: entry.toNationId,
|
||||
category: 'RELATION',
|
||||
source: 'BASELINE',
|
||||
eventType: 'RELATION_BASELINE',
|
||||
documentId: null,
|
||||
documentHash: null,
|
||||
previousDocumentId: null,
|
||||
year: state.currentYear,
|
||||
month: state.currentMonth,
|
||||
tick: BigInt(clock.tick),
|
||||
clockRevision: BigInt(clock.revision),
|
||||
executionId: 'relation-baseline',
|
||||
ordinal: index + 1,
|
||||
requestId: null,
|
||||
inputSequence: null,
|
||||
actor: null,
|
||||
before: null,
|
||||
after: { state: entry.state, term: entry.term, dead: entry.dead },
|
||||
});
|
||||
}
|
||||
world.updateWorldMeta({
|
||||
playAuditDiplomacy: {
|
||||
schemaVersion: 1,
|
||||
serverId,
|
||||
year: state.currentYear,
|
||||
month: state.currentMonth,
|
||||
tick: clock.tick,
|
||||
clockRevision: clock.revision,
|
||||
observedAt: observedAtIso,
|
||||
relationCount: relations.length,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { initializeAuditDiplomacy } from '../playAudit/diplomacy.js';
|
||||
import { initializeAuditPolicies } from '../playAudit/policy.js';
|
||||
import { startAuditRetentionWorker } from '../playAudit/retentionWorker.js';
|
||||
import { createPlayAuditHandler, initializeAuditCollection } from '../playAudit/collection.js';
|
||||
@@ -806,6 +807,7 @@ const createTurnDaemonRuntimeWithLease = async (
|
||||
worldRef = world;
|
||||
if (!databaseFlushEnabled) {
|
||||
initializeAuditPolicies(world);
|
||||
initializeAuditDiplomacy(world, new Date(clock.nowMs()));
|
||||
initializeAuditCollection(world, new Date(clock.nowMs()));
|
||||
}
|
||||
|
||||
@@ -929,8 +931,9 @@ const createTurnDaemonRuntimeWithLease = async (
|
||||
// 복구된 clock에서 기준을 고정하고 readiness 공개 전에 원자적으로 저장한다.
|
||||
// 명령 없는 PREOPEN도 기록하며 input_event나 게임 RNG를 만들지 않는다.
|
||||
initializeAuditPolicies(world);
|
||||
const diplomacyInitialized = initializeAuditDiplomacy(world, new Date(clock.nowMs()));
|
||||
initializeAuditCollection(world, new Date(clock.nowMs()));
|
||||
if (world.hasPendingAuditRecords()) {
|
||||
if (world.hasPendingAuditRecords() || diplomacyInitialized) {
|
||||
await dbHooks.flushChanges();
|
||||
dbHooks.takeCommittedReadModelChangeReceipt();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { initializeAuditDiplomacy } from '../src/playAudit/diplomacy.js';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { City, Nation } from '@sammo-ts/logic';
|
||||
import { InMemoryTurnWorld, type GeneralTurnHandler } from '../src/turn/inMemoryWorld.js';
|
||||
@@ -128,6 +129,37 @@ const buildWorld = (generalTurnHandler?: GeneralTurnHandler) => {
|
||||
return world;
|
||||
};
|
||||
describe('play audit collection durability state', () => {
|
||||
it('marks an empty diplomacy baseline without inventing relations and validates before queuing', () => {
|
||||
const world = buildWorld();
|
||||
world.removeNation(1);
|
||||
world.removeNation(2);
|
||||
expect(() => initializeAuditDiplomacy(world, new Date('invalid'))).toThrow(RangeError);
|
||||
expect(world.getState().meta.playAuditDiplomacy).toBeUndefined();
|
||||
expect(world.peekDirtyState().pendingAuditDiplomacy).toEqual([]);
|
||||
expect(initializeAuditDiplomacy(world)).toBe(true);
|
||||
expect(world.getState().meta.playAuditDiplomacy).toMatchObject({ relationCount: 0 });
|
||||
expect(world.peekDirtyState().pendingAuditDiplomacy).toEqual([]);
|
||||
expect(initializeAuditDiplomacy(world)).toBe(false);
|
||||
});
|
||||
|
||||
it('captures a single diplomacy baseline and restores its marker and queue together', () => {
|
||||
const world = buildWorld();
|
||||
const checkpoint = world.captureState();
|
||||
expect(initializeAuditDiplomacy(world, new Date('2026-09-16T00:00:00Z'))).toBe(true);
|
||||
const events = world.peekDirtyState().pendingAuditDiplomacy;
|
||||
expect(events).toHaveLength(2);
|
||||
expect(events.map((event) => [event.srcNationId, event.destNationId])).toEqual([
|
||||
[1, 2],
|
||||
[2, 1],
|
||||
]);
|
||||
expect(events[0]).toMatchObject({ source: 'BASELINE', before: null, after: { state: 2, term: 0, dead: 0 } });
|
||||
expect(initializeAuditDiplomacy(world)).toBe(false);
|
||||
expect(world.peekDirtyState().pendingAuditDiplomacy).toEqual(events);
|
||||
world.restoreState(checkpoint);
|
||||
expect(world.getState().meta.playAuditDiplomacy).toBeUndefined();
|
||||
expect(world.peekDirtyState().pendingAuditDiplomacy).toEqual([]);
|
||||
});
|
||||
|
||||
it('preserves consecutive diplomacy transitions in one turn and restores them with the checkpoint', () => {
|
||||
const world = buildWorld({
|
||||
execute: ({ general }) => ({
|
||||
|
||||
@@ -43,6 +43,7 @@ integration('initial audit durability before runtime readiness', () => {
|
||||
closeDb = () => connector.disconnect();
|
||||
await db.playAuditMonth.deleteMany();
|
||||
await db.playAuditPolicy.deleteMany();
|
||||
await db.playAuditDiplomacyEvent.deleteMany();
|
||||
await seedScenarioToDatabase({
|
||||
scenarioId: 903,
|
||||
databaseUrl: databaseUrl!,
|
||||
@@ -56,6 +57,15 @@ integration('initial audit durability before runtime readiness', () => {
|
||||
season: 1,
|
||||
},
|
||||
});
|
||||
await db.nation.createMany({
|
||||
data: [
|
||||
{ id: 91990, name: '기준 발신국', color: '#ffffff' },
|
||||
{ id: 91991, name: '기준 수신국', color: '#000000' },
|
||||
],
|
||||
});
|
||||
await db.diplomacy.create({
|
||||
data: { srcNationId: 91990, destNationId: 91991, stateCode: 7, term: 12, meta: { dead: 34 } },
|
||||
});
|
||||
}, 60_000);
|
||||
afterAll(async () => {
|
||||
await runtime?.close();
|
||||
@@ -80,6 +90,8 @@ integration('initial audit durability before runtime readiness', () => {
|
||||
expect(String(error)).toContain('fixture initial audit failure');
|
||||
expect(await db.playAuditMonth.count()).toBe(0);
|
||||
expect(await db.playAuditPolicy.count()).toBe(0);
|
||||
expect(await db.playAuditDiplomacyEvent.count()).toBe(0);
|
||||
expect(asRecord((await db.worldState.findFirstOrThrow()).meta).playAuditDiplomacy).toBeUndefined();
|
||||
expect(asRecord((await db.worldState.findFirstOrThrow()).meta).playAuditCollection).toBeUndefined();
|
||||
expect(
|
||||
(await db.nation.findMany()).every((nation) => asRecord(nation.meta)._playAuditPolicy === undefined)
|
||||
@@ -104,6 +116,37 @@ integration('initial audit durability before runtime readiness', () => {
|
||||
expect((await db.turnDaemonLease.findUniqueOrThrow({ where: { profile } })).clockReady).toBe(true);
|
||||
const initial = await db.playAuditMonth.findFirstOrThrow({ where: { serverId, kind: 'INITIAL' } });
|
||||
const policies = await db.playAuditPolicy.findMany({ where: { serverId }, orderBy: { id: 'asc' } });
|
||||
const diplomacy = await db.playAuditDiplomacyEvent.findMany({
|
||||
where: { serverId },
|
||||
orderBy: { ordinal: 'asc' },
|
||||
});
|
||||
expect(diplomacy).toHaveLength(
|
||||
await db.diplomacy.count({ where: { srcNationId: { gt: 0 }, destNationId: { gt: 0 } } })
|
||||
);
|
||||
expect(diplomacy).toHaveLength(2);
|
||||
expect(diplomacy[0]).toMatchObject({
|
||||
srcNationId: 91990,
|
||||
destNationId: 91991,
|
||||
before: null,
|
||||
after: { state: 7, term: 12, dead: 34 },
|
||||
});
|
||||
expect(diplomacy[1]).toMatchObject({
|
||||
srcNationId: 91991,
|
||||
destNationId: 91990,
|
||||
after: { state: 2, term: 0, dead: 0 },
|
||||
});
|
||||
expect(
|
||||
diplomacy.every(
|
||||
(event) =>
|
||||
event.source === 'BASELINE' &&
|
||||
event.before === null &&
|
||||
event.actor === null &&
|
||||
event.requestId === null
|
||||
)
|
||||
).toBe(true);
|
||||
const diplomacyMarker = asRecord((await db.worldState.findFirstOrThrow()).meta).playAuditDiplomacy;
|
||||
expect(diplomacyMarker).toMatchObject({ serverId, schemaVersion: 1, relationCount: diplomacy.length });
|
||||
|
||||
expect(policies).toHaveLength((await db.nation.count()) * 4);
|
||||
expect(
|
||||
policies.every(
|
||||
@@ -128,6 +171,10 @@ integration('initial audit durability before runtime readiness', () => {
|
||||
expect(await db.playAuditPolicy.findMany({ where: { serverId }, orderBy: { id: 'asc' } })).toEqual(policies);
|
||||
expect(await db.playAuditMonth.findMany({ where: { serverId, kind: 'INITIAL' } })).toEqual([initial]);
|
||||
expect(asRecord((await db.worldState.findFirstOrThrow()).meta).playAuditCollection).toEqual(marker);
|
||||
expect(await db.playAuditDiplomacyEvent.findMany({ where: { serverId }, orderBy: { ordinal: 'asc' } })).toEqual(
|
||||
diplomacy
|
||||
);
|
||||
expect(asRecord((await db.worldState.findFirstOrThrow()).meta).playAuditDiplomacy).toEqual(diplomacyMarker);
|
||||
expect(await clock()).toEqual(beforeClock);
|
||||
expect(await db.inputEvent.count()).toBe(beforeInputs);
|
||||
await expect(
|
||||
|
||||
Reference in New Issue
Block a user