UNPKG

@randsum/daggerheart

Version:

A flexible, type-safe dice roller for building Daggerheart-compatible applications

82 lines (81 loc) 2.18 kB
import { roll } from '@randsum/dice'; function rollArg({ modifier = 0, amplifyHope = false, amplifyFear = false }) { if (amplifyHope && amplifyFear) { return [{ quantity: 2, sides: 20, modifiers: { plus: modifier } }]; } if (amplifyHope) { return [{ quantity: 1, sides: 20, modifiers: { plus: modifier } }, { quantity: 1, sides: 12, modifiers: { plus: modifier } }]; } if (amplifyFear) { return [{ quantity: 1, sides: 12, modifiers: { plus: modifier } }, { quantity: 1, sides: 12, modifiers: { plus: modifier } }]; } return [{ quantity: 2, sides: 12, modifiers: { plus: modifier } }]; } export function rollDH({ modifier = 0, rollingWith, amplifyHope = false, amplifyFear = false }) { const { result: [hope, fear], total } = roll(...rollArg({ modifier, amplifyHope, amplifyFear })); if (hope === undefined || fear === undefined) { throw new Error('Failed to roll hope and fear'); } const [totalWithAdvantage, advantage] = calculateTotal(total, rollingWith); return { type: calculateType(hope, fear), total: totalWithAdvantage, rolls: { hope, advantage, fear, modifier } }; } function calculateType(hope, fear) { if (hope === fear) { return 'critical hope'; } if (hope > fear) { return 'hope'; } return 'fear'; } function calculateTotal(total, rollingWith) { if (rollingWith) { const advantage = advantageDie(); if (rollingWith === 'Advantage') { return [total + advantage, advantage]; } return [total - advantage, -advantage]; } return [total, undefined]; } function advantageDie() { return roll({ quantity: 1, sides: 6 }).total; }