feat: migrate diplomacy logic to new module and update imports

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