Added live scorecard functionality

Added new pitcher embed highlighting
This commit is contained in:
Cal Corum 2025-02-05 11:30:56 -06:00
parent c9b5b45961
commit 282a7e7ac2
4 changed files with 78 additions and 7 deletions

View File

@ -1,5 +1,6 @@
import asyncio
import logging
import os
from typing import Literal
import discord
@ -17,7 +18,7 @@ from command_logic.logic_gameplay import bunts, chaos, complete_game, doubles, f
from dice import ab_roll
from exceptions import *
import gauntlets
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 helpers import CARDSETS, DEFENSE_LITERAL, PD_PLAYERS_ROLE_NAME, SELECT_CARDSET_OPTIONS, Dropdown, get_channel, send_to_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
@ -38,14 +39,77 @@ class Gameplay(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.sheets = None
self.game_states = {} # game_id: {play: <Current Play>, ack: <bool>}
self.get_sheets.start()
self.live_scorecard.start()
@tasks.loop(count=1)
async def get_sheets(self):
logger.info(f'Getting sheets')
self.sheets = pygsheets.authorize(service_file='storage/paper-dynasty-service-creds.json', retries=1)
@tasks.loop(minutes=2)
async def live_scorecard(self):
logger.info(f'Checking live scorecard loop')
guild = self.bot.get_guild(int(os.environ.get('GUILD_ID')))
score_channel = discord.utils.get(guild.text_channels, name='live-pd-scores')
if score_channel is None:
logger.error(f'Could not find live-pd-channel')
return
if len(self.game_states) == 0:
logger.info(f'No active game_states')
return
player_role = discord.utils.get(guild.roles, name=PD_PLAYERS_ROLE_NAME)
all_embeds = []
logger.info(f'player role: {player_role}')
with Session(engine) as session:
for key in self.game_states:
if not self.game_states[key]['ack']:
if not self.game_states[key]['play'].game.active:
logger.info(f'Game {self.game_states[key]["play"].game.id} is complete, removing from game_states')
del self.game_states[key]
else:
try:
logger.info(f'Appending scorebug for Game {self.game_states[key]["play"].game.id}')
this_channel = discord.utils.get(guild.text_channels, id=self.game_states[key]["play"].game.channel_id)
logger.info(f'this_channel: {this_channel}')
this_embed = await get_scorebug_embed(session, self.game_states[key]["play"].game, full_length=False, live_scorecard=True)
this_embed.set_image(url=None)
this_embed.insert_field_at(
index=0,
name='Ballpark',
value=f'{this_channel.mention}'
)
all_embeds.append(this_embed)
self.game_states[key]['ack'] = True
except Exception as e:
logger.error(f'Unable to add to game_states: {e}')
logger.error(f'Game: {self.game_states[key]["play"].game.id}')
if len(all_embeds) == 0:
logger.info(f'No active game embeds, returning')
await score_channel.set_permissions(player_role, read_messages=False)
return
async for message in score_channel.history(limit=25):
await message.delete()
await score_channel.set_permissions(player_role, read_messages=True)
await score_channel.send(content=None, embeds=all_embeds)
@live_scorecard.before_loop
async def before_live_scoreboard(self):
await self.bot.wait_until_ready()
@get_sheets.before_loop
async def before_get_sheets(self):
logger.info(f'Waiting to get sheets')
@ -157,6 +221,10 @@ class Gameplay(commands.Cog):
next_play = complete_play(session, this_play)
logger.info(f'Completed play {this_play.id}')
logger.info(f'Updating self.game_states')
self.game_states[this_play.game.id] = {'play': this_play, 'ack': False}
logger.info(f'New state: {self.game_states}')
await self.post_play(session, interaction, next_play, buffer_message)
group_new_game = app_commands.Group(name='new-game', description='Start a new baseball game')

View File

@ -46,7 +46,7 @@ RANGE_CHECKS = {
}
async def get_scorebug_embed(session: Session, this_game: Game, full_length: bool = True, classic: bool = True) -> discord.Embed:
async def get_scorebug_embed(session: Session, this_game: Game, full_length: bool = True, classic: bool = True, live_scorecard: bool = False) -> discord.Embed:
gt_string = ' - Unlimited'
if this_game.game_type == 'minor-league':
gt_string = ' - Minor League'
@ -85,6 +85,8 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
if curr_play.pitcher.is_fatigued:
embed_color = COLORS['red']
elif curr_play.pitcher.after_play == curr_play.play_num - 1:
embed_color = COLORS['white']
elif curr_play.is_new_inning:
embed_color = COLORS['yellow']
else:
@ -223,7 +225,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}, OT: {catcher_rating.overthrow}'
cat_string = f'{curr_play.catcher.player.name_card_link('batter')}\nArm: {"+" if pitchingcard.hold > 0 else ""}{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:
@ -244,7 +246,7 @@ async def get_scorebug_embed(session: Session, this_game: Game, full_length: boo
ai_note = def_align.ai_note
logger.info(f'gameplay_models - get_scorebug_embed - ai_note: {ai_note}')
if len(ai_note) > 0:
if len(ai_note) > 0 and not live_scorecard:
gm_name = this_game.home_team.gmname if this_game.ai_team == 'home' else this_game.away_team.gmname
embed.add_field(name=f'{gm_name} will...', value=ai_note, inline=False)
else:
@ -301,7 +303,7 @@ async def new_game_checks(session: Session, interaction: discord.Interaction, aw
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()
logger.error(f'Conflict creating a new game in channel: {interaction.channel.name}')
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}'
)

View File

@ -305,7 +305,8 @@ ACTIVE_EVENT_LITERAL = Literal['1998 Season', 'Brilliant Stars']
COLORS = {
'sba': int('a6ce39', 16),
'yellow': int('FFEA00', 16),
'red': int('C70039', 16)
'red': int('C70039', 16),
'white': int('FFFFFF', 16)
}
INSULTS = [
'Ugh, who even are you?',

View File

@ -999,7 +999,7 @@ def get_pitching_statline(session: Session, this_lineup: Lineup) -> str:
)).one()
if outs is None:
return '**N E W P I T C H E R**'
return '***N E W P I T C H E R***'
whole_innings = math.floor(outs / 3)
rem_outs = outs % 3