Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix .dice/.roll borking when called without arguments. #625

Merged
merged 2 commits into from
Sep 29, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions willie/modules/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_number_of_faces(self):
return len(self.dice) + len(self.dropped)


def _roll_dice(dice_expression):
def _roll_dice(bot, dice_expression):
result = re.search(
r"""
(?P<dice_num>\d*)
Expand All @@ -131,11 +131,17 @@ def _roll_dice(dice_expression):
dice_num = int(result.group('dice_num') or 1)
dice_type = int(result.group('dice_type'))

# Dice can't have zero or a negative number of sides.
if dice_type <= 0:
bot.reply("I don't have any dice with %d sides. =(" % dice_type)
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.
if dice_num > 1000:
return None
bot.reply('I only have 1000 dice. =(')
return None # Signal there was a problem

dice = DicePouch(dice_num, dice_type, 0)

Expand Down Expand Up @@ -171,13 +177,18 @@ def roll(bot, trigger):
# Get a list of all dice expressions, evaluate them and then replace the
# expressions in the original string with the results. Replacing is done
# using string formatting, so %-characters must be escaped.
if not trigger.group(2):
return bot.reply("No dice to roll.")
arg_str = trigger.group(2)
dice_expressions = re.findall(dice_regexp, arg_str)
arg_str = arg_str.replace("%", "%%")
arg_str = re.sub(dice_regexp, "%s", arg_str)
dice = list(map(_roll_dice, dice_expressions))

f = lambda dice_expr: _roll_dice (bot, dice_expr)
dice = list(map(f, dice_expressions))

if None in dice:
bot.reply("I only have 1000 dice. =(")
# Stop computing roll if there was a problem rolling dice.
return

def _get_eval_str(dice):
Expand Down