UNPKG

@randsum/roller

Version:

A flexible, type-safe dice roller for tabletop RPGs, game development, and probability simulations

24 lines (20 loc) 799 B
import type { RollArgument, RollerRollResult } from '../types' import { argToParameter } from './argToParameter' import { generateRollRecord } from './generateRollRecord' export function roll<T = string>(...args: RollArgument<T>[]): RollerRollResult<T> { const parameters = args.flatMap((arg, index) => argToParameter(arg, index + 1)) const rolls = parameters.map(parameter => generateRollRecord(parameter)) const total = rolls.reduce((acc, cur) => { const factor = cur.parameters.arithmetic === 'subtract' ? -1 : 1 return acc + cur.total * factor }, 0) const isCustom = rolls.every(roll => roll.customResults) const result = rolls.flatMap(roll => isCustom ? (roll.customResults ?? []) : (roll.rolls.map(String) as T) ) return { rolls, result, total } }