Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Jochen Bauer
spn-website
Commits
3202268d
Commit
3202268d
authored
May 11, 2018
by
nachtgold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new highscores as new tabs
parent
76e125fc
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
109 additions
and
15 deletions
+109
-15
core/static/core/styles.css
core/static/core/styles.css
+9
-0
core/templates/core/base.html
core/templates/core/base.html
+4
-1
highscore/templates/highscore/table.html
highscore/templates/highscore/table.html
+11
-3
highscore/urls.py
highscore/urls.py
+3
-1
highscore/views.py
highscore/views.py
+82
-10
No files found.
core/static/core/styles.css
View file @
3202268d
...
...
@@ -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
;
}
...
...
core/templates/core/base.html
View file @
3202268d
...
...
@@ -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 %}
...
...
highscore/templates/highscore/table.html
View file @
3202268d
...
...
@@ -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>
Highscore
s
</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"
>
Scor
e
</th>
<th
class=
"five wide right aligned"
>
Valu
e
</th>
</tr>
</thead>
<tbody>
...
...
highscore/urls.py
View file @
3202268d
...
...
@@ -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'
),
]
highscore/views.py
View file @
3202268d
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
)
def
table
(
request
,
data
,
usr
,
title
):
i
=
0
context
=
{
'highscores'
:
data
}
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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment