From e8df6fc4c32cdc0b5a839ad7ba5df3d95a83a8a9 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sun, 22 Oct 2023 22:40:50 -0500 Subject: [PATCH] RE24 calc added & post-game embed complete --- cogs/gameplay.py | 141 +++++++++++++++++++++++++++++++++++++++---- db_calls_gameplay.py | 37 +++++++++--- 2 files changed, 156 insertions(+), 22 deletions(-) diff --git a/cogs/gameplay.py b/cogs/gameplay.py index 417febf..18438b7 100644 --- a/cogs/gameplay.py +++ b/cogs/gameplay.py @@ -26,7 +26,7 @@ from db_calls import db_get, db_patch, db_post, db_delete, get_team_by_abbrev from db_calls_gameplay import StratGame, StratPlay, StratLineup, StratManagerAi, get_sba_team, get_sba_player, \ post_game, patch_game, get_game_team, post_lineups, make_sub, get_player, player_link, get_team_lineups, \ 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_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 @@ -188,15 +188,14 @@ class Gameplay(commands.Cog): # Post Team Choice packs if this_game.ai_team is not None and not this_game.short_game and 'gauntlet' not in this_game.game_type and \ losing_team['is_ai']: - r_query = await db_get( - 'results', + g_query = await db_get( + 'games', params=[ - ('team_one_id', winning_team['id']), ('team_two_id', losing_team['id']), - ('game_type', this_game.game_type), ('season', this_game.season) + ('team1_id', human_team['id']), ('game_type', this_game.game_type), ('season', this_game.season) ] ) wins = 0 - for x in r_query['results']: + for x in g_query['games']: if (x['away_score'] > x['home_score'] and x['away_team']['id'] == human_team['id']) or ( x['home_score'] > x['away_score'] and x['home_team']['id'] == human_team['id']): wins += 1 @@ -211,7 +210,7 @@ class Gameplay(commands.Cog): } ) - if r_query['count'] > 0: + if g_query['count'] > 0: if this_game.game_type == 'minor-league' and wins % 6 == 0: await post_tc_pack() win_string += f'1x {losing_team["abbrev"]} Team Choice pack\n' @@ -1747,8 +1746,12 @@ class Gameplay(commands.Cog): ) return - away_team = await db_get('teams', object_id=this_game.away_team_id) - home_team = await db_get('teams', object_id=this_game.home_team_id) + gs = await db_get(f'plays/game-summary/{final_game["id"]}') + await db_patch('games', object_id=gs['game']['id'], + params=[('away_score', gs['runs']['away']), ('home_score', gs['runs']['home'])]) + + away_team = gs['teams']['away'] + home_team = gs['teams']['home'] winning_team = away_team if latest_play.away_score > latest_play.home_score else home_team losing_team = away_team if latest_play.away_score < latest_play.home_score else home_team @@ -1758,6 +1761,7 @@ class Gameplay(commands.Cog): loss_reward = r_data['loss_string'] # Post a notification to PD + logging.debug(f'getting inning') inning = f'{latest_play.inning_num if latest_play.inning_half == "Bot" else latest_play.inning_num - 1}' embed = get_team_embed( f'{away_team["lname"]} {latest_play.away_score} @ {latest_play.home_score} {home_team["lname"]} - F/' @@ -1765,6 +1769,14 @@ class Gameplay(commands.Cog): winning_team ) + logging.debug(f'setting location') + embed.add_field( + name='Location', + value=f'{interaction.guild.get_channel(this_game.channel_id).mention}', + inline=False + ) + + logging.debug(f'getting league name') if this_game.game_type == 'major-league': game_des = 'Major League' elif this_game.game_type == 'minor-league': @@ -1779,14 +1791,118 @@ class Gameplay(commands.Cog): game_des = 'Unlimited' embed.description = f'Score Report - {game_des} ' \ f'{"- 3-Inning Game" if this_game.short_game else " - 9-Inning Game"}' + logging.debug(f'building box score') embed.add_field( name='Box Score', - value=f'```\nTeam | R | H | E |\n```', + value=f'```\n' + f'Team | R | H | E |\n' + f'{away_team["abbrev"].replace("Gauntlet-", ""): <4} | {gs["runs"]["away"]: >2} | ' + f'{gs["hits"]["away"]: >2} | {gs["errors"]["away"]: >2} |\n' + f'{home_team["abbrev"].replace("Gauntlet-", ""): <4} | {gs["runs"]["home"]: >2} | ' + f'{gs["hits"]["home"]: >2} | {gs["errors"]["home"]: >2} |\n' + f'\n```' + ) + + logging.debug(f'getting potg string') + tp = gs["top-players"][0] + potg_string = f'{player_desc(tp["player"])} - ' + if 'hr' in tp: + potg_string += f'{tp["hit"]}-{tp["ab"]}' + if tp['hr'] > 0: + num = f'{tp["hr"]} ' if tp["hr"] > 1 else "" + potg_string += f', {num}HR' + if tp['triple'] > 0: + num = f'{tp["triple"]} ' if tp["triple"] > 1 else "" + potg_string += f', {num}3B' + if tp['double'] > 0: + num = f'{tp["double"]} ' if tp["double"] > 1 else "" + potg_string += f', {num}2B' + if tp['run'] > 0: + potg_string += f', {tp["run"]} R' + if tp['rbi'] > 0: + potg_string += f', {tp["rbi"]} RBI' + else: + potg_string = f'{player_desc(tp["player"])} - {tp["ip"]} IP, {tp["run"]} R' + if tp['run'] != tp['e_run']: + potg_string += f' ({tp["e_run"]} ER)' + potg_string += f', {tp["hit"]} H, {tp["so"]} K' + potg_string += f', {tp["re24"]:.2f} re24' + embed.add_field( + name='Player of the Game', + value=potg_string, inline=False ) + + logging.info(f'potg: {potg_string}') + logging.debug(f'getting pitcher string') + pit_string = f'Win: {gs["pitchers"]["win"]["p_name"]}\n' \ + f'Loss: {gs["pitchers"]["loss"]["p_name"]}\n' + if gs['pitchers']['save'] is not None: + pit_string += f'Save: {player_desc(gs["pitchers"]["save"])}' embed.add_field( - name='Location', - value=f'{interaction.guild.get_channel(this_game.channel_id).mention}' + name=f'Pitching', + value=pit_string, + ) + + def name_list(raw_list: list) -> str: + logging.info(f'raw_list: {raw_list}') + player_dict = {} + for x in raw_list: + if x['player_id'] not in player_dict: + player_dict[x['player_id']] = x + + data_dict = {} + for x in raw_list: + if x['player_id'] not in data_dict: + data_dict[x['player_id']] = 1 + else: + data_dict[x['player_id']] += 1 + + r_string = '' + logging.info(f'players: {player_dict} / data: {data_dict}') + + first = True + for p_id in data_dict: + r_string += f'{", " if not first else ""}{player_dict[p_id]["p_name"]}' + if data_dict[p_id] > 1: + r_string += f' {data_dict[p_id]}' + first = False + + return r_string + + logging.info(f'getting running string') + if len(gs['running']['sb']) + len(gs['running']['csc']) > 0: + run_string = '' + if len(gs['running']['sb']) > 0: + run_string += f'SB: {name_list(gs["running"]["sb"])}\n' + + if len(gs['running']['csc']) > 0: + run_string += f'CSc: {name_list(gs["running"]["csc"])}' + + embed.add_field( + name=f'Baserunning', + value=run_string + ) + + logging.info(f'getting xbh string') + if len(gs['xbh']['2b']) + len(gs['xbh']['3b']) + len(gs['xbh']['hr']) > 0: + bat_string = '' + if len(gs['xbh']['2b']) > 0: + bat_string += f'2B: {name_list(gs["xbh"]["2b"])}\n' + + if len(gs['xbh']['3b']) > 0: + bat_string += f'3B: {name_list(gs["xbh"]["3b"])}\n' + + if len(gs['xbh']['hr']) > 0: + bat_string += f'HR: {name_list(gs["xbh"]["hr"])}\n' + else: + bat_string = 'Oops! All bitches! No XBH from either team.' + + logging.info(f'building embed') + embed.add_field( + name='Batting', + value=bat_string, + inline=False ) embed.add_field( name=f'{winning_team["abbrev"]} Rewards', @@ -1801,6 +1917,7 @@ class Gameplay(commands.Cog): value=f'Please share the highlights in {get_channel(interaction, "pd-news-ticker").mention}!', inline=False ) + logging.info(f'sending scorebug') await send_to_channel(self.bot, 'pd-network-news', embed=embed) # Gauntlet results and reward diff --git a/db_calls_gameplay.py b/db_calls_gameplay.py index 5622b19..3acc6b2 100644 --- a/db_calls_gameplay.py +++ b/db_calls_gameplay.py @@ -1490,6 +1490,7 @@ def complete_play(play_id, batter_to_base: int = None): add_run_last_player_ab(this_play.on_third, this_play.error == 0) this_play.save() + # for runner in [this_play.on_first_final, this_play.on_second_final, this_play.on_third_final]: # if runner == 4: # score_increment += 1 @@ -1600,6 +1601,22 @@ def complete_play(play_id, batter_to_base: int = None): this_play.is_go_ahead = 1 this_play.save() + # RE24 Calc + re_data = { + 0: [0.457, 0.231, 0.077], + 1: [0.793, 0.438, 0.171], + 2: [1.064, 0.596, 0.259], + 4: [1.373, 0.772, 0.351], + 3: [1.340, 0.874, 0.287], + 5: [1.687, 1.042, 0.406], + 6: [1.973, 1.311, 0.448], + 7: [2.295, 1.440, 0.618] + } + start_re24 = re_data[this_play.on_base_code][this_play.starting_outs] + end_re24 = 0 if this_play.starting_outs + this_play.outs == 3 else re_data[new_obc][new_starting_outs] + this_play.re24 = end_re24 - start_re24 + score_increment + this_play.save() + batter = get_one_lineup(this_play.game.id, team_id=new_bteam_id, batting_order=new_batting_order) batter_id = batter.id if batter else None pitcher = get_one_lineup(this_play.game_id, team_id=new_pteam_id, position='P') @@ -2282,16 +2299,16 @@ def get_pitching_decisions(game: StratGame, db_game_id: int): # if next_p_first.away_score < next_p_first.home_score: -def get_final_scorebug(away_team, home_team, away_score, home_score): - return f'```' \ - f'Team | R | H | E |\n' \ - f'{away_team["abbrev"].replace("Gauntlet-", ""): <4} | {away_stats["score"]: >2} | ' \ - f'{away_stats["hits"]: >2} | ' \ - f'{away_stats["f_lines"][0]["tm_error"] if away_stats["f_lines"] else 0: >2} |\n' \ - f'{home_team["abbrev"].replace("Gauntlet-", ""): <4} | {home_stats["score"]: >2} | ' \ - f'{home_stats["hits"]: >2} | ' \ - f'{home_stats["f_lines"][0]["tm_error"] if home_stats["f_lines"] else 0: >2} |\n' \ - f'```' +# def get_final_scorebug(away_team, home_team, away_score, home_score): +# return f'```' \ +# f'Team | R | H | E |\n' \ +# f'{away_team["abbrev"].replace("Gauntlet-", ""): <4} | {away_stats["score"]: >2} | ' \ +# f'{away_stats["hits"]: >2} | ' \ +# f'{away_stats["f_lines"][0]["tm_error"] if away_stats["f_lines"] else 0: >2} |\n' \ +# f'{home_team["abbrev"].replace("Gauntlet-", ""): <4} | {home_stats["score"]: >2} | ' \ +# f'{home_stats["hits"]: >2} | ' \ +# f'{home_stats["f_lines"][0]["tm_error"] if home_stats["f_lines"] else 0: >2} |\n' \ +# f'```' db.close()