Files
initiative/packages/domain/src/roll-initiative.ts

22 lines
613 B
TypeScript

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