From fe06773c2042039902cbd42e8625d77fafd3e211 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sun, 30 Apr 2023 14:26:15 -0500 Subject: [PATCH] Update gameplay.py Add Team Choice packs with pre-selected teams to /open-packs --- cogs/gameplay.py | 548 ++++++++++++++++++++++++++--------------------- 1 file changed, 299 insertions(+), 249 deletions(-) diff --git a/cogs/gameplay.py b/cogs/gameplay.py index a98d06d..1309f80 100644 --- a/cogs/gameplay.py +++ b/cogs/gameplay.py @@ -169,6 +169,71 @@ class Gameplay(commands.Cog): return game_string + async def post_rewards(self, winning_team: dict, losing_team: dict, this_game: StratGame): + wr_query = db_get( + 'gamerewards', params=[('name', f'{"Short" if this_game.short_game else "Full"} Game Win')]) + lr_query = db_get( + 'gamerewards', params=[('name', f'{"Short" if this_game.short_game else "Full"} Game Loss')]) + if not wr_query['count'] or not lr_query['count']: + raise KeyError(f'Game Rewards were not found. Leaving this game active.') + + human_team = winning_team if losing_team['is_ai'] else losing_team + ai_team = winning_team if winning_team['is_ai'] else losing_team + + win_reward = wr_query['gamerewards'][0] + loss_reward = lr_query['gamerewards'][0] + win_string = f'1x {win_reward["pack_type"]["name"]} Pack\n' + + # Post Team Choice packs + if this_game.ai_team is not None and not this_game.short_game and losing_team['is_ai']: + r_query = db_get( + 'results', + params=[ + ('team_one_id', winning_team['id']), ('team_two_id', losing_team['id']), + ('game_type', this_game.game_type), ('season', this_game.season) + ] + ) + wins = 0 + for x in r_query['results']: + 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 + + def post_tc_pack(): + db_post( + 'packs/one', + payload={ + 'team_id': human_team['id'], + 'pack_type_id': 8, + 'pack_team_id': losing_team['id'] + } + ) + + if this_game.game_type == 'minor-league' and wins % 6 == 0: + post_tc_pack() + win_string += f'1x {losing_team["abbrev"]} Team Choice pack\n' + elif this_game.game_type == 'major-league' and wins % 4 == 0: + post_tc_pack() + win_string += f'1x {losing_team["abbrev"]} Team Choice pack\n' + elif this_game.game_type == 'hall-of-fame' and wins % 2 == 0: + post_tc_pack() + win_string += f'1x {losing_team["abbrev"]} Team Choice pack\n' + + win_string += f'{win_reward["money"]}₼\n' + loss_string = f'{loss_reward["money"]}₼\n' + + # Post rewards + give_packs(winning_team, num_packs=1, pack_type=win_reward['pack_type']) + db_post(f'teams/{winning_team["id"]}/money/{win_reward["money"]}') + db_post(f'teams/{losing_team["id"]}/money/{loss_reward["money"]}') + + data = { + 'win_string': win_string, + 'loss_string': loss_string + } + + return data + async def get_game_state(self, game: StratGame) -> dict: away_team = await get_game_team(game, team_id=game.away_team_id) home_team = await get_game_team(game, team_id=game.home_team_id) @@ -1569,17 +1634,14 @@ class Gameplay(commands.Cog): return logging.debug(f'decisions: {decisions}') - wr_query = db_get( - 'gamerewards', params=[('name', f'{"Short" if this_game.short_game else "Full"} Game Win')]) - lr_query = db_get( - 'gamerewards', params=[('name', f'{"Short" if this_game.short_game else "Full"} Game Loss')]) - if not wr_query['count'] or not lr_query['count']: - raise KeyError(f'Game Rewards were not found. Leaving this game active.') - win_reward = wr_query['gamerewards'][0] - loss_reward = lr_query['gamerewards'][0] winning_team = away_team if away_stats['score'] > home_stats['score'] else home_team losing_team = away_team if away_stats['score'] < home_stats['score'] else home_team + # Post Game Rewards + r_data = await self.post_rewards(winning_team, losing_team, this_game) + win_reward = r_data['win_string'] + loss_reward = r_data['loss_string'] + # Check for Gauntlet game so rewards go to main team if gauntlet_team is not None: if winning_team['gmid'] == gauntlet_team['gmid']: @@ -1591,256 +1653,250 @@ class Gameplay(commands.Cog): logging.debug(f'home_stats (in /endgame function)\n\n{home_stats}') logging.debug(f'winning_team: {winning_team}\nlosing_team: {losing_team}') - if this_game.is_pd: - logging.debug(f'Time to build statlines and submit the scorecard for this PD game!') - # Post result - success = db_post( - 'results', - payload={ - 'away_team_id': this_game.away_team_id, - 'home_team_id': this_game.home_team_id, - 'away_score': away_stats['score'], - 'home_score': home_stats['score'], - 'away_team_ranking': away_team['ranking'], - 'home_team_ranking': home_team['ranking'], - 'scorecard': f'Bot Game {this_game.id}', + logging.debug(f'Time to build statlines and submit the scorecard for this PD game!') + # Post result + success = db_post( + 'results', + payload={ + 'away_team_id': this_game.away_team_id, + 'home_team_id': this_game.home_team_id, + 'away_score': away_stats['score'], + 'home_score': home_stats['score'], + 'away_team_ranking': away_team['ranking'], + 'home_team_ranking': home_team['ranking'], + 'scorecard': f'Bot Game {this_game.id}', + 'week': this_game.week_num, + 'season': this_game.season, + 'ranked': this_game.ranked, + 'short_game': this_game.short_game, + 'game_type': this_game.game_type + } + ) + # Submit the stats + batter_stats = [] + pitcher_stats = [] + doubles = '' + triples = '' + homers = '' + s_bases = '' + caught_s = '' + + for line in [*away_stats['b_lines'], *home_stats['b_lines']]: + if line['pl_double']: + if len(doubles): + doubles += ', ' + doubles += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ + f'{" " if line["pl_double"] > 1 else ""}' \ + f'{line["pl_double"] if line["pl_double"] > 1 else ""}' + if line['pl_triple']: + if len(triples): + triples += ', ' + triples += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ + f'{" " if line["pl_triple"] > 1 else ""}' \ + f'{line["pl_triple"] if line["pl_triple"] > 1 else ""}' + if line['pl_homerun']: + if len(homers): + homers += ', ' + homers += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ + f'{" " if line["pl_homerun"] > 1 else ""}' \ + f'{line["pl_homerun"] if line["pl_homerun"] > 1 else ""}' + if line['pl_sb']: + if len(s_bases): + s_bases += ', ' + s_bases += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ + f'{" " if line["pl_sb"] > 1 else ""}' \ + f'{line["pl_sb"] if line["pl_sb"] > 1 else ""}' + batter_stats.append( + { + 'card_id': line['card_id'], + 'team_id': line['team_id'], + 'roster_num': + this_game.away_roster_num if this_game.away_team_id == line['team_id'] + else this_game.home_roster_num, + 'vs_team_id': + home_team['id'] if this_game.away_team_id == line['team_id'] + else away_team['id'], + 'pos': line['pos'], + 'pa': line['pl_pa'], + 'ab': line['pl_ab'], + 'run': line['pl_run'], + 'rbi': line['pl_rbi'], + 'hit': line['pl_hit'], + 'double': line['pl_double'], + 'triple': line['pl_triple'], + 'hr': line['pl_homerun'], + 'bb': line['pl_bb'], + 'so': line['pl_so'], + 'hbp': line['pl_hbp'], + 'sac': line['pl_sac'], + 'ibb': line['pl_ibb'], + 'gidp': line['pl_gidp'], + 'sb': line['pl_sb'], + 'cs': line['pl_cs'], + 'bphr': line['pl_bphr'], + 'bpfo': line['pl_bpfo'], + 'bp1b': line['pl_bp1b'], + 'bplo': line['pl_bplo'], 'week': this_game.week_num, 'season': this_game.season, - 'ranked': this_game.ranked, - 'short_game': this_game.short_game, - 'game_type': this_game.game_type + 'game_id': this_game.id, } ) - # Submit the stats - batter_stats = [] - pitcher_stats = [] - doubles = '' - triples = '' - homers = '' - s_bases = '' - caught_s = '' - for line in [*away_stats['b_lines'], *home_stats['b_lines']]: - if line['pl_double']: - if len(doubles): - doubles += ', ' - doubles += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ - f'{" " if line["pl_double"] > 1 else ""}' \ - f'{line["pl_double"] if line["pl_double"] > 1 else ""}' - if line['pl_triple']: - if len(triples): - triples += ', ' - triples += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ - f'{" " if line["pl_triple"] > 1 else ""}' \ - f'{line["pl_triple"] if line["pl_triple"] > 1 else ""}' - if line['pl_homerun']: - if len(homers): - homers += ', ' - homers += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ - f'{" " if line["pl_homerun"] > 1 else ""}' \ - f'{line["pl_homerun"] if line["pl_homerun"] > 1 else ""}' - if line['pl_sb']: - if len(s_bases): - s_bases += ', ' - s_bases += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ - f'{" " if line["pl_sb"] > 1 else ""}' \ - f'{line["pl_sb"] if line["pl_sb"] > 1 else ""}' - batter_stats.append( - { - 'card_id': line['card_id'], - 'team_id': line['team_id'], - 'roster_num': - this_game.away_roster_num if this_game.away_team_id == line['team_id'] - else this_game.home_roster_num, - 'vs_team_id': - home_team['id'] if this_game.away_team_id == line['team_id'] - else away_team['id'], - 'pos': line['pos'], - 'pa': line['pl_pa'], - 'ab': line['pl_ab'], - 'run': line['pl_run'], - 'rbi': line['pl_rbi'], - 'hit': line['pl_hit'], - 'double': line['pl_double'], - 'triple': line['pl_triple'], - 'hr': line['pl_homerun'], - 'bb': line['pl_bb'], - 'so': line['pl_so'], - 'hbp': line['pl_hbp'], - 'sac': line['pl_sac'], - 'ibb': line['pl_ibb'], - 'gidp': line['pl_gidp'], - 'sb': line['pl_sb'], - 'cs': line['pl_cs'], - 'bphr': line['pl_bphr'], - 'bpfo': line['pl_bpfo'], - 'bp1b': line['pl_bp1b'], - 'bplo': line['pl_bplo'], - 'week': this_game.week_num, - 'season': this_game.season, - 'game_id': this_game.id, - } - ) - - for line in [*away_stats['f_lines'], *home_stats['f_lines']]: - if line['pl_csc']: - if len(caught_s): - caught_s += ', ' - caught_s += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ - f'{" " if line["pl_csc"] > 1 else ""}' \ - f'{line["pl_csc"] if line["pl_csc"] > 1 else ""}' - batter_stats.append( - { - 'card_id': line['card_id'], - 'team_id': line['team_id'], - 'roster_num': - this_game.away_roster_num if this_game.away_team_id == line['team_id'] - else this_game.home_roster_num, - 'vs_team_id': - home_team['id'] if this_game.away_team_id == line['team_id'] - else away_team['id'], - 'pos': line['pos'], - 'xch': line['pl_xch'], - 'xhit': line['pl_xhit'], - 'error': line['pl_error'], - 'pb': line['pl_pb'], - 'sbc': line['pl_sbc'], - 'csc': line['pl_csc'], - 'week': this_game.week_num, - 'season': this_game.season, - 'game_id': this_game.id - } - ) - - for line in [*away_stats['p_lines'], *home_stats['p_lines']]: - pitcher_stats.append( - { - 'card_id': line['card_id'], - 'team_id': line['team_id'], - 'roster_num': - this_game.away_roster_num if this_game.away_team_id == line['team_id'] - else this_game.home_roster_num, - 'vs_team_id': - home_team['id'] if this_game.away_team_id == line['team_id'] - else away_team['id'], - 'ip': (math.floor(line['pl_outs'] / 3) * 1.0) + ((line['pl_outs'] % 3) / 3.0), - 'hit': line['pl_hit'], - 'run': line['pl_runs'], - 'erun': line['pl_eruns'], - 'so': line['pl_so'], - 'bb': line['pl_bb'], - 'hbp': line['pl_hbp'], - 'wp': line['pl_wild_pitch'], - 'balk': line['pl_balk'], - 'hr': line['pl_homerun'], - 'ir': 0, - 'irs': 0, - 'gs': 1 if line['card_id'] in decisions['starters'] else 0, - 'win': 1 if line['card_id'] == decisions['winner'] else 0, - 'loss': 1 if line['card_id'] == decisions['loser'] else 0, - 'hold': 1 if line['card_id'] in decisions['holds'] else 0, - 'sv': 1 if line['card_id'] == decisions['save'] else 0, - 'bsv': 1 if line['card_id'] in decisions['b_save'] else 0, - 'week': this_game.week_num, - 'season': this_game.season, - 'game_id': this_game.id - } - ) - - doubles += '\n' if len(doubles) else '' - triples += '\n' if len(triples) else '' - homers += '\n' if len(homers) else '' - s_bases += '\n' if len(s_bases) else '' - caught_s += '\n' if len(caught_s) else '' - - db_post('batstats', payload={'stats': batter_stats}) - db_post('pitstats', payload={'stats': pitcher_stats}) - - # Post a notification to PD - last_play = get_current_play(this_game.id) - inning = f'{last_play.inning_num if last_play.inning_half == "Bot" else last_play.inning_num - 1}' - embed = get_team_embed( - f'{away_team["lname"]} {away_stats["score"]} @ {home_stats["score"]} {home_team["lname"]} - F/' - f'{inning}', - winning_team + for line in [*away_stats['f_lines'], *home_stats['f_lines']]: + if line['pl_csc']: + if len(caught_s): + caught_s += ', ' + caught_s += f'{db_get("cards", object_id=line["card_id"])["player"]["p_name"]}' \ + f'{" " if line["pl_csc"] > 1 else ""}' \ + f'{line["pl_csc"] if line["pl_csc"] > 1 else ""}' + batter_stats.append( + { + 'card_id': line['card_id'], + 'team_id': line['team_id'], + 'roster_num': + this_game.away_roster_num if this_game.away_team_id == line['team_id'] + else this_game.home_roster_num, + 'vs_team_id': + home_team['id'] if this_game.away_team_id == line['team_id'] + else away_team['id'], + 'pos': line['pos'], + 'xch': line['pl_xch'], + 'xhit': line['pl_xhit'], + 'error': line['pl_error'], + 'pb': line['pl_pb'], + 'sbc': line['pl_sbc'], + 'csc': line['pl_csc'], + 'week': this_game.week_num, + 'season': this_game.season, + 'game_id': this_game.id + } ) - if this_game.game_type == 'major-league': - game_des = 'Major League' - elif this_game.game_type == 'minor-league': - game_des = 'Minor League' - elif this_game.ranked: - game_des = 'Ranked' - elif 'gauntlet' in this_game.game_type: - game_des = 'Gauntlet' - else: - game_des = 'Unlimited' - embed.description = f'Score Report - {game_des} ' \ - f'{"- 3-Inning Game" if this_game.short_game else " - 9-Inning Game"}' + for line in [*away_stats['p_lines'], *home_stats['p_lines']]: + pitcher_stats.append( + { + 'card_id': line['card_id'], + 'team_id': line['team_id'], + 'roster_num': + this_game.away_roster_num if this_game.away_team_id == line['team_id'] + else this_game.home_roster_num, + 'vs_team_id': + home_team['id'] if this_game.away_team_id == line['team_id'] + else away_team['id'], + 'ip': (math.floor(line['pl_outs'] / 3) * 1.0) + ((line['pl_outs'] % 3) / 3.0), + 'hit': line['pl_hit'], + 'run': line['pl_runs'], + 'erun': line['pl_eruns'], + 'so': line['pl_so'], + 'bb': line['pl_bb'], + 'hbp': line['pl_hbp'], + 'wp': line['pl_wild_pitch'], + 'balk': line['pl_balk'], + 'hr': line['pl_homerun'], + 'ir': 0, + 'irs': 0, + 'gs': 1 if line['card_id'] in decisions['starters'] else 0, + 'win': 1 if line['card_id'] == decisions['winner'] else 0, + 'loss': 1 if line['card_id'] == decisions['loser'] else 0, + 'hold': 1 if line['card_id'] in decisions['holds'] else 0, + 'sv': 1 if line['card_id'] == decisions['save'] else 0, + 'bsv': 1 if line['card_id'] in decisions['b_save'] else 0, + 'week': this_game.week_num, + 'season': this_game.season, + 'game_id': this_game.id + } + ) + + doubles += '\n' if len(doubles) else '' + triples += '\n' if len(triples) else '' + homers += '\n' if len(homers) else '' + s_bases += '\n' if len(s_bases) else '' + caught_s += '\n' if len(caught_s) else '' + + db_post('batstats', payload={'stats': batter_stats}) + db_post('pitstats', payload={'stats': pitcher_stats}) + + # Post a notification to PD + last_play = get_current_play(this_game.id) + inning = f'{last_play.inning_num if last_play.inning_half == "Bot" else last_play.inning_num - 1}' + embed = get_team_embed( + f'{away_team["lname"]} {away_stats["score"]} @ {home_stats["score"]} {home_team["lname"]} - F/' + f'{inning}', + winning_team + ) + + if this_game.game_type == 'major-league': + game_des = 'Major League' + elif this_game.game_type == 'minor-league': + game_des = 'Minor League' + elif this_game.ranked: + game_des = 'Ranked' + elif 'gauntlet' in this_game.game_type: + game_des = 'Gauntlet' + else: + game_des = 'Unlimited' + embed.description = f'Score Report - {game_des} ' \ + f'{"- 3-Inning Game" if this_game.short_game else " - 9-Inning Game"}' + embed.add_field( + name='Box Score', + value=get_final_scorebug(away_team, home_team, away_stats, home_stats), + inline=False + ) + embed.add_field( + name='Location', + value=f'{ctx.guild.get_channel(this_game.channel_id).mention}' + ) + embed.add_field( + name='Pitching', + value=f'Win: {db_get("cards", object_id=decisions["winner"])["player"]["p_name"]}\n' + f'Loss: {db_get("cards", object_id=decisions["loser"])["player"]["p_name"]}\n' + f'{"Save: " if decisions["save"] else ""}' + f'{db_get("cards", object_id=decisions["save"])["player"]["p_name"] if decisions["save"] else ""}', + inline=False + ) + if len(doubles) + len(triples) + len(homers) > 0: embed.add_field( - name='Box Score', - value=get_final_scorebug(away_team, home_team, away_stats, home_stats), + name='Batting', + value=f'{"2B: " if len(doubles) else ""}{doubles if len(doubles) else ""}' + f'{"3B: " if len(triples) else ""}{triples if len(triples) else ""}' + f'{"HR: " if len(homers) else ""}{homers if len(homers) else ""}', inline=False ) + if len(s_bases) + len(caught_s) > 0: embed.add_field( - name='Location', - value=f'{ctx.guild.get_channel(this_game.channel_id).mention}' - ) - embed.add_field( - name='Pitching', - value=f'Win: {db_get("cards", object_id=decisions["winner"])["player"]["p_name"]}\n' - f'Loss: {db_get("cards", object_id=decisions["loser"])["player"]["p_name"]}\n' - f'{"Save: " if decisions["save"] else ""}' - f'{db_get("cards", object_id=decisions["save"])["player"]["p_name"] if decisions["save"] else ""}', + name='Baserunning', + value=f'{"SB: " if len(s_bases) else ""}{s_bases if len(s_bases) else ""}' + f'{"CSc: " if len(caught_s) else ""}{caught_s if len(caught_s) else ""}', inline=False ) - if len(doubles) + len(triples) + len(homers) > 0: - embed.add_field( - name='Batting', - value=f'{"2B: " if len(doubles) else ""}{doubles if len(doubles) else ""}' - f'{"3B: " if len(triples) else ""}{triples if len(triples) else ""}' - f'{"HR: " if len(homers) else ""}{homers if len(homers) else ""}', - inline=False - ) - if len(s_bases) + len(caught_s) > 0: - embed.add_field( - name='Baserunning', - value=f'{"SB: " if len(s_bases) else ""}{s_bases if len(s_bases) else ""}' - f'{"CSc: " if len(caught_s) else ""}{caught_s if len(caught_s) else ""}', - inline=False - ) - embed.add_field( - name=f'{winning_team["abbrev"]} Rewards', - value=f'1x {win_reward["pack_type"]["name"]} Pack\n{win_reward["money"]}₼' - ) - embed.add_field( - name=f'{losing_team["abbrev"]} Rewards', - value=f'{loss_reward["money"]}₼' - ) - embed.add_field( - name='Highlights', - value=f'Please share the highlights in {get_channel(ctx, "pd-news-ticker").mention}!', - inline=False - ) - await send_to_channel(self.bot, 'pd-network-news', embed=embed) + embed.add_field( + name=f'{winning_team["abbrev"]} Rewards', + value=win_reward + ) + embed.add_field( + name=f'{losing_team["abbrev"]} Rewards', + value=loss_reward + ) + embed.add_field( + name='Highlights', + value=f'Please share the highlights in {get_channel(ctx, "pd-news-ticker").mention}!', + inline=False + ) + await send_to_channel(self.bot, 'pd-network-news', embed=embed) - # Post rewards - give_packs(winning_team, num_packs=1, pack_type=win_reward['pack_type']) - db_post(f'teams/{winning_team["id"]}/money/{win_reward["money"]}') - db_post(f'teams/{losing_team["id"]}/money/{loss_reward["money"]}') + # Gauntlet results and reward + if gauntlet_team is not None: + await gauntlets.post_result( + int(this_game.game_type.split('-')[3]), + is_win=winning_team['gmid'] == gauntlet_team['gmid'], + this_team=gauntlet_team, + bot=self.bot, + ctx=ctx + ) - # Gauntlet results and reward - if gauntlet_team is not None: - await gauntlets.post_result( - int(this_game.game_type.split('-')[3]), - is_win=winning_team['gmid'] == gauntlet_team['gmid'], - this_team=gauntlet_team, - bot=self.bot, - ctx=ctx - ) - - this_run = db_get('gauntletruns', object_id=int(this_game.game_type.split('-')[3])) - if this_run['losses'] == 2: + this_run = db_get('gauntletruns', object_id=int(this_game.game_type.split('-')[3])) + if this_run['losses'] == 2: await send_to_channel( bot=self.bot, channel_name='pd-network-news', @@ -1849,12 +1905,6 @@ class Gameplay(commands.Cog): embed=None ) - else: - logging.debug(f'Time to build statlines and share the scorecard for this SBa game!') - # Send stats to sheets - # Post sheets link - pass - patch_game(this_game.id, active=False) logging.info(f'Game {this_game.id} is complete')