Example #1
0
 def clear_glicko():
     ranks = Rank_Glicko.query().all()
     for rank in ranks:
         rank.rd = 350
         rank.rating = 350
         rank.last_match = 0
         rank.save(commit=False)
     Rank_Glicko.commit()
Example #2
0
 def clear_glicko():
     ranks = Rank_Glicko.query().all()
     for rank in ranks:
         rank.rd = 350
         rank.rating = 350
         rank.last_match = 0
         rank.save(commit=False)
     Rank_Glicko.commit()
Example #3
0
def import_results(args):
    """
    Imports the match data of a csv file into the result table.

    :param args: A list with arguments from the argument parser
    :type args: namespace
    """

    print "Importing results from", args.file

    try:
        csvfile, reader, hasHeader = utils.get_csv(args.file)
        line = 0

        if hasHeader:
            print "\tFirst line of csv file is ignored. It seems to be a " \
                  "header row.\n"

        # nickname --> player
        players = {}

        for row in reader:
            if line != 0 or (line == 0 and not hasHeader):
                if row[1] is row[2]:
                    continue

                player1 = players.get(row[1], None)
                player2 = players.get(row[2], None)
                if player1 is None:
                    player1, created = utils.add_player(row[1], commit=False)
                    players[player1.nickname.value] = player1
                if player2 is None:
                    player2, created = utils.add_player(row[2], commit=False)
                    players[player2.nickname.value] = player2

                dbRow = Match1on1(player1=player1.player_id.value,
                                  player2=player2.player_id.value,
                                  outcome=row[3],
                                  date=row[0])
                dbRow.save(commit=False)

            if line % 100 == 0:
                sys.stdout.write("\r" + "Imported %d entries..." % line)
                sys.stdout.flush()
            line = line + 1

        Rank_Elo.commit()
        Rank_Glicko.commit()
        Match1on1.commit()
        csvfile.close()
        print "\rImported %d entries." % (line - (1 if hasHeader else 0))
    except csv.Error:
        print "Error importing %s in line %d" % (args.file, line)
    except IOError:
        print "No such file: %s" % args.file
Example #4
0
def import_results(args):
    """
    Imports the match data of a csv file into the result table.

    :param args: A list with arguments from the argument parser
    :type args: namespace
    """

    print "Importing results from", args.file

    try:
        csvfile, reader, hasHeader = utils.get_csv(args.file)
        line = 0

        if hasHeader:
            print "\tFirst line of csv file is ignored. It seems to be a " \
                  "header row.\n"

        # nickname --> player
        players = {}

        for row in reader:
            if line != 0 or (line == 0 and not hasHeader):
                if row[1] is row[2]:
                    continue

                player1 = players.get(row[1], None)
                player2 = players.get(row[2], None)
                if player1 is None:
                    player1, created = utils.add_player(row[1], commit=False)
                    players[player1.nickname.value] = player1
                if player2 is None:
                    player2, created = utils.add_player(row[2], commit=False)
                    players[player2.nickname.value] = player2

                dbRow = Match1on1(player1=player1.player_id.value,
                    player2=player2.player_id.value, outcome=row[3],
                    date=row[0])
                dbRow.save(commit=False)

            if line % 100 == 0:
                sys.stdout.write("\r" + "Imported %d entries..." % line)
                sys.stdout.flush()
            line = line + 1

        Rank_Elo.commit()
        Rank_Glicko.commit()
        Match1on1.commit()
        csvfile.close()
        print "\rImported %d entries." % (line - (1 if hasHeader else 0))
    except csv.Error:
        print "Error importing %s in line %d" % (args.file, line)
    except IOError:
        print "No such file: %s" % args.file
Example #5
0
    def best_worst_glicko():
        ranks = Rank_Glicko.query().all()
        ranks = sorted(ranks, key=lambda x: x.rating.value - (x.rd.value), reverse=best)

        table = [['Rank', 'Rating', 'RD', 'Nick', 'Firstname', 'Lastname', 'ID']]
        #print "Rank\tRating\tRD\tNick\tForename\tSurname\tid"

        for i in range(min(args.amount, len(ranks))):
            player = Player().query().get(player_id=ranks[i].player_id.value)
            #print "%d\t%d\t%d\t%s\t%s,\t%s\t%s" % (i + 1, ranks[i].rating.value,
            #    ranks[i].rd.value, player.nickname.value, player.firstname.value,
            #    player.lastname.value, player.player_id.value)
            table.append([i + 1, int(ranks[i].rating.value),
                int(ranks[i].rd.value), player.nickname.value, player.firstname.value,
                player.lastname.value, player.player_id.value])
        utils.print_table(table)
Example #6
0
    def best_worst_glicko():
        ranks = Rank_Glicko.query().all()
        ranks = sorted(ranks,
                       key=lambda x: x.rating.value - (x.rd.value),
                       reverse=best)

        table = [[
            'Rank', 'Rating', 'RD', 'Nick', 'Firstname', 'Lastname', 'ID'
        ]]
        #print "Rank\tRating\tRD\tNick\tForename\tSurname\tid"

        for i in range(min(args.amount, len(ranks))):
            player = Player().query().get(player_id=ranks[i].player_id.value)
            #print "%d\t%d\t%d\t%s\t%s,\t%s\t%s" % (i + 1, ranks[i].rating.value,
            #    ranks[i].rd.value, player.nickname.value, player.firstname.value,
            #    player.lastname.value, player.player_id.value)
            table.append([
                i + 1,
                int(ranks[i].rating.value),
                int(ranks[i].rd.value), player.nickname.value,
                player.firstname.value, player.lastname.value,
                player.player_id.value
            ])
        utils.print_table(table)
Example #7
0
 def rating_glicko(player):
     if player is None:
         return None
     return Rank_Glicko.query().get(player_id=player.player_id.value)
Example #8
0
    def update_glicko():
        sys.stdout.write("Query matches...")
        matches = Match1on1.query().all()
        sys.stdout.write("\rBeginning to update %d matches" % len(matches))
        print ""

        # Query all ratings and store it in a dictionary. This is done to store
        # the newest rating data in memory. We do not have to commit.
        ratings = Rank_Glicko.query().all()
        rdict = {}
        for r in ratings:
            rdict[r.player_id.value] = r

        # sort matches by date
        matches = sorted(matches, key=lambda x: x.date.value)
        mDict = {}

        for match in matches:
            if match.date.value in mDict:
                mDict[match.date.value].append(match)
            else:
                mDict[match.date.value] = [match]

        # for each rating period...
        for period, pMatches in mDict.iteritems():
            # players in current period --> (RD, rating)
            pDict = {}
            for match in pMatches:
                for player in [match.player1.value, match.player2.value]:
                    if player not in pDict:
                        pDict[player] = (rdict[player].rd.value,
                                         rdict[player].rating.value)
                        # glicko.chess.c
                        curRD = glicko.getCurrentRD(
                            pDict[player][0], 15.8,
                            period - rdict[player].last_match.value)
                        curRating = rdict[player].rating.value
                        # search all matches the player participated, in period
                        ratingList = []
                        RDList = []
                        outcomeList = []
                        for m in pMatches:
                            # player is player1 of match
                            if m.player1.value == player:
                                if m.player2.value in pDict:
                                    ratingList.append(
                                        pDict[m.player2.value][1])
                                    RDList.append(pDict[m.player2.value][0])
                                else:
                                    ratingList.append(
                                        rdict[m.player2.value].rating.value)
                                    RDList.append(
                                        rdict[m.player2.value].rd.value)
                                outcomeList.append(m.outcome.value)
                            # player player2 of match
                            if m.player2.value == player:
                                if m.player1.value in pDict:
                                    ratingList.append(
                                        pDict[m.player1.value][1])
                                    RDList.append(pDict[m.player1.value][0])
                                else:
                                    ratingList.append(
                                        rdict[m.player1.value].rating.value)
                                    RDList.append(
                                        rdict[m.player1.value].rd.value)
                                outcomeList.append(1.0 - m.outcome.value)

                        # calculate new rating
                        newRating = glicko.newRating(curRD, curRating,
                                                     ratingList, RDList,
                                                     outcomeList)
                        newRD = glicko.newRD(curRD, newRating, ratingList,
                                             RDList)

                        rdict[player].rd.value = newRD
                        rdict[player].rating.value = newRating
                        rdict[player].last_match.value = period
                        rdict[player].save(commit=False)

            Rank_Glicko.commit()
            stage = period % 4
            if stage == 0:
                sys.stdout.write("\r| ")
            elif stage == 1:
                sys.stdout.write("\r/ ")
            elif stage == 2:
                sys.stdout.write("\r--")
            else:
                sys.stdout.write("\r\\ ")
            sys.stdout.flush()
        print "\rDone."
Example #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pychallenge.models import Config, Match1on1, Player, Rank_Elo, Rank_Glicko

if __name__ == "__main__":
    Config.create()
    Match1on1.create()
    Player.create()
    Rank_Elo.create()
    Rank_Glicko.create()
Example #10
0
    def update_glicko():
        sys.stdout.write("Query matches...")
        matches = Match1on1.query().all()
        sys.stdout.write("\rBeginning to update %d matches" % len(matches))
        print ""

        # Query all ratings and store it in a dictionary. This is done to store
        # the newest rating data in memory. We do not have to commit.
        ratings = Rank_Glicko.query().all()
        rdict = {}
        for r in ratings:
            rdict[r.player_id.value] = r

        # sort matches by date
        matches = sorted(matches, key=lambda x: x.date.value)
        mDict = {}

        for match in matches:
            if match.date.value in mDict:
                mDict[match.date.value].append(match)
            else:
                mDict[match.date.value] = [match]

        # for each rating period...
        for period, pMatches in mDict.iteritems():
            # players in current period --> (RD, rating)
            pDict = {}
            for match in pMatches:
                for player in [match.player1.value, match.player2.value]:
                    if player not in pDict:
                        pDict[player] = (rdict[player].rd.value,
                            rdict[player].rating.value)
                        # glicko.chess.c
                        curRD = glicko.getCurrentRD(pDict[player][0], 15.8,
                            period - rdict[player].last_match.value)
                        curRating = rdict[player].rating.value
                        # search all matches the player participated, in period
                        ratingList = []
                        RDList = []
                        outcomeList = []
                        for m in pMatches:
                            # player is player1 of match
                            if m.player1.value == player:
                                if m.player2.value in pDict:
                                    ratingList.append(
                                        pDict[m.player2.value][1])
                                    RDList.append(pDict[m.player2.value][0])
                                else:
                                    ratingList.append(
                                        rdict[m.player2.value].rating.value)
                                    RDList.append(
                                        rdict[m.player2.value].rd.value)
                                outcomeList.append(m.outcome.value)
                            # player player2 of match
                            if m.player2.value == player:
                                if m.player1.value in pDict:
                                    ratingList.append(
                                        pDict[m.player1.value][1])
                                    RDList.append(pDict[m.player1.value][0])
                                else:
                                    ratingList.append(
                                        rdict[m.player1.value].rating.value)
                                    RDList.append(
                                        rdict[m.player1.value].rd.value)
                                outcomeList.append(1.0 - m.outcome.value)

                        # calculate new rating
                        newRating = glicko.newRating(curRD, curRating,
                            ratingList, RDList, outcomeList)
                        newRD = glicko.newRD(curRD, newRating, ratingList,
                            RDList)

                        rdict[player].rd.value = newRD
                        rdict[player].rating.value = newRating
                        rdict[player].last_match.value = period
                        rdict[player].save(commit=False)

            Rank_Glicko.commit()
            stage = period % 4
            if stage == 0:
                sys.stdout.write("\r| ")
            elif stage == 1:
                sys.stdout.write("\r/ ")
            elif stage == 2:
                sys.stdout.write("\r--")
            else:
                sys.stdout.write("\r\\ ")
            sys.stdout.flush()
        print "\rDone."