Commit 3202268d authored by nachtgold's avatar nachtgold

new highscores as new tabs

parent 76e125fc
......@@ -49,6 +49,15 @@ ul.nav li:hover {
background-color: #804600;
}
ul.sub.nav {
background-color: #492903;
left: 50%;
margin-top: 6px;
position: relative;
width: 500px;
transform: translateX(-50%);
}
a:hover {
text-decoration: underline;
}
......
......@@ -34,7 +34,7 @@
<li><a href="{% url 'snake_edit_latest' %}">Code</a></li>
{% endif %}
<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 %}
<li><a href="{% url 'profile' %}">Profile</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
......@@ -45,6 +45,9 @@
</ul>
{% endblock %}
{% block subheader %}
{% endblock %}
<div class="content">
{% block content %}
{% endblock %}
......
......@@ -5,11 +5,19 @@
<link rel="stylesheet" type="text/css" href="{% static "highscore/highscore.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 %}
<div class="panel">
{% 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">
<tr>
<td class="two wide">{{usr.position}}</td>
......@@ -20,13 +28,13 @@
</table>
{% endif %}
<h2>Highscores</h2>
<h2>Best {{title}}s</h2>
<table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em">
<thead>
<tr>
<th class="two wide">Position</th>
<th class="nine wide">User</th>
<th class="five wide right aligned">Score</th>
<th class="five wide right aligned">Value</th>
</tr>
</thead>
<tbody>
......
......@@ -2,5 +2,7 @@ from django.urls import path
from . import views as highscore_views
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.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 django.db.models.expressions import RawSQL
def table(request):
if request.user.is_authenticated:
usr = SnakeGame.objects.filter(user=request.user).aggregate(score=Max('final_mass'))
def sattr(obj, attr, val):
if isinstance(obj, dict):
obj[attr] = val
else:
setattr(obj, attr, val)
data = SnakeGame.objects.values('user__username').annotate(score=Max('final_mass')).order_by('-score')
for i in range(len(data)):
data[i]['position'] = '{}.'.format(i+1)
if request.user.is_authenticated and data[i]['user__username'] == request.user.username:
usr['position'] = data[i]['position']
def gattr(obj, attr):
if type(obj) is dict:
return obj[attr]
else:
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:
context['usr'] = usr
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