mirror of
https://github.com/Without-Proper-Instructions/GoatCTF
synced 2025-10-08 07:40:46 +00:00
Change leaderboard to support multiple submissions
WARNING: Makes get_leaderboard O(num teams)
This commit is contained in:
parent
a88e67a7d8
commit
9f1d7d6d99
@ -49,13 +49,12 @@ class Team(models.Model):
|
||||
return points[0]['total_points'] if points else 0
|
||||
|
||||
@classmethod
|
||||
def get_leaderboard(self):
|
||||
sum_points = models.Sum('player__solution__challenge__points')
|
||||
def get_leaderboard(cls):
|
||||
"""Gets the leaderboard by most points and most recent solution."""
|
||||
max_solved_at = models.Max('player__solution__solved_at')
|
||||
all_annotated = Team.objects.all().annotate(points=sum_points)
|
||||
all_annotated = all_annotated.annotate(solved_at=max_solved_at)
|
||||
all_sorted = all_annotated.order_by('-points', 'solved_at')
|
||||
return all_sorted
|
||||
all_annotated = Team.objects.all().annotate(solved_at=max_solved_at)
|
||||
all_sorted = all_annotated.order_by('solved_at')
|
||||
return sorted(all_sorted, key=lambda t: t.points(), reverse=True)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not hasattr(self, 'creator'):
|
||||
|
@ -110,9 +110,9 @@ def test_leaderboard_counts_challenges_once(challenge, player_factory, fresh_tea
|
||||
other_team.save()
|
||||
member = Player(username='player', password='', team=other_team)
|
||||
member.save()
|
||||
Solution(challenge=challenge, solver=member).save()
|
||||
Solution(challenge=challenge, solver=other_team.creator).save()
|
||||
Solution(challenge=other_challenge, solver=fresh_team.creator).save()
|
||||
Solution(challenge=other_challenge, solver=member).save()
|
||||
Solution(challenge=other_challenge, solver=other_team.creator).save()
|
||||
Solution(challenge=challenge, solver=fresh_team.creator).save()
|
||||
leaderboard = Team.get_leaderboard()
|
||||
assert (leaderboard[0] == other_team and
|
||||
leaderboard[1] == fresh_team and
|
||||
|
Loading…
x
Reference in New Issue
Block a user