class EloTests(unittest.TestCase, CalculatorTests): def setUp(self): self.calculator = EloCalculator() def test_one_on_one(self): game_info = EloGameInfo(1200, 200) teams = Match([{1: (1200, 25)}, {2: (1400, 25)}], [1, 2]) new_ratings = self.calculator.new_ratings(teams, game_info) self.assertMatchQuality( 0.4805, self.calculator.match_quality(teams, game_info)) self.assertRating(1218.99, new_ratings.rating_by_id(1)) self.assertRating(1381.01, new_ratings.rating_by_id(2))
class EloTests(unittest.TestCase, CalculatorTests): def setUp(self): self.calculator = EloCalculator() def test_one_on_one(self): game_info = EloGameInfo(1200, 200) teams = Match([{1: (1200, 25)}, {2: (1400, 25)}], [1, 2]) new_ratings = self.calculator.new_ratings(teams, game_info) self.assertMatchQuality(0.4805, self.calculator.match_quality(teams, game_info)) self.assertRating(1218.99, new_ratings.rating_by_id(1)) self.assertRating(1381.01, new_ratings.rating_by_id(2))
class EloService(object): def __init__(self): self.calculator = EloCalculator() def update(self, match, session): ranking = [1, 2] if match.winner == match.user_1 else [2, 1] skills_match = SkillsMatch([{ 1: match.user_1.elo }, { 2: match.user_2.elo }], ranking) new_ratings = self.calculator.new_ratings(skills_match) print(new_ratings) match.user_1.elo = new_ratings.rating_by_id(1).mean match.user_2.elo = new_ratings.rating_by_id(2).mean session.add(match.user_1) session.add(match.user_2) session.commit()
def save(self, *args, **kwargs): obj = super(Match, self).save(*args, **kwargs) win = {} loss = {} if self.victory == self.TEAM_A: winners = self.team_a_players.all() lossers = self.team_b_players.all() elif self.victory == self.TEAM_B: winners = self.team_a_players.all() lossers = self.team_b_players.all() else: return obj for player in winners: player.victories += 1 player.save() win.update({ player.id: (player.ranking, self.FACTOR), }) for player in lossers: player.losses += 1 player.save() loss.update({ player.id: (player.ranking, self.FACTOR), }) # elo rating calculator = EloCalculator() game_info = EloGameInfo(self.BASE_ELO, self.BETA) teams = MatchElo([win, loss], [1, 2]) new_ratings = calculator.new_ratings(teams, game_info) for player in winners: player.ranking = int(new_ratings.rating_by_id(player.id).mean) player.save() for player in lossers: player.ranking = int(new_ratings.rating_by_id(player.id).mean) player.save() return obj