From df1c30eca813013fbe71fddf2a02f85cbe419b3b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 10 Feb 2019 16:40:29 +0100 Subject: [PATCH] Create a new and better roll! --- strings.py | 26 +++++++++++++++++++++++++- telegrambot.py | 33 +++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/strings.py b/strings.py index d49c2676..1b4a7302 100644 --- a/strings.py +++ b/strings.py @@ -1,4 +1,5 @@ from db import MatchmakingStatus +import dice import typing @@ -151,8 +152,31 @@ class MATCHMAKING: class ROLL: SUCCESS = "🎲 {result}" + SYMBOLS = { + dice.elements.Div: "/", + dice.elements.Mul: "*", + dice.elements.Sub: "-", + dice.elements.Add: "+", + dice.elements.Modulo: "%", + dice.elements.AddEvenSubOdd: "+-", + dice.elements.Highest: "h", + dice.elements.Lowest: "l", + dice.elements.Middle: "m", + dice.elements.Again: "a", + dice.elements.Successes: "e", + dice.elements.SuccessFail: "f", + dice.elements.ArrayAdd: ".+", + dice.elements.ArraySub: ".-", + dice.elements.Array: ",", + dice.elements.Extend: "|", + dice.elements.Reroll: "r", + dice.elements.Explode: "x", + dice.elements.ForceReroll: "rr" + } + class ERRORS: - INVALID_SYNTAX = "⚠ Il tiro dei dadi è fallito. Controlla la sintassi!" + INVALID_SYNTAX = "⚠ Sintassi del tiro di dadi non valida." + DICE_ERROR = "⚠ Il tiro di dadi è fallito." # Ship creator diff --git a/telegrambot.py b/telegrambot.py index 0d8a64d8..60907e75 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -85,9 +85,6 @@ def command(func: "function"): except TimedOut: logger.warning(f"Telegram timed out in {update}") except Exception: - # noinspection PyUnreachableCode - if __debug__: - raise logger.error(f"Critical error: {sys.exc_info()}") # noinspection PyBroadException try: @@ -117,9 +114,6 @@ def database_access(func: "function"): session = db.Session() return func(bot, update, session) except Exception: - # noinspection PyUnreachableCode - if __debug__: - raise logger.error(f"Database error: {sys.exc_info()}") sentry.captureException() finally: @@ -708,15 +702,38 @@ def cmd_markov(bot: telegram.Bot, update: telegram.Update): reply(bot, update, sentence) +def exec_roll(roll) -> str: + result = int(roll.evaluate()) + string = "" + if isinstance(roll, dice.elements.Dice): + string += f"{result}" + else: + for index, operand in enumerate(roll.original_operands): + if operand != roll.operands[index]: + string += f"{roll.operands[index]}" + else: + string += f"{operand}" + if index + 1 != len(roll.original_operands): + + string += strings.ROLL.SYMBOLS[roll.__class__] + string += f"={result}" + return string + + @command def cmd_roll(bot: telegram.Bot, update: telegram.Update): dice_string = update.message.text.split(" ", 1)[1] try: - result = dice.roll(f"{dice_string}t") + roll = dice.roll(f"{dice_string}", raw=True) except dice.DiceBaseException: reply(bot, update, strings.ROLL.ERRORS.INVALID_SYNTAX) return - reply(bot, update, strings.ROLL.SUCCESS, result=result) + try: + result = exec_roll(roll) + except dice.DiceFatalException: + reply(bot, update, strings.ROLL.ERRORS.DICE_ERROR) + return + reply(bot, update, strings.ROLL.SUCCESS, result=result, ignore_escaping=True) @command