From e2e114d06a8fe46451dae4da9f3a03abe0ed3b26 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Fri, 17 Nov 2023 22:17:40 -0600 Subject: [PATCH] Add Task Loop Check --- cogs/economy.py | 13 +-- cogs/gameplay.py | 291 +++++++++++++++++++++++++++++++++++++++++------ cogs/players.py | 13 +-- 3 files changed, 263 insertions(+), 54 deletions(-) diff --git a/cogs/economy.py b/cogs/economy.py index d74923c..96f75cf 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -117,15 +117,6 @@ class Economy(commands.Cog): @tasks.loop(minutes=10) async def notif_check(self): - guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) - if not guild: - logging.error(f'Cannot access guild; pausing for 15 seconds') - await asyncio.sleep(15) - guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) - if not guild: - logging.error(f'Still cannot access guild; trying again in 10 minutes') - return - # Check for notifications all_notifs = await db_get('notifs', params=[('ack', False)]) if not all_notifs: @@ -169,6 +160,10 @@ class Economy(commands.Cog): embed.add_field(name=p_list[player]['field_name'], value=p_list[player]['message'], inline=False) await send_to_channel(self.bot, topics[topic]['channel_name'], embed=embed) + @notif_check.before_loop + async def before_notif_check(self): + await self.bot.wait_until_ready() + @commands.hybrid_group(name='help-pd', help='FAQ for Paper Dynasty and the bot', aliases=['helppd']) @commands.check(legal_channel) async def pd_help_command(self, ctx: commands.Context): diff --git a/cogs/gameplay.py b/cogs/gameplay.py index 8dda6c9..303492d 100644 --- a/cogs/gameplay.py +++ b/cogs/gameplay.py @@ -41,14 +41,6 @@ class Gameplay(commands.Cog): @tasks.loop(minutes=3) async def live_scoreboard(self): guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) - if not guild: - logging.error(f'Cannot access guild; pausing for 15 seconds') - await asyncio.sleep(15) - guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) - if not guild: - logging.error(f'Still cannot access guild; trying again in 10 minutes') - return - score_channel = discord.utils.get(guild.text_channels, name='live-pd-scores') player_role = discord.utils.get(guild.roles, name=PD_PLAYERS_ROLE_NAME) @@ -98,6 +90,10 @@ class Gameplay(commands.Cog): except Exception as e: logging.error(f'Could not update live_scoreboard: {e}') + @live_scoreboard.before_loop + async def before_live_scoreboard(self): + await self.bot.wait_until_ready() + @tasks.loop(hours=36) async def update_ratings_guides(self): guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) @@ -513,7 +509,7 @@ class Gameplay(commands.Cog): embed.add_field(name=f'{game_state["home_team"]["abbrev"]} Lineup', value=home_lineup) return embed - async def groundballs( + async def groundballs_old( self, interaction, this_game, this_play: StratPlay, groundball_type: str, comp_play: bool = True): batter_to_base = None bases = ['third', 'second', 'first'] @@ -851,6 +847,137 @@ class Gameplay(commands.Cog): if comp_play: complete_play(this_play.id, batter_to_base=batter_to_base) + async def groundballs( + self, interaction, this_game, this_play: StratPlay, groundball_type: str, comp_play: bool = True): + batter_to_base = None + + if this_play.starting_outs == 2 or this_play.on_base_code == 0: + patch_play(this_play.id, pa=1, ab=1, outs=1) + + elif this_play.starting_outs == 1 and groundball_type == 'a' and runner_on_first(this_play): + patch_play(this_play.id, pa=1, ab=1, outs=2, on_first_final=False) + + else: + playing_in = False + if runner_on_third(this_play): + view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') + question = await interaction.channel.send( + f'Was the defender playing in?', view=view + ) + await view.wait() + + if view.value: + playing_in = True + else: + playing_in = False + + await question.delete() + logging.info(f'playing_in: {playing_in} / obc: {this_play.on_base_code} / gb_type: {groundball_type}') + + if not playing_in: + if groundball_type == 'a': + if this_play.on_base_code == 0: + batter_to_base = gb_result_1(this_play) + + elif this_play.on_base_code in [1, 4, 5, 7]: + batter_to_base = gb_result_2(this_play) + + elif this_play.on_base_code in [3, 6]: + view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') + question = await interaction.channel.send( + f'Was the ball hit to either the 2B or SS?', view=view + ) + await view.wait() + + if view.value: + to_mif = True + else: + to_mif = False + await question.delete() + + batter_to_base = gb_result_5(this_play, to_mif) + + else: + view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') + question = await interaction.channel.send( + f'Was the ball hit to either the 1B or 2B?', view=view + ) + await view.wait() + + if view.value: + to_right_side = True + else: + to_right_side = False + await question.delete() + batter_to_base = gb_result_6(this_play, to_right_side) + + elif groundball_type == 'b': + if this_play.on_base_code == 0: + batter_to_base = gb_result_1(this_play) + + elif this_play.on_base_code in [1, 4, 5, 7]: + batter_to_base = gb_result_4(this_play) + + elif this_play.on_base_code in [3, 6]: + view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') + question = await interaction.channel.send( + f'Was the ball hit to either the 2B or SS?', view=view + ) + await view.wait() + + if view.value: + to_mif = True + else: + to_mif = False + await question.delete() + + batter_to_base = gb_result_5(this_play, to_mif) + + else: + view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') + question = await interaction.channel.send( + f'Was the ball hit to either the 1B or 2B?', view=view + ) + await view.wait() + + if view.value: + to_right_side = True + else: + to_right_side = False + await question.delete() + batter_to_base = gb_result_6(this_play, to_right_side) + + else: + if this_play.on_base_code == 0: + batter_to_base = gb_result_1(this_play) + + else: + batter_to_base = gb_result_3(this_play) + + else: + if groundball_type == 'a': + if this_play.on_base_code == 7: + batter_to_base = gb_result_10(this_play) + else: + batter_to_base = gb_result_7(this_play) + + elif groundball_type == 'b': + if this_play.on_base_code == 7: + batter_to_base = gb_result_11(this_play) + elif this_play.on_base_code == 5: + batter_to_base = gb_result_9(this_play) + else: + batter_to_base = gb_result_1(this_play) + + else: + if this_play.on_base_code == 7: + batter_to_base = gb_result_11(this_play) + else: + batter_to_base = gb_result_8(this_play) + + if comp_play: + complete_play(this_play.id, batter_to_base=batter_to_base) + async def flyballs(self, interaction, this_game, this_play, flyball_type, comp_play: bool = True): num_outs = 1 @@ -3711,45 +3838,137 @@ class Gameplay(commands.Cog): if position.value not in ['LF', 'CF', 'RF']: view = ButtonOptions( responders=[interaction.user], - labels=['gb A', 'gb B', 'gb C', None if position.value != 'C' else 'FO', + labels=['G1', 'G2', 'G3', None if position.value != 'C' else 'FO', None if position.value != 'C' else 'PO'] ) question = await interaction.channel.send(f'What was the result of the play?', view=view) + logging.info(f'obc: {this_play.on_base_code}') await view.wait() + logging.info(f'gameplay - view: {view} / view.value: {view.value}') + gb_type = view.value + logging.info(f'gameplay - gb_type: {gb_type}') if not view.value: - await question.delete() - - if view.value == 'gb A': - await self.groundballs(interaction, this_game, this_play, groundball_type='a') - elif view.value == 'gb B': - await self.groundballs(interaction, this_game, this_play, groundball_type='b') - elif view.value == 'gb C': - await self.groundballs(interaction, this_game, this_play, groundball_type='c') - else: - patch_play(this_play.id, pa=1, ab=1, outs=1) - advance_runners(this_play.id, 0) - complete_play(this_play.id) - - patch_play(this_play.id, locked=False) - return + await question.edit('Okay, we can try this again later.') else: await question.delete() - if view.value in ['FO', 'PO']: - patch_play(this_play.id, pa=1, ab=1, outs=1) - advance_runners(this_play.id, 0) - complete_play(this_play.id) + + playing_in = False + batter_to_base = None + if runner_on_third(this_play): + view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') + question = await interaction.channel.send( + f'Was the defender playing in?', view=view + ) + await view.wait() + + if view.value: + playing_in = True + else: + playing_in = False + + await question.delete() + logging.info(f'bot playing_in: {playing_in} / view.value: {view.value} / gb_type: {gb_type}') + + if gb_type == 'G1': + if (not playing_in and this_play.on_base_code == 0) or \ + (playing_in and (this_play.on_base_code in [3, 6])): + batter_to_base = gb_result_1(this_play) + + elif playing_in and this_play.on_base_code == 5: + batter_to_base = gb_result_7(this_play) + + elif (not playing_in and this_play.on_base_code in [1, 5, 7]) or \ + (playing_in and (this_play.on_base_code in [1, 4])): + batter_to_base = gb_result_2(this_play) + + elif not playing_in and this_play.on_base_code in [3, 6]: + batter_to_base = gb_result_3(this_play) + + elif this_play.on_base_code == 2: + if position.value == '3B': + this_def = '3b' + elif position.value in ['1B', '2B']: + this_def = '1b-2b' + else: + this_def = 'ss-p-c' + batter_to_base = await gb_result_12(this_play, this_def, interaction) + + elif playing_in and this_play.on_base_code == 7: + batter_to_base = gb_result_10(this_play) + + elif not playing_in and this_play.on_base_code == 4: + if position.value in ['C', '3B']: + this_def = 'c-3b' + else: + this_def = 'else' + batter_to_base = gb_result_13(this_play, this_def) + + elif gb_type == 'G2': + if (not playing_in and this_play.on_base_code == 0) or \ + (playing_in and this_play.on_base_code in [3, 6]): + batter_to_base = gb_result_1(this_play) + + elif playing_in and this_play.on_base_code == 5: + batter_to_base = gb_result_7(this_play) + + elif (not playing_in and this_play.on_base_code in [1, 4, 5, 7]) or \ + (playing_in and (this_play.on_base_code in [1, 4])): + batter_to_base = gb_result_4(this_play) + + elif not playing_in and this_play.on_base_code in [3, 6]: + if position.value in ['SS', '2B']: + to_mif = True + else: + to_mif = False + batter_to_base = gb_result_5(this_play, to_mif) + + elif playing_in and this_play.on_base_code == 7: + batter_to_base = gb_result_11(this_play) + + elif this_play.on_base_code == 2: + if position.value == '3B': + this_def = '3b' + elif position.value in ['1B', '2B']: + this_def = '1b-2b' + else: + this_def = 'ss-p-c' + batter_to_base = await gb_result_12(this_play, this_def, interaction) + + elif gb_type == 'G3': + if not playing_in and this_play.on_base_code == 0: + batter_to_base = gb_result_1(this_play) + + elif playing_in and this_play.on_base_code == 5: + batter_to_base = gb_result_7(this_play) + + elif playing_in and this_play.on_base_code == 7: + batter_to_base = gb_result_11(this_play) + + elif not playing_in and this_play.on_base_code == 2: + if position.value == '3B': + this_def = '3b' + elif position.value in ['1B', '2B']: + this_def = '1b-2b' + else: + this_def = 'ss-p-c' + batter_to_base = await gb_result_12(this_play, this_def, interaction) + + elif playing_in and this_play.on_base_code in [3, 6]: + batter_to_base = await gb_decide( + this_play, interaction, await get_player(this_play.game, this_play.on_third), 3 + ) + + else: + batter_to_base = gb_result_3(this_play) else: - if view.value == 'gb A': - gb_code = 'a' - elif view.value == 'gb B': - gb_code = 'b' - else: - gb_code = 'c' + logging.info(f'no match; log out') + patch_play(this_play.id, pa=1, ab=1, outs=1) + advance_runners(this_play.id, 0) - await self.groundballs(interaction, this_game, this_play, gb_code) + complete_play(this_play.id, batter_to_base) else: view = ButtonOptions(responders=[interaction.user], labels=['fly A', 'fly B', 'fly C', None, None]) diff --git a/cogs/players.py b/cogs/players.py index 0710f91..b576bd6 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -298,15 +298,6 @@ class Players(commands.Cog): @tasks.loop(hours=18) async def build_player_list(self): - guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) - if not guild: - logging.error(f'Cannot access guild; pausing for 5 seconds') - await asyncio.sleep(5) - guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) - if not guild: - logging.error(f'Still cannot access guild; trying again in 18 hours') - return - all_players = await db_get('players', params=[('flat', True), ('inc_dex', False)], timeout=25) all_cardsets = await db_get('cardsets', params=[('flat', True)]) @@ -317,6 +308,10 @@ class Players(commands.Cog): self.cardset_list = [x['name'].lower() for x in all_cardsets['cardsets']] logging.info(f'There are now {len(self.cardset_list)} cardsets in the fuzzy search list.') + @build_player_list.before_loop + async def before_player_list(self): + await self.bot.wait_until_ready() + # def get_standings_embeds(self, current, which: str, title: str): # all_embeds = [ # discord.Embed(title=title), discord.Embed(title=title), discord.Embed(title=title),