-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolls.js
71 lines (69 loc) · 2.2 KB
/
rolls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Rolls dice based on two numbers.
function rollDice(count, max) {
//Checks for the 0 edge case (cause you cant roll 0)
if (count <= 0 || max <= 0) {return null;}
//Does the rolls
var values = [],
rollsum = 0;
for (var _ = 0; _ < count; _++) {
let roll = Math.floor(Math.random() * max) + 1;
values.push(roll);
rollsum += roll;
}
return { sum: rollsum, rolls: values };
}
//Converts the command line into usable data array
function parseCmd(line) {
//Regular expresion to break down the command
var reg = /(?:\+|-)?(?:\d+d\d+|\d+)/g,
str = line.replace(/\s/g, '');
//Splits the line
var cmd = [];
cmd = str.match(reg);
// for (var c = undefined; c !== null; c = reg.exec(str)) {
// //ingnores the initial undefined variable, as well as any slip ups
// if (c) cmd.push(c[0]);
// }
return cmd;
}
//Converts the command into a rolldata object
function parseRoll(line) {
var commands = parseCmd(line),
dieReg = /(^\d+)d(\d+)/, //Regex used to locate dice commands
rolls = [], //list of dice roll data
sum = 0; //total sum of calculation
if (!commands) {
return null;
}
for (var i = 0; i < commands.length; i++) {
//Handles + or - characters
var handle = commands[i][0];
if (handle === '+') {
sum = Math.abs(sum);
} else if (handle === '-') {
sum = -sum;
} else if (i !== 0){
return null;
}
//Cuts off the handle after the first pass
var value = (i === 0) ? commands[i] : commands[i].substr(1);
//Handle the dice/math
var dice = dieReg.exec(value);
if (dice) {
var count = Math.floor(parseInt(dice[1])),
max = Math.floor(parseInt(dice[2]));
var roll = rollDice(count, max);
if (roll) {
rolls.push(roll);
sum += roll.sum;
} else {
return null;
}
} else {
rolls.push(commands[i]);
sum += Math.floor(parseInt(value));
}
}
return {sum: Math.abs(sum), info: rolls};
}
exports.parse = parseRoll;