diff --git a/cogs/admins.py b/cogs/admins.py index cbc3e8d..1c924e7 100644 --- a/cogs/admins.py +++ b/cogs/admins.py @@ -1,7 +1,5 @@ -import copy import csv - -import discord +import json import db_calls from helpers import * @@ -186,6 +184,48 @@ class Admins(commands.Cog): await ctx.send(random_conf_gif()) + @app_commands.command(name='add-player-card', description='Mod: Manually upload a new PD card') + @app_commands.checks.has_any_role('Da Commish') + async def new_manual_card_slash( + self, interaction: discord.Interaction, player_type: Literal['batter', 'pitcher'], player_json: str, + bc_or_pc_json: str, position_list: str, ratings_vl_json: str, ratings_vr_json: str): + await interaction.response.defer() + + try: + d_player = json.loads(player_json) + except json.decoder.JSONDecodeError as e: + await interaction.edit_original_response(content=f'RIP. Failed to process that player.') + return + + try: + d_bcpc = json.loads(bc_or_pc_json) + except json.decoder.JSONDecodeError as e: + await interaction.edit_original_response(content=f'RIP. Failed to process that {player_type} card.') + return + + try: + d_positions = json.loads(position_list) + except json.decoder.JSONDecodeError as e: + await interaction.edit_original_response(content=f'RIP. Failed to process the position data.') + return + + try: + d_ratings_vl = json.loads(ratings_vl_json) + except json.decoder.JSONDecodeError as e: + await interaction.edit_original_response(content=f'RIP. Failed to process the vL ratings.') + return + + try: + d_ratings_vr = json.loads(ratings_vr_json) + except json.decoder.JSONDecodeError as e: + await interaction.edit_original_response(content=f'RIP. Failed to process the vR ratings.') + return + + logging.info(f'Data gathered:\n\n{d_player}\n\n{d_bcpc}\n\n{d_positions}\n\n{d_ratings_vl}\n\n{d_ratings_vr}') + + await interaction.edit_original_response( + content='Just spit out the debug info to the log, but processing was successful!') + @app_commands.command(name='reset-image', description='Force a refresh of a player\'s card images') async def reset_image(self, interaction: discord.Interaction, player_id: int): if not owner_only(interaction): diff --git a/cogs/gameplay.py b/cogs/gameplay.py index 012c14b..4e3e030 100644 --- a/cogs/gameplay.py +++ b/cogs/gameplay.py @@ -28,7 +28,8 @@ from db_calls_gameplay import StratGame, StratPlay, StratLineup, StratManagerAi, 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_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, get_dbready_plays + get_last_game_ids, get_plays, get_manager, get_one_game, load_ai, ai_batting, undo_subs, get_dbready_plays, \ + convert_stratlineup class Gameplay(commands.Cog): @@ -2773,61 +2774,90 @@ class Gameplay(commands.Cog): if False in (this_game, owner_team, this_play): return - if this_play is None: - this_card = await db_get(f'cards', object_id=int(new_player)) - if this_card["team"]["id"] != owner_team['id']: - raise SyntaxError(f'Easy there, champ. Looks like card ID {new_player} belongs to the ' - f'{this_card["team"]["sname"]}. Try again with only cards you own.') - player_id = this_card['player']['player_id'] - card_id = new_player - after_play = 0 + this_card = await db_get(f'cards', object_id=int(new_player)) + if this_card["team"]["id"] != owner_team['id']: + raise SyntaxError(f'Easy there, champ. Looks like card ID {new_player} belongs to the ' + f'{this_card["team"]["sname"]}. Try again with only cards you own.') + player_id = this_card['player']['player_id'] + card_id = new_player + new_post = False - old_pit_order = 10 - if batting_order is None: - batting_order = 10 + # Check for simple position change + in_lineup = get_one_lineup(this_game.id, team_id=owner_team['id'], active=True, card_id=card_id) + if in_lineup is not None: + new_pitcher = patch_lineup(in_lineup.id, position='P') + + view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') + q_text = f'It looks like you are forfeiting the DH by moving {player_desc(this_card["player"])} to ' \ + f'the mound. __You will not be able to undo this sub even with the undo-play command__ so real ' \ + f'quick: are you sure?' + question = await interaction.channel.send(q_text, view=view) + await view.wait() + + if view.value: + await question.edit(content=f'~~{q_text}~~\nFuck the DH \U0000270A', view=None) else: - batting_order = int(batting_order) - - else: - if this_play.pitcher.team_id != owner_team['id']: - await interaction.edit_original_response( - content='It looks like your team is batting right now - ' - 'please make this sub once you take the field.') + await question.delete() + await interaction.edit_original_response(content=f'~~{q_text}~~\nI will hold off for now.') + await interaction.channel.send( + content=None, + embed=await self.get_game_state_embed(this_game) + ) return - this_card = await db_get(f'cards', object_id=int(new_player)) - if this_card["team"]["id"] != owner_team['id']: - raise SyntaxError(f'Easy there, champ. Looks like card ID {new_player} belongs to the ' - f'{this_card["team"]["sname"]}. Try again with only cards you own.') - player_id = this_card['player']['player_id'] - card_id = new_player + old_pitcher = get_pitcher(this_game, this_play) + patch_lineup(old_pitcher.id, active=False) + patch_play(this_play.id, pitcher_id=new_pitcher.id) + this_play.pitcher = new_pitcher + else: + if this_play is None: + after_play = 0 + + old_pit_order = 10 + if batting_order is None: + batting_order = 10 + else: + batting_order = int(batting_order) - old_pit_order = this_play.pitcher.batting_order - if batting_order is None: - batting_order = old_pit_order else: - batting_order = int(batting_order) + if this_play.pitcher.team_id != owner_team['id']: + await interaction.edit_original_response( + content='It looks like your team is batting right now - ' + 'please make this sub once you take the field.') + return - after_play = this_play.play_num - 1 + old_pit_order = this_play.pitcher.batting_order + if batting_order is None: + batting_order = old_pit_order + else: + batting_order = int(batting_order) - this_lineup = { - 'game_id': this_game.id, - 'team_id': owner_team['id'], - 'player_id': player_id, - 'card_id': card_id, - 'position': 'P', - 'batting_order': batting_order, - 'after_play': after_play - } + after_play = this_play.play_num - 1 - make_sub(this_lineup) - if old_pit_order != batting_order: - patch_lineup(this_play.pitcher.id, active=False) + this_lineup = { + 'game_id': this_game.id, + 'team_id': owner_team['id'], + 'player_id': player_id, + 'card_id': card_id, + 'position': 'P', + 'batting_order': batting_order, + 'after_play': after_play + } - await interaction.edit_original_response( - content=None, - embed=await self.get_game_state_embed(this_game) - ) + make_sub(this_lineup) + if old_pit_order != batting_order: + patch_lineup(this_play.pitcher.id, active=False) + + if new_post: + await interaction.channel.send( + content=None, + embed=await self.get_game_state_embed(this_game) + ) + else: + await interaction.edit_original_response( + content=None, + embed=await self.get_game_state_embed(this_game) + ) @commands.hybrid_command(name='gamestate', help='Post the current game state', aliases=['gs']) @commands.has_any_role(SBA_PLAYERS_ROLE_NAME, PD_PLAYERS_ROLE_NAME) @@ -3910,7 +3940,7 @@ class Gameplay(commands.Cog): playing_in = False batter_to_base = None - if runner_on_third(this_play): + if runner_on_third(this_play) and gb_type not in ['FO', 'PO']: view = Confirm(responders=[interaction.user], timeout=60, label_type='yes') question = await interaction.channel.send( f'Was the defender playing in?', view=view diff --git a/db_calls_gameplay.py b/db_calls_gameplay.py index e0b6aab..e9ce6cf 100644 --- a/db_calls_gameplay.py +++ b/db_calls_gameplay.py @@ -650,13 +650,16 @@ db.create_tables([Lineup]) def get_one_lineup( game_id: int, lineup_id: int = None, team_id: int = None, batting_order: int = None, position: str = None, - active: bool = True, as_obj: bool = False) -> Optional[StratLineup]: - if not batting_order and not position and not lineup_id: - raise KeyError(f'One of batting_order, position, or lineup_id must not be None') + card_id: int = None, active: bool = True, as_obj: bool = False) -> Optional[StratLineup]: + if not batting_order and not position and not lineup_id and not card_id: + raise KeyError(f'One of batting_order, position, card_id , or lineup_id must not be None') if lineup_id: this_lineup = Lineup.get_by_id(lineup_id) - + elif card_id: + this_lineup = Lineup.get_or_none( + Lineup.game_id == game_id, Lineup.card_id == card_id, Lineup.active == active + ) elif batting_order: this_lineup = Lineup.get_or_none( Lineup.game_id == game_id, Lineup.team_id == team_id, Lineup.batting_order == batting_order, diff --git a/gameplay_helpers.py b/gameplay_helpers.py index e5f84a9..65e88a2 100644 --- a/gameplay_helpers.py +++ b/gameplay_helpers.py @@ -355,7 +355,8 @@ def gb_result_9(this_play: StratPlay) -> Optional[int]: def gb_result_10(this_play: StratPlay) -> Optional[int]: logging.info(f'GB 10') - patch_play(this_play.id, pa=1, ab=1, outs=2, on_third_final=False) + num_outs = 2 if this_play.starting_outs <= 1 else 1 + patch_play(this_play.id, pa=1, ab=1, outs=num_outs, on_third_final=False) advance_one_runner(this_play.id, from_base=2, num_bases=1) advance_one_runner(this_play.id, from_base=1, num_bases=1) @@ -457,7 +458,8 @@ async def gb_result_12(this_play: StratPlay, defender: Literal['1b-2b', '3b', 's def gb_result_13(this_play: StratPlay, defender: Literal['c-3b', 'else']) -> Optional[int]: logging.info(f'GB 13') if defender == 'c-3b': - patch_play(this_play.id, on_second_final=False, on_first_final=False, pa=1, ab=1, outs=2) + num_outs = 2 if this_play.starting_outs <= 1 else 1 + patch_play(this_play.id, on_second_final=False, on_first_final=False, pa=1, ab=1, outs=num_outs) return 1 else: return gb_result_2(this_play)