def generate_ratings(): """Generate ratings from scratch based on all previous matches.""" elo_rating = EloRating() matches = Match.objects.all().order_by('datetime') for match in matches: elo_rating.update_ratings(match.winner, match.loser) PlayerRating.add_ratings(elo_rating)
def test_use_current_rating(self): """Test that the EloRating class can use current ratings.""" player = Player.objects.create(first_name='Bob', last_name='Hope') test_rating = 1013 rated_player = PlayerRating.objects.create(player=player, rating=test_rating) rating = EloRating(use_current_ratings=True) self.assertEqual(test_rating, rating.get_rating(player))
def generate_ratings(): """Generate ratings from all previous matches.""" PlayerRating.objects.all().delete() matches = Match.objects.all().order_by('datetime') elo_rating = EloRating() for match in matches: elo_rating.update_ratings(match.winner, match.loser) for player, rating in elo_rating.ratings.items(): PlayerRating.objects.create(player=player, rating=rating)
def save(self, *args, **kwargs): if self.id: # occurs when the match already exists and is being updated super().save(*args, **kwargs) PlayerRating.generate_ratings() else: # occurs when it's a new match being added super().save(*args, **kwargs) elo_rating = EloRating(use_current_ratings=True) elo_rating.update_ratings(self.winner, self.loser) PlayerRating.add_ratings(elo_rating)
def test_new_ratings(self): """Test that new ratings are calculated properly.""" new_winner_rating, new_loser_rating = EloRating().calculate_new_ratings( self.winner_rating, self.loser_rating ) self.assertEqual(new_winner_rating, self.new_winner_rating) self.assertEqual(new_loser_rating, self.new_loser_rating)
def test_default_expected_score(self): """ Test that the default expected score is calculated properly. With two players who have never played before, their default expected score should be 0.5. """ default_expected_score = EloRating().get_expected_score('player1', 'player2') self.assertEqual(default_expected_score, 0.5)
def test_rating_updates(self): """Test that the EloRating class updates and stores ratings.""" rating = EloRating() rating.update_ratings(winner='player1', loser='player2') winner_rating = rating.get_rating('player1') loser_rating = rating.get_rating('player2') expected_winner_rating = DEFAULT_ELO_RATING + DEFAULT_K_FACTOR * 0.5 expected_loser_rating = DEFAULT_ELO_RATING - DEFAULT_K_FACTOR * 0.5 self.assertEqual(winner_rating, expected_winner_rating) self.assertEqual(loser_rating, expected_loser_rating)
def test_loser_expected_score(self): """Test that loser's expected score is calculated properly.""" loser_expected_score = EloRating.calculate_expected_score( self.loser_rating, self.winner_rating) self.assertEqual(loser_expected_score, self.loser_expected_score)
def test_default_rating(self): """Test that the default rating is correct.""" self.assertEqual(EloRating().get_rating('player'), DEFAULT_ELO_RATING)