From 347dc33b454696de39db5e44f25b7adf86fe17c5 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Fri, 20 Oct 2023 16:02:14 -0500 Subject: [PATCH] Add early game-end logic --- cogs/gameplay.py | 97 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/cogs/gameplay.py b/cogs/gameplay.py index f5a5314..8dcffdb 100644 --- a/cogs/gameplay.py +++ b/cogs/gameplay.py @@ -57,34 +57,37 @@ class Gameplay(commands.Cog): await score_channel.set_permissions(player_role, read_messages=False) return - embed = get_team_embed('Live Scoreboard') - valid_scores = False - for x in active_games: - await asyncio.sleep(1) - gs = await self.get_game_state(x) - if not gs['error']: - valid_scores = True - channel = guild.get_channel(gs["channel_id"]) - g_message = gs['scorebug'] - g_message += f'Pitcher: {gs["pitcher"]["name"]}\n' \ - f'Batter: {gs["batter"]["name"]}\n' \ - f'Location: {channel.mention if channel is not None else "Your Butt"}\n' \ - f'-------------------------' - embed.add_field( - name=f'{gs["away_team"]["sname"]} @ {gs["home_team"]["sname"]}', - value=g_message, - inline=False - ) + try: + embed = get_team_embed('Live Scoreboard') + valid_scores = False + for x in active_games: + await asyncio.sleep(1) + gs = await self.get_game_state(x) + if not gs['error']: + valid_scores = True + channel = guild.get_channel(gs["channel_id"]) + g_message = gs['scorebug'] + g_message += f'Pitcher: {gs["pitcher"]["name"]}\n' \ + f'Batter: {gs["batter"]["name"]}\n' \ + f'Location: {channel.mention if channel is not None else "Your Butt"}\n' \ + f'-------------------------' + embed.add_field( + name=f'{gs["away_team"]["sname"]} @ {gs["home_team"]["sname"]}', + value=g_message, + inline=False + ) - if valid_scores: - async for message in score_channel.history(limit=25): - await message.delete() + if valid_scores: + async for message in score_channel.history(limit=25): + await message.delete() - await score_channel.set_permissions(player_role, read_messages=True) - await score_channel.send(content=None, embed=embed) - else: - await score_channel.set_permissions(player_role, read_messages=False) - return + await score_channel.set_permissions(player_role, read_messages=True) + await score_channel.send(content=None, embed=embed) + else: + await score_channel.set_permissions(player_role, read_messages=False) + return + except Exception as e: + logging.error(f'Could not update live_scoreboard: {e}') @tasks.loop(hours=36) async def update_ratings_guides(self): @@ -1553,7 +1556,47 @@ class Gameplay(commands.Cog): await interaction.edit_original_response(content='Bruh. Only GMs of the active teams can end games.') return - await response.edit(content=None, embed=await self.get_game_state_embed(this_game)) + latest_play = get_latest_play(this_game.id) + """ + If the game isn't over + Not 0 outs + Top of inning <= 9 and not 10-run lead + Top 9th inning or earlier and not 10-run lead + """ + + valid_end = False + if latest_play.starting_outs == 0: + if latest_play.inning_half == 'top': + if latest_play.inning_num > 9 and latest_play.away_score != latest_play.home_score: + valid_end = True + if abs(latest_play.home_score - latest_play.away_score) >= 10: + valid_end = True + elif latest_play.inning_half == 'bot' and abs(latest_play.home_score - latest_play.away_score) >= 10: + valid_end = True + + if not valid_end: + view = Confirm(responders=[interaction.user]) + question = await interaction.channel.send( + 'It doesn\'t look like this game is over, yet. I can end it, but no rewards will be paid out.\n\n' + 'Should I end this game?', + view=view + ) + await view.wait() + + if view.value: + patch_game(this_game.id, active=False) + await question.edit( + content='Roger dodger - it is game over.', view=None + ) + return + + else: + await question.edit( + content='It stays.', view=None + ) + return + + await response.edit(content=None, embed=await self.get_game_state_embed(this_game, full_length=False)) view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') question = await interaction.edit_original_response(content=f'Should I end this game?', view=view) await view.wait()