Implement the 026-roll-initiative feature that adds d20 roll buttons for bestiary combatants' initiative using a click-to-edit pattern (d20 icon when empty, plain text when set), plus a Roll All button in the top bar that batch-rolls for all unrolled bestiary combatants, with randomness confined to the adapter layer and the domain receiving pre-resolved dice values
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
51
packages/application/src/roll-all-initiative-use-case.ts
Normal file
51
packages/application/src/roll-all-initiative-use-case.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import {
|
||||
type Creature,
|
||||
type CreatureId,
|
||||
calculateInitiative,
|
||||
type DomainError,
|
||||
type DomainEvent,
|
||||
isDomainError,
|
||||
rollInitiative,
|
||||
setInitiative,
|
||||
} from "@initiative/domain";
|
||||
import type { EncounterStore } from "./ports.js";
|
||||
|
||||
export function rollAllInitiativeUseCase(
|
||||
store: EncounterStore,
|
||||
rollDice: () => number,
|
||||
getCreature: (id: CreatureId) => Creature | undefined,
|
||||
): DomainEvent[] | DomainError {
|
||||
let encounter = store.get();
|
||||
const allEvents: DomainEvent[] = [];
|
||||
|
||||
for (const combatant of encounter.combatants) {
|
||||
if (!combatant.creatureId) continue;
|
||||
if (combatant.initiative !== undefined) continue;
|
||||
|
||||
const creature = getCreature(combatant.creatureId);
|
||||
if (!creature) continue;
|
||||
|
||||
const { modifier } = calculateInitiative({
|
||||
dexScore: creature.abilities.dex,
|
||||
cr: creature.cr,
|
||||
initiativeProficiency: creature.initiativeProficiency,
|
||||
});
|
||||
const value = rollInitiative(rollDice(), 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 allEvents;
|
||||
}
|
||||
Reference in New Issue
Block a user