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:
67
packages/application/src/roll-initiative-use-case.ts
Normal file
67
packages/application/src/roll-initiative-use-case.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import {
|
||||
type CombatantId,
|
||||
type Creature,
|
||||
type CreatureId,
|
||||
calculateInitiative,
|
||||
type DomainError,
|
||||
type DomainEvent,
|
||||
isDomainError,
|
||||
rollInitiative,
|
||||
setInitiative,
|
||||
} from "@initiative/domain";
|
||||
import type { EncounterStore } from "./ports.js";
|
||||
|
||||
export function rollInitiativeUseCase(
|
||||
store: EncounterStore,
|
||||
combatantId: CombatantId,
|
||||
diceRoll: number,
|
||||
getCreature: (id: CreatureId) => Creature | undefined,
|
||||
): 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 } = calculateInitiative({
|
||||
dexScore: creature.abilities.dex,
|
||||
cr: creature.cr,
|
||||
initiativeProficiency: creature.initiativeProficiency,
|
||||
});
|
||||
const value = rollInitiative(diceRoll, modifier);
|
||||
|
||||
if (isDomainError(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const result = setInitiative(encounter, combatantId, value);
|
||||
|
||||
if (isDomainError(result)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
store.save(result.encounter);
|
||||
return result.events;
|
||||
}
|
||||
Reference in New Issue
Block a user