diff --git a/core/static/core/styles.css b/core/static/core/styles.css index 7aa2f28abd15b0f1a50c0554c9ceb3fd0a1e753f..0e0103281a33e80ca246826556dadb31126f59ae 100644 --- a/core/static/core/styles.css +++ b/core/static/core/styles.css @@ -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; } diff --git a/core/templates/core/base.html b/core/templates/core/base.html index 9b02b7bae4d65ad05565292810507ee5e930f907..a86331c0f1f41e263304848c3f08fc1b4c2af13a 100644 --- a/core/templates/core/base.html +++ b/core/templates/core/base.html @@ -34,7 +34,7 @@
  • Code
  • {% endif %}
  • API
  • -
  • Highscores
  • +
  • Highscores
  • {% if user.is_authenticated %}
  • Profile
  • Logout
  • @@ -45,6 +45,9 @@ {% endblock %} +{% block subheader %} +{% endblock %} +
    {% block content %} {% endblock %} diff --git a/highscore/templates/highscore/table.html b/highscore/templates/highscore/table.html index 7c4f5efd373219aa74abd29c994a642a399c68bd..243d951ebcff80d7f465adbb620b8f1f8612adee 100644 --- a/highscore/templates/highscore/table.html +++ b/highscore/templates/highscore/table.html @@ -5,11 +5,19 @@ {% endblock css %} +{% block subheader %} + +{% endblock %} + {% block content %}
    {% if usr %} -

    Your score

    +

    Your {{title}}

    @@ -20,13 +28,13 @@
    {{usr.position}}
    {% endif %} -

    Highscores

    +

    Best {{title}}s

    - + diff --git a/highscore/urls.py b/highscore/urls.py index 3d56b53fca045f78c1eff5511e0c256b9eb9f889..d1b9b53303e29fbe8cf0462123b20f89d59ef3ca 100644 --- a/highscore/urls.py +++ b/highscore/urls.py @@ -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'), ] diff --git a/highscore/views.py b/highscore/views.py index 342355ea1dc2309c21f157b131f73d9f3e8f10a3..7f448d1b32334f6a857e11207052b4989b3c7962 100644 --- a/highscore/views.py +++ b/highscore/views.py @@ -1,20 +1,92 @@ 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')
    Position UserScoreValue