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:
Lukas
2026-03-10 16:29:09 +01:00
parent 5b0bac880d
commit d5f7b6ee36
20 changed files with 926 additions and 27 deletions

View 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;
}