Commit 3202268d authored by nachtgold's avatar nachtgold

new highscores as new tabs

parent 76e125fc
...@@ -49,6 +49,15 @@ ul.nav li:hover { ...@@ -49,6 +49,15 @@ ul.nav li:hover {
background-color: #804600; background-color: #804600;
} }
ul.sub.nav {
background-color: #492903;
left: 50%;
margin-top: 6px;
position: relative;
width: 500px;
transform: translateX(-50%);
}
a:hover { a:hover {
text-decoration: underline; text-decoration: underline;
} }
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
<li><a href="{% url 'snake_edit_latest' %}">Code</a></li> <li><a href="{% url 'snake_edit_latest' %}">Code</a></li>
{% endif %} {% endif %}
<li><a href="{% url 'docs' %}">API</a></li> <li><a href="{% url 'docs' %}">API</a></li>
<li><a href="{% url 'highscore_table' %}">Highscores</a></li> <li><a href="{% url 'highscore' %}">Highscores</a></li>
{% if user.is_authenticated %} {% if user.is_authenticated %}
<li><a href="{% url 'profile' %}">Profile</a></li> <li><a href="{% url 'profile' %}">Profile</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li> <li><a href="{% url 'logout' %}">Logout</a></li>
...@@ -45,6 +45,9 @@ ...@@ -45,6 +45,9 @@
</ul> </ul>
{% endblock %} {% endblock %}
{% block subheader %}
{% endblock %}
<div class="content"> <div class="content">
{% block content %} {% block content %}
{% endblock %} {% endblock %}
......
...@@ -5,11 +5,19 @@ ...@@ -5,11 +5,19 @@
<link rel="stylesheet" type="text/css" href="{% static "highscore/highscore.css" %}" /> <link rel="stylesheet" type="text/css" href="{% static "highscore/highscore.css" %}" />
{% endblock css %} {% endblock css %}
{% block subheader %}
<ul class="sub nav">
<li><a href="{% url 'highscore' %}">Max Score</a></li>
<li><a href="{% url 'highscore_maxage' %}">Max Age</a></li>
<li><a href="{% url 'highscore_consumerate' %}">Consume Rate</a></li>
</ul>
{% endblock %}
{% block content %} {% block content %}
<div class="panel"> <div class="panel">
{% if usr %} {% if usr %}
<h2>Your score</h2> <h2>Your {{title}}</h2>
<table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em"> <table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em">
<tr> <tr>
<td class="two wide">{{usr.position}}</td> <td class="two wide">{{usr.position}}</td>
...@@ -20,13 +28,13 @@ ...@@ -20,13 +28,13 @@
</table> </table>
{% endif %} {% endif %}
<h2>Highscores</h2> <h2>Best {{title}}s</h2>
<table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em"> <table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em">
<thead> <thead>
<tr> <tr>
<th class="two wide">Position</th> <th class="two wide">Position</th>
<th class="nine wide">User</th> <th class="nine wide">User</th>
<th class="five wide right aligned">Score</th> <th class="five wide right aligned">Value</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
......
...@@ -2,5 +2,7 @@ from django.urls import path ...@@ -2,5 +2,7 @@ from django.urls import path
from . import views as highscore_views from . import views as highscore_views
urlpatterns = [ urlpatterns = [
path('', highscore_views.table, name='highscore_table') path('maxage', highscore_views.maxage, name='highscore_maxage'),
path('consumerate', highscore_views.consumerate, name='highscore_consumerate'),
path('', highscore_views.score, name='highscore'),
] ]
from django.shortcuts import render from django.shortcuts import render
from django.db.models import Max from django.db.models import F, Max, ExpressionWrapper, FloatField
from django.db.models.query import RawQuerySet
from core.models import SnakeGame from core.models import SnakeGame
from django.db.models.expressions import RawSQL
def table(request): def sattr(obj, attr, val):
if request.user.is_authenticated: if isinstance(obj, dict):
usr = SnakeGame.objects.filter(user=request.user).aggregate(score=Max('final_mass')) obj[attr] = val
else:
setattr(obj, attr, val)
data = SnakeGame.objects.values('user__username').annotate(score=Max('final_mass')).order_by('-score') def gattr(obj, attr):
for i in range(len(data)): if type(obj) is dict:
data[i]['position'] = '{}.'.format(i+1) return obj[attr]
if request.user.is_authenticated and data[i]['user__username'] == request.user.username: else:
usr['position'] = data[i]['position'] return getattr(obj, attr)
context={'highscores': data}
def table(request, data, usr, title):
i = 0
for d in data:
sattr(d, 'position', '{}.'.format(i+1))
if request.user.is_authenticated and gattr(d, 'user__username') == request.user.username:
sattr(usr, 'position', gattr(d, 'position'))
i = i + 1
context={'highscores': data, 'title': title}
if request.user.is_authenticated: if request.user.is_authenticated:
context['usr'] = usr context['usr'] = usr
return render(request, 'highscore/table.html', context=context) return render(request, 'highscore/table.html', context=context)
def score(request):
data = SnakeGame.objects.values('user__username').annotate(score=Max('final_mass')).order_by('-score')
if request.user.is_authenticated:
usr = SnakeGame.objects.filter(user=request.user).aggregate(score=Max('final_mass'))
else:
usr = False
return table(request, data, usr, 'Highscore')
def maxage(request):
data = SnakeGame.objects.values('user__username').annotate(score=Max(F('end_frame')-F('start_frame'))).order_by('-score')
if request.user.is_authenticated:
usr = SnakeGame.objects.filter(user=request.user).aggregate(score=Max(F('end_frame')-F('start_frame')))
else:
usr = False
return table(request, data, usr, 'Max Age')
def consumerate(request):
data = SnakeGame.objects.raw(
'''SELECT 1 as "id", "auth_user"."username" as "user__username",
max(
(natural_food_consumed + carrison_food_consumed + hunted_food_consumed)
/
(end_frame - start_frame)
) AS "score"
FROM "core_snakegame" LEFT OUTER JOIN "auth_user"
ON ("core_snakegame"."user_id" = "auth_user"."id")
GROUP BY "auth_user"."username"
ORDER BY
(natural_food_consumed + carrison_food_consumed + hunted_food_consumed)
/
(end_frame - start_frame) DESC
''')
ndata = []
for d in data:
ndata.append({'user__username': d.user__username, 'score': d.score})
data = ndata
if request.user.is_authenticated:
usr = SnakeGame.objects.raw(
'''
SELECT 1 as "id",
max(
(natural_food_consumed + carrison_food_consumed + hunted_food_consumed)
/
(end_frame - start_frame)
) AS "score"
FROM "core_snakegame" LEFT OUTER JOIN "auth_user"
ON ("core_snakegame"."user_id" = "auth_user"."id")
WHERE "auth_user"."id" = %s
GROUP BY "auth_user"."id"
''', [request.user.id]
)[0]
else:
usr = False
return table(request, data, usr, 'Consume Rate')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment