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:
@@ -6,6 +6,8 @@ export { editCombatantUseCase } from "./edit-combatant-use-case.js";
|
||||
export type { EncounterStore } from "./ports.js";
|
||||
export { removeCombatantUseCase } from "./remove-combatant-use-case.js";
|
||||
export { retreatTurnUseCase } from "./retreat-turn-use-case.js";
|
||||
export { rollAllInitiativeUseCase } from "./roll-all-initiative-use-case.js";
|
||||
export { rollInitiativeUseCase } from "./roll-initiative-use-case.js";
|
||||
export { setAcUseCase } from "./set-ac-use-case.js";
|
||||
export { setHpUseCase } from "./set-hp-use-case.js";
|
||||
export { setInitiativeUseCase } from "./set-initiative-use-case.js";
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
70
packages/domain/src/__tests__/roll-initiative.test.ts
Normal file
70
packages/domain/src/__tests__/roll-initiative.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { rollInitiative } from "../roll-initiative.js";
|
||||
import { isDomainError } from "../types.js";
|
||||
|
||||
describe("rollInitiative", () => {
|
||||
describe("valid rolls", () => {
|
||||
it("normal roll: 15 + modifier 7 = 22", () => {
|
||||
expect(rollInitiative(15, 7)).toBe(22);
|
||||
});
|
||||
|
||||
it("boundary: roll 1 + modifier 0 = 1", () => {
|
||||
expect(rollInitiative(1, 0)).toBe(1);
|
||||
});
|
||||
|
||||
it("boundary: roll 20 + modifier 0 = 20", () => {
|
||||
expect(rollInitiative(20, 0)).toBe(20);
|
||||
});
|
||||
|
||||
it("negative modifier: roll 1 + (−3) = −2", () => {
|
||||
expect(rollInitiative(1, -3)).toBe(-2);
|
||||
});
|
||||
|
||||
it("zero modifier: roll 10 + 0 = 10", () => {
|
||||
expect(rollInitiative(10, 0)).toBe(10);
|
||||
});
|
||||
|
||||
it("large positive modifier: roll 20 + 12 = 32", () => {
|
||||
expect(rollInitiative(20, 12)).toBe(32);
|
||||
});
|
||||
});
|
||||
|
||||
describe("invalid dice rolls", () => {
|
||||
it("rejects 0", () => {
|
||||
const result = rollInitiative(0, 5);
|
||||
expect(isDomainError(result)).toBe(true);
|
||||
if (isDomainError(result)) {
|
||||
expect(result.code).toBe("invalid-dice-roll");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects 21", () => {
|
||||
const result = rollInitiative(21, 5);
|
||||
expect(isDomainError(result)).toBe(true);
|
||||
if (isDomainError(result)) {
|
||||
expect(result.code).toBe("invalid-dice-roll");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects non-integer (3.5)", () => {
|
||||
const result = rollInitiative(3.5, 0);
|
||||
expect(isDomainError(result)).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects negative dice roll", () => {
|
||||
const result = rollInitiative(-1, 0);
|
||||
expect(isDomainError(result)).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects NaN", () => {
|
||||
const result = rollInitiative(Number.NaN, 0);
|
||||
expect(isDomainError(result)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("determinism", () => {
|
||||
it("same input produces same output", () => {
|
||||
expect(rollInitiative(10, 5)).toBe(rollInitiative(10, 5));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -57,6 +57,7 @@ export {
|
||||
removeCombatant,
|
||||
} from "./remove-combatant.js";
|
||||
export { retreatTurn } from "./retreat-turn.js";
|
||||
export { rollInitiative } from "./roll-initiative.js";
|
||||
export { type SetAcSuccess, setAc } from "./set-ac.js";
|
||||
export { type SetHpSuccess, setHp } from "./set-hp.js";
|
||||
export {
|
||||
|
||||
21
packages/domain/src/roll-initiative.ts
Normal file
21
packages/domain/src/roll-initiative.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { DomainError } from "./types.js";
|
||||
|
||||
/**
|
||||
* Pure function that computes initiative from a resolved dice roll and modifier.
|
||||
* The dice roll must be an integer in [1, 20].
|
||||
* Returns the sum (diceRoll + modifier) or a DomainError for invalid inputs.
|
||||
*/
|
||||
export function rollInitiative(
|
||||
diceRoll: number,
|
||||
modifier: number,
|
||||
): number | DomainError {
|
||||
if (!Number.isInteger(diceRoll) || diceRoll < 1 || diceRoll > 20) {
|
||||
return {
|
||||
kind: "domain-error",
|
||||
code: "invalid-dice-roll",
|
||||
message: `Dice roll must be an integer between 1 and 20, got ${diceRoll}`,
|
||||
};
|
||||
}
|
||||
|
||||
return diceRoll + modifier;
|
||||
}
|
||||
Reference in New Issue
Block a user