diff --git a/cogs/gameplay.py b/cogs/gameplay.py index 15dfe1d..2dba184 100644 --- a/cogs/gameplay.py +++ b/cogs/gameplay.py @@ -18,14 +18,14 @@ from dice import sa_fielding_roll from helpers import SBA_PLAYERS_ROLE_NAME, PD_PLAYERS_ROLE_NAME, random_conf_gif, SBA_SEASON, PD_SEASON, IMAGES, \ get_team_embed, Confirm, get_pos_abbrev, SBA_COLOR, get_roster_lineups, Question, give_packs, send_to_channel, \ get_channel, get_or_create_role, team_role, get_cal_user, get_card_embeds, ButtonOptions, get_ratings_guide, \ - get_team_by_owner, get_roster_sheet + get_team_by_owner, get_roster_sheet, get_role from gameplay_helpers import * from db_calls import db_get, db_patch, db_post, db_delete, get_team_by_abbrev from db_calls_gameplay import StratGame, StratPlay, StratLineup, StratManagerAi, get_sba_team, get_sba_player, \ post_game, patch_game, get_game_team, post_lineups, make_sub, get_player, player_link, get_team_lineups, \ get_current_play, post_play, get_one_lineup, advance_runners, patch_play, complete_play, get_batting_stats, \ get_pitching_stats, undo_play, get_latest_play, advance_one_runner, count_team_games, get_final_scorebug, \ - get_fielding_stats, get_pitching_decisions, get_or_create_bullpen, get_last_inning_end_play, patch_lineup, \ + get_fielding_stats, get_pitching_decisions, get_or_create_bullpen, get_active_games, patch_lineup, \ get_last_game_ids, get_plays, get_manager, get_one_game, load_ai, ai_batting, undo_subs @@ -36,7 +36,7 @@ class Gameplay(commands.Cog): self.pitcher_ratings = None self.live_scoreboard.start() - @tasks.loop(minutes=5) + @tasks.loop(minutes=3) async def live_scoreboard(self): guild = self.bot.get_guild(int(os.environ.get('GUILD_ID'))) if not guild: @@ -47,76 +47,42 @@ class Gameplay(commands.Cog): logging.error(f'Still cannot access guild; trying again in 10 minutes') return - all_games = get_last_game_ids(6) + score_channel = discord.utils.get(guild.text_channels, name='live-pd-scores') + player_role = discord.utils.get(guild.roles, name=PD_PLAYERS_ROLE_NAME) - games_active = False - for game_id in all_games: - g_query = get_one_game(game_id) - if g_query.active: - games_active = True + active_games = get_active_games(6) + if len(active_games) == 0: + await score_channel.set_permissions(player_role, read_messages=False) + return - if games_active: - embed = get_team_embed(f'Live Scoreboard') - for game_id in all_games: - try: - gs = await self.get_game_state(get_one_game(game_id=game_id)) - logging.debug(f'curr_play: {gs["curr_play"]}') - g_message = gs['scorebug'] + 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 not gs['curr_play'].game.active: - g_dec = get_pitching_decisions(gs['curr_play'].game) - winner = await get_player(gs["curr_play"].game, g_dec["w_lineup"]) - loser = await get_player(gs["curr_play"].game, g_dec["l_lineup"]) - if g_dec['s_lineup']: - saver = await get_player(gs['curr_play'].game, g_dec['s_lineup']) - s_string = f'Save: {saver["name"]}\n' \ - f'Location: {guild.get_channel(gs["channel_id"]).mention}\n' \ - f'\n-------------------------' - else: - s_string = f'Location: {guild.get_channel(gs["channel_id"]).mention}\n' \ - f'-------------------------' + if valid_scores: + async for message in score_channel.history(limit=25): + await message.delete() - g_message += f'Win: {winner["name"]}\n' \ - f'Loss: {loser["name"]}\n' \ - f'{s_string}' - else: - games_active = True - g_message += f'Pitcher: {gs["pitcher"]["name"]}\n' \ - f'Batter: {gs["batter"]["name"]}\n' \ - f'Location: {guild.get_channel(gs["channel_id"]).mention}\n' \ - f'-------------------------' - - embed.add_field( - name=f'{gs["away_team"]["sname"]} @ {gs["home_team"]["sname"]}', - value=g_message, - inline=False - ) - - except Exception as e: - logging.error( - f'Skipping Game {game_id} in scoreboard: {e}' - ) - - current = await db_get('current') - - try: - s_channel = self.bot.get_channel(int(os.environ.get('SCOREBOARD_CHANNEL'))) - message_id = current['live_scoreboard'] - if message_id == 0: - s_message = await send_to_channel( - self.bot, - 'live-scoreboard', - content=None, - embed=embed - ) - await db_patch('current', object_id=current['id'], params=[('live_scoreboard', s_message.id)]) - - else: - s_message = await s_channel.fetch_message(message_id) - await s_message.edit(content=None, embed=embed) - - except Exception as e: - logging.error(f'Unable to post scoreboard: {e}') + 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 @tasks.loop(hours=36) async def update_ratings_guides(self): @@ -1548,34 +1514,36 @@ class Gameplay(commands.Cog): get_pitching_decisions(this_game) await ctx.send(random_conf_gif()) - @commands.hybrid_command(name='endgame', help='End game in this channel') - @commands.has_any_role(SBA_PLAYERS_ROLE_NAME, PD_PLAYERS_ROLE_NAME) - async def end_game_command(self, ctx: commands.Context): - this_game = get_one_game(channel_id=ctx.channel.id, active=True) + @app_commands.command(name='endgame', description='End game in this channel') + @app_commands.checks.has_any_role(PD_PLAYERS_ROLE_NAME) + async def end_game_command(self, interaction: discord.Interaction): + await interaction.response.defer() + + this_game = get_one_game(channel_id=interaction.channel.id, active=True) if not this_game: - await ctx.send(f'Ope, I don\'t see a game in this channel.') + await interaction.edit_original_response(content='Ope, I don\'t see a game in this channel.') return logging.info(f'Ending Game {this_game.id}') - response = await ctx.send(f'Let\'s see what we\'ve got here...') + response = await interaction.edit_original_response(content=f'Let\'s see what we\'ve got here...') - owner_team = await get_game_team(this_game, ctx.author.id) + owner_team = await get_game_team(this_game, interaction.user.id) gauntlet_team = None if 'gauntlet' in this_game.game_type: gauntlet_team = await get_game_team(this_game, team_abbrev=f'Gauntlet-{owner_team["abbrev"]}') if not {owner_team['id'], gauntlet_team['id']}.intersection( [this_game.away_team_id, this_game.home_team_id]): - await ctx.send('Bruh. Only GMs of the active teams can end games.') + await interaction.edit_original_response(content='Bruh. Only GMs of the active teams can end games.') return elif not owner_team['id'] in [this_game.away_team_id, this_game.home_team_id] and \ - ctx.author.id != self.bot.owner_id: - await ctx.send('Bruh. Only GMs of the active teams can end games.') + interaction.user.id != self.bot.owner_id: + 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)) - view = Confirm(responders=[ctx.author], timeout=60, label_type='yes') - question = await ctx.send(f'Should I end this game?', view=view) + 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() if view.value: @@ -1628,8 +1596,9 @@ class Gameplay(commands.Cog): bad_player = await get_player(this_game, line) logging.error(f'Unable to process stats for card_id {line.card_id} in Game {this_game.id}: ' f'{type(e)}: {e}') - await ctx.send(f'I was not able to process stats for {bad_player["name"]} - {get_cal_user(ctx).mention}' - f' help!') + await interaction.edit_original_response( + content=f'I was not able to process stats for {bad_player["name"]} - ' + f'{get_cal_user(interaction).mention} help!') home_pitchers = await get_team_lineups( this_game.id, team_id=home_team['id'], inc_inactive=True, pitchers_only=True, as_string=False @@ -1656,8 +1625,10 @@ class Gameplay(commands.Cog): bad_player = await get_player(this_game, line) logging.error(f'Unable to process stats for card_id {line.card_id} in Game {this_game.id}: ' f'{type(e)}: {e}') - await ctx.send(f'I was not able to process stats for {bad_player["name"]} - {get_cal_user(ctx).mention}' - f' help!') + await interaction.edit_original_response( + content=f'I was not able to process stats for {bad_player["name"]} - ' + f'{get_cal_user(interaction).mention} help!' + ) logging.debug(f'finished tallying pitcher stats') @@ -1666,8 +1637,10 @@ class Gameplay(commands.Cog): decisions = get_pitching_decisions(this_game) except AttributeError as e: logging.error(f'Could not pull decisions for Game {this_game.id}: {e}') - await ctx.send(f'I was not able to calculate the Winning and Losing Pitcher for this game. Is the game ' - f'ending early? {get_cal_user(ctx).mention} can probably help.') + await interaction.edit_original_response( + content=f'I was not able to calculate the Winning and Losing Pitcher for this game. Is the game ' + f'ending early? {get_cal_user(interaction).mention} can probably help.' + ) return logging.debug(f'decisions: {decisions}') @@ -1887,7 +1860,7 @@ class Gameplay(commands.Cog): ) embed.add_field( name='Location', - value=f'{ctx.guild.get_channel(this_game.channel_id).mention}' + value=f'{interaction.guild.get_channel(this_game.channel_id).mention}' ) wc_query = await db_get("cards", object_id=decisions["winner"]) lc_query = await db_get("cards", object_id=decisions["loser"]) @@ -1926,7 +1899,7 @@ class Gameplay(commands.Cog): ) embed.add_field( name='Highlights', - value=f'Please share the highlights in {get_channel(ctx, "pd-news-ticker").mention}!', + value=f'Please share the highlights in {get_channel(interaction, "pd-news-ticker").mention}!', inline=False ) await send_to_channel(self.bot, 'pd-network-news', embed=embed) @@ -1938,7 +1911,7 @@ class Gameplay(commands.Cog): is_win=winning_team['gmid'] == gauntlet_team['gmid'], this_team=gauntlet_team, bot=self.bot, - ctx=ctx + channel=interaction.channel ) this_run = await db_get('gauntletruns', object_id=int(this_game.game_type.split('-')[3])) @@ -1955,7 +1928,10 @@ class Gameplay(commands.Cog): logging.info(f'Game {this_game.id} is complete') if gauntlet_team is None: - await ctx.send(f'Good game! Go share the highlights in {get_channel(ctx, "pd-news-ticker").mention}!') + await interaction.edit_original_response( + content=f'Good game! Go share the highlights in ' + f'{get_channel(interaction, "pd-news-ticker").mention}!' + ) @app_commands.command( name='readlineup', diff --git a/db_calls.py b/db_calls.py index d796814..56f7e20 100644 --- a/db_calls.py +++ b/db_calls.py @@ -6,12 +6,10 @@ import os AUTH_TOKEN = {'Authorization': f'Bearer {os.environ.get("API_TOKEN")}'} DB_URL = 'https://pd.manticorum.com/api' master_debug = True -alt_database = None +alt_database = True -if alt_database == 'prod': - DB_URL = 'https://sombaseball.ddns.net/pd/api' -elif alt_database == 'local': - DB_URL = 'https://manticorum.ddns.net/pd/api' +if alt_database == 'dev': + DB_URL = 'https://pddev.manticorum.com/api' def param_char(other_params): diff --git a/db_calls_gameplay.py b/db_calls_gameplay.py index deda314..189b9ed 100644 --- a/db_calls_gameplay.py +++ b/db_calls_gameplay.py @@ -543,6 +543,14 @@ def get_last_game_ids(num_games: int = 10) -> list: return game_list +def get_active_games(limit: int = 10) -> list: + g_query = Game.select().where(Game.active) + if limit < 0: + limit = 1 + g_query = g_query.limit(limit) + return [x for x in g_query] + + class Lineup(BaseModel): game = ForeignKeyField(Game) team_id = IntegerField() diff --git a/gauntlets.py b/gauntlets.py index 2d60a5c..e8b2de2 100644 --- a/gauntlets.py +++ b/gauntlets.py @@ -790,7 +790,7 @@ async def get_embed(this_run=None, this_event=None, this_team=None): return embed -async def post_result(run_id: int, is_win: bool, this_team, bot, ctx): +async def post_result(run_id: int, is_win: bool, this_team, bot, channel): this_run = await db_get('gauntletruns', object_id=run_id) this_event = await db_get('events', object_id=this_run['gauntlet']['id']) t_query = await db_get('teams', params=[('abbrev', f'{this_team["abbrev"].replace("Gauntlet-","")}')]) @@ -819,7 +819,7 @@ async def post_result(run_id: int, is_win: bool, this_team, bot, ctx): reward_string += f'- 1x {x["reward"]["pack_type"]["name"]} Pack' if this_run['wins'] == 10: - choas_role = await get_or_create_role(ctx, "CHOAS ALERT", mentionable=True) + choas_role = await get_or_create_role(channel.guild, "CHOAS ALERT", mentionable=True) await send_to_channel( bot, 'pd-network-news', @@ -836,9 +836,9 @@ async def post_result(run_id: int, is_win: bool, this_team, bot, ctx): if len(reward_string) > 0: final_message += f"You earned the following rewards:\n{reward_string}" - final_message += f'\n\nGo share the highlights in {get_channel(ctx, "pd-news-ticker").mention}!' + final_message += f'\n\nGo share the highlights in {get_channel(channel, "pd-news-ticker").mention}!' - await ctx.send( + await channel.send( content=final_message, embed=await get_embed(this_run) ) @@ -854,7 +854,7 @@ async def post_result(run_id: int, is_win: bool, this_team, bot, ctx): l_message += 'That\'s the end of this run - better luck next time!' c_query = await db_post(f'cards/wipe-team/{this_team["id"]}') - await ctx.send( + await channel.send( content=l_message, embed=await get_embed(this_run) )