Right-click or long-press the d20 button (per-combatant or Roll All) to open a context menu with Advantage and Disadvantage options. Normal left-click behavior is unchanged. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
import {
|
|
type Creature,
|
|
type CreatureId,
|
|
calculateInitiative,
|
|
type DomainError,
|
|
type DomainEvent,
|
|
isDomainError,
|
|
type RollMode,
|
|
rollInitiative,
|
|
selectRoll,
|
|
setInitiative,
|
|
} from "@initiative/domain";
|
|
import type { EncounterStore } from "./ports.js";
|
|
|
|
export interface RollAllResult {
|
|
events: DomainEvent[];
|
|
skippedNoSource: number;
|
|
}
|
|
|
|
export function rollAllInitiativeUseCase(
|
|
store: EncounterStore,
|
|
rollDice: () => number,
|
|
getCreature: (id: CreatureId) => Creature | undefined,
|
|
mode: RollMode = "normal",
|
|
): RollAllResult | DomainError {
|
|
let encounter = store.get();
|
|
const allEvents: DomainEvent[] = [];
|
|
let skippedNoSource = 0;
|
|
|
|
for (const combatant of encounter.combatants) {
|
|
if (!combatant.creatureId) continue;
|
|
if (combatant.initiative !== undefined) continue;
|
|
|
|
const creature = getCreature(combatant.creatureId);
|
|
if (!creature) {
|
|
skippedNoSource++;
|
|
continue;
|
|
}
|
|
|
|
const { modifier } = calculateInitiative({
|
|
dexScore: creature.abilities.dex,
|
|
cr: creature.cr,
|
|
initiativeProficiency: creature.initiativeProficiency,
|
|
});
|
|
const roll1 = rollDice();
|
|
const effectiveRoll =
|
|
mode === "normal" ? roll1 : selectRoll(roll1, rollDice(), mode);
|
|
const value = rollInitiative(effectiveRoll, modifier);
|
|
|
|
if (isDomainError(value)) {
|
|
return value;
|
|
}
|
|
|
|
const result = setInitiative(encounter, combatant.id, value);
|
|
|
|
if (isDomainError(result)) {
|
|
return result;
|
|
}
|
|
|
|
encounter = result.encounter;
|
|
allEvents.push(...result.events);
|
|
}
|
|
|
|
store.save(encounter);
|
|
return { events: allEvents, skippedNoSource };
|
|
}
|