fix live sortie turn lifecycle parity
This commit is contained in:
@@ -100,6 +100,8 @@ export interface General<TriggerState extends GeneralTriggerState = GeneralTrigg
|
|||||||
itemInventory?: GeneralItemInventory;
|
itemInventory?: GeneralItemInventory;
|
||||||
meta: GeneralMeta;
|
meta: GeneralMeta;
|
||||||
lastTurn?: GeneralLastTurn;
|
lastTurn?: GeneralLastTurn;
|
||||||
|
turnTime?: Date;
|
||||||
|
recentWarTime?: Date | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface City {
|
export interface City {
|
||||||
|
|||||||
@@ -13,12 +13,6 @@ const MINUTES_PER_DAY = 24 * 60;
|
|||||||
|
|
||||||
const toMinuteOfDay = (date: Date): number => date.getHours() * 60 + date.getMinutes();
|
const toMinuteOfDay = (date: Date): number => date.getHours() * 60 + date.getMinutes();
|
||||||
|
|
||||||
const toLocalDateAtMinute = (date: Date, minuteOfDay: number, dayOffset = 0): Date => {
|
|
||||||
const hour = Math.floor(minuteOfDay / 60);
|
|
||||||
const minute = minuteOfDay % 60;
|
|
||||||
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + dayOffset, hour, minute, 0, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
const normalizeEntries = (entries: TurnScheduleEntries): TurnScheduleEntries => {
|
const normalizeEntries = (entries: TurnScheduleEntries): TurnScheduleEntries => {
|
||||||
const normalized = entries
|
const normalized = entries
|
||||||
.map((entry) => ({
|
.map((entry) => ({
|
||||||
@@ -52,26 +46,5 @@ export const getTickMinutesAt = (date: Date, schedule: TurnSchedule): number =>
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getNextTurnAt = (date: Date, schedule: TurnSchedule): Date => {
|
export const getNextTurnAt = (date: Date, schedule: TurnSchedule): Date => {
|
||||||
const entries = normalizeEntries(schedule.entries);
|
return new Date(date.getTime() + getTickMinutesAt(date, schedule) * 60_000);
|
||||||
const minuteOfDay = toMinuteOfDay(date);
|
|
||||||
const index = findCurrentEntryIndex(minuteOfDay, entries);
|
|
||||||
const currentIndex = index >= 0 ? index : entries.length - 1;
|
|
||||||
const startDayOffset = index >= 0 ? 0 : -1;
|
|
||||||
const nextIndex = (currentIndex + 1) % entries.length;
|
|
||||||
const nextDayOffset = startDayOffset + (nextIndex > currentIndex ? 0 : 1);
|
|
||||||
|
|
||||||
const currentEntry = getEntryAt(entries, currentIndex);
|
|
||||||
const nextEntry = getEntryAt(entries, nextIndex);
|
|
||||||
const segmentStart = toLocalDateAtMinute(date, currentEntry.startMinute, startDayOffset);
|
|
||||||
const segmentEnd = toLocalDateAtMinute(date, nextEntry.startMinute, nextDayOffset);
|
|
||||||
|
|
||||||
const elapsedMinutes = (date.getTime() - segmentStart.getTime()) / 60000;
|
|
||||||
const nextStep = Math.floor(elapsedMinutes / currentEntry.tickMinutes) + 1;
|
|
||||||
const nextCandidate = new Date(segmentStart.getTime() + nextStep * currentEntry.tickMinutes * 60000);
|
|
||||||
|
|
||||||
if (nextCandidate.getTime() < segmentEnd.getTime()) {
|
|
||||||
return nextCandidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
return segmentEnd;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import { getTechAbility, getTechCost } from '@sammo-ts/logic/world/unitSet.js';
|
|||||||
import type { WarActionPipeline, WarActionContext } from '../actions.js';
|
import type { WarActionPipeline, WarActionContext } from '../actions.js';
|
||||||
import type { WarEngineConfig } from '../types.js';
|
import type { WarEngineConfig } from '../types.js';
|
||||||
import type { WarCrewType } from '../crewType.js';
|
import type { WarCrewType } from '../crewType.js';
|
||||||
import { clamp, clampMin, getMetaNumber, getMetaString, increaseMetaNumber, round } from '../utils.js';
|
import { clamp, clampMin, getMetaNumber, increaseMetaNumber, round } from '../utils.js';
|
||||||
import { WAR_CRITICAL_RANGE, WarUnit, resolveNationTech } from './base.js';
|
import { WAR_CRITICAL_RANGE, WarUnit, resolveNationTech } from './base.js';
|
||||||
type CityUnit = WarUnit & { getCityId: () => number };
|
type CityUnit = WarUnit & { getCityId: () => number };
|
||||||
|
|
||||||
@@ -23,8 +23,6 @@ const isCityUnit = (unit: WarUnit | null): unit is CityUnit =>
|
|||||||
Boolean(unit && typeof (unit as CityUnit).getCityId === 'function');
|
Boolean(unit && typeof (unit as CityUnit).getCityId === 'function');
|
||||||
|
|
||||||
const META_EXP_LEVEL = 'explevel';
|
const META_EXP_LEVEL = 'explevel';
|
||||||
const META_TURN_TIME = 'turnTime';
|
|
||||||
const META_RECENT_WAR = 'recentWar';
|
|
||||||
const META_DEX_PREFIX = 'dex';
|
const META_DEX_PREFIX = 'dex';
|
||||||
const META_RANK_PREFIX = 'rank_';
|
const META_RANK_PREFIX = 'rank_';
|
||||||
const META_INTEL_EXP = 'intel_exp';
|
const META_INTEL_EXP = 'intel_exp';
|
||||||
@@ -119,18 +117,16 @@ export class WarUnitGeneral<
|
|||||||
}
|
}
|
||||||
|
|
||||||
const baseTurnTime = this.isAttacker()
|
const baseTurnTime = this.isAttacker()
|
||||||
? getMetaString(this.general.meta, META_TURN_TIME)
|
? this.general.turnTime
|
||||||
: oppose instanceof WarUnitGeneral
|
: oppose instanceof WarUnitGeneral
|
||||||
? getMetaString(oppose.general.meta, META_TURN_TIME)
|
? oppose.general.turnTime
|
||||||
: getMetaString(this.general.meta, META_TURN_TIME);
|
: this.general.turnTime;
|
||||||
if (!baseTurnTime) {
|
if (!baseTurnTime) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const base = baseTurnTime.slice(0, Math.max(0, baseTurnTime.length - 2));
|
|
||||||
const phase = clamp(this.getRealPhase(), 0, 99);
|
const phase = clamp(this.getRealPhase(), 0, 99);
|
||||||
const phaseText = phase.toString().padStart(2, '0');
|
this.general.recentWarTime = new Date(baseTurnTime.getTime());
|
||||||
this.general.meta[META_RECENT_WAR] = `${base}${phaseText}`;
|
this.general.meta.recent_war_phase = phase;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override getMaxPhase(): number {
|
public override getMaxPhase(): number {
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
|
||||||
|
import { getNextTurnAt, type TurnSchedule } from '../src/turn/calendar.js';
|
||||||
|
|
||||||
|
describe('turn calendar', () => {
|
||||||
|
it('adds the active turn interval without cutting a general timestamp to a global boundary', () => {
|
||||||
|
const schedule: TurnSchedule = { entries: [{ startMinute: 0, tickMinutes: 10 }] };
|
||||||
|
const current = new Date('2026-07-26T13:38:45.123Z');
|
||||||
|
|
||||||
|
expect(getNextTurnAt(current, schedule).toISOString()).toBe('2026-07-26T13:48:45.123Z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the interval active at the current timestamp when crossing a schedule segment', () => {
|
||||||
|
const schedule: TurnSchedule = {
|
||||||
|
entries: [
|
||||||
|
{ startMinute: 0, tickMinutes: 10 },
|
||||||
|
{ startMinute: 14 * 60, tickMinutes: 5 },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const current = new Date('2026-07-26T13:58:45.000Z');
|
||||||
|
|
||||||
|
expect(getNextTurnAt(current, schedule).toISOString()).toBe('2026-07-26T14:08:45.000Z');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -230,11 +230,13 @@ describe('war triggers', () => {
|
|||||||
const crewType = new WarCrewType(buildUnitSet().crewTypes![0]!);
|
const crewType = new WarCrewType(buildUnitSet().crewTypes![0]!);
|
||||||
const nation = buildNation();
|
const nation = buildNation();
|
||||||
const city = buildCity();
|
const city = buildCity();
|
||||||
|
const attackerGeneral = buildGeneral(80);
|
||||||
|
attackerGeneral.turnTime = new Date('2026-07-26T13:38:45.000Z');
|
||||||
|
|
||||||
const attacker = new WarUnitGeneral(
|
const attacker = new WarUnitGeneral(
|
||||||
rng,
|
rng,
|
||||||
config,
|
config,
|
||||||
buildGeneral(80),
|
attackerGeneral,
|
||||||
city,
|
city,
|
||||||
nation,
|
nation,
|
||||||
true,
|
true,
|
||||||
@@ -255,6 +257,8 @@ describe('war triggers', () => {
|
|||||||
|
|
||||||
attacker.setOppose(defender);
|
attacker.setOppose(defender);
|
||||||
defender.setOppose(attacker);
|
defender.setOppose(attacker);
|
||||||
|
expect(attackerGeneral.recentWarTime?.toISOString()).toBe('2026-07-26T13:38:45.000Z');
|
||||||
|
expect(attackerGeneral.meta.recent_war_phase).toBe(0);
|
||||||
|
|
||||||
attacker.beginPhase();
|
attacker.beginPhase();
|
||||||
|
|
||||||
|
|||||||
@@ -162,8 +162,8 @@ export const projectCoreDatabaseSnapshot = (rows: {
|
|||||||
inheritActiveActionPoints: readNumber(meta, 'inherit_active_action') * 3,
|
inheritActiveActionPoints: readNumber(meta, 'inherit_active_action') * 3,
|
||||||
makeLimit: readNumber(meta, 'makelimit'),
|
makeLimit: readNumber(meta, 'makelimit'),
|
||||||
penalty: asRecord(row.penalty),
|
penalty: asRecord(row.penalty),
|
||||||
killTurn: readNumber(meta, 'killturn'),
|
killTurn: readNumber(row, 'killTurn', readNumber(meta, 'killturn')),
|
||||||
mySet: readNumber(meta, 'myset'),
|
mySet: readNumber(row, 'mySet', readNumber(meta, 'myset')),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const cities = rows.cities.map((row) => {
|
const cities = rows.cities.map((row) => {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ export interface TurnCommandFixtureRequest {
|
|||||||
kind: 'general' | 'nation';
|
kind: 'general' | 'nation';
|
||||||
actorGeneralId: number;
|
actorGeneralId: number;
|
||||||
action: string;
|
action: string;
|
||||||
|
includeLifecycle?: boolean;
|
||||||
args?: unknown;
|
args?: unknown;
|
||||||
coreArgs?: unknown;
|
coreArgs?: unknown;
|
||||||
setup?: {
|
setup?: {
|
||||||
@@ -256,6 +257,7 @@ const buildGeneral = (row: Record<string, unknown>, fallbackTurnTime: Date): Tur
|
|||||||
specage2: readNumber(row, 'specAge2', readNumber(meta, 'specage2')),
|
specage2: readNumber(row, 'specAge2', readNumber(meta, 'specage2')),
|
||||||
makelimit: readNumber(row, 'makeLimit', readNumber(meta, 'makelimit')),
|
makelimit: readNumber(row, 'makeLimit', readNumber(meta, 'makelimit')),
|
||||||
killturn: readNumber(row, 'killTurn', readNumber(meta, 'killturn', 24)),
|
killturn: readNumber(row, 'killTurn', readNumber(meta, 'killturn', 24)),
|
||||||
|
myset: readNumber(row, 'mySet', readNumber(meta, 'myset')),
|
||||||
leadership_exp: readNumber(row, 'leadershipExp', readNumber(meta, 'leadership_exp')),
|
leadership_exp: readNumber(row, 'leadershipExp', readNumber(meta, 'leadership_exp')),
|
||||||
strength_exp: readNumber(row, 'strengthExp', readNumber(meta, 'strength_exp')),
|
strength_exp: readNumber(row, 'strengthExp', readNumber(meta, 'strength_exp')),
|
||||||
intel_exp: readNumber(row, 'intelExp', readNumber(meta, 'intel_exp')),
|
intel_exp: readNumber(row, 'intelExp', readNumber(meta, 'intel_exp')),
|
||||||
@@ -756,7 +758,7 @@ export const runCoreTurnCommandTrace = async (
|
|||||||
nationCooldowns: request.observe?.nationCooldowns ?? [],
|
nationCooldowns: request.observe?.nationCooldowns ?? [],
|
||||||
};
|
};
|
||||||
const reservedTurns = new InMemoryReservedTurnStore(emptyDatabaseClient as never, {
|
const reservedTurns = new InMemoryReservedTurnStore(emptyDatabaseClient as never, {
|
||||||
maxGeneralTurns: 10,
|
maxGeneralTurns: 30,
|
||||||
maxNationTurns: 12,
|
maxNationTurns: 12,
|
||||||
});
|
});
|
||||||
await reservedTurns.loadAll();
|
await reservedTurns.loadAll();
|
||||||
|
|||||||
@@ -25,6 +25,23 @@ const ignoredLifecyclePaths = [
|
|||||||
/^nations\[[^\]]+\]\.meta(?:\.|$)/,
|
/^nations\[[^\]]+\]\.meta(?:\.|$)/,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const comparedLifecycleIgnoredPaths = [
|
||||||
|
/^nationTurns/,
|
||||||
|
/^logs/,
|
||||||
|
/^messages/,
|
||||||
|
/^world\.turnTime$/,
|
||||||
|
/^generalTurns\[[^\]]+\]\.args(?:\.|$)/,
|
||||||
|
/^generals\[[^\]]+\]\.(?:lastTurn|recentWarTime|turnTime)(?:\.|$)/,
|
||||||
|
/^generals\[[^\]]+\]\.meta(?:\.|$)/,
|
||||||
|
/^nations\[[^\]]+\]\.meta(?:\.|$)/,
|
||||||
|
];
|
||||||
|
|
||||||
|
const timestampMillis = (value: unknown): number => {
|
||||||
|
const raw = String(value);
|
||||||
|
const normalized = raw.includes('T') ? raw : `${raw.replace(' ', 'T').replace(/\.(\d{3})\d*$/, '.$1')}Z`;
|
||||||
|
return new Date(normalized).getTime();
|
||||||
|
};
|
||||||
|
|
||||||
const normalizeStoredLogText = (value: unknown): string =>
|
const normalizeStoredLogText = (value: unknown): string =>
|
||||||
String(value)
|
String(value)
|
||||||
.replace(/^(?:<C>●<\/>|<S>◆<\/>|<R>★<\/>)(?:(?:\d+년 )?\d+월:|\d+년:)?/, '')
|
.replace(/^(?:<C>●<\/>|<S>◆<\/>|<R>★<\/>)(?:(?:\d+년 )?\d+월:|\d+년:)?/, '')
|
||||||
@@ -86,10 +103,12 @@ integration('core ↔ legacy command-boundary differential', () => {
|
|||||||
['live sortie emergency capital', 'fixtures/turn-differential/live-sortie-emergency-capital.json'],
|
['live sortie emergency capital', 'fixtures/turn-differential/live-sortie-emergency-capital.json'],
|
||||||
['live sortie conflict arbitration', 'fixtures/turn-differential/live-sortie-conflict-arbitration.json'],
|
['live sortie conflict arbitration', 'fixtures/turn-differential/live-sortie-conflict-arbitration.json'],
|
||||||
['live sortie tied conflict', 'fixtures/turn-differential/live-sortie-conflict-tie.json'],
|
['live sortie tied conflict', 'fixtures/turn-differential/live-sortie-conflict-tie.json'],
|
||||||
|
['live sortie outer lifecycle', 'fixtures/turn-differential/live-sortie-defender.json'],
|
||||||
])(
|
])(
|
||||||
'%s matches command RNG and canonical state delta',
|
'%s matches command RNG and canonical state delta',
|
||||||
async (_label, fixturePath) => {
|
async (label, fixturePath) => {
|
||||||
const request = readFixture(fixturePath);
|
const request = readFixture(fixturePath);
|
||||||
|
request.includeLifecycle = label === 'live sortie outer lifecycle';
|
||||||
if (request.action === 'che_출병') {
|
if (request.action === 'che_출병') {
|
||||||
request.observe!.includeNationHistoryLogs = true;
|
request.observe!.includeNationHistoryLogs = true;
|
||||||
request.observe!.includeGlobalHistoryLogs = true;
|
request.observe!.includeGlobalHistoryLogs = true;
|
||||||
@@ -154,6 +173,43 @@ integration('core ↔ legacy command-boundary differential', () => {
|
|||||||
});
|
});
|
||||||
expect(reference.after.generals.find((general) => general.id === 1)?.cityId).toBe(3);
|
expect(reference.after.generals.find((general) => general.id === 1)?.cityId).toBe(3);
|
||||||
}
|
}
|
||||||
|
if (request.includeLifecycle) {
|
||||||
|
const beforeActor = reference.before.generals.find((general) => general.id === 1);
|
||||||
|
const afterActor = reference.after.generals.find((general) => general.id === 1);
|
||||||
|
const coreBeforeActor = core.before.generals.find((general) => general.id === 1);
|
||||||
|
const coreAfterActor = core.after.generals.find((general) => general.id === 1);
|
||||||
|
expect(
|
||||||
|
reference.before.generalTurns.find((turn) => turn.generalId === 1 && turn.turnIndex === 0)?.action
|
||||||
|
).toBe('che_출병');
|
||||||
|
expect(
|
||||||
|
reference.after.generalTurns.find((turn) => turn.generalId === 1 && turn.turnIndex === 0)?.action
|
||||||
|
).toBe('휴식');
|
||||||
|
expect(timestampMillis(afterActor?.turnTime) - timestampMillis(beforeActor?.turnTime)).toBe(
|
||||||
|
10 * 60_000
|
||||||
|
);
|
||||||
|
expect(timestampMillis(coreAfterActor?.turnTime) - timestampMillis(coreBeforeActor?.turnTime)).toBe(
|
||||||
|
10 * 60_000
|
||||||
|
);
|
||||||
|
expect(timestampMillis(coreAfterActor?.turnTime)).toBe(timestampMillis(afterActor?.turnTime));
|
||||||
|
expect(afterActor?.mySet).toBe(Math.min(9, Number(beforeActor?.mySet) + 3));
|
||||||
|
expect(coreAfterActor?.mySet).toBe(afterActor?.mySet);
|
||||||
|
expect(coreAfterActor?.killTurn).toBe(afterActor?.killTurn);
|
||||||
|
expect(afterActor?.lastTurn).not.toBeNull();
|
||||||
|
expect((coreAfterActor?.lastTurn as Record<string, unknown>)?.command).toBe(
|
||||||
|
(afterActor?.lastTurn as Record<string, unknown>)?.command
|
||||||
|
);
|
||||||
|
for (const generalId of [1, 2]) {
|
||||||
|
const referenceRecentWar = reference.after.generals.find(
|
||||||
|
(general) => general.id === generalId
|
||||||
|
)?.recentWarTime;
|
||||||
|
const coreRecentWar = core.after.generals.find(
|
||||||
|
(general) => general.id === generalId
|
||||||
|
)?.recentWarTime;
|
||||||
|
expect(referenceRecentWar).not.toBeNull();
|
||||||
|
expect(coreRecentWar).not.toBeNull();
|
||||||
|
expect(timestampMillis(coreRecentWar)).toBe(timestampMillis(referenceRecentWar));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
expect(core.execution.outcome).toMatchObject({
|
expect(core.execution.outcome).toMatchObject({
|
||||||
requestedAction: request.action,
|
requestedAction: request.action,
|
||||||
@@ -178,7 +234,9 @@ integration('core ↔ legacy command-boundary differential', () => {
|
|||||||
compareTurnSnapshotDeltas(reference.before, reference.after, core.before, core.after, {
|
compareTurnSnapshotDeltas(reference.before, reference.after, core.before, core.after, {
|
||||||
ignoredPathPatterns:
|
ignoredPathPatterns:
|
||||||
request.action === 'che_출병'
|
request.action === 'che_출병'
|
||||||
? ignoredLifecyclePaths
|
? request.includeLifecycle
|
||||||
|
? comparedLifecycleIgnoredPaths
|
||||||
|
: ignoredLifecyclePaths
|
||||||
: [...ignoredLifecyclePaths, /^generals\[[^\]]+\]\.killTurn(?:\.|$)/],
|
: [...ignoredLifecyclePaths, /^generals\[[^\]]+\]\.killTurn(?:\.|$)/],
|
||||||
})
|
})
|
||||||
).toEqual([]);
|
).toEqual([]);
|
||||||
|
|||||||
Reference in New Issue
Block a user