Commit 2b11cf6f authored by nachtgold's avatar nachtgold

highscore shows the gamers rank

parent 0bb3d2c8
......@@ -5,7 +5,6 @@
.ui.table.highscore-table {
font-size: .6em;
margin-left: 200px;
width: 60%;
}
/*
......
......@@ -7,22 +7,37 @@
{% block content %}
<h2>Highscores</h2>
<table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em">
<thead>
<tr>
<th class="eleven wide">User</th>
<th class="five wide right aligned">Score</th>
</tr>
</thead>
<tbody>
{% for score in highscores %}
<tr>
<td>{{score.user__username}}</td>
<td class="right aligned">{{score.score|floatformat:1}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="panel">
{% if usr %}
<h2>Your score</h2>
<table class="highscore-table ui celled selectable inverted table" cellspacing="0" style="margin-bottom:5em">
<tr>
<td class="two wide">{{usr.position}}</td>
<td class="nine wide">{{request.user.username}}</td>
<td class="five wide right aligned">{{usr.score|floatformat:1}}</td>
</tr>
</tbody>
</table>
{% endif %}
<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 %}
\ No newline at end of file
......@@ -3,5 +3,18 @@ from django.db.models import Max
from core.models import SnakeGame
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')
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)
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