Skip to content

Commit

Permalink
Merge pull request #969 from Ganon11/ganon11-dice-inputs
Browse files Browse the repository at this point in the history
[dice] Case insensitivity and better error handling for negative numbers.
  • Loading branch information
embolalia committed Dec 12, 2015
2 parents cd6c62c + ee75628 commit 047a99e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions sopel/modules/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ def get_number_of_faces(self):
def _roll_dice(bot, dice_expression):
result = re.search(
r"""
(?P<dice_num>\d*)
(?P<dice_num>-?\d*)
d
(?P<dice_type>\d+)
(v(?P<drop_lowest>\d+))?
(?P<dice_type>-?\d+)
(v(?P<drop_lowest>-?\d+))?
$""",
dice_expression,
re.IGNORECASE | re.VERBOSE)
Expand All @@ -140,6 +140,11 @@ def _roll_dice(bot, dice_expression):
bot.reply("I don't have any dice with %d sides. =(" % dice_type)
return None # Signal there was a problem

# Can't roll a negative number of dice.
if dice_num < 0:
bot.reply("I'd rather not roll a negative amount of dice. =(")
return None # Signal there was a problem

# Upper limit for dice should be at most a million. Creating a dict with
# more than a million elements already takes a noticeable amount of time
# on a fast computer and ~55kB of memory.
Expand All @@ -151,7 +156,10 @@ def _roll_dice(bot, dice_expression):

if result.group('drop_lowest'):
drop = int(result.group('drop_lowest'))
dice.drop_lowest(drop)
if drop >= 0:
dice.drop_lowest(drop)
else:
bot.reply("I can't drop the lowest %d dice. =(" % drop)

return dice

Expand All @@ -176,7 +184,7 @@ def roll(bot, trigger):
"""
# This regexp is only allowed to have one captured group, because having
# more would alter the output of re.findall.
dice_regexp = r"\d*d\d+(?:v\d+)?"
dice_regexp = r"-?\d*[dD]-?\d+(?:[vV]-?\d+)?"

# Get a list of all dice expressions, evaluate them and then replace the
# expressions in the original string with the results. Replacing is done
Expand Down

0 comments on commit 047a99e

Please sign in to comment.