-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (40 loc) · 1.09 KB
/
index.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
module.exports = function(robot) {
robot.hear(/roll ([\w\+ ]+)$/, function(msg, done) {
var die = msg.match[1];
var breakdown = /(\d+)?d(\d+) *(\+\s*\d+)?/.exec(die);
if (!breakdown) {
done();
return;
}
var sides = breakdown[2] | 0;
var count = 1;
var add = 0;
if (breakdown[1]) {
count = breakdown[1] | 0;
}
if (breakdown[3]) {
add = parseInt(breakdown[3].substr(1), 10);
}
var rolls = [];
for (var i = 0; i < count; i++) {
rolls.push(roll(sides, add));
}
msg.reply(rolls.join('\n'), done);
});
};
function roll(sides, add) {
var r = (Math.random() * sides | 0) + 1;
var total = r + add;
var output = 'Rolled ' + r.toString();
if (add) {
output += ' + ' + add.toString() + ' = ' + total.toString();
}
if (sides === 20) {
if (r === 20) {
output += ' (NAT 20!)';
} else if (r === 1) {
output += ' (CRITICAL MISS)';
}
}
return output;
}