Roll is a node.js package for rolling dice and adding modifiers (such as "2d6+1").
Installation (via npm)
$ npm install -g roll$ roll 2d6+3
9
$ roll d20
15
$ roll d%
99
$ roll -d 2d20
Dice: 13, 8
Total: 21
$ roll --detail 5d%
Dice: 45, 86, 86, 4, 72
Total: 293Installation (via npm)
$ npm install rollGet an instance of the library:
varRoll=require('roll'),roll=newRoll();Rolling a single die:
varoneDie=roll.roll('d6');console.log(oneDie.result);//random number between 1 and 6 (inclusive)Rolling multiple dice:
vartwoTwenties=roll.roll('2d20');console.log(twoTwenties.result);//random number between 2 and 40 (inclusive)Rolling multiple sets of dice:
varbunchOfDice=roll.roll('2d20+1d12');console.log(bunchOfDice.result);//random number between 3 and 52 (inclusive)Rolling a percentage:
varchance=roll.roll('d%');//same as '1d100', 'd100', or '1d%'console.log(chance.result);//random number between 1 and 100 (inclusive)Simple calculation (+, -, *, /):
varattack=roll.roll('2d6+2');console.log(attack.result);//random number between 3 and 8 (inclusive)Seeing what was rolled, rather than the sum:
varyahtzee=roll.roll('5d6');console.log(yahtzee.rolled);//yahtzee.rolled will return something like [5, 2, 4, 6, 1] rather than the sumvarblessedSneaker=roll.roll('2d20b1+1d4+5');console.log(blessedSneaker.rolled);// blessedSneaker.rolled will return an array containing an array for each component that is a roll of the dice, in the order in which they occurred, e.g. [[19,3],[1]]Getting the highest two dice of the set:
varpickBestTwo=roll.roll('6d20b2');//roll 6 dice and give me the 2 highestconsole.log(pickBestTwo.calculations[1]);//pickBestTwo.calculations[0] is the same as .result, .calculations[1] is prior to the sum operationProcessing rolls without parsing a string:
varattack=roll.roll({quantity: 2,sides: 6,transformations: [//can list n-number of pipeline operations to perform on the result'sum',//take the array of rolled dice and sum them together['add',2]//add 2 to the sum]});console.log(attack.result);//random number between 3 and 8 (inclusive)Using custom transformations:
vardropOnes=function(results){returnresults.filter(function(result){returnresult!==1;});};varnoOnes=roll.roll({quantity: 5,sides: 4,transformations: [dropOnes,// remove any 1s because we have teh lucky bootz'sum']});Using a custom seed:
varsrand=require('srand');//https://github.com/isaacs/node-srand (npm install srand)srand.seed(1000);roll=newRoll(function(){returnsrand.random();});console.log(roll.roll('2d6+5').result);Validating user input:
varuserInput='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);}Inspired by Phillip Newton's Games::Dice.
