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()
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
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."
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."