def update_ranks(players, ranks): """ Update skills based on ranks from a match """ teams = [skills.Team({player.name: skills.GaussianRating(player.mu, player.sigma)}) for player in players] match = skills.Match(teams, ranks) calc = trueskill.FactorGraphTrueSkillCalculator() game_info = trueskill.TrueSkillGameInfo() updated = calc.new_ratings(match, game_info) print ("Updating ranks") for team in updated: for i in team.keys(): skill_data = team[i] update_player_skill(players, i, skill_data)
def update_skills(players, ranks): """ Update player skills based on ranks from a match """ teams = [skills.Team({player.name: skills.GaussianRating(player.mu, player.sigma)}) for player in players] match = skills.Match(teams, ranks) calc = trueskill.FactorGraphTrueSkillCalculator() game_info = trueskill.TrueSkillGameInfo() updated = calc.new_ratings(match, game_info) for team in updated: player_name, skill_data = next(iter(team.items())) #in Halite, teams will always be a team of one player player = next(player for player in players if player.name == str(player_name)) #this suggests that players should be a dictionary instead of a list player.mu = skill_data.mean player.sigma = skill_data.stdev player.update_skill() print("skill = %4f mu = %3f sigma = %3f name = %s" % (player.skill, player.mu, player.sigma, str(player_name)))
def update_rating(match_map): logger.info('Updating ratings for match_map %d' % match_map.pk) cts = PlayerMatch.objects.filter(match_map=match_map, first_side=Match.SIDE_CT) ts = PlayerMatch.objects.filter(match_map=match_map, first_side=Match.SIDE_T) if len(cts) == 0: logger.warning('Got empty CT team for match_map %d' % match_map.pk) return if len(ts) == 0: logger.warning('Got empty T team for match_map %d' % match_map.pk) return ct_team = [] for pm in cts: partial_play = (pm.rounds_won + pm.rounds_lost + pm.rounds_tied) / \ (match_map.score_1 + match_map.score_2) if partial_play < 0.0001: partial_play = 0.0001 if partial_play > 1.0: logger.error('Got partial play percentage > 1 for match_map %d' % match_map.pk) partial_play = 1.0 player = skills.Player(player_id=pm.player.pk, partial_play_percentage=partial_play) rating = skills.GaussianRating(pm.player.rating, pm.player.rating_variance) ct_team.append(( player, rating, )) t_team = [] for pm in ts: partial_play = (pm.rounds_won + pm.rounds_lost + pm.rounds_tied) / \ (match_map.score_1 + match_map.score_2) if partial_play < 0.0001: partial_play = 0.0001 if partial_play > 1.0: logger.error('Got partial play percentage > 1 for match_map %d' % match_map.pk) partial_play = 1.0 player = skills.Player(player_id=pm.player.pk, partial_play_percentage=partial_play) rating = skills.GaussianRating(pm.player.rating, pm.player.rating_variance) t_team.append(( player, rating, )) if match_map.score_1 > match_map.score_2: rank = [1, 2] elif match_map.score_1 < match_map.score_2: rank = [2, 1] else: rank = [1, 1] teams = skills.Match([ct_team, t_team], rank=rank) calc = GpSkillCalculator() game_info = TrueSkillGameInfo() new_ratings = calc.new_ratings(teams, game_info) for team in [cts, ts]: for pm in team: player = pm.player old_skill = player.get_conservative_rating() rating = new_ratings.rating_by_id(player.pk) player.rating = rating.mean player.rating_variance = rating.stdev player.save() new_skill = player.get_conservative_rating() logger.info( 'Player %s: %f -> %f (%+f)' % (player.fullname, old_skill, new_skill, new_skill - old_skill))