fix live sortie turn lifecycle parity

This commit is contained in:
2026-07-26 23:33:26 +00:00
parent fc019a786c
commit 740c4bb1db
8 changed files with 103 additions and 44 deletions
+2
View File
@@ -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 {
+1 -28
View File
@@ -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);
};
+6 -10
View File
@@ -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 {
+24
View File
@@ -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');
});
});
+5 -1
View File
@@ -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();