mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-27 15:52:39 +00:00
Improved the dice rolling algo
This commit is contained in:
@@ -2,6 +2,8 @@ var React = require('react');
|
||||
var _ = require('lodash');
|
||||
var cx = require('classnames');
|
||||
|
||||
var RollDice = require('naturalCrit/rollDice');
|
||||
|
||||
var AttackSlot = React.createClass({
|
||||
getDefaultProps: function() {
|
||||
return {
|
||||
@@ -18,27 +20,7 @@ var AttackSlot = React.createClass({
|
||||
},
|
||||
|
||||
rollDice : function(key, notation){
|
||||
var additive = 0;
|
||||
var dice = _.reduce([/\+(.*)/, /\-(.*)/], function(r, regexp){
|
||||
var res = r.match(regexp);
|
||||
if(res){
|
||||
additive = res[0]*1;
|
||||
r = r.replace(res[0], '')
|
||||
}
|
||||
return r;
|
||||
}, notation)
|
||||
|
||||
var numDice = dice.split('d')[0];
|
||||
var die = dice.split('d')[1];
|
||||
|
||||
var diceRoll = _.times(numDice, function(){
|
||||
return _.random(1, die);
|
||||
});
|
||||
var res = _.sum(diceRoll) + additive;
|
||||
if(numDice == 1 && die == 20){
|
||||
if(diceRoll[0] == 1) res = 'Fail!';
|
||||
if(diceRoll[0] == 20) res = 'Crit!';
|
||||
}
|
||||
var res = RollDice(notation);
|
||||
this.state.lastRoll[key] = res
|
||||
this.state.lastRoll[key + 'key'] = _.uniqueId(key);
|
||||
this.setState({
|
||||
|
||||
17
client/naturalCrit/sidebar/dmDice/dmDice.jsx
Normal file
17
client/naturalCrit/sidebar/dmDice/dmDice.jsx
Normal file
@@ -0,0 +1,17 @@
|
||||
var React = require('react');
|
||||
var _ = require('lodash');
|
||||
var cx = require('classnames');
|
||||
|
||||
var DmDice = React.createClass({
|
||||
|
||||
render : function(){
|
||||
var self = this;
|
||||
return(
|
||||
<div className='dmDice'>
|
||||
DmDice Ready!
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = DmDice;
|
||||
3
client/naturalCrit/sidebar/dmDice/dmDice.less
Normal file
3
client/naturalCrit/sidebar/dmDice/dmDice.less
Normal file
@@ -0,0 +1,3 @@
|
||||
.dmDice{
|
||||
|
||||
}
|
||||
17
client/naturalCrit/sidebar/encounters/encounters.jsx
Normal file
17
client/naturalCrit/sidebar/encounters/encounters.jsx
Normal file
@@ -0,0 +1,17 @@
|
||||
var React = require('react');
|
||||
var _ = require('lodash');
|
||||
var cx = require('classnames');
|
||||
|
||||
var Encounters = React.createClass({
|
||||
|
||||
render : function(){
|
||||
var self = this;
|
||||
return(
|
||||
<div className='encounters'>
|
||||
Encounters Ready!
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Encounters;
|
||||
3
client/naturalCrit/sidebar/encounters/encounters.less
Normal file
3
client/naturalCrit/sidebar/encounters/encounters.less
Normal file
@@ -0,0 +1,3 @@
|
||||
.encounters{
|
||||
|
||||
}
|
||||
@@ -103,8 +103,13 @@ var Sidebar = React.createClass({
|
||||
</div>
|
||||
<div className='addPlayers'>
|
||||
<h3> <i className='fa fa-group' /> Players </h3>
|
||||
|
||||
<textarea value={this.props.players} onChange={this.props.onPlayerChange} />
|
||||
</div>
|
||||
|
||||
<div className='dmDice'>
|
||||
<h3> <i className='fa fa-random' /> DM Dice </h3>
|
||||
|
||||
ah yeah
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@@ -111,7 +111,6 @@
|
||||
}
|
||||
.addPlayers{
|
||||
h3{
|
||||
//background-color : fade(@purple, 25%);
|
||||
color : white;
|
||||
background-color: @purple;
|
||||
}
|
||||
@@ -121,5 +120,13 @@
|
||||
margin : 10px;
|
||||
}
|
||||
}
|
||||
|
||||
.dmDice{
|
||||
h3{
|
||||
color : white;
|
||||
background-color: @teal;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
32
shared/naturalCrit/rollDice.js
Normal file
32
shared/naturalCrit/rollDice.js
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user