zod arg parse 작업 진행

This commit is contained in:
2026-01-15 15:01:58 +00:00
parent 24fbace987
commit f12d3cf4fe
4 changed files with 27 additions and 18 deletions
@@ -11,9 +11,11 @@ import type {
import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js'; import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js';
import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js'; import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js';
import { JosaUtil } from '@sammo-ts/common'; import { JosaUtil } from '@sammo-ts/common';
import { z } from 'zod';
import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js'; import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js';
import type { GeneralTurnCommandSpec } from './index.js'; import type { GeneralTurnCommandSpec } from './index.js';
import type { MapDefinition } from '@sammo-ts/logic/world/types.js'; import type { MapDefinition } from '@sammo-ts/logic/world/types.js';
import { parseArgsWithSchema } from '../parseArgs.js';
export interface NPCSelfArgs { export interface NPCSelfArgs {
optionText: string; optionText: string;
@@ -27,6 +29,12 @@ export type NPCSelfResolveContext<TriggerState extends GeneralTriggerState = Gen
const ACTION_NAME = 'NPC능동'; const ACTION_NAME = 'NPC능동';
const ACTION_KEY = 'che_NPC능동'; const ACTION_KEY = 'che_NPC능동';
const NPC_SELF_ARGS_SCHEMA = z.discriminatedUnion('optionText', [
z.object({
optionText: z.literal('순간이동'),
destCityId: z.number(),
}),
]);
export class ActionResolver< export class ActionResolver<
TriggerState extends GeneralTriggerState = GeneralTriggerState, TriggerState extends GeneralTriggerState = GeneralTriggerState,
@@ -90,18 +98,7 @@ export class ActionDefinition<
} }
parseArgs(raw: unknown): NPCSelfArgs | null { parseArgs(raw: unknown): NPCSelfArgs | null {
const data = raw as Partial<NPCSelfArgs>; return parseArgsWithSchema(NPC_SELF_ARGS_SCHEMA, raw);
if (!data.optionText) return null;
if (data.optionText === '순간이동') {
if (typeof data.destCityId !== 'number') return null;
// We can check city existence here deeply but constraint is better place usually,
// simplified parse just structural.
return { optionText: data.optionText, destCityId: data.destCityId };
}
// Only '순간이동' implemented in legacy file shown.
return null;
} }
buildConstraints(_ctx: ConstraintContext, args: NPCSelfArgs): Constraint[] { buildConstraints(_ctx: ConstraintContext, args: NPCSelfArgs): Constraint[] {
@@ -17,10 +17,12 @@ import type {
import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js'; import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js';
import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js'; import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js';
import { JosaUtil } from '@sammo-ts/common'; import { JosaUtil } from '@sammo-ts/common';
import { z } from 'zod';
import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js'; import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js';
import type { GeneralTurnCommandSpec } from './index.js'; import type { GeneralTurnCommandSpec } from './index.js';
import type { MapDefinition } from '@sammo-ts/logic/world/types.js'; import type { MapDefinition } from '@sammo-ts/logic/world/types.js';
import type { ActionContextBuilder } from '@sammo-ts/logic/actions/turn/actionContext.js'; import type { ActionContextBuilder } from '@sammo-ts/logic/actions/turn/actionContext.js';
import { parseArgsWithSchema } from '../parseArgs.js';
export interface ForcedMoveArgs { export interface ForcedMoveArgs {
destCityId: number; destCityId: number;
@@ -36,6 +38,9 @@ export interface ForcedMoveResolveContext<
const ACTION_NAME = '강행'; const ACTION_NAME = '강행';
const ACTION_KEY = 'che_강행'; const ACTION_KEY = 'che_강행';
const FORCED_MOVE_ARGS_SCHEMA = z.object({
destCityId: z.number(),
});
export class ActionResolver< export class ActionResolver<
TriggerState extends GeneralTriggerState = GeneralTriggerState, TriggerState extends GeneralTriggerState = GeneralTriggerState,
@@ -159,9 +164,7 @@ export class ActionDefinition<
} }
parseArgs(raw: unknown): ForcedMoveArgs | null { parseArgs(raw: unknown): ForcedMoveArgs | null {
const data = raw as Partial<ForcedMoveArgs>; return parseArgsWithSchema(FORCED_MOVE_ARGS_SCHEMA, raw);
if (typeof data.destCityId !== 'number') return null;
return { destCityId: data.destCityId };
} }
buildConstraints(ctx: ConstraintContext, _args: ForcedMoveArgs): Constraint[] { buildConstraints(ctx: ConstraintContext, _args: ForcedMoveArgs): Constraint[] {
@@ -17,10 +17,12 @@ import type {
import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js'; import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js';
import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js'; import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js';
import { JosaUtil } from '@sammo-ts/common'; import { JosaUtil } from '@sammo-ts/common';
import { z } from 'zod';
import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js'; import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js';
import type { ActionContextBuilder } from '@sammo-ts/logic/actions/turn/actionContext.js'; import type { ActionContextBuilder } from '@sammo-ts/logic/actions/turn/actionContext.js';
import type { GeneralTurnCommandSpec } from './index.js'; import type { GeneralTurnCommandSpec } from './index.js';
import type { MapDefinition } from '@sammo-ts/logic/world/types.js'; import type { MapDefinition } from '@sammo-ts/logic/world/types.js';
import { parseArgsWithSchema } from '../parseArgs.js';
export interface MoveArgs { export interface MoveArgs {
destCityId: number; destCityId: number;
@@ -36,6 +38,9 @@ export interface MoveResolveContext<
const ACTION_NAME = '이동'; const ACTION_NAME = '이동';
const ACTION_KEY = 'che_이동'; const ACTION_KEY = 'che_이동';
const MOVE_ARGS_SCHEMA = z.object({
destCityId: z.number(),
});
// Helper for map connectivity // Helper for map connectivity
const connectedCity = (): Constraint => ({ const connectedCity = (): Constraint => ({
@@ -155,9 +160,7 @@ export class ActionDefinition<
} }
parseArgs(raw: unknown): MoveArgs | null { parseArgs(raw: unknown): MoveArgs | null {
const data = raw as Partial<MoveArgs>; return parseArgsWithSchema(MOVE_ARGS_SCHEMA, raw);
if (typeof data.destCityId !== 'number') return null;
return { destCityId: data.destCityId };
} }
buildConstraints(ctx: ConstraintContext, _args: MoveArgs): Constraint[] { buildConstraints(ctx: ConstraintContext, _args: MoveArgs): Constraint[] {
@@ -0,0 +1,6 @@
import type { ZodType } from 'zod';
export const parseArgsWithSchema = <T>(schema: ZodType<T>, raw: unknown): T | null => {
const result = schema.safeParse(raw);
return result.success ? result.data : null;
};