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:
@@ -499,6 +499,28 @@ export const beNeutralCity = (): Constraint => ({
|
||||
},
|
||||
});
|
||||
|
||||
export const constructableCity = (): Constraint => ({
|
||||
name: 'constructableCity',
|
||||
requires: (ctx) => (ctx.cityId !== undefined ? [{ kind: 'city', id: ctx.cityId }] : []),
|
||||
test: (ctx, view) => {
|
||||
const city = readCity(view, ctx.cityId);
|
||||
if (!city) {
|
||||
if (ctx.cityId === undefined) {
|
||||
return unknownOrDeny(ctx, [], '도시 정보가 없습니다.');
|
||||
}
|
||||
const req: RequirementKey = { kind: 'city', id: ctx.cityId };
|
||||
return unknownOrDeny(ctx, [req], '도시 정보가 없습니다.');
|
||||
}
|
||||
if (city.nationId !== 0) {
|
||||
return { kind: 'deny', reason: '공백지가 아닙니다.' };
|
||||
}
|
||||
if (![5, 6].includes(city.level)) {
|
||||
return { kind: 'deny', reason: '중, 소 도시에만 가능합니다.' };
|
||||
}
|
||||
return allow();
|
||||
},
|
||||
});
|
||||
|
||||
export const reqCityLevel = (levels: number[]): Constraint => ({
|
||||
name: 'reqCityLevel',
|
||||
requires: (ctx) => (ctx.cityId !== undefined ? [{ kind: 'city', id: ctx.cityId }] : []),
|
||||
|
||||
@@ -1,7 +1,37 @@
|
||||
import type { General } from '@sammo-ts/logic/domain/entities.js';
|
||||
import { allow, readDestGeneral, resolveDestGeneralId, resolveDestNationId, unknownOrDeny } from './helpers.js';
|
||||
import {
|
||||
allow,
|
||||
compareValues,
|
||||
readDestGeneral,
|
||||
resolveDestGeneralId,
|
||||
resolveDestNationId,
|
||||
unknownOrDeny,
|
||||
type CompareOperator,
|
||||
} from './helpers.js';
|
||||
import type { Constraint, ConstraintContext, RequirementKey, StateView } from './types.js';
|
||||
|
||||
const readNumericField = (source: Record<string, unknown>, key: string): number | null => {
|
||||
const value = source[key];
|
||||
return typeof value === 'number' ? value : null;
|
||||
};
|
||||
|
||||
const readStringField = (source: Record<string, unknown>, key: string): string | null => {
|
||||
const value = source[key];
|
||||
return typeof value === 'string' ? value : null;
|
||||
};
|
||||
|
||||
const readNationNumeric = (nation: Record<string, unknown>, key: string): number | null => {
|
||||
const direct = readNumericField(nation, key);
|
||||
if (direct !== null) {
|
||||
return direct;
|
||||
}
|
||||
const meta = nation.meta;
|
||||
if (!meta || typeof meta !== 'object' || Array.isArray(meta)) {
|
||||
return null;
|
||||
}
|
||||
return readNumericField(meta as Record<string, unknown>, key);
|
||||
};
|
||||
|
||||
export const notBeNeutral = (): Constraint => ({
|
||||
name: 'notBeNeutral',
|
||||
requires: (ctx) => [{ kind: 'general', id: ctx.actorId }],
|
||||
@@ -78,6 +108,25 @@ export const beMonarch = (): Constraint => ({
|
||||
},
|
||||
});
|
||||
|
||||
export const beLord = (): Constraint => ({
|
||||
name: 'beLord',
|
||||
requires: (ctx) => [{ kind: 'general', id: ctx.actorId }],
|
||||
test: (ctx, view) => {
|
||||
const req: RequirementKey = { kind: 'general', id: ctx.actorId };
|
||||
if (!view.has(req)) {
|
||||
return unknownOrDeny(ctx, [req], '장수 정보가 없습니다.');
|
||||
}
|
||||
const general = view.get(req) as General | null;
|
||||
if (!general) {
|
||||
return unknownOrDeny(ctx, [req], '장수 정보가 없습니다.');
|
||||
}
|
||||
if (general.officerLevel === 12) {
|
||||
return allow();
|
||||
}
|
||||
return { kind: 'deny', reason: '군주가 아닙니다.' };
|
||||
},
|
||||
});
|
||||
|
||||
export const reqGeneralGold = (
|
||||
getRequiredGold: (ctx: ConstraintContext, view: StateView) => number,
|
||||
requirements: RequirementKey[] = []
|
||||
@@ -237,6 +286,110 @@ export const noPenalty = (penaltyKey: string): Constraint => ({
|
||||
},
|
||||
});
|
||||
|
||||
export const reqGeneralValue = (
|
||||
key: string,
|
||||
keyNick: string,
|
||||
comp: CompareOperator,
|
||||
reqVal: unknown,
|
||||
errMsg: string | null = null
|
||||
): Constraint => ({
|
||||
name: 'reqGeneralValue',
|
||||
requires: (ctx) => [{ kind: 'general', id: ctx.actorId }],
|
||||
test: (ctx, view) => {
|
||||
const req: RequirementKey = { kind: 'general', id: ctx.actorId };
|
||||
if (!view.has(req)) {
|
||||
return unknownOrDeny(ctx, [req], '장수 정보가 없습니다.');
|
||||
}
|
||||
const general = view.get(req) as General | null;
|
||||
if (!general) {
|
||||
return unknownOrDeny(ctx, [req], '장수 정보가 없습니다.');
|
||||
}
|
||||
const source = general as unknown as Record<string, unknown>;
|
||||
let target = source[key];
|
||||
if (target === undefined && source.meta && typeof source.meta === 'object' && !Array.isArray(source.meta)) {
|
||||
target = (source.meta as Record<string, unknown>)[key];
|
||||
}
|
||||
|
||||
if (compareValues(target, comp, reqVal)) {
|
||||
return allow();
|
||||
}
|
||||
if (errMsg) {
|
||||
return { kind: 'deny', reason: errMsg };
|
||||
}
|
||||
return { kind: 'deny', reason: `${keyNick} 조건을 만족하지 않습니다.` };
|
||||
},
|
||||
});
|
||||
|
||||
export const allowJoinDestNation = (relYear: number): Constraint => ({
|
||||
name: 'allowJoinDestNation',
|
||||
requires: (ctx) => {
|
||||
const reqs: RequirementKey[] = [
|
||||
{ kind: 'general', id: ctx.actorId },
|
||||
{ kind: 'env', key: 'openingPartYear' },
|
||||
{ kind: 'env', key: 'initialNationGenLimit' },
|
||||
{ kind: 'env', key: 'maxGeneral' },
|
||||
];
|
||||
const destNationId = resolveDestNationId(ctx);
|
||||
if (destNationId !== undefined) {
|
||||
reqs.push({ kind: 'destNation', id: destNationId });
|
||||
}
|
||||
return reqs;
|
||||
},
|
||||
test: (ctx, view) => {
|
||||
const generalReq: RequirementKey = { kind: 'general', id: ctx.actorId };
|
||||
if (!view.has(generalReq)) {
|
||||
return unknownOrDeny(ctx, [generalReq], '장수 정보가 없습니다.');
|
||||
}
|
||||
const general = view.get(generalReq) as General | null;
|
||||
if (!general) {
|
||||
return unknownOrDeny(ctx, [generalReq], '장수 정보가 없습니다.');
|
||||
}
|
||||
|
||||
const destNationId = resolveDestNationId(ctx);
|
||||
if (destNationId === undefined) {
|
||||
return unknownOrDeny(ctx, [], '국가 정보가 없습니다.');
|
||||
}
|
||||
const destReq: RequirementKey = { kind: 'destNation', id: destNationId };
|
||||
if (!view.has(destReq)) {
|
||||
return unknownOrDeny(ctx, [destReq], '국가 정보가 없습니다.');
|
||||
}
|
||||
const destNation = view.get(destReq) as Record<string, unknown> | null;
|
||||
if (!destNation) {
|
||||
return unknownOrDeny(ctx, [destReq], '국가 정보가 없습니다.');
|
||||
}
|
||||
|
||||
const openingPartYear = view.get({ kind: 'env', key: 'openingPartYear' }) as number | null;
|
||||
const initialNationGenLimit = view.get({ kind: 'env', key: 'initialNationGenLimit' }) as number | null;
|
||||
const defaultMaxGeneral = view.get({ kind: 'env', key: 'maxGeneral' }) as number | null;
|
||||
|
||||
const gennum = readNationNumeric(destNation, 'gennum') ?? 0;
|
||||
const scout = readNationNumeric(destNation, 'scout') ?? 0;
|
||||
const name = readStringField(destNation, 'name') ?? '';
|
||||
|
||||
const openingLimit = typeof openingPartYear === 'number' ? openingPartYear : 0;
|
||||
const initialLimit = typeof initialNationGenLimit === 'number' ? initialNationGenLimit : 0;
|
||||
const normalLimit = typeof defaultMaxGeneral === 'number' ? defaultMaxGeneral : initialLimit;
|
||||
const genLimit = relYear < openingLimit ? initialLimit : normalLimit;
|
||||
if (genLimit > 0 && gennum >= genLimit) {
|
||||
return { kind: 'deny', reason: '임관이 제한되고 있습니다.' };
|
||||
}
|
||||
|
||||
if (scout === 1) {
|
||||
return { kind: 'deny', reason: '임관이 금지되어 있습니다.' };
|
||||
}
|
||||
|
||||
if (general.npcState < 2 && name.startsWith('ⓤ')) {
|
||||
return { kind: 'deny', reason: '유저장은 태수국에 임관할 수 없습니다.' };
|
||||
}
|
||||
|
||||
if (general.npcState !== 9 && name.startsWith('ⓞ')) {
|
||||
return { kind: 'deny', reason: '이민족 국가에 임관할 수 없습니다.' };
|
||||
}
|
||||
|
||||
return allow();
|
||||
},
|
||||
});
|
||||
|
||||
export const reqGeneralCrewMargin = (
|
||||
getCrewTypeId: (ctx: ConstraintContext, view: StateView) => number | null,
|
||||
requirements: RequirementKey[] = []
|
||||
|
||||
@@ -120,3 +120,28 @@ export const parsePercent = (value: string): number | null => {
|
||||
}
|
||||
return Number(match[1]) / 100;
|
||||
};
|
||||
|
||||
export type CompareOperator = '>' | '>=' | '==' | '<=' | '<' | '!=' | '===' | '!==';
|
||||
|
||||
export const compareValues = (target: unknown, op: CompareOperator, source: unknown): boolean => {
|
||||
const lhs = target as any;
|
||||
const rhs = source as any;
|
||||
switch (op) {
|
||||
case '<':
|
||||
return lhs < rhs;
|
||||
case '<=':
|
||||
return lhs <= rhs;
|
||||
case '==':
|
||||
return lhs == rhs;
|
||||
case '!=':
|
||||
return lhs != rhs;
|
||||
case '===':
|
||||
return lhs === rhs;
|
||||
case '!==':
|
||||
return lhs !== rhs;
|
||||
case '>=':
|
||||
return lhs >= rhs;
|
||||
case '>':
|
||||
return lhs > rhs;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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 };
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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