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:
@@ -1,12 +1,15 @@
|
||||
import { allow } from './helpers.js';
|
||||
import { allow, compareValues, type CompareOperator } from './helpers.js';
|
||||
import type { Constraint } from './types.js';
|
||||
|
||||
export const alwaysFail = (reason: string): Constraint => ({
|
||||
name: 'alwaysFail',
|
||||
export const denyWithReason = (reason: string): Constraint => ({
|
||||
name: 'denyWithReason',
|
||||
requires: () => [],
|
||||
test: () => ({ kind: 'deny', reason }),
|
||||
});
|
||||
|
||||
// TODO: 점진 이전을 위해 유지. 신규 코드에서는 denyWithReason을 사용한다.
|
||||
export const alwaysFail = denyWithReason;
|
||||
|
||||
export const notOpeningPart = (relYear: number, openingPartYear: number): Constraint => ({
|
||||
name: 'notOpeningPart',
|
||||
requires: () => [],
|
||||
@@ -43,3 +46,24 @@ export const beOpeningPart = (): Constraint => ({
|
||||
return { kind: 'deny', reason: '초반 제한 중에는 불가능합니다.' };
|
||||
},
|
||||
});
|
||||
|
||||
export const reqEnvValue = (
|
||||
key: string,
|
||||
comp: CompareOperator,
|
||||
reqVal: unknown,
|
||||
failMessage: string
|
||||
): Constraint => ({
|
||||
name: 'reqEnvValue',
|
||||
requires: () => [{ kind: 'env', key }],
|
||||
test: (_ctx, view) => {
|
||||
const req = { kind: 'env', key } as const;
|
||||
if (!view.has(req)) {
|
||||
return { kind: 'deny', reason: failMessage };
|
||||
}
|
||||
const envValue = view.get(req);
|
||||
if (compareValues(envValue, comp, reqVal)) {
|
||||
return allow();
|
||||
}
|
||||
return { kind: 'deny', reason: failMessage };
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user