fix live sortie turn lifecycle parity
This commit is contained in:
@@ -100,6 +100,8 @@ export interface General<TriggerState extends GeneralTriggerState = GeneralTrigg
|
||||
itemInventory?: GeneralItemInventory;
|
||||
meta: GeneralMeta;
|
||||
lastTurn?: GeneralLastTurn;
|
||||
turnTime?: Date;
|
||||
recentWarTime?: Date | null;
|
||||
}
|
||||
|
||||
export interface City {
|
||||
|
||||
@@ -13,12 +13,6 @@ const MINUTES_PER_DAY = 24 * 60;
|
||||
|
||||
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 normalized = entries
|
||||
.map((entry) => ({
|
||||
@@ -52,26 +46,5 @@ export const getTickMinutesAt = (date: Date, schedule: TurnSchedule): number =>
|
||||
};
|
||||
|
||||
export const getNextTurnAt = (date: Date, schedule: TurnSchedule): Date => {
|
||||
const entries = normalizeEntries(schedule.entries);
|
||||
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;
|
||||
return new Date(date.getTime() + getTickMinutesAt(date, schedule) * 60_000);
|
||||
};
|
||||
|
||||
@@ -15,7 +15,7 @@ import { getTechAbility, getTechCost } from '@sammo-ts/logic/world/unitSet.js';
|
||||
import type { WarActionPipeline, WarActionContext } from '../actions.js';
|
||||
import type { WarEngineConfig } from '../types.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';
|
||||
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');
|
||||
|
||||
const META_EXP_LEVEL = 'explevel';
|
||||
const META_TURN_TIME = 'turnTime';
|
||||
const META_RECENT_WAR = 'recentWar';
|
||||
const META_DEX_PREFIX = 'dex';
|
||||
const META_RANK_PREFIX = 'rank_';
|
||||
const META_INTEL_EXP = 'intel_exp';
|
||||
@@ -119,18 +117,16 @@ export class WarUnitGeneral<
|
||||
}
|
||||
|
||||
const baseTurnTime = this.isAttacker()
|
||||
? getMetaString(this.general.meta, META_TURN_TIME)
|
||||
? this.general.turnTime
|
||||
: oppose instanceof WarUnitGeneral
|
||||
? getMetaString(oppose.general.meta, META_TURN_TIME)
|
||||
: getMetaString(this.general.meta, META_TURN_TIME);
|
||||
? oppose.general.turnTime
|
||||
: this.general.turnTime;
|
||||
if (!baseTurnTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
const base = baseTurnTime.slice(0, Math.max(0, baseTurnTime.length - 2));
|
||||
const phase = clamp(this.getRealPhase(), 0, 99);
|
||||
const phaseText = phase.toString().padStart(2, '0');
|
||||
this.general.meta[META_RECENT_WAR] = `${base}${phaseText}`;
|
||||
this.general.recentWarTime = new Date(baseTurnTime.getTime());
|
||||
this.general.meta.recent_war_phase = phase;
|
||||
}
|
||||
|
||||
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 nation = buildNation();
|
||||
const city = buildCity();
|
||||
const attackerGeneral = buildGeneral(80);
|
||||
attackerGeneral.turnTime = new Date('2026-07-26T13:38:45.000Z');
|
||||
|
||||
const attacker = new WarUnitGeneral(
|
||||
rng,
|
||||
config,
|
||||
buildGeneral(80),
|
||||
attackerGeneral,
|
||||
city,
|
||||
nation,
|
||||
true,
|
||||
@@ -255,6 +257,8 @@ describe('war triggers', () => {
|
||||
|
||||
attacker.setOppose(defender);
|
||||
defender.setOppose(attacker);
|
||||
expect(attackerGeneral.recentWarTime?.toISOString()).toBe('2026-07-26T13:38:45.000Z');
|
||||
expect(attackerGeneral.meta.recent_war_phase).toBe(0);
|
||||
|
||||
attacker.beginPhase();
|
||||
|
||||
|
||||
@@ -162,8 +162,8 @@ export const projectCoreDatabaseSnapshot = (rows: {
|
||||
inheritActiveActionPoints: readNumber(meta, 'inherit_active_action') * 3,
|
||||
makeLimit: readNumber(meta, 'makelimit'),
|
||||
penalty: asRecord(row.penalty),
|
||||
killTurn: readNumber(meta, 'killturn'),
|
||||
mySet: readNumber(meta, 'myset'),
|
||||
killTurn: readNumber(row, 'killTurn', readNumber(meta, 'killturn')),
|
||||
mySet: readNumber(row, 'mySet', readNumber(meta, 'myset')),
|
||||
};
|
||||
});
|
||||
const cities = rows.cities.map((row) => {
|
||||
|
||||
@@ -40,6 +40,7 @@ export interface TurnCommandFixtureRequest {
|
||||
kind: 'general' | 'nation';
|
||||
actorGeneralId: number;
|
||||
action: string;
|
||||
includeLifecycle?: boolean;
|
||||
args?: unknown;
|
||||
coreArgs?: unknown;
|
||||
setup?: {
|
||||
@@ -256,6 +257,7 @@ const buildGeneral = (row: Record<string, unknown>, fallbackTurnTime: Date): Tur
|
||||
specage2: readNumber(row, 'specAge2', readNumber(meta, 'specage2')),
|
||||
makelimit: readNumber(row, 'makeLimit', readNumber(meta, 'makelimit')),
|
||||
killturn: readNumber(row, 'killTurn', readNumber(meta, 'killturn', 24)),
|
||||
myset: readNumber(row, 'mySet', readNumber(meta, 'myset')),
|
||||
leadership_exp: readNumber(row, 'leadershipExp', readNumber(meta, 'leadership_exp')),
|
||||
strength_exp: readNumber(row, 'strengthExp', readNumber(meta, 'strength_exp')),
|
||||
intel_exp: readNumber(row, 'intelExp', readNumber(meta, 'intel_exp')),
|
||||
@@ -756,7 +758,7 @@ export const runCoreTurnCommandTrace = async (
|
||||
nationCooldowns: request.observe?.nationCooldowns ?? [],
|
||||
};
|
||||
const reservedTurns = new InMemoryReservedTurnStore(emptyDatabaseClient as never, {
|
||||
maxGeneralTurns: 10,
|
||||
maxGeneralTurns: 30,
|
||||
maxNationTurns: 12,
|
||||
});
|
||||
await reservedTurns.loadAll();
|
||||
|
||||
@@ -25,6 +25,23 @@ const ignoredLifecyclePaths = [
|
||||
/^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 =>
|
||||
String(value)
|
||||
.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 conflict arbitration', 'fixtures/turn-differential/live-sortie-conflict-arbitration.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',
|
||||
async (_label, fixturePath) => {
|
||||
async (label, fixturePath) => {
|
||||
const request = readFixture(fixturePath);
|
||||
request.includeLifecycle = label === 'live sortie outer lifecycle';
|
||||
if (request.action === 'che_출병') {
|
||||
request.observe!.includeNationHistoryLogs = 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);
|
||||
}
|
||||
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({
|
||||
requestedAction: request.action,
|
||||
@@ -178,7 +234,9 @@ integration('core ↔ legacy command-boundary differential', () => {
|
||||
compareTurnSnapshotDeltas(reference.before, reference.after, core.before, core.after, {
|
||||
ignoredPathPatterns:
|
||||
request.action === 'che_출병'
|
||||
? ignoredLifecyclePaths
|
||||
? request.includeLifecycle
|
||||
? comparedLifecycleIgnoredPaths
|
||||
: ignoredLifecyclePaths
|
||||
: [...ignoredLifecyclePaths, /^generals\[[^\]]+\]\.killTurn(?:\.|$)/],
|
||||
})
|
||||
).toEqual([]);
|
||||
|
||||
Reference in New Issue
Block a user