From 5d58700661a0bd049089f0b8d27542d5de538b20 Mon Sep 17 00:00:00 2001 From: Burathar Date: Wed, 22 Jul 2020 18:17:59 +0200 Subject: [PATCH] implement player location update --- app/main/routes.py | 35 ++++++++++++++++-- app/models/user.py | 6 ++-- app/templates/catch_bunny.html | 2 +- app/templates/game_bunny_dashboard.html | 2 +- app/templates/game_hunter_dashboard.html | 2 +- app/templates/index.html | 2 +- app/templates/player_base.html | 45 ++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 app/templates/player_base.html diff --git a/app/main/routes.py b/app/main/routes.py index 150e05b..e2ff6f1 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -2,7 +2,7 @@ import fnmatch import json from os import listdir from pathlib import Path -from datetime import datetime +from datetime import datetime, timedelta from flask import render_template, flash, redirect, url_for, request, abort, send_file, current_app, send_from_directory from flask_login import current_user, login_required @@ -13,8 +13,10 @@ from sqlalchemy import and_ from app import db from app.main import bp from app.utils import generate_qr_code, serve_pil_image -from app.models import User, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder, PlayerCaughtPlayer, Review -from app.main.forms import CreateGameForm, ObjectiveForm, PlayerAddForm, UserCreateForm, PlayerUpdateForm, CatchBunnyForm +from app.models import User, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, \ + LocationEncoder, PlayerCaughtPlayer, Review, Location +from app.main.forms import CreateGameForm, ObjectiveForm, PlayerAddForm, UserCreateForm, \ + PlayerUpdateForm, CatchBunnyForm @bp.before_app_request def before_request(): @@ -302,3 +304,30 @@ def objective(objective_hash): return redirect(url_for('main.game_dashboard', game_name=objective.game.name)) return render_template('objective.html', title='Objective view', objective=objective, owner=True, form=form, qrcode=qrcode) + +@bp.route('/user//send_location', methods=['POST']) +@login_required +def send_location(username): + user = User.query.filter_by(name=username).first_or_404() + last_location = user.last_location() + + latitude = request.form.get('lat', default=None, type=float) + longitude = request.form.get('long', default=None, type=float) + if latitude is None or longitude is None: + return '', 400 + + # Check if previous two locations are exactly the same, if so, only update timestamp of last location + if last_location: + if datetime.utcnow() - last_location.timestamp < timedelta(milliseconds=1): + return '', 204 + if latitude == last_location.latitude and longitude == last_location.longitude and len(user.locations) >= 2: + before_last_location = user.locations[-2] + if before_last_location: + if latitude == before_last_location.latitude and longitude == before_last_location.longitude: + last_location.timestamp = datetime.utcnow() + db.session.commit() + return '', 204 + + user.locations.append(Location(longitude=longitude, latitude=latitude)) + db.session.commit() + return '', 204 \ No newline at end of file diff --git a/app/models/user.py b/app/models/user.py index 3df2ad8..d38bfd5 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -53,12 +53,12 @@ class User(UserMixin, db.Model): return [location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end] def last_location(self, game=None): - # pylint: disable=not-an-iterable + # pylint: disable=[not-an-iterable, unsubscriptable-object] if not self.locations: return None if game is None: - return max(self.locations, key=lambda location: location.timestamp) - return max(self.locations_during_game(game), key=lambda location: location.timestamp) + return self.locations[-1] + return self.locations_during_game(game)[-1] def role_in_game(self, game): '''Returns the role as Role enum of player in given game. Returns None if player does not participate in game''' diff --git a/app/templates/catch_bunny.html b/app/templates/catch_bunny.html index 5058cc2..e6487c1 100644 --- a/app/templates/catch_bunny.html +++ b/app/templates/catch_bunny.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'player_base.html' %} {% import 'bootstrap/wtf.html' as wtf %} {% block player_app_content %} diff --git a/app/templates/game_bunny_dashboard.html b/app/templates/game_bunny_dashboard.html index 0f09d02..46cb04a 100644 --- a/app/templates/game_bunny_dashboard.html +++ b/app/templates/game_bunny_dashboard.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'player_base.html' %} {% block head %} {{ super() }} diff --git a/app/templates/game_hunter_dashboard.html b/app/templates/game_hunter_dashboard.html index d72a5ae..56c9e59 100644 --- a/app/templates/game_hunter_dashboard.html +++ b/app/templates/game_hunter_dashboard.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'player_base.html' %} {% block head %} {{ super() }} diff --git a/app/templates/index.html b/app/templates/index.html index d2fedd0..74fd73b 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'player_base.html' %} {% block player_app_content %}

Hi, {{ current_user.name }}!

diff --git a/app/templates/player_base.html b/app/templates/player_base.html new file mode 100644 index 0000000..866e40e --- /dev/null +++ b/app/templates/player_base.html @@ -0,0 +1,45 @@ +{% extends 'base.html' %} + +{% block app_content %} + + {% block player_app_content %}{% endblock %} +{% endblock %} + +{% block scripts %} +{{ super() }} + +{% endblock %} \ No newline at end of file