addCombatant now accepts an optional init parameter for pre-filled stats (HP, AC, initiative, creatureId, color, icon, playerCharacterId), making combatant creation a single atomic operation with domain validation. This eliminates the multi-step store.save() bypass in addFromBestiary and addFromPlayerCharacter, and removes the CombatantOpts/applyCombatantOpts helpers. Also extracts shared initiative sort logic into initiative-sort.ts used by both addCombatant and setInitiative. Closes #15 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
27 lines
545 B
TypeScript
27 lines
545 B
TypeScript
import {
|
|
addCombatant,
|
|
type CombatantId,
|
|
type CombatantInit,
|
|
type DomainError,
|
|
type DomainEvent,
|
|
isDomainError,
|
|
} from "@initiative/domain";
|
|
import type { EncounterStore } from "./ports.js";
|
|
|
|
export function addCombatantUseCase(
|
|
store: EncounterStore,
|
|
id: CombatantId,
|
|
name: string,
|
|
init?: CombatantInit,
|
|
): DomainEvent[] | DomainError {
|
|
const encounter = store.get();
|
|
const result = addCombatant(encounter, id, name, init);
|
|
|
|
if (isDomainError(result)) {
|
|
return result;
|
|
}
|
|
|
|
store.save(result.encounter);
|
|
return result.events;
|
|
}
|