-
Notifications
You must be signed in to change notification settings - Fork 2
/
dicecup.js
76 lines (63 loc) · 1.5 KB
/
dicecup.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
72
73
74
75
76
handleDiceCmd = function (msg) {
var regex = /(?:roll|throw|flip|toss)\s+(a|one|two|three|four|five|six|seven|eight|nine|ten|\d+|)\s*(die|dice|dies|coin|coins|[dDwW]\d+)/;
var match = msg.match(regex);
if (!match)
return;
var number = 1;
var numberLookup = {
one : 1,
two : 2,
three : 3,
four : 4,
five : 5,
six : 6,
seven : 7,
eight : 8,
nine : 9,
ten : 10,
};
if (numberLookup[match[1]]) {
number = numberLookup[match[1]];
} else if (parseInt(match[1])) {
number = parseInt(match[1]);
}
if (number > 20) {
return "Sorry, but I will not throw more than 20 dice for you...";
}
var sides = 6;
var lookup = null;
var match2 = match[2].match(/[dDwW](\d+)/);
if (match[2] == 'coin' || match[2] == 'coins') {
sides = 2;
lookup = function (n) {return ["heads", "tails"][n]};
} else if (match2) {
sides = parseInt(match2[1]);
}
/*
if (sides == 6) {
lookup = function (n) {return ["⚀","⚁","⚂","⚃","⚄","⚅"][n]};
}
*/
var result = "Alea Iacta Est"+": ";
var total = 0;
for (var i = 0; i < number; i++) {
n = Math.floor(Math.random() * sides);
if (lookup) {
result += lookup(n);
} else {
result += (n + 1);
total += n + 1;
}
if (i+1 == number) {
result += ".";
} else if (i+2 == number) {
result += " and ";
} else {
result += ", ";
}
}
if (number > 1 && total) {
result += " (∑: "+ total +")";
}
return result;
}