dicerolls now have descriptions

This commit is contained in:
Patrick Hurst 2010-08-16 05:32:28 -04:00
parent 91124d1f8a
commit 7876fd57ac
1 changed files with 7 additions and 4 deletions

View File

@ -9,7 +9,7 @@ from util import hook
whitespace_re = re.compile(r'\s+')
valid_diceroll = r'^([+-]?(\d+|\d*d(\d+|F))([+-](\d+|\d*d(\d+|F)))*)$'
valid_diceroll = r'^([+-]?(?:\d+|\d*d(?:\d+|F))(?:[+-](?:\d+|\d*d(?:\d+|F)))*)( .+)?$'
valid_diceroll_re = re.compile(valid_diceroll, re.I)
sign_re = re.compile(r'[+-]?(?:\d*d)?(?:\d+|F)', re.I)
split_re = re.compile(r'([\d+-]*)d?(F|\d*)', re.I)
@ -36,10 +36,13 @@ def nrolls(count, n):
def dice(inp):
".dice <diceroll> -- simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \
"D20s, subtract 1D5, add 4"
try:
inp = inp.groups()[0] # try to grab the roll
desc = None
try: # if inp is a re.match object...
(inp, desc) = inp.groups()
except AttributeError:
pass # we got called via hook.command, inp is already the roll
if desc == None: (inp, desc) = valid_diceroll_re.match(inp).groups()
desc = desc.strip()
spec = whitespace_re.sub('', inp)
if not valid_diceroll_re.match(spec):
return "Invalid diceroll"
@ -65,4 +68,4 @@ def dice(inp):
except OverflowError:
return "Thanks for overflowing a float, jerk >:["
return str(sum)
return "%s: %d" % (desc, sum)