-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdice.py
60 lines (44 loc) · 1.56 KB
/
dice.py
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
import random, re
from halibot import HalModule
die_re = re.compile("(\d+)[dD](\d+)")
MAX_COUNT = 10
MAX_SIDES = 10000
class DiceModule(HalModule):
class Configurer(HalConfigurer):
def configure(self):
self.optionInt("max-count", prompt="Max number of dice to roll at once", default=MAX_COUNT)
self.optionInt("max-sides", prompt="Max number of sides per die", default=MAX_SIDES)
def roll(self, count, sides):
if count == 0:
return "0"
if count < 0:
# This shouldn't actually be possible as the regex doesn't match -
return "You can't think of a way to roll a dice {} times.".format(count)
if sides < 1:
return "You find it difficult to roll a die with {} sides.".format(sides)
if count > self.config.get("max-count", MAX_COUNT) or sides > self.config.get("max-sides", MAX_SIDES):
return "No."
total = 0
response = ''
for i in range(0, count):
x = random.randint(1, sides)
total += x
response += "{}/{}".format(x, sides)
if i != count - 1:
response += " + "
response += " = {}".format(total)
return response
def receive(self, msg):
coarse = msg.body.strip().split(" ")
cmd = coarse[0]
rolls = list(filter( lambda x: re.match(die_re, x), coarse[1:] ))
if cmd == '!roll':
if len(rolls) == 0:
self.reply(msg, body="You make a motion as if to roll some dice, but as you open your hands to throw them, only air escapes.")
return
for r in rolls:
m = re.match(die_re, r)
count = int(m.group(1))
sides = int(m.group(2))
response = self.roll(count, sides)
self.reply(msg, body=response)