From a09224ae6b343c087b5eb79443a5c94ef4636b6b Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sun, 22 Oct 2023 16:41:58 -0500 Subject: [PATCH] Implement new reliever call --- ai_manager.py | 200 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 121 insertions(+), 79 deletions(-) diff --git a/ai_manager.py b/ai_manager.py index 285c475..b75588b 100644 --- a/ai_manager.py +++ b/ai_manager.py @@ -441,93 +441,135 @@ async def get_starting_pitcher(team_object: dict, game_id: int, is_home: bool, l async def get_relief_pitcher(this_play: StratPlay, ai_team: dict, league_name: str = None) -> dict: - used_codes = [] + used_ids = [] used_players = await get_team_lineups( - game_id=this_play.game.id, team_id=ai_team['id'], inc_inactive=True, as_string=False + game_id=this_play.game.id, team_id=ai_team['id'], inc_inactive=True, as_string=False, pitchers_only=True ) for x in used_players: - c_query = await db_get('cards', object_id=x.card_id) - used_codes.append(c_query["player"]["strat_code"]) - logging.info(f'get_rp - used_players: {used_codes}') + used_ids.append(f'{x.player_id}') - reliever = None - attempts = 0 - while reliever is None: - attempts += 1 - if attempts > 3: - raise ValueError(f'Could not find a reliever for the {ai_team["sname"]}. Cal plz.') + logging.debug(f'used ids: {used_ids}') + id_string = "&used_pitcher_ids=".join(used_ids) + this_game = this_play.game + ai_score = this_play.away_score if this_game.away_team_id == ai_team['id'] else this_play.home_score + human_score = this_play.home_score if this_game.away_team_id == ai_team['id'] else this_play.away_score - set_params = [('cardset_id_exclude', 2)] - if league_name == 'minor-league': - set_params = copy.deepcopy(MINOR_CARDSET_PARAMS) - elif league_name == 'major-league': - set_params = copy.deepcopy(MAJOR_CARDSET_PARAMS) - elif league_name == 'hall-of-fame': - set_params = copy.deepcopy(HOF_CARDSET_PARAMS) - elif 'gauntlet-1' in league_name: - set_params = copy.deepcopy(MINOR_CARDSET_PARAMS) - elif 'gauntlet-2' in league_name: - set_params = copy.deepcopy(GAUNTLET2_PARAMS) + logging.debug(f'scores - ai: {ai_score} / human: {human_score}') + if abs(ai_score - human_score) >= 7: + need = 'length' + elif this_play.inning_num >= 9 and abs(ai_score - human_score) <= 3: + need = 'closer' + elif this_play.inning_num in [7, 8] and abs(ai_score - human_score) <= 3: + need = 'setup' + elif abs(ai_score - human_score) <= 3: + need = 'middle' + else: + need = 'length' - # Pull relievers sorted by current cost - params = [ - ('mlbclub', ai_team['lname']), ('pos_include', 'RP'), ('inc_dex', False), ('sort_by', 'cost-desc'), - ('limit', 15) - ] - params.extend(set_params) + logging.debug(f'need: {need}') + rp_query = await db_get(f'teams/{ai_team["id"]}/rp/{league_name}?need={need}&used_pitcher_ids={id_string}') + card_id = await get_or_create_card(rp_query, ai_team) + return { + 'game_id': this_play.game.id, + 'team_id': ai_team['id'], + 'player_id': rp_query['player_id'], + 'card_id': card_id, + 'position': 'P', + 'batting_order': 10, + 'after_play': this_play.play_num - 1 + } - use_best = False - if attempts == 1: - # Try to get long man - if this_play.inning_num < 6: - logging.info(f'get_rp - game {this_play.game.id} try for long man') - params.append(('pos_include', 'SP')) - # use_best = True + """ + END NEW GET RP + """ - # Try to get closer - elif this_play.inning_num > 8: - if (this_play.inning_half == 'top' and this_play.home_score >= this_play.away_score) or \ - (this_play.inning_half == 'bot' and this_play.away_score >= this_play.home_score): - logging.info(f'get_rp - game {this_play.game.id} try for closer') - params.append(('pos_include', 'CP')) - use_best = True - else: - params.append(('pos_exclude', 'CP')) - - # Try to exclude long men - elif attempts == 1: - logging.info(f'get_rp - game {this_play.game.id} try to exclude long men') - params.append(('pos_exclude', 'SP')) - - try: - pitchers = await db_get( - endpoint='players', - params=params, - timeout=10 - ) - except ConnectionError as e: - logging.error(f'Could not get pitchers for {ai_team["lname"]}: {e}') - raise ConnectionError(f'Error pulling starting pitchers for the {ai_team["lname"]}. Cal help plz.') - - if pitchers['count'] > 0: - if use_best or this_play.inning_num > 9 or attempts > 2: - start = 0 - else: - start = 9 - this_play.inning_num - - for count, guy in enumerate(pitchers['players']): - if count >= start and guy['strat_code'] not in used_codes: - card_id = await get_or_create_card(guy, ai_team) - - return { - 'game_id': this_play.game.id, - 'team_id': ai_team['id'], - 'player_id': guy['player_id'], - 'card_id': card_id, - 'position': 'P', - 'batting_order': 10, - 'after_play': this_play.play_num - 1 - } + # used_codes = [] + # used_players = await get_team_lineups( + # game_id=this_play.game.id, team_id=ai_team['id'], inc_inactive=True, as_string=False + # ) + # for x in used_players: + # c_query = await db_get('cards', object_id=x.card_id) + # used_codes.append(c_query["player"]["strat_code"]) + # logging.info(f'get_rp - used_players: {used_codes}') + # + # reliever = None + # attempts = 0 + # while reliever is None: + # attempts += 1 + # if attempts > 3: + # raise ValueError(f'Could not find a reliever for the {ai_team["sname"]}. Cal plz.') + # + # set_params = [('cardset_id_exclude', 2)] + # if league_name == 'minor-league': + # set_params = copy.deepcopy(MINOR_CARDSET_PARAMS) + # elif league_name == 'major-league': + # set_params = copy.deepcopy(MAJOR_CARDSET_PARAMS) + # elif league_name == 'hall-of-fame': + # set_params = copy.deepcopy(HOF_CARDSET_PARAMS) + # elif 'gauntlet-1' in league_name: + # set_params = copy.deepcopy(MINOR_CARDSET_PARAMS) + # elif 'gauntlet-2' in league_name: + # set_params = copy.deepcopy(GAUNTLET2_PARAMS) + # + # # Pull relievers sorted by current cost + # params = [ + # ('mlbclub', ai_team['lname']), ('pos_include', 'RP'), ('inc_dex', False), ('sort_by', 'cost-desc'), + # ('limit', 15) + # ] + # params.extend(set_params) + # + # use_best = False + # if attempts == 1: + # # Try to get long man + # if this_play.inning_num < 6: + # logging.info(f'get_rp - game {this_play.game.id} try for long man') + # params.append(('pos_include', 'SP')) + # # use_best = True + # + # # Try to get closer + # elif this_play.inning_num > 8: + # if (this_play.inning_half == 'top' and this_play.home_score >= this_play.away_score) or \ + # (this_play.inning_half == 'bot' and this_play.away_score >= this_play.home_score): + # logging.info(f'get_rp - game {this_play.game.id} try for closer') + # params.append(('pos_include', 'CP')) + # use_best = True + # else: + # params.append(('pos_exclude', 'CP')) + # + # # Try to exclude long men + # elif attempts == 1: + # logging.info(f'get_rp - game {this_play.game.id} try to exclude long men') + # params.append(('pos_exclude', 'SP')) + # + # try: + # pitchers = await db_get( + # endpoint='players', + # params=params, + # timeout=10 + # ) + # except ConnectionError as e: + # logging.error(f'Could not get pitchers for {ai_team["lname"]}: {e}') + # raise ConnectionError(f'Error pulling starting pitchers for the {ai_team["lname"]}. Cal help plz.') + # + # if pitchers['count'] > 0: + # if use_best or this_play.inning_num > 9 or attempts > 2: + # start = 0 + # else: + # start = 9 - this_play.inning_num + # + # for count, guy in enumerate(pitchers['players']): + # if count >= start and guy['strat_code'] not in used_codes: + # card_id = await get_or_create_card(guy, ai_team) + # + # return { + # 'game_id': this_play.game.id, + # 'team_id': ai_team['id'], + # 'player_id': guy['player_id'], + # 'card_id': card_id, + # 'position': 'P', + # 'batting_order': 10, + # 'after_play': this_play.play_num - 1 + # } def get_pitcher(this_game: StratGame, this_play: StratPlay):