Fixed chaos button bug

Added batting statline to batter & OT to catchers
This commit is contained in:
Cal Corum 2024-12-25 16:10:24 -06:00
parent 9dbd7896c4
commit f40347e0bb
6 changed files with 85 additions and 65 deletions

View File

@ -15,7 +15,7 @@ from exceptions import *
from helpers import DEFENSE_LITERAL, SBA_COLOR, get_channel
from in_game.game_helpers import legal_check
from in_game.gameplay_models import BattingCard, Game, Lineup, PositionRating, RosterLink, Team, Play
from in_game.gameplay_queries import get_available_batters, get_batter_card, get_position, get_available_pitchers, get_card_or_none, get_channel_game_or_none, get_db_ready_decisions, get_db_ready_plays, get_game_lineups, get_last_team_play, get_one_lineup, get_player_id_from_dict, get_player_name_from_dict, get_player_or_none, get_sorted_lineups, get_team_or_none, get_players_last_pa, post_game_rewards
from in_game.gameplay_queries import get_available_batters, get_batter_card, get_batting_statline, get_pitching_statline, get_position, get_available_pitchers, get_card_or_none, get_channel_game_or_none, get_db_ready_decisions, get_db_ready_plays, get_game_lineups, get_last_team_play, get_one_lineup, get_player_id_from_dict, get_player_name_from_dict, get_player_or_none, get_sorted_lineups, get_team_or_none, get_players_last_pa, post_game_rewards
from in_game.managerai_responses import DefenseResponse
from utilities.buttons import ButtonOptions, Confirm, ask_confirm
from utilities.dropdown import DropdownView, SelectBatterSub, SelectStartingPitcher, SelectViewDefense
@ -170,7 +170,6 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
battingcard = curr_play.batter.card.batterscouting.battingcard
pit_string = f'{pitchingcard.hand.upper()}HP | {curr_play.pitcher.player.name_card_link('pitching')}'
bat_string = f'{curr_play.batter.batting_order}. {battingcard.hand.upper()} | {curr_play.batter.player.name_card_link('batting')}'
if len(baserunner_string) > 0:
logger.info(f'Adding pitcher hold to scorebug')
pitchingcard = curr_play.pitcher.card.pitcherscouting.pitchingcard
@ -179,14 +178,18 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
# battingcard = curr_play.batter.card.batterscouting.battingcard
# bat_string += f'\nBunt: {battingcard.bunting}, HnR: {battingcard.hit_and_run}'
pit_string += f'\n{get_pitching_statline(session, curr_play.pitcher)}'
embed.add_field(
name='Pitcher',
value=pit_string
)
bat_string = f'{curr_play.batter.batting_order}. {battingcard.hand.upper()} | {curr_play.batter.player.name_card_link('batting')}\n{get_batting_statline(session, curr_play.batter)}'
embed.add_field(
name='Batter',
value=bat_string
)
logger.info(f'Setting embed image to batter card')
embed.set_image(url=curr_play.batter.player.batter_card_url)
@ -201,7 +204,7 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
else:
log_exception(PositionNotFoundException, f'No catcher rating found for {curr_play.catcher.card.player.name}')
cat_string = f'{curr_play.catcher.player.name_card_link('batter')}\nArm: {catcher_rating.arm}, PB: {catcher_rating.pb}'
cat_string = f'{curr_play.catcher.player.name_card_link('batter')}\nArm: {catcher_rating.arm}, PB: {catcher_rating.pb}, OT: {catcher_rating.overthrow}'
embed.add_field(name='Catcher', value=cat_string)
if curr_play.ai_is_batting and curr_play.on_base_code > 0:

View File

@ -2812,7 +2812,8 @@ def ab_roll(this_team: Team, this_game: Game, allow_chaos: bool = True) -> AbRol
logger.info(f'Creating chaos roll')
this_roll = AbRoll(
d_twenty=d_twenty_two,
roll_message=f'```md\n# {d_twenty_two}\nDetails: [1d20 ({d_twenty_two})]```\n'
roll_message=f'```md\n# {d_twenty_two}\nDetails: [1d20 ({d_twenty_two})]```\n',
is_chaos=True
)
logger.info(f'Getting dice embed for this_roll: {this_roll}')
embed = get_dice_embed(this_team)

View File

@ -314,6 +314,7 @@ class ManagerAi(ManagerAiBase, table=True):
catcher_defense = session.exec(select(PositionRating).where(PositionRating.player_id == this_play.catcher.player_id, PositionRating.position == 'C', PositionRating.variant == this_play.catcher.card.variant)).one()
catcher_hold = catcher_defense.arm
battery_hold = pitcher_hold + catcher_hold
logger.info(f'game state: {num_outs} outs, {run_diff} run diff, battery_hold: {battery_hold}')
if to_base == 2:
runner = this_play.on_first
@ -638,63 +639,6 @@ class ManagerAi(ManagerAiBase, table=True):
return this_resp
# @property
# def ai_note(self) -> str: # TODO: test these three functions with specific OBCs
# if self.inning_half == 'top':
# if self.game.ai_team == 'away':
# return self.batting_ai_note
# else:
# return self.pitching_ai_note
# else:
# if self.game.ai_team == 'away':
# return self.pitching_ai_note
# else:
# return self.batting_ai_note
# @property
# def batting_ai_note(self) -> str:
# ai_note = '' # TODO: migrate Manager AI to their own local model
# return ai_note
# @property
# def pitching_ai_note(self) -> str:
# def_alignment = self.defense_alignment(session)
# ai_note = ''
# # Holding Baserunners
# if self.starting_outs == 2 and self.on_base_code > 0:
# if self.on_base_code == 1:
# ai_note += f'- hold {self.on_first.player.name}\n'
# elif self.on_base_code == 2:
# ai_note += f'- hold {self.on_second.player.name}\n'
# elif self.on_base_code in [4, 5, 7]:
# ai_note += f'- hold {self.on_first.player.name} on first\n'
# # elif self.on_base_code == 5:
# # ai_note += f'- hold the runner on first\n'
# elif self.on_base_code == 6:
# ai_note += f'- hold {self.on_second.player.name} on 2nd\n'
# elif self.on_base_code in [1, 5]:
# runner = self.on_first.player
# if self.on_first.card.batterscouting.battingcard.steal_auto:
# ai_note += f'- hold {runner.name} on 1st\n'
# elif self.on_base_code in [2, 4]:
# if self.on_second.card.batterscouting.battingcard.steal_low + max(self.pitcher.card.pitcherscouting.pitchingcard.hold, 5) >= 14:
# ai_note += f'- hold {self.on_second.player.name} on 2nd\n'
# # Defensive Alignment
# if self.on_third and self.starting_outs < 2:
# if self.could_walkoff:
# ai_note += f'- play the outfield and infield in'
# elif abs(self.away_score - self.home_score) <= 3:
# ai_note += f'- play the whole infield in\n'
# else:
# ai_note += f'- play the corners in\n'
# if len(ai_note) == 0 and self.on_base_code > 0:
# ai_note += f'- play straight up\n'
# return ai_note
class CardsetBase(SQLModel):
id: int | None = Field(default=None, primary_key=True)
@ -1019,8 +963,6 @@ class Lineup(SQLModel, table=True):
def uppercase_strings(cls, value: str) -> str:
return value.upper()
# TODO: add function to return string value of game stats
class PlayBase(SQLModel):
id: int | None = Field(default=None, primary_key=True)

View File

@ -864,3 +864,76 @@ def get_batter_card(this_card: Card = None, this_lineup: Lineup = None) -> Batti
logger.info(f'Getting batter card for {this_lineup.player.name}')
return this_lineup.card.batterscouting.battingcard
log_exception(KeyError, 'Either a Card or Lineup must be provided to get_batter_card')
def get_batting_statline(session: Session, this_lineup: Lineup) -> str:
logger.info(f'Getting batting statline for {this_lineup.player.name} in Game {this_lineup.game.id}')
at_bats = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.batter == this_lineup, Play.ab == 1, Play.complete == True
)).one()
hits = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.batter == this_lineup, Play.hit == 1
)).one()
bat_string = f'{hits}-{at_bats}'
logger.info(f'at-bat bat_string: {bat_string}')
homeruns = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.batter == this_lineup, Play.homerun == 1
)).one()
if homeruns > 0:
number_string = f'{homeruns} ' if homeruns > 1 else ""
bat_string += f', {number_string}HR'
triples = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.batter == this_lineup, Play.triple == 1
)).one()
if triples > 0:
number_string = f'{triples} ' if triples > 1 else ""
bat_string += f', {number_string}3B'
doubles = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.batter == this_lineup, Play.double == 1
)).one()
if doubles > 0:
number_string = f'{doubles} ' if doubles > 1 else ""
bat_string += f', {number_string}2B'
stolenbases = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.runner == this_lineup, Play.sb == 1
)).one()
if stolenbases > 0:
number_string = f'{stolenbases} ' if stolenbases > 1 else ""
bat_string += f', {number_string}SB'
walks = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.batter == this_lineup, Play.bb == 1
)).one()
if walks > 0:
number_string = f'{walks} ' if walks > 1 else ""
bat_string += f', {number_string}BB'
strikeouts = session.exec(select(func.count(Play.id)).where(
Play.game == this_lineup.game, Play.batter == this_lineup, Play.so == 1
)).one()
if strikeouts > 0:
number_string = f'{strikeouts} ' if strikeouts > 1 else ""
bat_string += f', {number_string}K'
logger.info(f'bat_string: {bat_string}')
if bat_string == '0-0':
return '1st AB'
else:
return bat_string
def get_pitching_statline(session: Session, this_lineup: Lineup) -> str:
logger.info(f'Getting pitching statline for {this_lineup.player.name} in Game {this_lineup.game.id}')
outs = session.exec(select(func.sum(Play.outs)).where(
Play.game == this_lineup.game, Play.pitcher == this_lineup, Play.complete == True
)).one()
## TODO: complete pitching statline

View File

@ -334,7 +334,7 @@ class ScorebugButtons(discord.ui.View):
async def button_ab(self, interaction: discord.Interaction, button: discord.ui.Button):
logger.info(f'User {interaction.user.id} rolling AB in Game {self.play.game.id}')
this_roll = ab_roll(self.team, self.play.game, allow_chaos=not self.had_chaos)
this_roll = ab_roll(self.team, self.play.game, allow_chaos=not self.had_chaos and self.play.on_base_code > 0)
logger.info(f'this_roll: {this_roll}')
if this_roll.is_chaos:
logger.info('AB Roll Is Chaos')

View File

@ -144,7 +144,7 @@ class SelectStartingPitcher(discord.ui.Select):
await interaction.response.edit_message(
content=f'The {self.team.lname} are starting {human_sp_card.player.name_with_desc}',
content=f'The {self.team.lname} are starting **{human_sp_card.player.name_with_desc}**!\n\nRun `/read-lineup` when you are ready to begin.',
view=None
)
@ -290,6 +290,7 @@ class SelectBatterSub(discord.ui.Select):
view=view
)
class SelectPokemonEvolution(discord.ui.Select):
def __init__(self, *, placeholder = 'Evolve the selected Pokemon', min_values = 1, max_values = 1, options = List[SelectOption], this_team):
logging.info(f'Inside SelectPokemonEvolution init function')