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