Skip to content

Commit

Permalink
Prettyify wrs page, add wr duration
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Aug 14, 2024
1 parent 30e3301 commit 2324733
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
71 changes: 37 additions & 34 deletions highscores/templates/highscores/world_records.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>World Records</title>
{% load static %}
<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>
{% 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">
World Records
</h1>
</div>
</div>
<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">Time Set</th>
<th style="text-align: center">Active For</th>
</tr>
</thead>
<tbody>
{% for record in world_records %}
<tr>
<td style="text-align: center"><b>{{ forloop.counter }}</b></td>
<td>
<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.time_set }}</td>
<td style="text-align: center">{{ record.active_for }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
14 changes: 14 additions & 0 deletions highscores/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.db.models import Sum, Max
from django.utils.timezone import make_aware
from datetime import datetime

from .lib import extract_form_data, game_slug_to_submit_func
from .models import Leaderboard, Score
Expand Down Expand Up @@ -65,6 +67,18 @@ def world_records(request: HttpRequest) -> HttpResponse:
# 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
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})


Expand Down

0 comments on commit 2324733

Please sign in to comment.