def match(challenger, defendant, winner): ''' Updates the ELO value for the specified player Called in GetStats() Parameters ---------- player : object of player class The player to update the ELO value for ''' # Calculate the expected score for both players challenger.expected = expected(challenger.ELO, defendant.ELO) defendant.expected = expected(defendant.ELO, challenger.ELO) # Set actual score based on the match winner if winner == challenger: challenger.result = 1 challenger.wins += 1 defendant.result = 0 defendant.losses += 1 else: # winner == defendant challenger.result = 0 challenger.losses += 1 defendant.result = 1 defendant.wins += 1 # Increment number of games played challenger.games += 1 defendant.games += 1 # Set new ELO ratings challenger.UpdateELO() defendant.UpdateELO()
def test_elo(): exp = 0 exp += expected(1613, 1609) exp += expected(1613, 1477) exp += expected(1613, 1388) exp += expected(1613, 1586) exp += expected(1613, 1720) score = (0 + 0.5 + 1 + 1 + 0) assert round(elo(1613, exp, score, k=32)) == 1601 assert round(elo(1613, exp, 3, k=32)) == 1617
def update_elo(conn, user_id, opponent_name, did_the_user_win): cursor = conn.cursor() cursor.execute("SELECT elo FROM users WHERE \ player_name = '%s'" % opponent_name) opponent_elo = cursor.fetchone() cursor.execute("SELECT elo FROM users WHERE \ id_usr = '******'" % user_id) user_elo = cursor.fetchone() expected_score = elo.expected(int(user_elo[0]), int(opponent_elo[0])) if did_the_user_win == 1: score = 1 score_opponent = 0 else: score = 0 score_opponent = 1 # update user elo updated_user_elo = round( elo.elo(int(user_elo[0]), expected_score, score, k=32)) # update opponent elo, his expected score is 1 - expected_score updated_opponent_elo = round( elo.elo(int(opponent_elo[0]), 1 - expected_score, score_opponent, k=32)) return updated_user_elo, updated_opponent_elo
def update(): if request.method == 'POST': #Update player elo according to result winners = request.form['winner'].split(',') loosers = request.form['looser'].split(',') winners_elo = 0 loosers_elo = 0 for player in winners: winners_elo += int(db[player]) for player in loosers: loosers_elo += int(db[player]) #You may want to flatten elo per player number #winners_elo = winners_elo / len(winners) #lossers_elo = loosers_elo / len(loosers) for player in winners: exp_score = expected(winners_elo, loosers_elo) elo = elo_gain(exp_score, 1) db[player] = str(int(db[player]) + int(round(elo))) for player in loosers: exp_score = expected(loosers_elo, winners_elo) elo = elo_gain(exp_score, 0) db[player] = str(int(db[player]) + int(round(elo))) savedb() return redirect(url_for('index'))
def test_expected(): assert round(expected(1613, 1609), 3) == 0.506 assert round(expected(1613, 1477), 3) == 0.686 assert round(expected(1613, 1388), 3) == 0.785 assert round(expected(1613, 1586), 3) == 0.539 assert round(expected(1613, 1720), 3) == 0.351 pairs = [ (0, 0), (1, 1), (10, 20), (123, 456), (2400, 2500), ] for a, b in pairs: assert round(expected(a, b) + expected(b, a), 3) == 1.0
("Crusaders","Brumbies"), ("Waratahs","Force"), ("Cheetahs","Lions"), ("Kings","Jaguares"), ("Stormers","Bulls"), ] for match in matches: # home team team1 = match[0] # away team team2 = match[1] team1_expected = elo.expected(teams[team1][0], teams[team2][0]) team2_expected = elo.expected(teams[team2][0], teams[team1][0]) # assume avg points scored team1_tot_pts = teams[team1][1] team2_tot_pts = teams[team2][1] team1_predicted_score = team1_tot_pts * team1_expected team2_predicted_score = team2_tot_pts * team2_expected diff = team1_predicted_score - team2_predicted_score if diff > 0: winner = team1 else: winner = team2
def expected(self, opponent): return expected(self.score, opponent.score)