def calc_game_2p(p1_place, p1_r, p2_place, p2_r, name1, name2):
    elo_players = [EloPlayer(p1_place, p1_r), EloPlayer(p2_place, p2_r)]
    new_elos = calc_elo(elo_players)

    sql1 = "UPDATE players SET Rating = %d WHERE Name = '%s'" % (new_elos[0], name1)
    sql2 = "UPDATE players SET Rating = %d WHERE Name = '%s'" % (new_elos[1], name2)

    mycursor.execute(sql1)
    mycursor.execute(sql2)
    db.commit()

    print(new_elos)
def calc_game_4p(p1_place, p1_r, p2_place, p2_r, p3_place, p3_r, p4_place, p4_r, name1, name2, name3, name4):
    elo_players = [EloPlayer(p1_place, p1_r), EloPlayer(p2_place, p2_r), EloPlayer(p3_place, p3_r), EloPlayer(p4_place, p4_r)]
    new_elos = calc_elo(elo_players)
    
    sql1 = "UPDATE players SET Rating = %d WHERE Name = '%s'" % (new_elos[0], name1)
    sql2 = "UPDATE players SET Rating = %d WHERE Name = '%s'" % (new_elos[1], name2)
    sql3 = "UPDATE players SET Rating = %d WHERE Name = '%s'" % (new_elos[2], name3)
    sql4 = "UPDATE players SET Rating = %d WHERE Name = '%s'" % (new_elos[3], name4)

    mycursor.execute(sql1)
    mycursor.execute(sql2)
    mycursor.execute(sql3)
    mycursor.execute(sql4)
    db.commit()

    print(new_elos)
Example #3
0
    def generateJockeyElo(self, year_index):

        jockeyList = self._resultDictDf['jockey'].unique()
        jockeyDict = {}
        for x in jockeyList:
            jockeyDict[x] = 1500
        self._initJockeyElo = jockeyDict

        # features engineering
        tmpDf = self._resultDictDf
        if (year_index):
            tmpDf = tmpDf[tmpDf.year_index == year_index]

        tmpDf = self._resultDictDf['source'].apply(lambda x: x.split("_")[0])
        tmpDf = self._resultDictDf['source'].apply(
            lambda x: x.split("_")[-1]).astype(int)
        tmpDf = self._resultDictDf.sort_values(["date", "race_no"])
        tmpDf = tmpDf[[
            'source', 'date', 'race_no', 'jockey', 'finishing_position'
        ]]

        # transform result df into list of dict
        sourceList = tmpDf['source'].unique()
        master_list = []
        counter = 0
        for item in sourceList:
            print(" transforming df into list of dict... ", counter + 1)
            counter += 1
            resultDf = tmpDf[tmpDf.source == item]
            tmpDict = {}
            for row in resultDf.iterrows():
                tmpDict[row[1]['jockey']] = row[1]["finishing_position"]
            master_list.append(tmpDict)
            # if counter > 5:
            # break
        self._resultListOfDict = master_list

        #cal the new elo
        resultList = []
        for source, result in enumerate(self._resultListOfDict):
            eloPlayerList = []
            for participant in result:
                eloPlayerList.append(
                    EloPlayer(place=result[participant],
                              elo=self._initJockeyElo[participant]))
            list_of_player = list(result.keys())
            new_elos = calc_new_elos(eloPlayerList, self._kFactor)
            for index, item in enumerate(new_elos):
                self._initJockeyElo[list_of_player[index]] = item
                item_to_be_saved = [
                    sourceList[source], list_of_player[index],
                    eloPlayerList[index].elo, item
                ]
                resultList.append(item_to_be_saved)
        header_list = ['source', 'jockey', 'original_elo', 'new_elo']
        df = pd.DataFrame(data=resultList, columns=header_list)
        self._eloTrainerDf = df
Example #4
0
from random import randint

from multi_elo import EloPlayer, calc_new_elos

# генерация рандомных игркоов
elo_players = [
    EloPlayer(place=place, elo=randint(1200, 1800)) for place in range(1, 5)
]
for place, player in enumerate(elo_players, start=1):
    print('{}: {}'.format(place, player.elo))

# Создание коэффициента рейтинга
k = 16

# расчёт новых ЭЛО показателей
new_elos = calc_new_elos(elo_players, k)

for place, new_elo in enumerate(new_elos, start=1):
    print('{}: {}'.format(place, new_elo))
Example #5
0
                    matchtype=match_type,
                    date=match_date_obj)
 db.session.add(match_data)
 db.session.flush(
 )  # This will give us an ID for the match that has not yet been commited
 match_count += 1
 formatted_match = []
 place = 1
 usernames = []
 for player in match['match_scores']:
     username = player['username']
     usernames.append(username)
     if (username_to_rating.has_key(username)):
         rating = username_to_rating[username]['rating']
         formatted_match.append(
             EloPlayer(place=place, elo=rating))
     else:
         formatted_match.append(
             EloPlayer(place=place, elo=starting_rating))
     match_score = MatchScore(
         match_id=match_data.id,
         username=username,
         score=player['score'],
         place=place,
     )
     db.session.add(match_score)
     place += 1
 ratings = calc_elo(formatted_match, k_factor)
 for i in range(len(ratings)):
     username = usernames[i]
     old_rating = 1500