feat: migrate diplomacy logic to new module and update imports
This commit is contained in:
@@ -18,7 +18,7 @@ import { finalizeLogEntry, type LogEntryDraft } from '@sammo-ts/logic';
|
||||
import type { TurnDaemonHooks } from '../lifecycle/types.js';
|
||||
import type { InMemoryTurnWorld } from './inMemoryWorld.js';
|
||||
import type { InMemoryReservedTurnStore } from './reservedTurnStore.js';
|
||||
import { buildDiplomacyMeta } from './diplomacy.js';
|
||||
import { buildDiplomacyMeta } from '@sammo-ts/logic';
|
||||
|
||||
export interface DatabaseTurnHooks {
|
||||
hooks: TurnDaemonHooks;
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
buildDiplomacyKey,
|
||||
processDiplomacyMonth,
|
||||
type DiplomacyPatch,
|
||||
} from './diplomacy.js';
|
||||
} from '@sammo-ts/logic';
|
||||
|
||||
export interface GeneralTurnContext {
|
||||
general: TurnGeneral;
|
||||
|
||||
@@ -35,7 +35,7 @@ import {
|
||||
buildDefaultDiplomacy,
|
||||
buildDiplomacyKey,
|
||||
type DiplomacyPatch,
|
||||
} from './diplomacy.js';
|
||||
} from '@sammo-ts/logic';
|
||||
import {
|
||||
buildCommandEnv,
|
||||
buildReservedTurnDefinitions,
|
||||
|
||||
@@ -24,7 +24,7 @@ import { loadMapDefinitionByName } from '../scenario/mapLoader.js';
|
||||
import type { UnitSetLoaderOptions } from '../scenario/unitSetLoader.js';
|
||||
import { loadUnitSetDefinitionByName } from '../scenario/unitSetLoader.js';
|
||||
import type { TurnDiplomacy, TurnGeneral, TurnWorldLoadResult } from './types.js';
|
||||
import { readDiplomacyMeta } from './diplomacy.js';
|
||||
import { readDiplomacyMeta } from '@sammo-ts/logic';
|
||||
|
||||
interface TurnWorldLoaderOptions {
|
||||
databaseUrl: string;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { TurnDiplomacy } from './types.js';
|
||||
import { clamp } from 'es-toolkit';
|
||||
|
||||
// 외교 상태 코드 (legacy 기준).
|
||||
@@ -15,20 +14,14 @@ export const DEFAULT_WAR_TERM = 6;
|
||||
// TODO: 불가침 제의/수락 등 외교 커맨드 전환 규칙을 이 모듈에 추가 예정.
|
||||
const MAX_WAR_TERM = 13;
|
||||
|
||||
export const buildDiplomacyKey = (srcNationId: number, destNationId: number): string =>
|
||||
`${srcNationId}:${destNationId}`;
|
||||
|
||||
export const buildDefaultDiplomacy = (
|
||||
srcNationId: number,
|
||||
destNationId: number
|
||||
): TurnDiplomacy => ({
|
||||
fromNationId: srcNationId,
|
||||
toNationId: destNationId,
|
||||
state: DIPLOMACY_STATE.TRADE,
|
||||
term: 0,
|
||||
dead: 0,
|
||||
meta: {},
|
||||
});
|
||||
export interface DiplomacyEntry {
|
||||
fromNationId: number;
|
||||
toNationId: number;
|
||||
state: number;
|
||||
term: number;
|
||||
dead: number;
|
||||
meta: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface DiplomacyPatch {
|
||||
state?: number;
|
||||
@@ -38,10 +31,27 @@ export interface DiplomacyPatch {
|
||||
meta?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export const buildDiplomacyKey = (
|
||||
srcNationId: number,
|
||||
destNationId: number
|
||||
): string => `${srcNationId}:${destNationId}`;
|
||||
|
||||
export const buildDefaultDiplomacy = (
|
||||
srcNationId: number,
|
||||
destNationId: number
|
||||
): DiplomacyEntry => ({
|
||||
fromNationId: srcNationId,
|
||||
toNationId: destNationId,
|
||||
state: DIPLOMACY_STATE.TRADE,
|
||||
term: 0,
|
||||
dead: 0,
|
||||
meta: {},
|
||||
});
|
||||
|
||||
export const applyDiplomacyPatch = (
|
||||
entry: TurnDiplomacy,
|
||||
entry: DiplomacyEntry,
|
||||
patch: DiplomacyPatch
|
||||
): TurnDiplomacy => {
|
||||
): DiplomacyEntry => {
|
||||
const nextDead =
|
||||
typeof patch.dead === 'number'
|
||||
? patch.dead
|
||||
@@ -68,21 +78,22 @@ export const readDiplomacyMeta = (
|
||||
};
|
||||
|
||||
export const buildDiplomacyMeta = (
|
||||
entry: TurnDiplomacy
|
||||
entry: DiplomacyEntry
|
||||
): Record<string, unknown> => ({
|
||||
...entry.meta,
|
||||
dead: entry.dead,
|
||||
});
|
||||
|
||||
|
||||
export const processDiplomacyMonth = (
|
||||
diplomacy: TurnDiplomacy[],
|
||||
diplomacy: DiplomacyEntry[],
|
||||
generalCounts: Map<number, number>
|
||||
): TurnDiplomacy[] => {
|
||||
): DiplomacyEntry[] => {
|
||||
const next = diplomacy.map((entry) => ({
|
||||
...entry,
|
||||
meta: { ...entry.meta },
|
||||
}));
|
||||
const byKey = new Map<string, TurnDiplomacy>(
|
||||
const byKey = new Map<string, DiplomacyEntry>(
|
||||
next.map((entry) => [
|
||||
buildDiplomacyKey(entry.fromNationId, entry.toNationId),
|
||||
entry,
|
||||
@@ -2,6 +2,7 @@ export * from './domain/entities.js';
|
||||
export type { RandomGenerator } from '@sammo-ts/common';
|
||||
export * from './actions/index.js';
|
||||
export * from './constraints/index.js';
|
||||
export * from './diplomacy/index.js';
|
||||
export * from './logging/index.js';
|
||||
export * from './messages/index.js';
|
||||
export * from './items/index.js';
|
||||
|
||||
@@ -4,8 +4,8 @@ import {
|
||||
DEFAULT_WAR_TERM,
|
||||
DIPLOMACY_STATE,
|
||||
processDiplomacyMonth,
|
||||
} from '../src/turn/diplomacy.js';
|
||||
import type { TurnDiplomacy } from '../src/turn/types.js';
|
||||
type DiplomacyEntry,
|
||||
} from '@sammo-ts/logic';
|
||||
|
||||
const buildEntry = (
|
||||
fromNationId: number,
|
||||
@@ -13,7 +13,7 @@ const buildEntry = (
|
||||
state: number,
|
||||
term: number,
|
||||
dead = 0
|
||||
): TurnDiplomacy => ({
|
||||
): DiplomacyEntry => ({
|
||||
fromNationId,
|
||||
toNationId,
|
||||
state,
|
||||
Reference in New Issue
Block a user