Simplify modules/dice.py

This commit is contained in:
jesopo 2018-11-17 12:18:26 +00:00
parent 43e39c42c7
commit 73ecb42ca9

View file

@ -10,29 +10,20 @@ class Module(ModuleManager.BaseModule):
:help: Roll some dice, DND style! :help: Roll some dice, DND style!
:usage: [1-5]d[1-20] :usage: [1-5]d[1-20]
""" """
raw_input = event["args_split"][0] roll = event["args_split"][0].lower()
roll = raw_input.split("d") count, sides = roll.partition("d")
results = [] if not count.isdigit() or not sides.isdigit():
if len(roll) is not 2:
raise utils.EventError(ERROR_FORMAT) raise utils.EventError(ERROR_FORMAT)
if roll[0].isdigit() is False or roll[1].isdigit() is False: count_n = min(int(roll[0]), 5)
raise utils.EventError(ERROR_FORMAT) sides_n = min(int(roll[1]), 20)
roll = [int(roll[0]), int(roll[1])] reults = random.choices(range(1, die_sides), k=die_count)
num_of_die = 5 if roll[0] > 5 else roll[0]
sides_of_die = 20 if roll[1] > 20 else roll[1]
str_roll = str(num_of_die) + "d" + str(sides_of_die)
for i in range(0, num_of_die):
results.append(random.randint(1, sides_of_die))
total_n = sum(results) total_n = sum(results)
total = "" total = ""
if len(results) > 1: if len(results) > 1:
total = " (total: %d)" % total_n total = " (total: %d)" % total_n
event["stdout"].write("Rolled %s and got %s%s" % ( event["stdout"].write("Rolled %s and got %s%s" % (
str_roll, ", ".join(str(r) for r in results), total)) roll, total_n, ", ".join(results)))