Skip to content

Commit

Permalink
introduced ranked beta
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieVeitch committed Mar 6, 2023
1 parent 7b2aaa7 commit 152bd1d
Show file tree
Hide file tree
Showing 9 changed files with 960 additions and 461 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ tqdm = "*"
jupyter = "*"
pandas = "==1.4.2"
streamlit-aggrid = "*"
matplotlib = "*"
scikit-learn = "*"

[dev-packages]

Expand Down
1,226 changes: 816 additions & 410 deletions Pipfile.lock

Large diffs are not rendered by default.

42 changes: 7 additions & 35 deletions analysis/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ def remove_uneven_teams(games: pd.DataFrame) -> pd.Index:
return valid_games


def remove_no_damage_rounds(games: pd.DataFrame) -> pd.Index:
"""
get the list of games that have had damage dealt in all rounds
:param games: all data stored in the mongo database
:return: the list of games that have had damage dealt in all rounds
"""
damage_dealt = get_damage_dealt(games=games)
min_damage_dealt = damage_dealt.groupby('matchID').min()
valid_games = min_damage_dealt[min_damage_dealt != 0].index

return valid_games


def remove_non_highlander(games: pd.DataFrame) -> pd.Index:
"""
remove all games that weren't playing highlander in any round
Expand All @@ -49,36 +36,21 @@ def preprocess(games: pd.DataFrame) -> pd.DataFrame:
:param games: all data stored in the mongo database
:return: the list of games that should be used for ranking
"""

ranked_games: pd.DataFrame = games[
games['ranked'] &
games['rebalance'] &
~games['perfectKits'] &
~games['isPreRelease'].replace(np.nan, True)
~games.perfectKits &
games.rebalance &
(games["kit1"] != "Spectate") &
(games.matchTimestamp > "2023-03-01")
].copy()

has_even_teams = remove_uneven_teams(games=ranked_games)
has_damage = remove_no_damage_rounds(games=ranked_games)
has_highlander = remove_non_highlander(games=ranked_games)

ranked_games.set_index('matchID', inplace=True)
ranked_games = ranked_games.loc[has_even_teams.intersection(has_damage).intersection(has_highlander)]
ranked_games = games.copy().set_index('matchID')
ranked_games = ranked_games.loc[has_even_teams.intersection(has_highlander)]

recent_names = player_names(games=ranked_games)
ranked_games['name'] = ranked_games['uid'].replace(recent_names)

ranked_games.drop(
columns=[
'_id',
'uid',
'serverName',
'ranked',
'rebalance',
'perfectKits',
'isPreRelease',
'version'
],
inplace=True
)

return ranked_games
return ranked_games.reset_index()
91 changes: 91 additions & 0 deletions analysis/ranked.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import itertools
import pandas as pd


def get_result(elo, p1, p2) -> float:
"""
:param p1: the elo of player 1
:param p2: the elo of player 2
:return: the expected result of player 1
"""
p2 = elo[p2]
p1 = elo[p1]

exponent = (p2 - p1) / 400.0
return 1 / ((10.0 ** exponent) + 1)


def update_elo(game_round: pd.DataFrame, k: int, g: int) -> pd.Series:
"""
update the elo for each player in the game round
:param game_round: the game round to update the elo for
:param elo: the current elo for each player
:param k: the k factor for the elo
:param g: the g factor for the elo
:return: the updated elo for each player
"""
try:
elo = pd.read_csv("database/elo.csv", index_col=0).squeeze()
except FileNotFoundError:
elo = pd.Series(dtype=float, name="2023-03-01")

if game_round.matchTimestamp.unique()[0] < elo.name:
print("outdated game round")
return elo

winners = game_round[game_round['result'] == "Win"]["name"].unique().tolist()
losers = game_round[game_round['result'] == "Loss"]["name"].unique().tolist()

for player in winners + losers:
if player not in elo.index:
elo[player] = 1000

expected_results = pd.DataFrame(columns=winners, index=losers)
expected_results = expected_results.apply(
lambda winner: winner.index.to_series().apply(lambda loser: get_result(elo, winner.name, loser))
)
loser_elo = expected_results.apply(lambda loser: (k * g)*(loser - 1), axis=1).mean(axis=1)
winner_elo = expected_results.apply(lambda winner: (k * g) * (1 - winner)).mean()

elo = elo.add(winner_elo, fill_value=0.0)
elo = elo.add(loser_elo, fill_value=0.0)

elo = elo.rename(game_round.matchTimestamp.unique()[0])
elo.to_csv("database/elo.csv")

try:
timeseries_elo = pd.read_csv("database/timeseries_elo.csv", index_col=0)
timeseries_elo = pd.concat([timeseries_elo, elo], axis=1)
except FileNotFoundError:
timeseries_elo = elo

timeseries_elo.to_csv("database/timeseries_elo.csv")

return elo


def trigger_update(games: pd.DataFrame) -> None:
"""
:param games:
:return:
"""
try:
elo = pd.read_csv("database/elo.csv", index_col=0).squeeze()
except FileNotFoundError:
elo = pd.Series(dtype=float, name="2023-03-01")

games = games[games['matchTimestamp'] > elo.name]
chronological_matches = games.sort_values("matchTimestamp", ascending=True).matchID.unique()

for match in chronological_matches:

match_played = games[games["matchID"] == match]
rounds = match_played["round"].sort_values(ascending=True).unique()

for round_played in rounds:
update_elo(
match_played[match_played["round"] == round_played],
k=32,
g=1
)

14 changes: 14 additions & 0 deletions database/elo.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
,2023-03-06 01:55:26
DustShelter_Inc,1001.5317741589588
Fun13us,1101.588475146853
L1GHTMAN2k,968.8683288311493
MrBlaBlak,948.42744492315
TTVbiohaZard1324,899.4598874131813
Test97point3,1031.1316711688498
TragicRAT55,1077.872539483852
dsnvacation,979.9420269562636
lexvi1,1038.8063425633643
pon47k,938.89203628405
sparkyfan236,1043.5106348693398
spooksi,986.4583569874478
zerytoe,983.5104812135404
7 changes: 4 additions & 3 deletions database/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import datetime
import pandas as pd
import numpy as np
from tqdm import tqdm

from analysis.utilities import player_names


def load_database() -> pd.DataFrame:
Expand All @@ -15,11 +16,11 @@ def load_database() -> pd.DataFrame:
os.environ["LIGHTHOUSE_MONGO_KEY"],
tlsAllowInvalidCertificates=True
)
games = pd.DataFrame(list(tqdm(client["ranking"].ranking.find())))
games = pd.DataFrame(list(client["ranking"].ranking.find()))
games["team"] = games["team"].replace([2, 3], ["imc", "militia"])

games['matchTimestamp'] = games['matchTimestamp'].apply(
lambda date: str(datetime.datetime.fromtimestamp(date).date())
lambda date: str(datetime.datetime.fromtimestamp(date))
)

return games
Loading

0 comments on commit 152bd1d

Please sign in to comment.