Changed scores to use metres instead of km to allow for more granularity

This commit is contained in:
root 2019-11-26 13:51:49 +01:00
parent 88204b19af
commit bfdf69878b
8 changed files with 38 additions and 28 deletions

View File

@ -3,13 +3,28 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import User, Coord from .models import User, Coord, Challenge, Game, GameRound
@admin.register(Coord) @admin.register(Coord)
class CoordAdmin(admin.ModelAdmin): class CoordAdmin(admin.ModelAdmin):
list_display = 'id', list_display = 'id',
class CoordInline(admin.TabularInline):
model = Coord
fields = 'lat', 'lng', 'user',
extra = 0
@admin.register(Challenge)
class ChallengeAdmin(admin.ModelAdmin):
list_display = 'id', 'name', 'user',
raw_id_fields = 'user',
search_fields = 'name', 'user__display_name',
ordering = 'id',
inlines = [CoordInline]
class CustomUserAdmin(UserAdmin): class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm add_form = CustomUserCreationForm
form = CustomUserChangeForm form = CustomUserChangeForm

View File

@ -126,16 +126,11 @@ class GameRound(models.Model):
guess_lng = models.CharField(_('longitude'), max_length=50, blank=True, null=True) guess_lng = models.CharField(_('longitude'), max_length=50, blank=True, null=True)
result = models.PositiveIntegerField(default=0) result = models.PositiveIntegerField(default=0)
def get_distance(self): def save(self, *args, **kwargs):
if not self.guess_lat or not self.guess_lng: if not self.guess_lat or not self.guess_lng:
self.distance = 0 self.result = 0
self.save() else:
return 0 actual_coord = (self.coord.lat, self.coord.lng,)
guess_coord = (self.guess_lat, self.guess_lng,)
actual_coord = (self.coord.lat, self.coord.lng,) self.result = distance.distance(actual_coord, guess_coord).km * 1000
guess_coord = (self.guess_lat, self.guess_lng,) super().save(*args, **kwargs)
result = distance.distance(actual_coord, guess_coord).km
self.result = result
self.save()
return result

View File

@ -194,13 +194,12 @@ class RoundView(views.LoginRequiredMixin, UpdateView):
self.object.guess_lat = 0 self.object.guess_lat = 0
self.object.guess_lng = 0 self.object.guess_lng = 0
self.object.save() self.object.save()
round = self.get_object()
return redirect( return redirect(
reverse_lazy( reverse_lazy(
'game:round-recap-view', 'game:round-recap-view',
kwargs={ kwargs={
'game_pk': round.game.pk, 'game_pk': self.object.game.pk,
'round_pk': round.pk, 'round_pk': self.object.pk,
} }
) )
) )
@ -281,7 +280,7 @@ class RoundRecapView(views.UserPassesTestMixin, TemplateView):
context['guess_lat'] = round.guess_lat context['guess_lat'] = round.guess_lat
context['guess_lng'] = round.guess_lng context['guess_lng'] = round.guess_lng
context['game_id'] = round.game.id context['game_id'] = round.game.id
context['distance'] = "{0:.3f}".format(round.get_distance()) context['distance'] = int(round.result)
next_round = GameRound.objects.filter( next_round = GameRound.objects.filter(
game=round.game, game=round.game,
@ -313,7 +312,6 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView):
self.request.user.deactive_games() self.request.user.deactive_games()
coord_results = [] coord_results = []
distance_total = 0
for round in GameRound.objects.filter(game=game).select_related('coord'): for round in GameRound.objects.filter(game=game).select_related('coord'):
coord_results.append( coord_results.append(
[ [
@ -321,12 +319,13 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView):
[round.guess_lat, round.guess_lng] [round.guess_lat, round.guess_lng]
] ]
) )
distance_total += round.get_distance()
context['results'] = coord_results context['results'] = coord_results
context['average_distance'] = GameRound.objects.filter(game=game)\ context['average_distance'] = int(
GameRound.objects.filter(game=game)\
.aggregate(Avg('result'))\ .aggregate(Avg('result'))\
.get('result__avg', 0) .get('result__avg', 0)
)
# not every game is part of a challenge, so keep this in a try # not every game is part of a challenge, so keep this in a try
try: try:
context['all_average'] = game.challenge.average context['all_average'] = game.challenge.average

View File

@ -40,6 +40,7 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.sites', 'django.contrib.sites',
'django.contrib.humanize',
'allauth', 'allauth',
'allauth.account', 'allauth.account',

View File

@ -1,5 +1,5 @@
{% extends 'game_base.html' %} {% extends 'game_base.html' %}
{% load humanize %}
{% block content %} {% block content %}
<div class="container"> <div class="container">
<div class="row"> <div class="row">
@ -20,7 +20,7 @@
{% for challenge in challenges %} {% for challenge in challenges %}
<tr> <tr>
<td>{{challenge.name}}</td> <td>{{challenge.name}}</td>
<td>{{challenge.average}}</td> <td>{{challenge.average|intcomma}}</td>
<td><a class="btn btn-success btn-block" href="{% url 'game:new-game' %}?challenge={{challenge.id}}" role="button">Play</a></td> <td><a class="btn btn-success btn-block" href="{% url 'game:new-game' %}?challenge={{challenge.id}}" role="button">Play</a></td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -1,5 +1,5 @@
{% extends 'game_base.html' %} {% extends 'game_base.html' %}
{% load humanize %}
{% block content %} {% block content %}
<div class="container"> <div class="container">
@ -14,7 +14,7 @@
{% endif %} {% endif %}
<div class="alert alert-info"> <div class="alert alert-info">
Your average guess was {{average_distance}}km away.{% if all_average %} The average for this challenge is {{all_average}}km{%endif%} Your average guess was {{average_distance|intcomma}}m away.{% if all_average %} The average for this challenge is {{all_average|intcomma}}m{%endif%}
</div> </div>
<div id="map" class="map"></div> <div id="map" class="map"></div>

View File

@ -1,5 +1,5 @@
{% extends 'game_base.html' %} {% extends 'game_base.html' %}
{% load humanize %}
{% block content %} {% block content %}
<div class="container"> <div class="container">
<div class="row"> <div class="row">
@ -44,7 +44,7 @@
{% for challenge in user_challenges %} {% for challenge in user_challenges %}
<tr> <tr>
<td>{{challenge.name}}</td> <td>{{challenge.name}}</td>
<td>{{challenge.average}}</td> <td>{{challenge.average|intcomma}}</td>
<td><a class="btn btn-warning btn-block" href="{% url 'game:edit-challenge' pk=challenge.pk %}" role="button">Edit</a></td> <td><a class="btn btn-warning btn-block" href="{% url 'game:edit-challenge' pk=challenge.pk %}" role="button">Edit</a></td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -1,5 +1,5 @@
{% extends 'game_base.html' %} {% extends 'game_base.html' %}
{% load humanize %}
{% block content %} {% block content %}
<div class="container"> <div class="container">
@ -14,7 +14,7 @@
{% endif %} {% endif %}
<div class="alert alert-info"> <div class="alert alert-info">
Your guess was {{distance}}km away. Your guess was {{distance|intcomma}}m away.
</div> </div>
<div id="map" class="map"></div> <div id="map" class="map"></div>