-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroll.py
48 lines (39 loc) · 1.09 KB
/
roll.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
import re, random
class Roll:
dice = 1
sides = 6
offset = 0
def __call__(self):
lst = []
t = 0
for i in range(self.dice):
x = random.randint(1, self.sides)
lst.append(x)
t += x
t += self.offset
return t, lst
def __str__(self):
off = ((('+' if self.offset >= 0 else '') + str(self.offset)) if self.offset != 0 else '')
return '%sd%d%s' % (self.dice if self.dice != 1 else '', self.sides, off)
re_die = re.compile(r'(\d+)?([dD])(\d+)(?:([-+])(\d+))?$')
def parseRoll(msg):
if ',' in msg:
msg = msg.split(',')
else:
msg = [msg]
lst = []
for w in msg:
w = w.strip()
m = re_die.match(w)
if not m:
return None
r = Roll()
r.dice = int(m.group(1) or 1)
r.expand = (m.group(2) == 'D')
r.sides = int(m.group(3))
if m.group(4):
sign = (-1 if m.group(4) == '-' else 1)
num = int(m.group(5))
r.offset = sign * num
lst.append(r)
return lst