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 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 UpdateELO(self): ''' Updates the ELO value for the specified player Called in match() ''' self.ELO = elo(self.ELO, self.expected, self.result, self.GetK()) if self.ELO > self.ELOHighest: self.ELOHighest = self.ELO elif self.ELO < self.ELOLowest: self.ELOLowest = self.ELO
def _parse_xml(data): doc = xmltodict.parse(data) participantDict = {} tournamentName = doc['Tournament']['Description'] if 'Description' in doc[ 'Tournament'] else None tournamentLevel = 1 if tournamentName and tournamentName.endswith('A') else \ 0.75 if tournamentName and tournamentName.endswith('B') else \ 0.5 if tournamentName and tournamentName.endswith('C') else \ 0.25 for participant in _parse_participant( doc['Tournament']['IndividualParticipant']): participantDict[participant['id']] = { 'info': participant, 'result': [], 'scores': [], 'net': 0 } for rnd in _parse_round(doc['Tournament']['TournamentRound']): for rst in rnd['results']: participantDict[rst[0]]['result'].append([rst[1], rst[2], rst[3]]) participantDict[rst[1]]['result'].append( [rst[0], _reverse_result(rst[2]), rst[3]]) for item in participantDict.items(): identity = item[0] participant = item[1] for r in participant['result']: versus = r[0] result = r[1] handicap = r[2] m = participantDict[identity] o = participantDict[versus] score = round( (elo(m['info']['rating'], o['info']['rating'], result, handicap)[0] - m['info']['rating']) * tournamentLevel, 2) participant['net'] = participant['net'] + score participant['scores'].append(score) participant['net'] = max(round(participant['net']), -100 * tournamentLevel) return (tournamentName, participantDict)
def get_elo_at_time(cur, conn, userID, time): return elo.elo(cur, conn, userID, time)
import numpy as np import pandas as pd import itertools import elo as elo elo=elo.elo() class elo_team: def __init__(self): self.k_factor=16 def team_elo_recent(self,Major_chongqing_major_Elo,k_score_recent1=16, k_score_recent2=32): dota_team_id=set(list(Major_chongqing_major_Elo.W_id)+list(Major_chongqing_major_Elo.L_id)) n_teams = len(dota_team_id) mean_elo=1000 current_elos = np.ones(shape=(n_teams))*mean_elo current_count = np.zeros(n_teams) last_start_time = np.zeros(shape=n_teams) last_win_before = np.zeros(shape=(n_teams)) for row in Major_chongqing_major_Elo.itertuples(): idx = row.Index w_id = row.W_team_id l_id = row.L_team_id start_time = row.start_time current_time = row.current_time recent_game =1 if start_time > (current_time - 5270400) else 0 # print(type(w_id)) # Get current elos w_elo_before = current_elos[w_id] l_elo_before = current_elos[l_id] w_team_times = current_count[w_id] l_team_times = current_count[l_id]
def post_game(self, opponentscore, score): self.score = round( elo(self.score, self.expected(Team("DUMMY_TEAM", opponentscore)), score), 3)