Refactor constraints and action definitions for improved clarity and functionality

- Renamed `requireMinimumTerm` to `reqMinimumTreatyTerm` for consistency in naming conventions.
- Introduced `battleGroundCity` constraint to encapsulate logic for checking if a city is in a war zone.
- Added `hasRouteToDestCity` constraint to validate city accessibility based on distance and resource requirements.
- Replaced `alwaysFail` with `denyWithReason` for better error handling in action definitions.
- Implemented `reqEnvValue` for environment value checks with comparison operators.
- Enhanced nation constraints with `reqNationValue` and `reqDestNationValue` for flexible value comparisons.
- Added `wanderingNation` constraint to check if a nation is classified as wandering.
- Improved general constraints with `reqGeneralValue` for dynamic value checks.
- Updated various action definitions to utilize new constraints for better maintainability and readability.
This commit is contained in:
2026-02-06 18:55:43 +00:00
parent d7cb8c77e3
commit 15730feafe
35 changed files with 1004 additions and 298 deletions
@@ -2,12 +2,12 @@ import type { GeneralTriggerState } from '@sammo-ts/logic/domain/entities.js';
import type { Constraint, ConstraintContext } from '@sammo-ts/logic/constraints/types.js';
import {
beChief,
destGeneralInDestNation,
disallowDiplomacyBetweenStatus,
existsDestGeneral,
existsDestNation,
notBeNeutral,
occupiedCity,
reqDestNationValue,
suppliedCity,
} from '@sammo-ts/logic/constraints/presets.js';
import { allow, unknownOrDeny } from '@sammo-ts/logic/constraints/helpers.js';
@@ -64,8 +64,8 @@ const parseMonth = (raw: unknown): number | null => {
const resolveMonthIndex = (year: number, month: number): number => year * 12 + month - 1;
const requireFutureTerm = (): Constraint => ({
name: 'RequireNonAggressionFutureTerm',
const reqFutureTreatyTerm = (): Constraint => ({
name: 'reqFutureTreatyTerm',
requires: () => [
{ kind: 'arg', key: 'year' },
{ kind: 'arg', key: 'month' },
@@ -114,21 +114,6 @@ const requireFutureTerm = (): Constraint => ({
},
});
const notSameDestGeneral = (): Constraint => ({
name: 'NotSameDestGeneral',
requires: () => [{ kind: 'arg', key: 'destGeneralId' }],
test: (ctx) => {
const destGeneralId = ctx.args.destGeneralId;
if (typeof destGeneralId !== 'number') {
return unknownOrDeny(ctx, [{ kind: 'arg', key: 'destGeneralId' }], '장수 정보가 없습니다.');
}
if (destGeneralId === ctx.actorId) {
return { kind: 'deny', reason: '대상이 올바르지 않습니다.' };
}
return allow();
},
});
// 불가침 수락은 메시지와 연결되는 즉시 국가 커맨드로 사용한다.
export class ActionDefinition<
TriggerState extends GeneralTriggerState = GeneralTriggerState,
@@ -161,9 +146,8 @@ export class ActionDefinition<
suppliedCity(),
existsDestNation(),
existsDestGeneral(),
destGeneralInDestNation(),
notSameDestGeneral(),
requireFutureTerm(),
reqDestNationValue('level', '국가규모', '>', 0, '상대국 정보가 없습니다.'),
reqFutureTreatyTerm(),
disallowDiplomacyBetweenStatus({
0: '아국과 이미 교전중입니다.',
1: '아국과 이미 선포중입니다.',
@@ -3,12 +3,11 @@ import type { Constraint, ConstraintContext } from '@sammo-ts/logic/constraints/
import {
allowDiplomacyBetweenStatus,
beChief,
destGeneralInDestNation,
existsDestGeneral,
existsDestNation,
notBeNeutral,
reqDestNationValue,
} from '@sammo-ts/logic/constraints/presets.js';
import { allow, unknownOrDeny } from '@sammo-ts/logic/constraints/helpers.js';
import type { GeneralActionDefinition } from '@sammo-ts/logic/actions/definition.js';
import type { GeneralActionOutcome, GeneralActionResolveContext } from '@sammo-ts/logic/actions/engine.js';
import { createDiplomacyPatchEffect, createLogEffect } from '@sammo-ts/logic/actions/engine.js';
@@ -37,21 +36,6 @@ const parseGeneralId = (raw: unknown): number | null => {
return raw > 0 ? Math.floor(raw) : null;
};
const notSameDestGeneral = (): Constraint => ({
name: 'NotSameDestGeneral',
requires: () => [{ kind: 'arg', key: 'destGeneralId' }],
test: (ctx) => {
const destGeneralId = ctx.args.destGeneralId;
if (typeof destGeneralId !== 'number') {
return unknownOrDeny(ctx, [{ kind: 'arg', key: 'destGeneralId' }], '장수 정보가 없습니다.');
}
if (destGeneralId === ctx.actorId) {
return { kind: 'deny', reason: '대상이 올바르지 않습니다.' };
}
return allow();
},
});
// 불가침 파기 수락은 메시지와 연결되는 즉시 국가 커맨드로 사용한다.
export class ActionDefinition<
TriggerState extends GeneralTriggerState = GeneralTriggerState,
@@ -75,8 +59,7 @@ export class ActionDefinition<
notBeNeutral(),
existsDestNation(),
existsDestGeneral(),
destGeneralInDestNation(),
notSameDestGeneral(),
reqDestNationValue('level', '국가규모', '>', 0, '상대국 정보가 없습니다.'),
allowDiplomacyBetweenStatus([DIPLOMACY_NON_AGGRESSION], '불가침 중인 상대국에게만 가능합니다.'),
];
}
@@ -3,12 +3,11 @@ import type { Constraint, ConstraintContext } from '@sammo-ts/logic/constraints/
import {
allowDiplomacyBetweenStatus,
beChief,
destGeneralInDestNation,
existsDestGeneral,
existsDestNation,
notBeNeutral,
reqDestNationValue,
} from '@sammo-ts/logic/constraints/presets.js';
import { allow, unknownOrDeny } from '@sammo-ts/logic/constraints/helpers.js';
import type { GeneralActionDefinition } from '@sammo-ts/logic/actions/definition.js';
import type { GeneralActionOutcome, GeneralActionResolveContext } from '@sammo-ts/logic/actions/engine.js';
import { createDiplomacyPatchEffect, createLogEffect } from '@sammo-ts/logic/actions/engine.js';
@@ -37,21 +36,6 @@ const parseGeneralId = (raw: unknown): number | null => {
return raw > 0 ? Math.floor(raw) : null;
};
const notSameDestGeneral = (): Constraint => ({
name: 'NotSameDestGeneral',
requires: () => [{ kind: 'arg', key: 'destGeneralId' }],
test: (ctx) => {
const destGeneralId = ctx.args.destGeneralId;
if (typeof destGeneralId !== 'number') {
return unknownOrDeny(ctx, [{ kind: 'arg', key: 'destGeneralId' }], '장수 정보가 없습니다.');
}
if (destGeneralId === ctx.actorId) {
return { kind: 'deny', reason: '대상이 올바르지 않습니다.' };
}
return allow();
},
});
// 종전 수락은 메시지와 연결되는 즉시 국가 커맨드로 사용한다.
export class ActionDefinition<
TriggerState extends GeneralTriggerState = GeneralTriggerState,
@@ -75,8 +59,7 @@ export class ActionDefinition<
notBeNeutral(),
existsDestNation(),
existsDestGeneral(),
destGeneralInDestNation(),
notSameDestGeneral(),
reqDestNationValue('level', '국가규모', '>', 0, '상대국 정보가 없습니다.'),
allowDiplomacyBetweenStatus([0, 1], '상대국과 선포, 전쟁중이지 않습니다.'),
];
}