Began implementing dice rolls in the maths parser

This commit is contained in:
Stefan Zermatten
2020-06-30 14:40:20 +02:00
parent 56f9e82326
commit 7be4280508
5 changed files with 116 additions and 7 deletions

View File

@@ -1,8 +1,20 @@
import { create, all } from 'mathjs';
import { Random } from 'meteor/random'
const math = create(all);
math.import({
'if': function(pred, a, b) {
return pred ? a : b;
},
'roll': function(number, diceSize){
if (number > 100) throw 'Can only roll 100 dice at a time';
let rollTotal = 0;
let i, roll;
for (i = 0; i < number; i++){
roll = ~~(Random.fraction() * diceSize) + 1
rollTotal += roll;
}
return rollTotal;
}
});