new_game_checks refactor

This commit is contained in:
Cal Corum 2025-01-24 10:08:11 -06:00
parent 03337255c3
commit 36dbde848e
3 changed files with 280 additions and 54 deletions

View File

@ -13,24 +13,25 @@ import sqlalchemy
from sqlmodel import func, or_
from api_calls import db_get
from command_logic.logic_gameplay import advance_runners, bunts, chaos, complete_game, doubles, flyballs, frame_checks, get_full_roster_from_sheets, get_lineups_from_sheets, checks_log_interaction, complete_play, get_scorebug_embed, groundballs, hit_by_pitch, homeruns, is_game_over, lineouts, manual_end_game, new_game_conflicts, popouts, read_lineup, show_defense_cards, singles, starting_pitcher_dropdown_view, steals, strikeouts, sub_batter_dropdown_view, triples, undo_play, update_game_settings, walks, xchecks, activate_last_play
from command_logic.logic_gameplay import bunts, chaos, complete_game, doubles, flyballs, frame_checks, get_full_roster_from_sheets, checks_log_interaction, complete_play, get_scorebug_embed, groundballs, hit_by_pitch, homeruns, is_game_over, lineouts, manual_end_game, new_game_checks, new_game_conflicts, popouts, read_lineup, show_defense_cards, singles, starting_pitcher_dropdown_view, steals, strikeouts, sub_batter_dropdown_view, triples, undo_play, update_game_settings, walks, xchecks, activate_last_play
from dice import ab_roll
from exceptions import GameNotFoundException, GoogleSheetsException, LineupsMissingException, TeamNotFoundException, PlayNotFoundException, GameException, log_exception
from exceptions import *
import gauntlets
from helpers import DEFENSE_LITERAL, PD_PLAYERS_ROLE_NAME, get_channel, team_role, user_has_role, random_gif, random_from_list
from helpers import CARDSETS, DEFENSE_LITERAL, PD_PLAYERS_ROLE_NAME, SELECT_CARDSET_OPTIONS, Dropdown, get_channel, team_role, user_has_role, random_gif, random_from_list
# from in_game import ai_manager
from in_game.ai_manager import get_starting_pitcher, get_starting_lineup
from in_game.game_helpers import PUBLIC_FIELDS_CATEGORY_NAME, legal_check
from in_game.gameplay_models import Lineup, Play, Session, engine, player_description, select, Game
from in_game.gameplay_queries import get_one_lineup, get_position, get_available_pitchers, get_channel_game_or_none, get_active_games_by_team, get_game_lineups, get_team_or_none, get_card_or_none
from in_game.gameplay_models import GameCardsetLink, Lineup, Play, Session, engine, player_description, select, Game
from in_game.gameplay_queries import get_cardset_or_none, get_one_lineup, get_position, get_channel_game_or_none, get_active_games_by_team, get_game_lineups, get_team_or_none
from utilities.buttons import Confirm, ScorebugButtons, ask_confirm
from utilities.dropdown import DropdownView, SelectStartingPitcher
from utilities.dropdown import DropdownView
CLASSIC_EMBED = True
logger = logging.getLogger('discord_app')
CLASSIC_EMBED = True
CARDSETS
class Gameplay(commands.Cog):
@ -172,34 +173,26 @@ class Gameplay(commands.Cog):
await interaction.response.defer()
with Session(engine) as session:
try:
await new_game_conflicts(session, interaction)
except GameException as e:
return
away_team = await get_team_or_none(session, team_abbrev=away_team_abbrev)
if away_team is None:
await interaction.edit_original_response(
content=f'Hm. I\'m not sure who **{away_team_abbrev}** is - check on that and try again!'
)
teams = await new_game_checks(session, interaction, away_team_abbrev, home_team_abbrev)
if teams is None:
logger.error(f'Received None from new_game_checks, cancelling new game')
return
home_team = await get_team_or_none(session, team_abbrev=home_team_abbrev)
if home_team is None:
await interaction.edit_original_response(
content=f'Hm. I\'m not sure who **{home_team_abbrev}** is - check on that and try again!'
)
return
if not away_team.is_ai ^ home_team.is_ai:
await interaction.edit_original_response(
content=f'I don\'t see an AI team in this MLB Campaign game. Run `/new-game mlb-campaign` again with an AI for a campaign game or `/new-game <ranked / unlimited>` for a PvP game.'
)
return
away_team = teams['away_team']
home_team = teams['home_team']
ai_team = away_team if away_team.is_ai else home_team
human_team = away_team if home_team.is_ai else home_team
conflict_games = get_active_games_by_team(session, team=human_team)
if len(conflict_games) > 0:
await interaction.edit_original_response(
content=f'Ope. The {human_team.sname} are already playing over in {interaction.guild.get_channel(conflict_games[0].channel_id).mention}'
)
return
conflict_games = get_active_games_by_team(session, team=human_team)
if len(conflict_games) > 0:
await interaction.edit_original_response(
@ -269,12 +262,12 @@ class Gameplay(commands.Cog):
final_message = await interaction.channel.send(
content=f'{ai_team.gmname} is filling out the {ai_team.sname} lineup card...'
)
logger.info(f'Pulling lineup in Game {this_game.id}')
logger.info(f'Pulling lineup...')
batter_lineups = await get_starting_lineup(
session,
team=ai_team,
game=this_game,
league_name=league.value,
league_name=this_game.league_name,
sp_name=ai_sp_lineup.player.name
)
@ -291,27 +284,26 @@ class Gameplay(commands.Cog):
# Commit game and lineups
session.add(this_game)
session.commit()
await final_message.edit(content=f'The {ai_team.sname} lineup is in, pulling in scouting data...')
for batter in batter_lineups:
if batter.position != 'DH':
await get_position(session, batter.card, batter.position)
logger.info(f'Pulling team roles')
away_role = await team_role(interaction, this_game.away_team)
home_role = await team_role(interaction, this_game.home_team)
# session.refresh(this_game)
away_role = await team_role(interaction, away_team)
home_role = await team_role(interaction, home_team)
logger.info(f'Building scorebug embed')
embed = await get_scorebug_embed(session, this_game)
embed.clear_fields()
embed.add_field(
name=f'{ai_team.abbrev} Lineup',
value=this_game.team_lineup(session, ai_team)
)
# Get pitchers from rosterlinks
logger.info(f'Pulling and caching full {human_team.abbrev} roster')
done = await get_full_roster_from_sheets(session, interaction, self.sheets, this_game, human_team, int(roster.value))
logger.info(f'done: {done}')
if done:
sp_view = starting_pitcher_dropdown_view(session, this_game, human_team, this_game.league_name, [interaction.user])
await interaction.channel.send(content=f'### {human_team.lname} Starting Pitcher', view=sp_view)
@ -510,7 +502,187 @@ class Gameplay(commands.Cog):
embed=embed
)
# TODO: add new-game exhibition, unlimited, and ranked
@group_new_game.command(name='exhibition', description='Start a new custom game against an AI')
@app_commands.choices(
roster=[
Choice(value='1', name='Primary'),
Choice(value='2', name='Secondary'),
Choice(value='3', name='Ranked')
]
)
@app_commands.checks.has_any_role(PD_PLAYERS_ROLE_NAME)
async def new_game_exhibition_command(self, interaction: discord.Interaction, away_team_abbrev: str, home_team_abbrev: str, roster: Choice[str], cardsets: Literal['Minor League', 'Major League', 'Hall of Fame', 'Flashback', 'Custom'] = 'Custom'):
await interaction.response.defer()
with Session(engine) as session:
teams = await new_game_checks(session, interaction, away_team_abbrev, home_team_abbrev)
if teams is None:
logger.error(f'Received None from new_game_checks, cancelling new game')
return
away_team = teams['away_team']
home_team = teams['home_team']
if not away_team.is_ai ^ home_team.is_ai:
await interaction.edit_original_response(
content=f'I don\'t see an AI team in this Exhibition game. Run `/new-game exhibition` again with an AI for a custom game or `/new-game <ranked / unlimited>` for a PvP game.'
)
return
current = await db_get('current')
week_num = current['week']
logger.info(f'gameplay - new_game_mlb_campaign - Season: {current["season"]} / Week: {week_num} / Away Team: {away_team.description} / Home Team: {home_team.description}')
ai_team = away_team if away_team.is_ai else home_team
human_team = away_team if home_team.is_ai else home_team
this_game = Game(
away_team_id=away_team.id,
home_team_id=home_team.id,
away_roster_id=69 if away_team.is_ai else int(roster.value),
home_roster_id=69 if home_team.is_ai else int(roster.value),
channel_id=interaction.channel_id,
season=current['season'],
week=week_num,
first_message=None if interaction.message is None else interaction.message.channel.id,
ai_team='away' if away_team.is_ai else 'home',
game_type='exhibition'
)
async def new_game_setup():
# Get AI SP
await interaction.edit_original_response(
content=f'{ai_team.gmname} is looking for a Starting Pitcher...'
)
ai_sp_lineup = await get_starting_pitcher(
session,
ai_team,
this_game,
True if home_team.is_ai else False,
'exhibition'
)
logger.info(f'Chosen SP: {ai_sp_lineup.player.name_with_desc}')
session.add(ai_sp_lineup)
await interaction.edit_original_response(
content=f'The {ai_team.sname} are starting **{ai_sp_lineup.player.name_with_desc}**:\n\n{ai_sp_lineup.player.pitcher_card_url}'
)
# Get AI Lineup
final_message = await interaction.channel.send(
content=f'{ai_team.gmname} is filling out the {ai_team.sname} lineup card...'
)
logger.info(f'Pulling lineup...')
batter_lineups = await get_starting_lineup(
session,
team=ai_team,
game=this_game,
league_name=this_game.league_name,
sp_name=ai_sp_lineup.player.name
)
for x in batter_lineups:
session.add(x)
# Check for last game settings
logger.info(f'Checking human team\'s automation preferences...')
g_query = session.exec(select(Game).where(or_(Game.home_team == human_team, Game.away_team == human_team)).order_by(Game.id.desc()).limit(1)).all()
if len(g_query) > 0:
last_game = g_query[0]
this_game.auto_roll = last_game.auto_roll
this_game.roll_buttons = last_game.roll_buttons
logger.info(f'Setting auto_roll to {last_game.auto_roll} and roll_buttons to {last_game.roll_buttons}')
# Commit game and lineups
session.add(this_game)
session.commit()
await final_message.edit(content=f'The {ai_team.sname} lineup is in, pulling in scouting data...')
for batter in batter_lineups:
if batter.position != 'DH':
await get_position(session, batter.card, batter.position)
logger.info(f'Pulling team roles')
away_role = await team_role(interaction, this_game.away_team)
home_role = await team_role(interaction, this_game.home_team)
logger.info(f'Building scorebug embed')
embed = await get_scorebug_embed(session, this_game)
embed.clear_fields()
embed.add_field(
name=f'{ai_team.abbrev} Lineup',
value=this_game.team_lineup(session, ai_team)
)
logger.info(f'Pulling and caching full {human_team.abbrev} roster')
done = await get_full_roster_from_sheets(session, interaction, self.sheets, this_game, human_team, int(roster.value))
if done:
sp_view = starting_pitcher_dropdown_view(session, this_game, human_team, this_game.league_name, [interaction.user])
await interaction.channel.send(content=f'### {human_team.lname} Starting Pitcher', view=sp_view)
await final_message.edit(
content=f'{away_role.mention} @ {home_role.mention} is set!\n\n'
f'Go ahead and set your lineup with the `/set lineup` command!',
embed=embed
)
if cardsets != 'Custom':
c_list = CARDSETS[cardsets]
for row in c_list['primary']:
this_cardset = await get_cardset_or_none(session, cardset_id=row)
if this_cardset is not None:
this_link = GameCardsetLink(
game=this_game,
cardset=this_cardset,
priority=1
)
session.add(this_link)
for row in c_list['secondary']:
this_cardset = await get_cardset_or_none(session, cardset_id=row)
this_link = GameCardsetLink(
game=this_game,
cardset=this_cardset,
priority=2
)
await new_game_setup()
else:
async def my_callback(interaction: discord.Interaction, values):
logger.info(f'Setting custom cardsets inside callback')
await interaction.response.defer(thinking=True)
logger.info(f'values: {values}')
for cardset_id in values:
logger.info(f'Getting cardset: {cardset_id}')
this_cardset = await get_cardset_or_none(session, cardset_id)
logger.info(f'this_cardset: {this_cardset}')
this_link = GameCardsetLink(
game=this_game,
cardset=this_cardset,
priority=1
)
session.add(this_link)
logger.info(f'Done processing links')
session.commit()
await interaction.edit_original_response(content="Got it...")
await new_game_setup()
my_dropdown = Dropdown(
option_list=SELECT_CARDSET_OPTIONS,
placeholder='Select up to 8 cardsets to include',
callback=my_callback,
max_values=len(SELECT_CARDSET_OPTIONS)
)
view = DropdownView([my_dropdown])
await interaction.edit_original_response(
content=None,
view=view
)
# TODO: add new-game unlimited, and ranked
@commands.command(name='force-endgame', help='Mod: Force a game to end without stats')
async def force_end_game_command(self, ctx: commands.Context):

View File

@ -6,17 +6,18 @@ import discord
from discord import SelectOption
from discord.app_commands import Choice
import pandas as pd
from sqlmodel import Session, select, func
from sqlmodel import Session, or_, select, func
from sqlalchemy import delete
from typing import Literal
from api_calls import db_delete, db_get, db_post
from dice import DTwentyRoll, d_twenty_roll, frame_plate_check, sa_fielding_roll
from exceptions import *
from helpers import COLORS, DEFENSE_LITERAL, SBA_COLOR, get_channel
from helpers import COLORS, DEFENSE_LITERAL, SBA_COLOR, get_channel, team_role
from in_game.ai_manager import get_starting_lineup
from in_game.game_helpers import PUBLIC_FIELDS_CATEGORY_NAME, 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_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.gameplay_queries import get_active_games_by_team, 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
@ -63,23 +64,16 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
curr_play = this_game.current_play_or_none(session)
if curr_play.pitcher.is_fatigued:
embed_color = COLORS['red']
elif curr_play.is_new_inning:
embed_color = COLORS['yellow']
else:
embed_color = COLORS['sba']
embed = discord.Embed(
title=f'{this_game.away_team.sname} @ {this_game.home_team.sname}{gt_string}',
color=embed_color
title=f'{this_game.away_team.sname} @ {this_game.home_team.sname}{gt_string}'
)
if curr_play is None:
logger.info(f'There is no play in game {this_game.id}, trying to initialize play')
try:
curr_play = this_game.initialize_play(session)
except LineupsMissingException as e:
logger.debug(f'get_scorebug_embed - Could not initialize play')
logger.info(f'get_scorebug_embed - Could not initialize play')
if curr_play is not None:
embed.add_field(
@ -89,6 +83,14 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
)
logger.info(f'curr_play: {curr_play}')
if curr_play.pitcher.is_fatigued:
embed_color = COLORS['red']
elif curr_play.is_new_inning:
embed_color = COLORS['yellow']
else:
embed_color = COLORS['sba']
embed.color = embed_color
def steal_string(batting_card: BattingCard) -> str:
steal_string = '-/- (---)'
if batting_card.steal_jump > 0:
@ -258,6 +260,7 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
value=this_game.team_lineup(session, this_game.home_team)
)
else:
logger.info(f'There is no play in game {this_game.id}, posting lineups')
embed.add_field(
name=f'{this_game.away_team.abbrev} Lineup',
value=this_game.team_lineup(session, this_game.away_team)
@ -270,9 +273,57 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
return embed
async def new_game_checks(session: Session, interaction: discord.Interaction, away_team_abbrev: str, home_team_abbrev: str):
try:
logger.info(f'Checking for game conflicts in {interaction.channel.name}')
await new_game_conflicts(session, interaction)
except GameException as e:
return None
logger.info(f'Getting teams')
away_team = await get_team_or_none(session, team_abbrev=away_team_abbrev)
if away_team is None:
logger.error(f'Away team not found')
await interaction.edit_original_response(
content=f'Hm. I\'m not sure who **{away_team_abbrev}** is - check on that and try again!'
)
return None
home_team = await get_team_or_none(session, team_abbrev=home_team_abbrev)
if home_team is None:
logger.error(f'Home team not found')
await interaction.edit_original_response(
content=f'Hm. I\'m not sure who **{home_team_abbrev}** is - check on that and try again!'
)
return None
human_team = away_team if home_team.is_ai else home_team
logger.info(f'Checking for other team games')
conflict_games = get_active_games_by_team(session, team=human_team)
if len(conflict_games) > 0:
logger.error()
await interaction.edit_original_response(
content=f'Ope. The {human_team.sname} are already playing over in {interaction.guild.get_channel(conflict_games[0].channel_id).mention}'
)
return None
if interaction.user.id not in [away_team.gmid, home_team.gmid]:
await interaction.edit_original_response(
content='You can only start a new game if you GM one of the teams.'
)
return None
return {
'away_team': away_team,
'home_team': home_team
}
def starting_pitcher_dropdown_view(session: Session, this_game: Game, human_team: Team, game_type: str = None, responders: list[discord.User] = None):
pitchers = get_available_pitchers(session, this_game, human_team, sort='starter-desc')
logger.info(f'sorted pitchers: {pitchers}')
if len(pitchers) == 0:
log_exception(MissingRosterException, 'No pitchers were found to select SP')
sp_selection = SelectStartingPitcher(
this_game=this_game,
this_team=human_team,
@ -503,6 +554,8 @@ def complete_play(session:Session, this_play: Play):
outs = session.exec(select(func.sum(Play.outs)).where(
Play.game == this_play.game, Play.pitcher == new_pitcher, Play.complete == True
)).one()
if outs is None:
outs = 0
pow_outs = new_pitcher.card.pitcherscouting.pitchingcard.starter_rating * 3
if outs >= pow_outs and not new_pitcher.is_fatigued:
new_pitcher.is_fatigued = True

View File

@ -43,6 +43,7 @@ CARDSETS = {
},
'Hall of Fame': {
'primary': [x for x in range(1, MAX_CARDSET_ID)],
'secondary': [],
'human': ranked_cardsets
},
'Flashback': {