def initialize_team(team_gamedata_dict, team_livedata_dict, full_gamedata_dict): team = Team(team_gamedata_dict['name'], team_gamedata_dict['abbreviation']) set_player_list(team_livedata_dict, full_gamedata_dict, team) if team_livedata_dict.get('pitchers'): team.pitcher_list = [ PlayerAppearance(team[team_livedata_dict['pitchers'][0]], 1, 1, 'top', 1) ] else: team.pitcher_list = [] for _, player_dict in team_livedata_dict['players'].items(): if player_dict.get('battingOrder'): batting_order = int(player_dict['battingOrder']) position = player_dict['allPositions'][0]['code'] if batting_order % 100 == 0: batting_index = int((batting_order / 100) - 1) team.batting_order_list_list[batting_index] = [ PlayerAppearance(team[int(player_dict['person']['id'])], position, 1, 'top', 1) ] return team
def set_starting_pitchers(game, away_starting_pitcher_id, home_starting_pitcher_id): game.away_team.pitcher_list.append( PlayerAppearance(game.away_team[away_starting_pitcher_id], 1, 1, 'top', 1)) game.home_team.pitcher_list.append( PlayerAppearance(game.home_team[home_starting_pitcher_id], 1, 1, 'top', 1))
def process_substitution(substitution_obj, inning_num, inning_half_str, next_batter_num, substituting_team): player_appearance_obj = PlayerAppearance(substitution_obj.incoming_player, substitution_obj.position, inning_num, inning_half_str, next_batter_num) batting_list_list = substituting_team.batting_order_list_list player_appearance_list = None processed_flag = False if substitution_obj.batting_order: batting_index = substitution_obj.batting_order - 1 player_appearance_list = batting_list_list[batting_index] else: for this_appearance_list in batting_list_list: if (this_appearance_list[-1].player_obj.mlb_id == substitution_obj.outgoing_player.mlb_id): player_appearance_list = this_appearance_list if player_appearance_list: processed_flag = True set_player_position_from_list(player_appearance_obj, player_appearance_list) final_appearance = player_appearance_list[-1] final_appearance.end_inning_num = inning_num final_appearance.end_inning_half = inning_half_str final_appearance.end_inning_batter_num = next_batter_num player_appearance_list.append(player_appearance_obj) if player_appearance_obj.position == 1: processed_flag = True pitching_appearance_list = substituting_team.pitcher_list final_appearance = pitching_appearance_list[-1] final_appearance.end_inning_num = inning_num final_appearance.end_inning_half = inning_half_str final_appearance.end_inning_batter_num = next_batter_num pitching_appearance_list.append(player_appearance_obj) if not processed_flag: raise ValueError('Invalid substitution.')
def process_switch(switch_obj, inning_num, inning_half_str, next_batter_num, switching_team): player_appearance_obj = PlayerAppearance(switch_obj.player, switch_obj.new_position_num, inning_num, inning_half_str, next_batter_num) batting_list_list = switching_team.batting_order_list_list old_player_appearance_list = None for this_appearance_list in batting_list_list: if (this_appearance_list[-1].player_obj.mlb_id == switch_obj.player.mlb_id): old_player_appearance_list = this_appearance_list if not old_player_appearance_list: if (switching_team.pitcher_list[-1].player_obj.mlb_id == switch_obj.player.mlb_id): old_player_appearance_list = switching_team.pitcher_list if not old_player_appearance_list: raise ValueError('Invalid player switch') final_appearance = old_player_appearance_list[-1] final_appearance.end_inning_num = inning_num final_appearance.end_inning_half = inning_half_str final_appearance.end_inning_batter_num = next_batter_num if switch_obj.new_batting_order: new_player_appearance_list = batting_list_list[ switch_obj.new_batting_order - 1] new_player_appearance_list.append(player_appearance_obj) else: old_player_appearance_list.append(player_appearance_obj) if player_appearance_obj.position == 1: switching_team.pitcher_list.append(player_appearance_obj)
def init_player_list(player_obj, position): return [PlayerAppearance(player_obj, position, 1, 'top', 1)]
def process_substitution(substitution_obj, inning_num, inning_half_str, next_batter_num, substituting_team): player_appearance_obj = PlayerAppearance(substitution_obj.incoming_player, substitution_obj.position, inning_num, inning_half_str, next_batter_num) batting_list_list = substituting_team.batting_order_list_list player_appearance_list = None processed_flag = False if substitution_obj.batting_order: batting_index = substitution_obj.batting_order - 1 player_appearance_list = batting_list_list[batting_index] else: for this_appearance_list in batting_list_list: if (this_appearance_list[-1].player_obj.mlb_id == substitution_obj.outgoing_player.mlb_id): player_appearance_list = this_appearance_list if not player_appearance_list: position_list = [ batting_list[-1].position for batting_list in batting_list_list ] duplicate_position_set = set([ x for x in position_list if ((position_list.count(x) > 1) and (x not in ['PH', 'PR'])) ]) if duplicate_position_set: duplicate_position = [x for x in duplicate_position_set][0] duplicate_appearance_list = [] for batting_list in batting_list_list: if batting_list[-1].position == duplicate_position: duplicate_appearance_list.append(batting_list) first_player_start = int('{}{}{}'.format( duplicate_appearance_list[0][-1].start_inning_num, int(duplicate_appearance_list[0][-1].start_inning_half == 'bottom'), duplicate_appearance_list[0][-1].start_inning_batter_num, )) second_player_start = int('{}{}{}'.format( duplicate_appearance_list[1][-1].start_inning_num, int(duplicate_appearance_list[1][-1].start_inning_half == 'bottom'), duplicate_appearance_list[1][-1].start_inning_batter_num, )) if first_player_start < second_player_start: player_appearance_list = duplicate_appearance_list[0] else: player_appearance_list = duplicate_appearance_list[1] if not player_appearance_list: for this_appearance_list in batting_list_list: outgoing_id = substitution_obj.outgoing_player.mlb_id if (len(this_appearance_list) > 1 and this_appearance_list[-2].player_obj.mlb_id == outgoing_id): player_appearance_list = this_appearance_list if player_appearance_list: processed_flag = True set_player_position_from_list(player_appearance_obj, player_appearance_list) final_appearance = player_appearance_list[-1] final_appearance.end_inning_num = inning_num final_appearance.end_inning_half = inning_half_str final_appearance.end_inning_batter_num = next_batter_num player_appearance_list.append(player_appearance_obj) if player_appearance_obj.position == 1: processed_flag = True pitching_appearance_list = substituting_team.pitcher_list final_appearance = pitching_appearance_list[-1] final_appearance.end_inning_num = inning_num final_appearance.end_inning_half = inning_half_str final_appearance.end_inning_batter_num = next_batter_num pitching_appearance_list.append(player_appearance_obj) if not processed_flag: raise ValueError('Invalid substitution.')