def sort_player_into_team(): """Create directories and files and sort data into appropriate folders. """ # calculate the player ratings player_ratings = {} for player_id in PLAYER_DICT.keys(): rating = rate_player.SinglePlayerRating(player_id) player_ratings[PLAYER_DICT[player_id]] = rating.get_rating() for team_abb in TEAM_DICT.values(): sorted_dir = os.path.join(PLAYER_RATING_PATH, f'{team_abb}.json') if not os.path.exists(sorted_dir): # sort player ratings into teams directories sorted_player_ratings = [] for index in PLAYER_DICT.keys(): player_name = PLAYER_DICT[index] player_path = os.path.join(PLAYER_SEASON_PATH, f'{player_name}.json') with open(player_path, 'r') as player_file: file = json.load(player_file) # put all the player rating for the same team into a dictionary if file[-1]["TEAM_ABBREVIATION"] == team_abb and \ file[-1]["SEASON_ID"] != '2016-17': sorted_player_ratings.append(player_ratings[player_name]) with open(sorted_dir, 'w') as outfile: json.dump(sorted_player_ratings, outfile) return True # used in test cases
def init(): """Controls the simulations and initialize playoff data. :return: the result of the simulated regular season games """ results = run_simulation() for team in TEAM_DICT.values(): team_result = results[0][team] team_path = os.path.join(SIM_RESULT_PATH, team + '.json') with open(team_path, 'w') as result_file: dump(team_result, result_file) # create win count file win_count_path = os.path.join(SIM_RESULT_PATH, 'win_count.json') with open(win_count_path, 'w') as win_count_file: dump(results[1], win_count_file) # create loss count file loss_count_path = os.path.join(SIM_RESULT_PATH, 'loss_count.json') with open(loss_count_path, 'w') as loss_count_file: dump(results[2], loss_count_file) initialize_playoff() return results[0]
def run_simulation(): """Runs season simulation and create data sets storing the results. :return: A tuple containing four data sets, each storing different results """ # create variables results = {team_abb: {} for team_abb in TEAM_DICT.values()} win_count = {team_abb: 0 for team_abb in TEAM_DICT.values()} loss_count = {team_abb: 0 for team_abb in TEAM_DICT.values()} for team_abb in TEAM_DICT.values(): other_teams = list(TEAM_DICT.values()) other_teams.remove(team_abb) for other_team in other_teams: results[team_abb][other_team] = [] counter = 0 # debug use variable # loop through the game list and simulate all the games for day in GAME_LIST: for game in day: team1_id = str(game[0]) team2_id = str(game[1]) simulator = sim.GameSimulation(team1_id, team2_id) team1_abb = TEAM_DICT[team1_id] team2_abb = TEAM_DICT[team2_id] counter += 1 # check which team won the game if simulator.winner == team1_id: results[team1_abb][team2_abb].append('W') results[team2_abb][team1_abb].append('L') win_count[team1_abb] += 1 loss_count[team2_abb] += 1 elif simulator.winner == team2_id: results[team2_abb][team1_abb].append('W') results[team1_abb][team2_abb].append('L') win_count[team2_abb] += 1 loss_count[team1_abb] += 1 return results, win_count, loss_count, counter
def rank_teams(season): team_list = TEAM_DICT.values() team_win_list = [] for team_abb in team_list: with open(join(TEAM_SEASON_PATH, team_abb + '.json')) as data_file: data = load(data_file)['resultSets'][0]['rowSet'] for row in data: if row[3] == season: team_win_list.append((team_abb, data[data.index(row)][5])) team_win_list.sort(key=lambda win: win[1], reverse=True) result = {} rank = 1 for team in team_win_list: result[rank] = team[0] rank += 1 return result
def season_games(request, team, season): team_logo = f"images/{team}.png" team_name = ' '.join(TEAM_NAME_DICT[team]) team_list = [item for item in TEAM_DICT.values()] team_list.remove(team) if season == '2017-18': template = 'stats_team_pages/simulated_team_page.html' game_dict = get_simulated_games(team) else: template = 'stats_team_pages/team_page.html' game_dict = get_team_page_data(team) context = { 'team': team, 'team_logo': team_logo, 'team_name': team_name, 'team_list': team_list, 'game_dict': game_dict, 'season': season } return render(request, template, context)
def create_box_score_files(): """Creates a file storing box score data of a game given the ID """ # create a list of game IDs using the team game log files game_id_list = [] for team in TEAM_DICT.values(): team_path = os.path.join(TEAM_BASE_PATH, team + '.json') with open(team_path, 'r') as game_log_path: data = json.load(game_log_path) for single_game in data['resultSets'][0]['rowSet']: game_id_list.append(single_game[1]) # output a message to let the user know that the program is running print("Retrieving box score data and creating files ...") # retrieve data using game IDs from the list and store them in a file for game_id in game_id_list: path = os.path.join(GAME_BASE_PATH, game_id + '.json') if not os.path.exists(path): print("Retrieving " + game_id + " data") box_score_data = game.Boxscore(game_id).json with open(path, 'w') as box_score_file: json.dump(box_score_data, box_score_file)