import random import pandas as pd import os class Model: def __init__(self, name, elo): self.name = name self.elo = elo self.games_played = 0 class Matchmaking: def __init__(self): self.models = [] self.queue = [] self.start_elo = 1200 self.k = 20 self.max_diff = 500 self.matches = pd.DataFrame() def read_history(self): path = "match_history" files = os.listdir(path) for file in files: self.matches = pd.concat([self.matches, pd.read_csv(os.path.join(path, file))], ignore_index=True) self.matches["datetime"] = pd.to_datetime(self.matches["datetime"], format="%Y-%m-%d %H:%M:%S.%f", errors="coerce") self.matches = self.matches.dropna() self.matches = self.matches.sort_values("datetime") self.matches.reset_index(drop=True, inplace=True) model_names = self.matches["model1"].unique() self.models = [Model(name, self.start_elo) for name in model_names] def compute_elo(self): for i, row in self.matches.iterrows(): model1 = self.get_model(row["model1"]) model2 = self.get_model(row["model2"]) result = row["result"] delta = model1.elo - model2.elo win_probability = 1 / (1 + 10 ** (-delta / 500)) model1.elo += self.k * (result - win_probability) model2.elo -= self.k * (result - win_probability) model1.games_played += 1 model2.games_played += 1 def get_model(self, name): for model in self.models: if model.name == name: return model return None