Commit bd7c06f9 authored by esclear's avatar esclear
Browse files

Merge branch 'branch/user_requests' into 'master'

improved Highscore view

See merge request GPN18Programmierspiel/website!12
parents e76c5c3e 635afbac
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
.ui.table.highscore-table { .ui.table.highscore-table {
font-size: .6em; font-size: .6em;
margin-left: 200px;
width: 60%; width: 60%;
} }
/* /*
......
...@@ -7,22 +7,37 @@ ...@@ -7,22 +7,37 @@
{% block content %} {% block content %}
<h2>Highscores</h2> <div class="panel">
<table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em"> {% if usr %}
<thead> <h2>Your score</h2>
<tr> <table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em">
<th class="eleven wide">User</th> <tr>
<th class="five wide right aligned">Score</th> <td class="two wide">{{usr.position}}</td>
</tr> <td class="nine wide">{{request.user.username}}</td>
</thead> <td class="five wide right aligned">{{usr.score|floatformat:1}}</td>
<tbody> </tr>
{% for score in highscores %} </tbody>
<tr> </table>
<td>{{score.user__username}}</td> {% endif %}
<td class="right aligned">{{score.score|floatformat:1}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h2>Highscores</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>
</tr>
</thead>
<tbody>
{% for score in highscores %}
<tr>
<td>{{score.position}}</td>
<td>{{score.user__username}}</td>
<td class="right aligned">{{score.score|floatformat:1}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -3,5 +3,18 @@ from django.db.models import Max ...@@ -3,5 +3,18 @@ from django.db.models import Max
from core.models import SnakeGame from core.models import SnakeGame
def table(request): def table(request):
if request.user.is_authenticated:
usr = SnakeGame.objects.filter(user=request.user).aggregate(score=Max('final_mass'))
data = SnakeGame.objects.values('user__username').annotate(score=Max('final_mass')).order_by('-score') data = SnakeGame.objects.values('user__username').annotate(score=Max('final_mass')).order_by('-score')
return render(request, 'highscore/table.html', context={'highscores': data}) 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']
context={'highscores': data}
if request.user.is_authenticated:
context['usr'] = usr
return render(request, 'highscore/table.html', context=context)
...@@ -26,7 +26,7 @@ function step() ...@@ -26,7 +26,7 @@ function step()
-- to find food in your head's surroundings, call something like that: -- to find food in your head's surroundings, call something like that:
local food = findFood(100, 0.8) local food = findFood(100, 0.8)
-- this will give you all food in maximum distance of 100 around your head, -- this will give you all food in maximum distance of 100 around your head,
-- with a mass of at least 0.8 ordered by food value, decreasing -- with a mass of at least 0.8 ordered by food value (largest to lowest)
-- you can iterate over the result: -- you can iterate over the result:
for i, item in pairs(food) do for i, item in pairs(food) do
......
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