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
- DiceRoller
- new DiceRoller([data])
- instance
- .log ⇒
Array.<DiceRoll>
- .output ⇒
string
- .total ⇒
number
- .clearLog()
- .export([format]) ⇒
string
|null
- .import(data) ⇒
Array.<DiceRoll>
- .roll(...notations) ⇒
DiceRoll
|Array.<DiceRoll>
- .toJSON() ⇒
Object
- .toString() ⇒
string
- .log ⇒
- static
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 alog[]
property
Param | Type | Description |
---|---|---|
[data] | Object | Array.<DiceRoll> | The data to import |
[data.log] | Array.<DiceRoll> | If data is an object, it must contain an array of DiceRoll s |
Array.<DiceRoll>
diceRoller.log ⇒ The list of roll logs.
Kind: instance property of DiceRoller
string
diceRoller.output ⇒ String representation of the rolls in the log
Kind: instance property of DiceRoller
Example
2d20+1d6: [20,2]+[2] = 24; 1d8: [6] = 6
number
diceRoller.total ⇒ 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
string
| null
diceRoller.export([format]) ⇒ 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
Param | Type | Default | Description |
---|---|---|---|
[format] | exportFormats | exportFormats#JSON | The format to export the data as |
Array.<DiceRoll>
diceRoller.import(data) ⇒ 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 invalidRequiredArgumentError
data is requiredTypeError
log must be an array
See: log
Param | Type | Description |
---|---|---|
data | string | Object | Array.<DiceRoll> | The data to import |
[data.log] | Array.<DiceRoll> | If data is an object, it must contain an array of DiceRoll s |
DiceRoll
| Array.<DiceRoll>
diceRoller.roll(...notations) ⇒ 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 invalidRequiredArgumentError
notation is required
Param | Type | Description |
---|---|---|
...notations | string | The notations to roll |
Example (Single notation)
diceRoller.roll('2d6');
Example (Multiple notations)
roll('2d6', '4d10', 'd8+4d6');
Object
diceRoller.toJSON() ⇒ Return an object for JSON serialising.
This is called automatically when JSON encoding the object.
Kind: instance method of DiceRoller
string
diceRoller.toString() ⇒ 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
DiceRoller.import(data) ⇒ 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 invalidRequiredArgumentError
data is requiredTypeError
log must be an array
See: instance method import
Param | Type | Description |
---|---|---|
data | string | Object | Array.<DiceRoll> | The data to import |
[data.log] | Array.<DiceRoll> | If data is an object, it must contain an array of DiceRoll s |