From bfdf69878b7d06b56140ca71a77550b5f84267e0 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 Nov 2019 13:51:49 +0100 Subject: [PATCH] Changed scores to use metres instead of km to allow for more granularity --- geogame/main/admin.py | 17 ++++++++++++++++- geogame/main/models.py | 19 +++++++------------ geogame/main/views.py | 13 ++++++------- geogame/settings.py | 1 + geogame/templates/main/challenge_list.html | 4 ++-- geogame/templates/main/game_recap.html | 4 ++-- geogame/templates/main/profile.html | 4 ++-- geogame/templates/main/round_recap.html | 4 ++-- 8 files changed, 38 insertions(+), 28 deletions(-) diff --git a/geogame/main/admin.py b/geogame/main/admin.py index fcdbd37..f2b3914 100644 --- a/geogame/main/admin.py +++ b/geogame/main/admin.py @@ -3,13 +3,28 @@ from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from .forms import CustomUserCreationForm, CustomUserChangeForm -from .models import User, Coord +from .models import User, Coord, Challenge, Game, GameRound @admin.register(Coord) class CoordAdmin(admin.ModelAdmin): 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): add_form = CustomUserCreationForm form = CustomUserChangeForm diff --git a/geogame/main/models.py b/geogame/main/models.py index c25a264..8b4b4f3 100644 --- a/geogame/main/models.py +++ b/geogame/main/models.py @@ -126,16 +126,11 @@ class GameRound(models.Model): guess_lng = models.CharField(_('longitude'), max_length=50, blank=True, null=True) result = models.PositiveIntegerField(default=0) - def get_distance(self): + def save(self, *args, **kwargs): if not self.guess_lat or not self.guess_lng: - self.distance = 0 - self.save() - return 0 - - actual_coord = (self.coord.lat, self.coord.lng,) - guess_coord = (self.guess_lat, self.guess_lng,) - - result = distance.distance(actual_coord, guess_coord).km - self.result = result - self.save() - return result \ No newline at end of file + self.result = 0 + else: + actual_coord = (self.coord.lat, self.coord.lng,) + guess_coord = (self.guess_lat, self.guess_lng,) + self.result = distance.distance(actual_coord, guess_coord).km * 1000 + super().save(*args, **kwargs) \ No newline at end of file diff --git a/geogame/main/views.py b/geogame/main/views.py index b05b6ec..c145b60 100644 --- a/geogame/main/views.py +++ b/geogame/main/views.py @@ -194,13 +194,12 @@ class RoundView(views.LoginRequiredMixin, UpdateView): self.object.guess_lat = 0 self.object.guess_lng = 0 self.object.save() - round = self.get_object() return redirect( reverse_lazy( 'game:round-recap-view', kwargs={ - 'game_pk': round.game.pk, - 'round_pk': round.pk, + 'game_pk': self.object.game.pk, + 'round_pk': self.object.pk, } ) ) @@ -281,7 +280,7 @@ class RoundRecapView(views.UserPassesTestMixin, TemplateView): context['guess_lat'] = round.guess_lat context['guess_lng'] = round.guess_lng context['game_id'] = round.game.id - context['distance'] = "{0:.3f}".format(round.get_distance()) + context['distance'] = int(round.result) next_round = GameRound.objects.filter( game=round.game, @@ -313,7 +312,6 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView): self.request.user.deactive_games() coord_results = [] - distance_total = 0 for round in GameRound.objects.filter(game=game).select_related('coord'): coord_results.append( [ @@ -321,12 +319,13 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView): [round.guess_lat, round.guess_lng] ] ) - distance_total += round.get_distance() context['results'] = coord_results - context['average_distance'] = GameRound.objects.filter(game=game)\ + context['average_distance'] = int( + GameRound.objects.filter(game=game)\ .aggregate(Avg('result'))\ .get('result__avg', 0) + ) # not every game is part of a challenge, so keep this in a try try: context['all_average'] = game.challenge.average diff --git a/geogame/settings.py b/geogame/settings.py index bdba9c7..b433889 100644 --- a/geogame/settings.py +++ b/geogame/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', + 'django.contrib.humanize', 'allauth', 'allauth.account', diff --git a/geogame/templates/main/challenge_list.html b/geogame/templates/main/challenge_list.html index b2f7112..03340a0 100644 --- a/geogame/templates/main/challenge_list.html +++ b/geogame/templates/main/challenge_list.html @@ -1,5 +1,5 @@ {% extends 'game_base.html' %} - +{% load humanize %} {% block content %}
@@ -20,7 +20,7 @@ {% for challenge in challenges %} {{challenge.name}} - {{challenge.average}} + {{challenge.average|intcomma}} Play {% endfor %} diff --git a/geogame/templates/main/game_recap.html b/geogame/templates/main/game_recap.html index eae11c6..7948434 100644 --- a/geogame/templates/main/game_recap.html +++ b/geogame/templates/main/game_recap.html @@ -1,5 +1,5 @@ {% extends 'game_base.html' %} - +{% load humanize %} {% block content %}
@@ -14,7 +14,7 @@ {% endif %}
- 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%}
diff --git a/geogame/templates/main/profile.html b/geogame/templates/main/profile.html index 3a026c4..b14a67f 100644 --- a/geogame/templates/main/profile.html +++ b/geogame/templates/main/profile.html @@ -1,5 +1,5 @@ {% extends 'game_base.html' %} - +{% load humanize %} {% block content %}
@@ -44,7 +44,7 @@ {% for challenge in user_challenges %} {{challenge.name}} - {{challenge.average}} + {{challenge.average|intcomma}} Edit {% endfor %} diff --git a/geogame/templates/main/round_recap.html b/geogame/templates/main/round_recap.html index 0d07dd4..3e66c47 100644 --- a/geogame/templates/main/round_recap.html +++ b/geogame/templates/main/round_recap.html @@ -1,5 +1,5 @@ {% extends 'game_base.html' %} - +{% load humanize %} {% block content %}
@@ -14,7 +14,7 @@ {% endif %}
- Your guess was {{distance}}km away. + Your guess was {{distance|intcomma}}m away.