Skip to content

Commit

Permalink
add wrs page
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Aug 14, 2024
1 parent f9d406f commit f67014f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
33 changes: 33 additions & 0 deletions highscores/templates/highscores/world_records.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>World Records</title>
<link rel="stylesheet" href="{% static 'styles.css' %}">
</head>
<body>
<h1>World Records</h1>
<table>
<thead>
<tr>
<th>Rank</th>
<th>Player</th>
<th>Score</th>
<th>Game</th>
<th>Time Set</th>
</tr>
</thead>
<tbody>
{% for record in world_records %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ record.player.username }}</td>
<td>{{ record.score }}</td>
<td>{{ record.leaderboard.game }}</td>
<td>{{ record.time_set }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
1 change: 1 addition & 0 deletions highscores/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
name="game leaderboard"),
path("<str:game_slug>/<str:name>/",
views.leaderboard_robot, name="robot leaderboard"),
path('world-records/', views.world_records, name='world-records'),
]
17 changes: 17 additions & 0 deletions highscores/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
SUBMIT_PAGE = "highscores/submit.html"
SUBMIT_ACCEPTED_PAGE = "highscores/submit_accepted.html"
SUBMIT_ERROR_PAGE = "highscores/submit_error.html"
WR_PAGE = "highscores/world_records.html"


def home(request: HttpRequest) -> HttpResponse:
Expand Down Expand Up @@ -50,6 +51,22 @@ def leaderboard_robot(request: HttpRequest, game_slug: str, name: str) -> HttpRe

return render(request, "highscores/leaderboard_ranks.html", {"ls": context, "robot_name": name})

def world_records(request: HttpRequest) -> HttpResponse:
# Get all leaderboards
leaderboards = Leaderboard.objects.all()

# Collect the highest scores for each leaderboard
world_records = []
for leaderboard in leaderboards:
highest_score = Score.objects.filter(leaderboard=leaderboard, approved=True).order_by('-score', 'time_set').first()
if highest_score:
world_records.append(highest_score)

# Sort the world records by the date they were set
world_records.sort(key=lambda x: x.time_set)

return render(request, WR_PAGE, {"world_records": world_records})


def leaderboard_combined(request: HttpRequest, game_slug: str) -> HttpResponse:
if not Leaderboard.objects.filter(game_slug=game_slug).exists():
Expand Down

0 comments on commit f67014f

Please sign in to comment.