Skip to content

Commit

Permalink
Add percentile to individual robot leaderboards
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Aug 14, 2024
1 parent 98e6952 commit 15043e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 10 additions & 10 deletions highscores/templates/highscores/leaderboard_ranks.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{% extends 'home/base.html' %} {% block content %}
{% extends 'home/base.html' %}

{% block content %}
<div class="position-relative overflow-hidden text-center bg-danger">
<div class="p-lg-4 mx-auto">
<h1 style="color: white" class="display-4 fw-normal">
{{robot_name}} Leaderboard
{{ robot_name }} Leaderboard
</h1>
</div>
</div>
Expand All @@ -13,23 +15,21 @@ <h1 style="color: white" class="display-4 fw-normal">
<th>Player Name</th>
<th style="text-align: center">Score</th>
<th style="text-align: center">Submission Date</th>
<th style="text-align: center">Percentile</th>
</tr>
</thead>
<tbody>
{% for item in ls %}
<tr>
<td style="text-align: center"><b>{{item.0}}</b></td>
<td style="text-align: center"><b>{{ item.0 }}</b></td>
<td>
<a href="/user/{{item.1.player.id}}" style="color: #fff"
>{{item.1.player}}</a
>
<a href="/user/{{ item.1.player.id }}" style="color: #fff">{{ item.1.player }}</a>
</td>
<td style="text-align: center">{{item.1.score}}</td>
<td style="text-align: center">{{ item.1.score }}</td>
<td style="text-align: center">
<a href="{{item.1.source}}" style="color: #fff"
>{{item.1.time_set}}</a
>
<a href="{{ item.1.source }}" style="color: #fff">{{ item.1.time_set }}</a>
</td>
<td style="text-align: center">{{ item.2|floatformat:2 }}%</td>
</tr>
{% endfor %}
</tbody>
Expand Down
12 changes: 10 additions & 2 deletions highscores/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ def leaderboard_robot(request: HttpRequest, game_slug: str, name: str) -> HttpRe
sorted_board = Score.objects.filter(
leaderboard__game_slug=game_slug, leaderboard__name=name, approved=True).order_by('-score', 'time_set')

# Find the highest score (world record)
if sorted_board.exists():
highest_score = sorted_board.first().score
else:
highest_score = 1 # Avoid division by zero

i = 1
context = []
# Create ranking numbers and append them to sorted values
# Create ranking numbers, calculate percentiles, and append them to sorted values
for item in sorted_board:
context.append([i, item])
percentile = (item.score / highest_score) * 100
context.append([i, item, percentile])
i += 1

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()
Expand Down

0 comments on commit 15043e4

Please sign in to comment.