Skip to content

Instantly share code, notes, and snippets.

@AndreVallestero
Created September 20, 2023 18:33
Show Gist options
  • Save AndreVallestero/3aa148f133f72fbc44d6f0d5990f0ef7 to your computer and use it in GitHub Desktop.
Save AndreVallestero/3aa148f133f72fbc44d6f0d5990f0ef7 to your computer and use it in GitHub Desktop.
import json, csv
K = 32 # ELO constant (adjust as needed)
def expected_win_probability(rating1, rating2):
return 1 / (1 + 10 ** ((rating2 - rating1) / 400))
with open("league_starting_elo_map_v1.csv", "r") as f:
starting_elos = list(csv.reader(f))[1:]
with open("league_team_map.json") as f:
league_team_map = json.load(f)
# Initialize elo based on league
elo_ratings = {}
for leagueIdName, teams in league_team_map.items():
starting_elo = int(next(elo[1] for elo in starting_elos if elo[0].split("|")[0] == leagueIdName.split("|")[0]))
for team in teams:
if team in elo_ratings:
if elo_ratings[team] > starting_elo: continue
elo_ratings[team] = starting_elo
# Read the CSV file and update ELO ratings
with open("game_results_v1.csv", mode='r') as file:
reader = csv.reader(file)
next(reader) # Skip the header row
for row in reader:
team1_id = row[1]
team2_id = row[2]
winner_id = row[3]
if (team1_id not in elo_ratings or team2_id not in elo_ratings): continue
# Calculate expected win probabilities
expected_win1 = expected_win_probability(elo_ratings[team1_id], elo_ratings[team2_id])
expected_win2 = expected_win_probability(elo_ratings[team2_id], elo_ratings[team1_id])
# Update ELO ratings based on the winner
if winner_id == team1_id:
elo_ratings[team1_id] += K * (1 - expected_win1)
elo_ratings[team2_id] += K * (0 - expected_win2)
elif winner_id == team2_id:
elo_ratings[team1_id] += K * (0 - expected_win1)
elo_ratings[team2_id] += K * (1 - expected_win2)
# Create elo rating rows and sort
elo_list = [[team_id, elo] for team_id, elo in elo_ratings.items()]
elo_list.sort(key=lambda x: x[1], reverse=True)
with open("esports-data/teams.json", encoding="utf-8") as f:
team_data = json.load(f)
for row in elo_list:
team_id = row[0]
# Add league column
highest_starting_elo = -1
highest_starting_league = None
for league_id_name, team_ids in league_team_map.items():
if team_id in team_ids:
starting_elo = int(next(elo[1] for elo in starting_elos if elo[0].split("|")[0] == league_id_name.split("|")[0]))
if starting_elo > highest_starting_elo:
highest_starting_elo = starting_elo
highest_starting_league = league_id_name
row.append(highest_starting_league)
# Add name column
for team in team_data:
if team["team_id"] == team_id:
row.append(team["name"])
break
with open("elo_ratings.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["team_id", "elo", "league", "name"])
writer.writerows(elo_list)
print(f'ELO ratings saved to elo_rating.csv')
League Starting Elo
98767991299243165|LCS 1300
109511549831443335|LCS Challengers 1000
109518549825754242|LCS Challengers Qualifiers 1000
107898214974993351|College Championship 1000
98767991332355509|CBLOL 1000
98767991310872058|LCK 1800
98767991355908944|LCL 1000
105709090213554609|LCO 1000
98767991302996019|LEC 1500
98767991349978712|LJL 1000
101382741235120470|LLA 1000
98767991314006698|LPL 1800
104366947889790212|PCS 1000
98767991343597634|TCL 1000
107213827295848783|VCS 1000
100695891328981122|EMEA Masters 1300
105266103462388553|La Ligue Française 1100
105266098308571975|NLC 1000
107407335299756365|Elite Series 1000
105266101075764040|Liga Portuguesa 1000
105266094998946936|PG Nationals 1000
105266088231437431|Ultraliga 1000
105266074488398661|SuperLiga 1100
105266091639104326|Prime League 1000
105266106309666619|Hitpoint Masters 1000
105266111679554379|Esports Balkan League 1000
105266108767593290|Greek Legends League 1000
109545772895506419|Arabian League 1000
108203770023880322|LCK Academy 1000
106827757669296909|LJL Academy 900
98767991335774713|LCK Challengers 1400
105549980953490846|CBLOL Academy 900
110371976858004491|North Regional League 1000
110372322609949919|South Regional League 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment