@randsum/dice
Version:
A flexible, type-safe dice roller for tabletop RPGs, game development, and probability simulations
125 lines (124 loc) • 5.36 kB
text/typescript
import { CustomRollOptions, ModifierOptions, NumericRollOptions } from "@randsum/core";
import { CustomDiceNotation, NumericDiceNotation } from "@randsum/notation";
import { ComparisonOptions, CustomRollOptions as CustomRollOptions2, DropOptions, ModifierOptions as ModifierOptions2, NumericRollBonus, NumericRollOptions as NumericRollOptions2, ReplaceOptions, RerollOptions, RollOptions, UniqueOptions } from "@randsum/core";
import { CustomDiceNotation as CustomDiceNotation2, DiceNotation, NumericDiceNotation as NumericDiceNotation2 } from "@randsum/notation";
interface NumericDie2 {
readonly type: "numeric";
readonly sides: number;
readonly faces: number[];
readonly isCustom: false;
roll(quantity?: number): number;
rollSpread(quantity?: number): number[];
rollModified(quantity: number, modifiers?: ModifierOptions): NumericRollResult;
toOptions: NumericRollOptions;
}
interface CustomDie2 {
readonly type: "custom";
readonly sides: number;
readonly faces: string[];
readonly isCustom: true;
roll(quantity?: number): string;
rollSpread(quantity?: number): string[];
rollModified(quantity: number, modifiers?: ModifierOptions): CustomRollResult;
toOptions: CustomRollOptions;
}
type BaseD = NumericDie2 | CustomDie2;
type NumericRollArgument = NumericDie2 | NumericRollOptions | NumericDiceNotation | number | `${number}`;
type CustomRollArgument = CustomDie2 | CustomRollOptions | CustomDiceNotation | string[];
type RollArgument = NumericRollArgument | CustomRollArgument;
interface BaseRollParams<A extends RollArgument = RollArgument> {
description: string[];
argument: A;
}
interface NumericRollParams extends BaseRollParams<NumericRollArgument> {
options: NumericRollOptions;
die: NumericDie2;
notation: NumericDiceNotation;
}
interface CustomRollParams extends BaseRollParams<CustomRollArgument> {
options: CustomRollOptions;
die: CustomDie2;
notation: CustomDiceNotation;
}
type RollParams = NumericRollParams | CustomRollParams;
interface BaseSingleRollResult<P extends RollParams = RollParams> {
parameters: P;
rawResult: number | string;
type: "numeric" | "custom";
rawRolls: number[] | string[];
modifiedRolls: {
rolls: string[] | number[]
total: string | number
};
total: string | number;
}
interface SingleNumericRollResult extends BaseSingleRollResult<NumericRollParams> {
type: "numeric";
rawResult: number;
rawRolls: number[];
modifiedRolls: {
rolls: number[]
total: number
};
total: number;
}
interface SingleCustomRollResult extends BaseSingleRollResult<CustomRollParams> {
type: "custom";
rawResult: string;
rawRolls: string[];
modifiedRolls: {
rolls: string[]
total: string
};
total: string;
}
interface BaseRollResult {
rolls: (SingleNumericRollResult | SingleCustomRollResult)[];
rawResults: (string | number)[];
total: string | number;
type: "numeric" | "custom" | "mixed";
}
interface NumericRollResult extends BaseRollResult {
type: "numeric";
rolls: SingleNumericRollResult[];
rawResults: number[];
total: number;
}
interface CustomRollResult extends BaseRollResult {
type: "custom";
rolls: SingleCustomRollResult[];
rawResults: string[];
total: string;
}
interface MixedRollResult extends BaseRollResult {
type: "mixed";
rolls: (SingleNumericRollResult | SingleCustomRollResult)[];
rawResults: (string | number)[];
total: string;
}
type SingleRollResult = SingleNumericRollResult | SingleCustomRollResult;
type RollResult = NumericRollResult | CustomRollResult | MixedRollResult;
declare function D(sides: number): NumericDie2;
declare function D(faces: string[]): CustomDie2;
declare const D4: NumericDie2;
declare const D6: NumericDie2;
declare const D8: NumericDie2;
declare const D10: NumericDie2;
declare const D12: NumericDie2;
declare const D20: NumericDie2;
declare const D100: NumericDie2;
declare const coin: CustomDie2;
declare const fudgeDice: CustomDie2;
declare const alphaNumDie: CustomDie2;
declare function roll(...args: NumericRollArgument[]): NumericRollResult;
declare function roll(...args: CustomRollArgument[]): CustomRollResult;
declare function roll(...args: (NumericRollArgument | CustomRollArgument)[]): RollResult;
declare function isD(arg: unknown): arg is BaseD;
declare function isNumericResult(result: RollResult): result is NumericRollResult;
declare function isCustomResult(result: RollResult): result is CustomRollResult;
declare function isMixedResult(result: RollResult): result is MixedRollResult;
export * from "@randsum/core";
export * from "@randsum/notation";
export * from "@randsum/core";
export * from "@randsum/notation";
export { roll, isNumericResult, isMixedResult, isD, isCustomResult, fudgeDice, coin, alphaNumDie, UniqueOptions, SingleRollResult, SingleNumericRollResult, SingleCustomRollResult, RollResult, RollParams, RollOptions, RollArgument, RerollOptions, ReplaceOptions, NumericRollResult, NumericRollParams, NumericRollOptions2 as NumericRollOptions, NumericRollBonus, NumericRollArgument, NumericDie2 as NumericDie, NumericDiceNotation2 as NumericDiceNotation, ModifierOptions2 as ModifierOptions, MixedRollResult, DropOptions, DiceNotation, D8, D6, D4, D20, D12, D100, D10, D, CustomRollResult, CustomRollParams, CustomRollOptions2 as CustomRollOptions, CustomRollArgument, CustomDie2 as CustomDie, CustomDiceNotation2 as CustomDiceNotation, ComparisonOptions, BaseSingleRollResult, BaseD };