feat: afterExecuteGeneral 핸들러 추가 및 실행 결과 처리 로직 구현

This commit is contained in:
2026-01-24 09:57:13 +00:00
parent 377f8cae5b
commit cb04c930b8
@@ -6,8 +6,16 @@ import type { TurnGeneral } from './types.js';
export interface InMemoryTurnProcessorOptions { export interface InMemoryTurnProcessorOptions {
tickMinutes?: number; tickMinutes?: number;
beforeExecuteGeneral?: (general: TurnGeneral) => Promise<void>; beforeExecuteGeneral?: (general: TurnGeneral) => Promise<void>;
afterExecuteGeneral?: (general: TurnGeneral, result: TurnGeneralExecutionResult) => Promise<void>;
} }
export type TurnGeneralExecutionResult = {
ok: boolean;
executedAt: Date;
nextTurnAt?: Date;
error?: unknown;
};
const resolveTickMinutes = (world: InMemoryTurnWorld, override?: number): number => { const resolveTickMinutes = (world: InMemoryTurnWorld, override?: number): number => {
if (override !== undefined) { if (override !== undefined) {
return Math.max(1, override); return Math.max(1, override);
@@ -21,11 +29,13 @@ export class InMemoryTurnProcessor implements TurnProcessor {
private readonly world: InMemoryTurnWorld; private readonly world: InMemoryTurnWorld;
private readonly tickMinutes: number; private readonly tickMinutes: number;
private readonly beforeExecuteGeneral?: (general: TurnGeneral) => Promise<void>; private readonly beforeExecuteGeneral?: (general: TurnGeneral) => Promise<void>;
private readonly afterExecuteGeneral?: (general: TurnGeneral, result: TurnGeneralExecutionResult) => Promise<void>;
constructor(world: InMemoryTurnWorld, options: InMemoryTurnProcessorOptions = {}) { constructor(world: InMemoryTurnWorld, options: InMemoryTurnProcessorOptions = {}) {
this.world = world; this.world = world;
this.tickMinutes = resolveTickMinutes(world, options.tickMinutes); this.tickMinutes = resolveTickMinutes(world, options.tickMinutes);
this.beforeExecuteGeneral = options.beforeExecuteGeneral; this.beforeExecuteGeneral = options.beforeExecuteGeneral;
this.afterExecuteGeneral = options.afterExecuteGeneral;
} }
async run(targetTime: Date, budget: TurnRunBudget, checkpoint?: TurnCheckpoint): Promise<TurnRunResult> { async run(targetTime: Date, budget: TurnRunBudget, checkpoint?: TurnCheckpoint): Promise<TurnRunResult> {
@@ -52,7 +62,24 @@ export class InMemoryTurnProcessor implements TurnProcessor {
if (this.beforeExecuteGeneral) { if (this.beforeExecuteGeneral) {
await this.beforeExecuteGeneral(general); await this.beforeExecuteGeneral(general);
} }
this.world.executeGeneralTurn(general); let nextTurnAt: Date | undefined;
let executionError: unknown;
try {
nextTurnAt = this.world.executeGeneralTurn(general);
} catch (error) {
executionError = error;
}
if (this.afterExecuteGeneral) {
await this.afterExecuteGeneral(general, {
ok: executionError === undefined,
executedAt,
nextTurnAt,
error: executionError,
});
}
if (executionError !== undefined) {
throw executionError;
}
processedGenerals += 1; processedGenerals += 1;
nextCheckpoint = { nextCheckpoint = {
turnTime: executedAt.toISOString(), turnTime: executedAt.toISOString(),