diff --git a/app/__init__.py b/app/__init__.py index 8b3f4ce..c7b97a1 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -9,6 +9,7 @@ from flask_bootstrap import Bootstrap from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_login import LoginManager +from flask_moment import Moment app = Flask(__name__) @@ -18,6 +19,7 @@ db = SQLAlchemy(app) migrate = Migrate(app, db) login = LoginManager(app) login.login_view = 'login' +moment = Moment(app) from app import routes, models, errors diff --git a/app/models.py b/app/models.py index 031d5f9..238a904 100644 --- a/app/models.py +++ b/app/models.py @@ -9,7 +9,9 @@ import json from json import JSONEncoder from datetime import datetime import pytz +from flask_moment import Moment +moment = Moment() class Role(Enum): none = 0 @@ -80,6 +82,9 @@ class Game(db.Model): lazy='select', backref=db.backref('game', lazy='joined')) + def last_player_locations(self): + return [player.last_location(self) for player in self.players if player.locations] + class Player(UserMixin, db.Model): """ !Always call set_auth_hash() after creating new instance! """ __tablename__ = 'player' @@ -143,10 +148,11 @@ class Player(UserMixin, db.Model): if not self.locations: return None if game is None: - return max(location.timestamp for location in self.locations) + return max(location for location in self.locations) game_start = game.start_time or datetime.min game_end = game.end_time or datetime.max - return max(location.timestamp for location in self.locations if location.timestamp > game_start and location.timestamp < game_end) + return max((location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end), + key=lambda location: location.timestamp) @login.user_loader def load_user(id): @@ -188,6 +194,18 @@ class Location(db.Model): latitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None),nullable=False) timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) + def __str__(self): + return f'{self.longitude}, {self.latitude}' + +class LocationEncoder(JSONEncoder): + def default(self, location): + return { + 'player_name' : location.player.name, + 'longitude' : location.longitude, + 'latitude' : location.latitude, + 'timestamp_utc' : str(location.timestamp) + } + class Notification(db.Model): __tablename__ = 'notification' id = db.Column(db.Integer, primary_key=True) diff --git a/app/routes.py b/app/routes.py index 1c4b560..85fbd8c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,7 +2,7 @@ from flask import render_template, flash, redirect, url_for, request, abort, sen from flask_login import login_user, logout_user, current_user, login_required from sqlalchemy import and_ from app import app, db -from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder +from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm import json import qrcode @@ -69,7 +69,7 @@ def game_dashboard(game_name): #game = Game.query.filter(Game.game_players.any(and_(GamePlayer.player.has(Player.name == current_user.name), GamePlayer.role == 'owner'))).first_or_404() game = Game.query.filter_by(name = game_name).first_or_404() if not is_game_owner(game): abort(403) - return render_template('game_dashboard.html', title = 'Game Dashboard', game=game, json=json, encoder=ObjectiveMinimalEncoder) + return render_template('game_dashboard.html', title = 'Game Dashboard', game=game, json=json, objective_encoder=ObjectiveMinimalEncoder, location_encoder=LocationEncoder) @app.route('/game//player/') @login_required diff --git a/app/static/assets/leaflet/images/marker-icon-2x-black.png b/app/static/assets/leaflet/images/marker-icon-2x-black.png new file mode 100644 index 0000000..23c94cf Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-black.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-blue.png b/app/static/assets/leaflet/images/marker-icon-2x-blue.png new file mode 100644 index 0000000..0015b64 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-blue.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-gold.png b/app/static/assets/leaflet/images/marker-icon-2x-gold.png new file mode 100644 index 0000000..6992d65 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-gold.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-green.png b/app/static/assets/leaflet/images/marker-icon-2x-green.png new file mode 100644 index 0000000..c359abb Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-green.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-grey.png b/app/static/assets/leaflet/images/marker-icon-2x-grey.png new file mode 100644 index 0000000..43b3eb4 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-grey.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-orange.png b/app/static/assets/leaflet/images/marker-icon-2x-orange.png new file mode 100644 index 0000000..c3c8632 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-orange.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-red.png b/app/static/assets/leaflet/images/marker-icon-2x-red.png new file mode 100644 index 0000000..1c26e9f Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-red.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-violet.png b/app/static/assets/leaflet/images/marker-icon-2x-violet.png new file mode 100644 index 0000000..ea748aa Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-violet.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-2x-yellow.png b/app/static/assets/leaflet/images/marker-icon-2x-yellow.png new file mode 100644 index 0000000..8b677d9 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-2x-yellow.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-black.png b/app/static/assets/leaflet/images/marker-icon-black.png new file mode 100644 index 0000000..d262ae4 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-black.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-blue.png b/app/static/assets/leaflet/images/marker-icon-blue.png new file mode 100644 index 0000000..e2e9f75 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-blue.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-gold.png b/app/static/assets/leaflet/images/marker-icon-gold.png new file mode 100644 index 0000000..162fada Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-gold.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-green.png b/app/static/assets/leaflet/images/marker-icon-green.png new file mode 100644 index 0000000..56db5ea Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-green.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-grey.png b/app/static/assets/leaflet/images/marker-icon-grey.png new file mode 100644 index 0000000..ebbab8e Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-grey.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-orange.png b/app/static/assets/leaflet/images/marker-icon-orange.png new file mode 100644 index 0000000..fbbce7b Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-orange.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-red.png b/app/static/assets/leaflet/images/marker-icon-red.png new file mode 100644 index 0000000..3e64e06 Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-red.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-violet.png b/app/static/assets/leaflet/images/marker-icon-violet.png new file mode 100644 index 0000000..28efc3c Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-violet.png differ diff --git a/app/static/assets/leaflet/images/marker-icon-yellow.png b/app/static/assets/leaflet/images/marker-icon-yellow.png new file mode 100644 index 0000000..b011eea Binary files /dev/null and b/app/static/assets/leaflet/images/marker-icon-yellow.png differ diff --git a/app/static/assets/leaflet/images/marker-shadow.png b/app/static/assets/leaflet/images/marker-shadow.png index 9fd2979..84c5808 100644 Binary files a/app/static/assets/leaflet/images/marker-shadow.png and b/app/static/assets/leaflet/images/marker-shadow.png differ diff --git a/app/templates/game_dashboard.html b/app/templates/game_dashboard.html index be9f05e..0f4b7d6 100644 --- a/app/templates/game_dashboard.html +++ b/app/templates/game_dashboard.html @@ -32,7 +32,10 @@ {{ player.found_objectives | selectattr('game', '==', game)|list|length}} {{ player.caught_players | selectattr('game', '==', game)|list|length}} {{ player.caught_by_players | selectattr('game', '==', game)|list|length}} - {{ player.last_location(game) }} + {% with location = player.last_location(game) %} + {% if location %}{{ moment(location.timestamp).fromNow()}}: {% endif %} + {{ location }} + {% endwith %} {% endfor %} @@ -77,6 +80,7 @@ {% block scripts %} {{ super() }} +{{ moment.include_moment() }} {% endblock %} \ No newline at end of file