From dfdde884b8022e0a7c3c7e210eb940ece7c0cfba Mon Sep 17 00:00:00 2001 From: jesopo Date: Sat, 15 Feb 2020 23:31:55 +0000 Subject: implement command spec modifiers, use for `pattern`, use pattern for dice.py --- modules/dice.py | 72 ++++++++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 39 deletions(-) (limited to 'modules') diff --git a/modules/dice.py b/modules/dice.py index 8e34c9e0..8a787d94 100644 --- a/modules/dice.py +++ b/modules/dice.py @@ -15,43 +15,37 @@ class Module(ModuleManager.BaseModule): @utils.hook("received.command.roll") @utils.hook("received.command.dice", alias_of="roll") @utils.kwarg("help", "Roll dice DND-style") - @utils.kwarg("usage", "[1-%s]d[1-%d]" % (MAX_DICE, MAX_SIDES)) + @utils.spec("?<1d20>pattern(^(\d+)d(\d+)((?:\s*[-+]\d+)*))") def roll_dice(self, event): - args = None - if event["args_split"]: - args = event["args"] - else: - args = "1d6" - - match = RE_DICE.match(args) - if match: - roll = match.group(0) - dice_count = int(match.group(1) or "1") - side_count = int(match.group(2)) - modifiers_str = "".join(match.group(3).split()) - modifiers = RE_MODIFIERS.findall(modifiers_str) - - if dice_count > 6: - raise utils.EventError("Max number of dice is %s" % MAX_DICE) - if side_count > MAX_SIDES: - raise utils.EventError("Max number of sides is %s" - % MAX_SIDES) - - results = random.choices(range(1, side_count+1), k=dice_count) - - total_n = sum(results) - for modifier in modifiers: - if modifier[0] == "+": - total_n += int(modifier[1:]) - else: - total_n -= int(modifier[1:]) - - total = "" - if len(results) > 1 or modifiers: - total = " (total: %d)" % total_n - - results_str = ", ".join(str(r) for r in results) - event["stdout"].write("Rolled %s and got %s%s" % ( - roll, results_str, total)) - else: - event["stderr"].write("Invalid format. Example: 2d12+2") + dice_count, side_count = 1, 6 + roll = "1d6" + modifiers = [] + + if event["spec"][0]: + dice_count = int(event["spec"][0].group(1) or "1") + side_count = int(event["spec"][0].group(2)) + roll = event["spec"][0].group(0) + modifiers = RE_MODIFIERS.findall(event["spec"][0].group(3)) + + if dice_count > 6: + raise utils.EventError("Max number of dice is %s" % MAX_DICE) + if side_count > MAX_SIDES: + raise utils.EventError("Max number of sides is %s" + % MAX_SIDES) + + results = random.choices(range(1, side_count+1), k=dice_count) + + total_n = sum(results) + for modifier in modifiers: + if modifier[0] == "+": + total_n += int(modifier[1:]) + else: + total_n -= int(modifier[1:]) + + total = "" + if len(results) > 1 or modifiers: + total = " (total: %d)" % total_n + + results_str = ", ".join(str(r) for r in results) + event["stdout"].write("Rolled %s and got %s%s" % ( + roll, results_str, total)) -- cgit v1.3.1-10-gc9f91