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,7 +1,34 @@
|
||||
import type { City, General, Nation } from '@sammo-ts/logic/domain/entities.js';
|
||||
import { allow, readGeneral, readMetaNumber, readNation, resolveDestNationId, unknownOrDeny } from './helpers.js';
|
||||
import {
|
||||
allow,
|
||||
compareValues,
|
||||
parsePercent,
|
||||
readGeneral,
|
||||
readMetaNumber,
|
||||
readNation,
|
||||
resolveDestNationId,
|
||||
unknownOrDeny,
|
||||
type CompareOperator,
|
||||
} from './helpers.js';
|
||||
import type { Constraint, ConstraintContext, RequirementKey, StateView } from './types.js';
|
||||
|
||||
const readNationField = (nation: Nation, key: string): unknown => {
|
||||
const source = nation as unknown as Record<string, unknown>;
|
||||
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
||||
return source[key];
|
||||
}
|
||||
return nation.meta[key];
|
||||
};
|
||||
|
||||
const readNationMaxField = (nation: Nation, key: string): unknown => {
|
||||
const maxKey = `${key}_max`;
|
||||
const source = nation as unknown as Record<string, unknown>;
|
||||
if (Object.prototype.hasOwnProperty.call(source, maxKey)) {
|
||||
return source[maxKey];
|
||||
}
|
||||
return nation.meta[maxKey];
|
||||
};
|
||||
|
||||
export const notWanderingNation = (): Constraint => ({
|
||||
name: 'notWanderingNation',
|
||||
requires: (ctx) => (ctx.nationId !== undefined ? [{ kind: 'nation', id: ctx.nationId }] : []),
|
||||
@@ -40,6 +67,25 @@ export const beWanderingNation = (): Constraint => ({
|
||||
},
|
||||
});
|
||||
|
||||
export const wanderingNation = (): Constraint => ({
|
||||
name: 'wanderingNation',
|
||||
requires: (ctx) => (ctx.nationId !== undefined ? [{ kind: 'nation', id: ctx.nationId }] : []),
|
||||
test: (ctx, view) => {
|
||||
const nation = readNation(view, ctx.nationId);
|
||||
if (!nation) {
|
||||
if (ctx.nationId === undefined) {
|
||||
return unknownOrDeny(ctx, [], '국가 정보가 없습니다.');
|
||||
}
|
||||
const req: RequirementKey = { kind: 'nation', id: ctx.nationId };
|
||||
return unknownOrDeny(ctx, [req], '국가 정보가 없습니다.');
|
||||
}
|
||||
if (nation.level === 0) {
|
||||
return allow();
|
||||
}
|
||||
return { kind: 'deny', reason: '방랑군이 아닙니다.' };
|
||||
},
|
||||
});
|
||||
|
||||
export const availableStrategicCommand = (allowTurnCnt = 0): Constraint => ({
|
||||
name: 'availableStrategicCommand',
|
||||
requires: (ctx) => {
|
||||
@@ -273,6 +319,148 @@ export const checkNationNameDuplicate = (name: string): Constraint => ({
|
||||
},
|
||||
});
|
||||
|
||||
export const reqNationValue = (
|
||||
key: string,
|
||||
keyNick: string,
|
||||
comp: CompareOperator,
|
||||
reqVal: number | string,
|
||||
errMsg: string | null = null
|
||||
): Constraint => ({
|
||||
name: 'reqNationValue',
|
||||
requires: (ctx) => (ctx.nationId !== undefined ? [{ kind: 'nation', id: ctx.nationId }] : []),
|
||||
test: (ctx, view) => {
|
||||
const nation = readNation(view, ctx.nationId);
|
||||
if (!nation) {
|
||||
if (ctx.nationId === undefined) {
|
||||
return unknownOrDeny(ctx, [], '국가 정보가 없습니다.');
|
||||
}
|
||||
const req: RequirementKey = { kind: 'nation', id: ctx.nationId };
|
||||
return unknownOrDeny(ctx, [req], '국가 정보가 없습니다.');
|
||||
}
|
||||
|
||||
const target = readNationField(nation, key);
|
||||
let required: unknown = reqVal;
|
||||
if (typeof reqVal === 'string') {
|
||||
const ratio = parsePercent(reqVal);
|
||||
if (ratio !== null) {
|
||||
const maxValue = readNationMaxField(nation, key);
|
||||
if (typeof maxValue === 'number') {
|
||||
required = maxValue * ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (compareValues(target, comp, required)) {
|
||||
return allow();
|
||||
}
|
||||
if (errMsg) {
|
||||
return { kind: 'deny', reason: errMsg };
|
||||
}
|
||||
return { kind: 'deny', reason: `${keyNick} 조건을 만족하지 않습니다.` };
|
||||
},
|
||||
});
|
||||
|
||||
export const reqDestNationValue = (
|
||||
key: string,
|
||||
keyNick: string,
|
||||
comp: CompareOperator,
|
||||
reqVal: number | string,
|
||||
errMsg: string | null = null
|
||||
): Constraint => ({
|
||||
name: 'reqDestNationValue',
|
||||
requires: (ctx) => {
|
||||
const destNationId = resolveDestNationId(ctx);
|
||||
if (destNationId === undefined) {
|
||||
return [];
|
||||
}
|
||||
return [{ kind: 'destNation', id: destNationId }];
|
||||
},
|
||||
test: (ctx, view) => {
|
||||
const destNationId = resolveDestNationId(ctx);
|
||||
if (destNationId === undefined) {
|
||||
return unknownOrDeny(ctx, [], '상대 국가 정보가 없습니다.');
|
||||
}
|
||||
const req: RequirementKey = { kind: 'destNation', id: destNationId };
|
||||
if (!view.has(req)) {
|
||||
return unknownOrDeny(ctx, [req], '상대 국가 정보가 없습니다.');
|
||||
}
|
||||
const nation = view.get(req) as Nation | null;
|
||||
if (!nation) {
|
||||
return unknownOrDeny(ctx, [req], '상대 국가 정보가 없습니다.');
|
||||
}
|
||||
|
||||
const target = readNationField(nation, key);
|
||||
let required: unknown = reqVal;
|
||||
if (typeof reqVal === 'string') {
|
||||
const ratio = parsePercent(reqVal);
|
||||
if (ratio !== null) {
|
||||
const maxValue = readNationMaxField(nation, key);
|
||||
if (typeof maxValue === 'number') {
|
||||
required = maxValue * ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (compareValues(target, comp, required)) {
|
||||
return allow();
|
||||
}
|
||||
if (errMsg) {
|
||||
return { kind: 'deny', reason: errMsg };
|
||||
}
|
||||
return { kind: 'deny', reason: `${keyNick} 조건을 만족하지 않습니다.` };
|
||||
},
|
||||
});
|
||||
|
||||
export const allowWar = (): Constraint => ({
|
||||
name: 'allowWar',
|
||||
requires: (ctx) => (ctx.nationId !== undefined ? [{ kind: 'nation', id: ctx.nationId }] : []),
|
||||
test: (ctx, view) => {
|
||||
const nation = readNation(view, ctx.nationId);
|
||||
if (!nation) {
|
||||
if (ctx.nationId === undefined) {
|
||||
return unknownOrDeny(ctx, [], '국가 정보가 없습니다.');
|
||||
}
|
||||
const req: RequirementKey = { kind: 'nation', id: ctx.nationId };
|
||||
return unknownOrDeny(ctx, [req], '국가 정보가 없습니다.');
|
||||
}
|
||||
|
||||
const source = nation as unknown as Record<string, unknown>;
|
||||
const war = typeof source.war === 'number' ? source.war : nation.meta.war;
|
||||
if (typeof war !== 'number' || war === 0) {
|
||||
return allow();
|
||||
}
|
||||
return { kind: 'deny', reason: '현재 전쟁 금지입니다.' };
|
||||
},
|
||||
});
|
||||
|
||||
export const reqNationAuxValue = (
|
||||
key: string,
|
||||
defaultValue: number,
|
||||
comp: CompareOperator,
|
||||
reqVal: number,
|
||||
errMsg: string
|
||||
): Constraint => ({
|
||||
name: 'reqNationAuxValue',
|
||||
requires: (ctx) => (ctx.nationId !== undefined ? [{ kind: 'nation', id: ctx.nationId }] : []),
|
||||
test: (ctx, view) => {
|
||||
const nation = readNation(view, ctx.nationId);
|
||||
if (!nation) {
|
||||
if (ctx.nationId === undefined) {
|
||||
return unknownOrDeny(ctx, [], '국가 정보가 없습니다.');
|
||||
}
|
||||
const req: RequirementKey = { kind: 'nation', id: ctx.nationId };
|
||||
return unknownOrDeny(ctx, [req], '국가 정보가 없습니다.');
|
||||
}
|
||||
|
||||
const raw = nation.meta[key];
|
||||
const target = typeof raw === 'number' ? raw : defaultValue;
|
||||
if (compareValues(target, comp, reqVal)) {
|
||||
return allow();
|
||||
}
|
||||
return { kind: 'deny', reason: errMsg };
|
||||
},
|
||||
});
|
||||
|
||||
export const nearNation = (): Constraint => ({
|
||||
name: 'nearNation',
|
||||
requires: (ctx) => {
|
||||
|
||||
Reference in New Issue
Block a user