0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-31 10:52:42 +00:00

Improved the dice rolling algo

This commit is contained in:
Scott Tolksdorf
2015-11-21 15:53:04 -05:00
parent 97ce6ab9ee
commit fc21364a0c
8 changed files with 89 additions and 23 deletions

View File

@@ -0,0 +1,32 @@
var _ = require('lodash');
module.exports = function(notation){
notation = notation.replace(/ /g,'');
var parts = notation.split(/(?=[\-\+])/g);
return _.reduce(parts, function(r, part){
if(!_.isNumber(r)) return r;
var res = 0;
var neg = false;
if(part[0] == '-') neg = true;
if(part[0] == '-' || part[0] == '+') part = part.substring(1);
//Check for dice
if(part.indexOf('d') !== -1){
var numDice = part.split('d')[0];
var die = part.split('d')[1];
var diceRolls = _.times(numDice, function(){
return _.random(1, die);
});
res = _.sum(diceRolls);
if(numDice == 1 && die == 20){
if(diceRolls[0] == 1) return 'Fail!';
if(diceRolls[0] == 20) return 'Crit!';
}
}else{
res = part * 1;
}
return r + (neg ? -res : res);
}, 0)
}