From 15043e4748dd6f2da55f3e82b53ab9fbf44511ad Mon Sep 17 00:00:00 2001 From: Brennan Bibic Date: Wed, 14 Aug 2024 12:43:53 -0400 Subject: [PATCH] Add percentile to individual robot leaderboards --- .../highscores/leaderboard_ranks.html | 20 +++++++++---------- highscores/views.py | 12 +++++++++-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/highscores/templates/highscores/leaderboard_ranks.html b/highscores/templates/highscores/leaderboard_ranks.html index 8ce05ea..18c0ea8 100644 --- a/highscores/templates/highscores/leaderboard_ranks.html +++ b/highscores/templates/highscores/leaderboard_ranks.html @@ -1,8 +1,10 @@ -{% extends 'home/base.html' %} {% block content %} +{% extends 'home/base.html' %} + +{% block content %}

- {{robot_name}} Leaderboard + {{ robot_name }} Leaderboard

@@ -13,23 +15,21 @@

Player Name Score Submission Date + Percentile {% for item in ls %} - {{item.0}} + {{ item.0 }} - {{item.1.player}} + {{ item.1.player }} - {{item.1.score}} + {{ item.1.score }} - {{item.1.time_set}} + {{ item.1.time_set }} + {{ item.2|floatformat:2 }}% {% endfor %} diff --git a/highscores/views.py b/highscores/views.py index 03aedee..fdfd4eb 100644 --- a/highscores/views.py +++ b/highscores/views.py @@ -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()