From 16a74828a21c60ca705b2a405a9274abc126f5ac Mon Sep 17 00:00:00 2001 From: LaDfBC Date: Fri, 1 Jan 2021 21:45:28 -0600 Subject: [PATCH] Fixed no-participants. No longer allowed to guess lower than 0 or above 1000. Improved resolution output --- src/main/database_module/guess_dao.py | 8 +++++++- src/main/discord_module/bot_runner.py | 28 ++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/database_module/guess_dao.py b/src/main/database_module/guess_dao.py index 0797605..376f6ac 100644 --- a/src/main/database_module/guess_dao.py +++ b/src/main/database_module/guess_dao.py @@ -21,7 +21,7 @@ class GuessDAO(): def __init__(self): self._database_session = DatabaseSession() - def insert(self, guess_info): + def insert(self, guess_info, allow_update=False): session = self._database_session.get_or_create_session() guess = Guess( @@ -39,6 +39,12 @@ class GuessDAO(): session.add(guess) session.commit() return True + elif allow_update: + session\ + .query(Guess)\ + .filter(Guess.member_id == guess_info[MEMBER_ID], Guess.play_id == guess_info[PLAY_ID], Guess.member_name == guess_info[MEMBER_NAME])\ + .update({Guess.guessed_number: guess_info[GUESSED_NUMBER]}) + return True else: return False diff --git a/src/main/discord_module/bot_runner.py b/src/main/discord_module/bot_runner.py index 41f594e..26222ed 100644 --- a/src/main/discord_module/bot_runner.py +++ b/src/main/discord_module/bot_runner.py @@ -50,7 +50,16 @@ async def on_message(message): await message.channel.send("@flappy ball, pitch is in! Send me your guesses with a !guess command.") if content.startswith("!guess"): - guess_value = __parse_guess__(content) + guess_value = None + try: + guess_value = __parse_guess__(content) + except ValueError: + await message.channel.send("That number is not between 1 and 1000. We're still in MLN so don't try to cheat.") + return + + if guess_value is None: + await message.channel.send("I don't know what you did but I'm pretty sure you're tyring to break the bot so please stop.") + return if not play_dao.is_active_play(server_id): await message.channel.send("Hey, there's no active play! Start one up first with !ghostball.") @@ -61,7 +70,7 @@ async def on_message(message): GUESSED_NUMBER: guess_value, MEMBER_NAME: str(message.author.name)} - if guess_dao.insert(guess_object): + if guess_dao.insert(guess_object, allow_update=True): await message.add_reaction(emoji="\N{THUMBS UP SIGN}") # Closes off the active play to be ready for the next set @@ -98,8 +107,12 @@ async def on_message(message): for guess in guesses: response_message += guess[1] + " --- " + str(guess[2]) + " --- " + str(guess[3]) + "\n" - response_message += "\nCongrats to <@" + str(guesses[0][0]) + "> for being the closest! \n" - response_message += "And tell <@" + str(guesses[-1][0]) + "> they suck." + if len(guesses) < 2: + response_message += "Not enough people participated to give best and worst awards. Stop being lazy." + + else: + response_message += "\nCongrats to <@" + str(guesses[0][0]) + "> for being the closest! \n" + response_message += "And tell <@" + str(guesses[-1][0]) + "> they suck." await message.channel.send(response_message) @@ -170,7 +183,12 @@ def __parse_points_message__(message_content): def __parse_guess__(message_content): pieces = message_content.split(' ') try: - return pieces[1] + guess_value = pieces[1] + guess_as_int = int(guess_value) + if guess_as_int > 1000 or guess_as_int < 1: + raise ValueError("Number not between 1 and 1000 inclusive") + else: + return guess_value except TypeError: return None