import type { DomainEvent } from "./events.js"; import { sortByInitiative } from "./initiative-sort.js"; import { type CombatantId, type DomainError, type Encounter, findCombatant, isDomainError, } from "./types.js"; export interface SetInitiativeSuccess { readonly encounter: Encounter; readonly events: DomainEvent[]; } /** * Pure function that sets, changes, or clears a combatant's initiative value. * * After updating the value, combatants are stable-sorted: * 1. Combatants with initiative — descending by value * 2. Combatants without initiative — preserve relative order * * The active combatant's turn is preserved through the reorder * by tracking identity (CombatantId) rather than position. * * roundNumber is never changed. */ export function setInitiative( encounter: Encounter, combatantId: CombatantId, value: number | undefined, ): SetInitiativeSuccess | DomainError { const found = findCombatant(encounter, combatantId); if (isDomainError(found)) return found; if (value !== undefined && !Number.isInteger(value)) { return { kind: "domain-error", code: "invalid-initiative", message: `Initiative must be an integer, got ${value}`, }; } const previousValue = found.combatant.initiative; // Create new combatants array with updated initiative const updated = encounter.combatants.map((c) => c.id === combatantId ? { ...c, initiative: value } : c, ); // Record active combatant's id before reorder const activeCombatantId = encounter.combatants.length > 0 ? encounter.combatants[encounter.activeIndex].id : combatantId; const { sorted, activeIndex: newActiveIndex } = sortByInitiative( updated, activeCombatantId, ); return { encounter: { combatants: sorted, activeIndex: newActiveIndex, roundNumber: encounter.roundNumber, }, events: [ { type: "InitiativeSet", combatantId, previousValue, newValue: value, }, ], }; }