Skip to content

Commit

Permalink
Merge pull request #15 from lordnull/mw-add-min-max-possible
Browse files Browse the repository at this point in the history
Mw add min max possible
  • Loading branch information
Morgul committed Feb 4, 2016
2 parents 307705e + f2ca65e commit 21124f7
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 24 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ For more advanced usage, scopes can be nested.
var result = dice.roll("[takes.priority] + [nested.value]", scope);
result == 10; // because we look for an exact match of property name first

Statistics
==========

If you want some analysis for your rolls, you can get a result set created with
some basic stats tagged in.

Using `dice.statistics(rollString, scope, samples)` returns an object:

```javascript
{
'results': [dice_roll_result], // The result set generated
'mean': number, // The average of result set generated
'min': number, // The smallest value in the result set
'max': number, // The largest value in the result set
'min_possible': number, // The smallest value that could have
// been generated.
'max_possible': number, // The largest value that could have
// been generated.
}
```

Contributing
============

Expand Down
82 changes: 81 additions & 1 deletion build/dice.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports={
var dice = {
parse: require('./parser').parse,
eval: require('./evaluate').eval,
ops: require('./evaluate').ops,
version: require('../package').version
};

Expand All @@ -75,14 +76,93 @@ dice.statistics = function(str, scope, samples){
var mean = resultSet.reduce(function(n, acc){ return n + acc; }, 0) / samples;
var min = resultSet.reduce(function(n, acc){ return n < acc ? n : acc; }, resultSet[0]);
var max = resultSet.reduce(function(n, acc){ return n > acc ? n : acc; }, resultSet[0]);

var parsed = dice.parse(str);

var minMaxPossible = determine_min_max_possible(parsed, scope);

return {
'results': resultSet,
'mean': mean,
'min': parseInt(min.toFixed()),
'max': parseInt(max.toFixed())
'max': parseInt(max.toFixed()),
'min_possible': minMaxPossible[0],
'max_possible': minMaxPossible[1]
};
};

function determine_min_max_possible(opObject, scope){
if(opObject.op == 'static'){
return [opObject.value, opObject.value];
}
if(opObject.op == 'lookup'){
var lookup = dice.ops.lookup.call(opObject, scope);
return [lookup(scope), lookup(scope)];
}
if(opObject.op == 'floor'){
var minmax = determine_min_max_possible(opObject.args[0], scope);
return [Math.floor(minmax[0]), Math.floor(minmax[1])];
}
if(opObject.op == 'ceil'){
var minmax = determine_min_max_possible(opObject.args[0], scope);
return [Math.ceil(minmax[0]), Math.ceil(minmax[1])];
}
if(opObject.op == 'round'){
var minmax = determine_min_max_possible(opObject.args[0], scope);
return [Math.round(minmax[0]), Math.round(minmax[1])];
}
if(opObject.op == 'd'){
var multipleMinMax = determine_min_max_possible(opObject.args[0], scope);
var randPartMinMax = determine_min_max_possible(opObject.args[1], scope);
var min = randPartMinMax[0] * multipleMinMax[0];
var max = randPartMinMax[1] * multipleMinMax[1];
return [min, max];
}
if(opObject.op == 'w'){
var multipleMinMax = determine_min_max_possible(opObject.args[0], scope);
var randPartMinMax = determine_min_max_possible(opObject.args[1], scope);
var min = randPartMinMax[0] * multipleMinMax[0];
var max = randPartMinMax[1] * multipleMinMax[1];
return [min, max];
}
if(opObject.op == 'random'){
var minMinMax = determine_min_max_possible(opObject.args[0], scope);
var maxMinMax = determine_min_max_possible(opObject.args[1], scope);
return [minMinMax[0], maxMinMax[1]];
}
if(opObject.op == '+'){
var leftMinMax = determine_min_max_possible(opObject.args[0], scope);
var rightMinMax = determine_min_max_possible(opObject.args[1], scope);
var min = leftMinMax[0] + rightMinMax[0];
var max = leftMinMax[1] + rightMinMax[1];
return [min, max];
}
if(opObject.op == '-'){
var leftMinMax = determine_min_max_possible(opObject.args[0], scope);
var rightMinMax = determine_min_max_possible(opObject.args[1], scope);
var min = leftMinMax[0] - rightMinMax[0];
var max = leftMinMax[1] - rightMinMax[1];
return [min, max];
}
if(opObject.op == '*'){
var leftMinMax = determine_min_max_possible(opObject.args[0], scope);
var rightMinMax = determine_min_max_possible(opObject.args[1], scope);
var min = leftMinMax[0] * rightMinMax[0];
var max = leftMinMax[1] * rightMinMax[1];
return [min, max];
}
if(opObject.op == '/'){
var leftMinMax = determine_min_max_possible(opObject.args[0], scope);
var rightMinMax = determine_min_max_possible(opObject.args[1], scope);
var min = leftMinMax[0] / rightMinMax[1];
var max = leftMinMax[1] / rightMinMax[0];
return [min, max];
}
if(opObject.op == 'paren_express'){
return determine_min_max_possible(opObject.args[0], scope);
}
}

function stringify_expression(evaled_op){
var sub = stringify(evaled_op.expression);
var prefix = evaled_op.op[0];
Expand Down
Loading

0 comments on commit 21124f7

Please sign in to comment.