Skip to content

Commit

Permalink
Better info on games, wr count leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Aug 14, 2024
1 parent 2324733 commit a8454c9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
24 changes: 22 additions & 2 deletions highscores/templates/highscores/world_records.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@ <h1 style="color: white" class="display-4 fw-normal">
</h1>
</div>
</div>

<h2 class="text-center">World Records Count Per Player</h2>
<table class="table table-dark table-striped table-hover table-sm">
<thead>
<tr>
<th>Player Name</th>
<th style="text-align: center">Number of Records</th>
</tr>
</thead>
<tbody>
{% for player, count in player_counts %}
<tr>
<td>{{ player }}</td>
<td style="text-align: center">{{ count }}</td>
</tr>
{% endfor %}
</tbody>
</table>

<h2 class="text-center">Individual World Records</h2>
<table class="table table-dark table-striped table-hover table-sm">
<thead>
<tr>
<th style="text-align: center">Rank</th>
<th>Player Name</th>
<th style="text-align: center">Score</th>
<th style="text-align: center">Game</th>
<th style="text-align: center">Leaderboard</th>
<th style="text-align: center">Time Set</th>
<th style="text-align: center">Active For</th>
</tr>
Expand All @@ -27,7 +47,7 @@ <h1 style="color: white" class="display-4 fw-normal">
<a href="/user/{{ record.player.id }}" style="color: #fff">{{ record.player.username }}</a>
</td>
<td style="text-align: center">{{ record.score }}</td>
<td style="text-align: center">{{ record.leaderboard.game }}</td>
<td style="text-align: center">{{ record.leaderboard.game }} - {{ record.robot_name }}</td>
<td style="text-align: center">{{ record.time_set }}</td>
<td style="text-align: center">{{ record.active_for }}</td>
</tr>
Expand Down
14 changes: 10 additions & 4 deletions highscores/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db.models import Sum, Max
from django.utils.timezone import make_aware
from datetime import datetime
from collections import Counter

from .lib import extract_form_data, game_slug_to_submit_func
from .models import Leaderboard, Score
Expand Down Expand Up @@ -62,24 +63,29 @@ def world_records(request: HttpRequest) -> HttpResponse:
for leaderboard in leaderboards:
highest_score = Score.objects.filter(leaderboard=leaderboard, approved=True).order_by('-score', 'time_set').first()
if highest_score:
highest_score.robot_name = leaderboard.name # Include robot name
world_records.append(highest_score)

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

# Calculate how long each record has been active
now = make_aware(datetime.now())
for record in world_records:
now = make_aware(datetime.now())
time_set = record.time_set
active_duration = now - time_set

years, remainder = divmod(active_duration.total_seconds(), 31536000) # 60*60*24*365
months, remainder = divmod(remainder, 2592000) # 60*60*24*30
days, _ = divmod(remainder, 86400) # 60*60*24

record.active_for = f"{int(years)} years, {int(months)} months, {int(days)} days"

return render(request, WR_PAGE, {"world_records": world_records})
# Count the number of records per player
player_counts = Counter(record.player.username for record in world_records)
player_counts = sorted(player_counts.items(), key=lambda x: x[1], reverse=True)

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


def leaderboard_combined(request: HttpRequest, game_slug: str) -> HttpResponse:
Expand Down

0 comments on commit a8454c9

Please sign in to comment.