Files
initiative/packages/application/src/roll-initiative-use-case.ts
Lukas e62c49434c
All checks were successful
CI / check (push) Successful in 2m21s
CI / build-image (push) Successful in 24s
Add Pathfinder 2e game system mode
Implements PF2e as an alternative game system alongside D&D 5e/5.5e.
Settings modal "Game System" selector switches conditions, bestiary,
stat block layout, and initiative calculation between systems.

- Valued conditions with increment/decrement UX (Clumsy 2, Frightened 3)
- 2,502 PF2e creatures from bundled search index (77 sources)
- PF2e stat block: level, traits, Perception, Fort/Ref/Will, ability mods
- Perception-based initiative rolling
- System-scoped source cache (D&D and PF2e sources don't collide)
- Backwards-compatible condition rehydration (ConditionId[] → ConditionEntry[])
- Difficulty indicator hidden in PF2e mode (excluded from MVP)

Closes dostulata/initiative#19

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 01:26:22 +02:00

71 lines
1.7 KiB
TypeScript

import {
type AnyCreature,
type CombatantId,
type CreatureId,
type DomainError,
type DomainEvent,
isDomainError,
type RollMode,
rollInitiative,
selectRoll,
setInitiative,
} from "@initiative/domain";
import { creatureInitiativeModifier } from "./creature-initiative-modifier.js";
import type { EncounterStore } from "./ports.js";
export function rollInitiativeUseCase(
store: EncounterStore,
combatantId: CombatantId,
diceRolls: readonly [number, ...number[]],
getCreature: (id: CreatureId) => AnyCreature | undefined,
mode: RollMode = "normal",
): DomainEvent[] | DomainError {
const encounter = store.get();
const combatant = encounter.combatants.find((c) => c.id === combatantId);
if (!combatant) {
return {
kind: "domain-error",
code: "combatant-not-found",
message: `No combatant found with ID "${combatantId}"`,
};
}
if (!combatant.creatureId) {
return {
kind: "domain-error",
code: "no-creature-link",
message: `Combatant "${combatant.name}" has no linked creature`,
};
}
const creature = getCreature(combatant.creatureId);
if (!creature) {
return {
kind: "domain-error",
code: "creature-not-found",
message: `Creature not found for ID "${combatant.creatureId}"`,
};
}
const modifier = creatureInitiativeModifier(creature);
const effectiveRoll =
mode === "normal"
? diceRolls[0]
: selectRoll(diceRolls[0], diceRolls[1] ?? diceRolls[0], mode);
const value = rollInitiative(effectiveRoll, modifier);
if (isDomainError(value)) {
return value;
}
const result = setInitiative(encounter, combatantId, value);
if (isDomainError(result)) {
return result;
}
store.save(result.encounter);
return result.events;
}