Added undo_subs to the /log undo-play
This commit is contained in:
parent
ace530c2bd
commit
7f9a92e654
@ -26,7 +26,7 @@ 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_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_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_last_inning_end_play, patch_lineup, \
|
||||||
get_last_game_ids, get_plays, get_manager, get_one_game, load_ai, ai_batting
|
get_last_game_ids, get_plays, get_manager, get_one_game, load_ai, ai_batting, undo_subs
|
||||||
|
|
||||||
|
|
||||||
class Gameplay(commands.Cog):
|
class Gameplay(commands.Cog):
|
||||||
@ -3004,12 +3004,13 @@ class Gameplay(commands.Cog):
|
|||||||
last_completed = get_latest_play(this_game.id)
|
last_completed = get_latest_play(this_game.id)
|
||||||
logging.debug(f'Undoing play {last_completed.id} in Game {this_game.id}')
|
logging.debug(f'Undoing play {last_completed.id} in Game {this_game.id}')
|
||||||
undo_play(this_game.id)
|
undo_play(this_game.id)
|
||||||
|
|
||||||
latest_play = get_latest_play(this_game.id)
|
|
||||||
logging.debug(f'Latest completed play is Play {latest_play.id}; batter_to_base: {latest_play.batter_final}')
|
|
||||||
except AttributeError as e:
|
except AttributeError as e:
|
||||||
logging.error(f'Could not undo second play in game {this_game.id}')
|
logging.error(f'Could not undo second play in game {this_game.id}')
|
||||||
|
|
||||||
|
latest_play = get_latest_play(this_game.id)
|
||||||
|
logging.debug(f'Latest completed play is Play {latest_play.id}; batter_to_base: {latest_play.batter_final}')
|
||||||
|
undo_subs(this_game, latest_play.play_num)
|
||||||
|
|
||||||
await interaction.edit_original_response(
|
await interaction.edit_original_response(
|
||||||
content=None, embed=await self.get_game_state_embed(this_game, full_length=False)
|
content=None, embed=await self.get_game_state_embed(this_game, full_length=False)
|
||||||
)
|
)
|
||||||
|
|||||||
@ -549,6 +549,7 @@ class Lineup(BaseModel):
|
|||||||
position = CharField()
|
position = CharField()
|
||||||
batting_order = IntegerField()
|
batting_order = IntegerField()
|
||||||
after_play = IntegerField()
|
after_play = IntegerField()
|
||||||
|
replacing_id = IntegerField(null=True)
|
||||||
active = BooleanField(default=True)
|
active = BooleanField(default=True)
|
||||||
|
|
||||||
|
|
||||||
@ -561,6 +562,7 @@ class StratLineup:
|
|||||||
position: str
|
position: str
|
||||||
batting_order: int
|
batting_order: int
|
||||||
after_play: int
|
after_play: int
|
||||||
|
replacing_id: int = None
|
||||||
active: bool = True
|
active: bool = True
|
||||||
card_id: int = None
|
card_id: int = None
|
||||||
|
|
||||||
@ -656,12 +658,15 @@ def post_lineups(lineups: list):
|
|||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
def patch_lineup(lineup_id, active: Optional[bool] = None, position: Optional[str] = None) -> StratLineup:
|
def patch_lineup(lineup_id, active: Optional[bool] = None, position: Optional[str] = None,
|
||||||
|
replacing_id: Optional[int] = None) -> StratLineup:
|
||||||
this_lineup = Lineup.get_by_id(lineup_id)
|
this_lineup = Lineup.get_by_id(lineup_id)
|
||||||
if active is not None:
|
if active is not None:
|
||||||
this_lineup.active = active
|
this_lineup.active = active
|
||||||
if position is not None:
|
if position is not None:
|
||||||
this_lineup.position = position
|
this_lineup.position = position
|
||||||
|
if replacing_id is not None:
|
||||||
|
this_lineup.replacing_id = replacing_id
|
||||||
this_lineup.save()
|
this_lineup.save()
|
||||||
|
|
||||||
# return_lineup = model_to_dict(this_lineup)
|
# return_lineup = model_to_dict(this_lineup)
|
||||||
@ -700,6 +705,7 @@ def make_sub(lineup_dict: dict):
|
|||||||
|
|
||||||
if subbed_player:
|
if subbed_player:
|
||||||
subbed_player = patch_lineup(subbed_player.id, active=False)
|
subbed_player = patch_lineup(subbed_player.id, active=False)
|
||||||
|
lineup_dict['replacing_id'] = subbed_player.id
|
||||||
|
|
||||||
new_lineup = Lineup.create(**lineup_dict)
|
new_lineup = Lineup.create(**lineup_dict)
|
||||||
# return_lineup = model_to_dict(new_lineup)
|
# return_lineup = model_to_dict(new_lineup)
|
||||||
@ -732,6 +738,23 @@ def make_sub(lineup_dict: dict):
|
|||||||
return return_value
|
return return_value
|
||||||
|
|
||||||
|
|
||||||
|
def undo_subs(game: StratGame, new_play_num: int):
|
||||||
|
logging.info(f'get new players')
|
||||||
|
new_players = Lineup.select().where((Lineup.game_id == game.id) & (Lineup.after_play > new_play_num))
|
||||||
|
logging.info(f'new_player count: {new_players.count()}')
|
||||||
|
replacements = [(x.id, x.replacing_id) for x in new_players]
|
||||||
|
|
||||||
|
for x in replacements:
|
||||||
|
logging.info(f'replacing {x[0]} with {x[1]}')
|
||||||
|
old_player = get_one_lineup(game_id=game.id, lineup_id=x[1])
|
||||||
|
logging.info(f'old_player: {old_player}')
|
||||||
|
patch_lineup(old_player.id, active=True)
|
||||||
|
logging.info(f'activated!')
|
||||||
|
|
||||||
|
logging.info(f'done activating old players')
|
||||||
|
Lineup.delete().where((Lineup.game_id == game.id) & (Lineup.after_play > new_play_num)).execute()
|
||||||
|
|
||||||
|
|
||||||
async def get_player(game, lineup_member) -> dict:
|
async def get_player(game, lineup_member) -> dict:
|
||||||
if isinstance(game, Game):
|
if isinstance(game, Game):
|
||||||
if game.is_pd:
|
if game.is_pd:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user