roll
Roll is a node.js package for rolling dice and adding modifiers (such as "2d6+1").


How To Use (From Shell)
Installation (via npm)
$ npm install -g roll
Usage
$ roll 2d6+3
9
$ roll d20
15
$ roll d%
99
How To Use (As Library)
Installation (via npm)
$ npm install roll
Usage
Get an instance of the library:
var Roll = require('roll'),
roll = new Roll();
Rolling a single die:
var oneDie = roll.roll('d6');
console.log(oneDie.result);
Rolling multiple dice:
var twoTwenties = roll.roll('2d20');
console.log(twoTwenties.result);
Rolling multiple sets of dice:
var bunchOfDice = roll.roll('2d20+1d12');
console.log(bunchOfDice.result);
Rolling a percentage:
var chance = roll.roll('d%');
console.log(chance.result);
Simple calculation (+, -, *, /):
var attack = roll.roll('2d6+2');
console.log(attack.result);
Seeing what was rolled, rather than the sum:
var yahtzee = roll.roll('5d6');
console.log(yahtzee.rolled);
var blessedSneaker = roll.roll('2d20b1+1d4+5');
console.log(blessedSneaker.rolled);
Getting the highest two dice of the set:
var pickBestTwo = roll.roll('6d20b2');
console.log(pickBestTwo.calculations[1]);
Processing rolls without parsing a string:
var attack = roll.roll({
quantity: 2,
sides: 6,
transformations: [
'sum',
['add', 2]
]
});
console.log(attack.result);
Using custom transformations:
var dropOnes = function(results){
return results.filter(function (result) {
return result !== 1;
});
};
var noOnes = roll.roll({
quantity: 5,
sides: 4,
transformations: [
dropOnes,
'sum'
]
});
Using a custom seed:
var srand = require('srand');
srand.seed(1000);
roll = new Roll(function () {
return srand.random();
});
console.log(roll.roll('2d6+5').result);
Validating user input:
var userInput = 'this isn\'t a valid roll',
valid = roll.validate(userInput);
if (!valid) {
console.error('"%s" is not a valid input string for node-roll!', userInput);
}
Credits
Inspired by Phillip Newton's Games::Dice.
License
MIT License
Author
Troy Goode (troygoode@gmail.com)