DiceRoller

DiceRoller

A DiceRoller handles dice rolling functionality, keeps a history of rolls and can output logs etc.

Kind: global class
See: DiceRoll if you don't need to keep a log history of rolls

new DiceRoller([data])

Create a DiceRoller.

The optional data property should be either an array of DiceRoll objects, or an object with a log property that contains the DiceRoll objects.

Throws:

  • TypeError if data is an object, it must have a log[] property
ParamTypeDescription
[data]Object | Array.<DiceRoll>The data to import
[data.log]Array.<DiceRoll>If data is an object, it must contain an array of DiceRolls

diceRoller.log ⇒ Array.<DiceRoll>

The list of roll logs.

Kind: instance property of DiceRoller

diceRoller.output ⇒ string

String representation of the rolls in the log

Kind: instance property of DiceRoller
Example

2d20+1d6: [20,2]+[2] = 24; 1d8: [6] = 6

diceRoller.total ⇒ number

The sum of all the rolls in the log

Kind: instance property of DiceRoller
See: log

diceRoller.clearLog()

Clear the roll history log.

Kind: instance method of DiceRoller
See: log

diceRoller.export([format]) ⇒ string | null

Export the object in the given format. If no format is specified, JSON is returned.

Kind: instance method of DiceRoller
Returns: string | null - The exported data, in the specified format
Throws:

  • TypeError Invalid export format

See: toJSON

ParamTypeDefaultDescription
[format]exportFormatsexportFormats#JSONThe format to export the data as

diceRoller.import(data) ⇒ Array.<DiceRoll>

Add the data to the existing roll log.

data can be an array of DiceRoll objects, an object with a log property that contains DiceRoll objects, or a JSON / base64 encoded representation of either.

Kind: instance method of DiceRoller
Returns: Array.<DiceRoll> - The roll log
Throws:

  • DataFormatError data format invalid
  • RequiredArgumentError data is required
  • TypeError log must be an array

See: log

ParamTypeDescription
datastring | Object | Array.<DiceRoll>The data to import
[data.log]Array.<DiceRoll>If data is an object, it must contain an array of DiceRolls

diceRoller.roll(...notations) ⇒ DiceRoll | Array.<DiceRoll>

Roll the given dice notation(s) and return the corresponding DiceRoll objects.

You can roll a single notation, or multiple at once.

Kind: instance method of DiceRoller
Returns: DiceRoll | Array.<DiceRoll> - If a single notation is passed, a single DiceRoll is returned, otherwise an array of DiceRoll objects is returned
Throws:

  • NotationError notation is invalid
  • RequiredArgumentError notation is required
ParamTypeDescription
...notationsstringThe notations to roll

Example (Single notation)

diceRoller.roll('2d6');

Example (Multiple notations)

roll('2d6', '4d10', 'd8+4d6');

diceRoller.toJSON() ⇒ Object

Return an object for JSON serialising.

This is called automatically when JSON encoding the object.

Kind: instance method of DiceRoller

diceRoller.toString() ⇒ string

Return the String representation of the object.

This is called automatically when casting the object to a string.

Kind: instance method of DiceRoller
See: output

DiceRoller.import(data) ⇒ DiceRoller

Create a new DiceRoller instance with the given data.

data can be an array of DiceRoll objects, an object with a log property that contains the DiceRoll objects, or a JSON / base64 encoded representation of either.

Kind: static method of DiceRoller
Returns: DiceRoller - The new DiceRoller instance
Throws:

  • DataFormatError data format invalid
  • RequiredArgumentError data is required
  • TypeError log must be an array

See: instance method import

ParamTypeDescription
datastring | Object | Array.<DiceRoll>The data to import
[data.log]Array.<DiceRoll>If data is an object, it must contain an array of DiceRolls