Skip to content

@dice-roller/rpg-dice-roller / Roller

Class: Roller<T> ​

Defined in: src/Roller.ts:40

Rolls notation and keeps a history of the rolls.

For a single roll without a history, use new Roll(notation) instead.

Example ​

ts
const roller = new Roller();
roller.roll("4d6");
roller.roll("2d10+3");
roller.history.total;  // e.g. 27
`${roller}`;           // e.g. "4d6: [3, 5, 2, 2] = 12; 2d10+3: [4, 8]+3 = 15"

Type Parameters ​

T ​

T = string

the output type of the configured presenter

Constructors ​

Constructor ​

new Roller<T>(options?): Roller<T>

Defined in: src/Roller.ts:46

Parameters ​

options? ​

RollerOptions<T> = {}

Returns ​

Roller<T>

Properties ​

history ​

readonly history: RollHistory

Defined in: src/Roller.ts:42

Mutable history of rolls made or imported by this roller.


settings ​

readonly settings: RollSettings<T>

Defined in: src/Roller.ts:44

Shared by every roll of this roller (and their re-rolls).

Accessors ​

dice ​

Get Signature ​

get dice(): DieRegistry

Defined in: src/Roller.ts:58

Returns ​

DieRegistry


generator ​

Get Signature ​

get generator(): NumberGenerator

Defined in: src/Roller.ts:54

Returns ​

NumberGenerator


limits ​

Get Signature ​

get limits(): RollLimits

Defined in: src/Roller.ts:66

Returns ​

RollLimits


modifiers ​

Get Signature ​

get modifiers(): ModifierRegistry

Defined in: src/Roller.ts:62

Returns ​

ModifierRegistry


presenter ​

Get Signature ​

get presenter(): Presenter<T>

Defined in: src/Roller.ts:50

Returns ​

Presenter<T>

Methods ​

export() ​

Call Signature ​

export(format): object

Defined in: src/Roller.ts:92

Exports the history as JSON (default), a JSON-safe object, or UTF-8 base64 JSON. May evaluate uncached min/max totals.

Parameters ​
format ​

"object"

Returns ​
rolls ​

readonly rolls: readonly object[]

total ​

readonly total: EncodedNumber

Can be NaN, e.g. for a history of 1/0 and -1/0.

type ​

readonly type: "history"

version ​

readonly version: 1

Call Signature ​

export(format?): string

Defined in: src/Roller.ts:93

Exports the history as JSON (default), a JSON-safe object, or UTF-8 base64 JSON. May evaluate uncached min/max totals.

Parameters ​
format? ​

"json" | "base64"

Returns ​

string

Call Signature ​

export(format): string | { rolls: readonly object[]; total: EncodedNumber; type: "history"; version: 1; }

Defined in: src/Roller.ts:94

Exports the history as JSON (default), a JSON-safe object, or UTF-8 base64 JSON. May evaluate uncached min/max totals.

Parameters ​
format ​

"object" | "json" | "base64"

Returns ​

string


Type Literal ​

{ rolls: readonly object[]; total: EncodedNumber; type: "history"; version: 1; }

rolls ​

readonly rolls: readonly object[]

total ​

readonly total: EncodedNumber

Can be NaN, e.g. for a history of 1/0 and -1/0.

type ​

readonly type: "history"

version ​

readonly version: 1


import() ​

import(data): Roll<T>[]

Defined in: src/Roller.ts:109

Imports exported rolls (a roll or a whole history, as an object, JSON or base64), and adds them to the history, without rolling.

The data is validated first, so nothing is added if any of it is invalid. Imported rolls share this roller's settings for later re-rolls; re-rolling does not add the new roll to the history.

Parameters ​

data ​

string | EncodedExportData

Returns ​

Roll<T>[]

the imported rolls

Throws ​

the data is invalid


parse() ​

parse(notation): ExpressionNode

Defined in: src/Roller.ts:71

Parses notation into the AST, without rolling.

Parameters ​

notation ​

string

Returns ​

ExpressionNode


present() ​

Call Signature ​

present(): T

Defined in: src/Roller.ts:85

Presents the history with the presenter, or the one from the options (default TextPresenter).

Returns ​

T

Call Signature ​

present<U>(presenter): U

Defined in: src/Roller.ts:86

Presents the history with the presenter, or the one from the options (default TextPresenter).

Type Parameters ​
U ​

U

Parameters ​
presenter ​

Presenter<U>

Returns ​

U


roll() ​

roll(input): Roll<T>

Defined in: src/Roller.ts:76

Rolls notation (or an AST), and adds the roll to the history only if it succeeds.

Parameters ​

input ​

string | ExpressionNode

Returns ​

Roll<T>


toString() ​

toString(): string

Defined in: src/Roller.ts:121

Always the default TextPresenter output of the history.

Returns ​

string

MIT Licensed · Credits