From 8dda257a6f52f9b1580fc6b778f052a1fa3a7da2 Mon Sep 17 00:00:00 2001 From: Burathar Date: Fri, 31 Jul 2020 01:07:36 +0200 Subject: [PATCH] auto update players on map owner dashboard --- app/main/routes.py | 5 ++- app/models/game.py | 3 ++ app/models/user.py | 1 - app/static/assets/leaflet/utils.js | 37 ++++++++++++++++ app/templates/game_owner_dashboard.html | 58 ++++++++++++++++++++---- app/templates/game_player.html | 59 +++++++------------------ 6 files changed, 109 insertions(+), 54 deletions(-) diff --git a/app/main/routes.py b/app/main/routes.py index a077d35..eff101d 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -145,7 +145,10 @@ def get_user_locations(game, username, mode, last_update): if mode == 'accumulative': if game.end_time < last_update: return [] - return [location for location in user.locations_during_game(game) if location.timestamp - last_update > timedelta(milliseconds=1)] + locations = user.locations_during_game(game) + if not locations: + return None + return [location for location in locations if location.timestamp - last_update > timedelta(milliseconds=1)] @bp.route('/user/') @login_required diff --git a/app/models/game.py b/app/models/game.py index df1ffb8..2822301 100644 --- a/app/models/game.py +++ b/app/models/game.py @@ -32,6 +32,9 @@ class Game(db.Model): backref=db.backref('game', lazy='joined'), cascade="save-update, merge, delete, delete-orphan") + def usernames(self): + return [user.name for user in self.users] + def last_player_locations(self, offset=None): # pylint: disable=not-an-iterable return [location for location in diff --git a/app/models/user.py b/app/models/user.py index 127012a..60fa731 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -24,7 +24,6 @@ class User(UserMixin, db.Model): games = association_proxy('user_games', 'game', creator=lambda game: GamePlayer(game=game)) - locations = db.relationship( 'Location', lazy='select', diff --git a/app/static/assets/leaflet/utils.js b/app/static/assets/leaflet/utils.js index 4a1d6d4..4bfb76a 100644 --- a/app/static/assets/leaflet/utils.js +++ b/app/static/assets/leaflet/utils.js @@ -89,3 +89,40 @@ function getMap(){ }).addTo( map ); return map } + +var csrftoken = $('meta[name=csrf-token]').attr('content') + $.ajaxSetup({ + beforeSend: function(xhr, settings) { + if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) { + xhr.setRequestHeader("X-CSRFToken", csrftoken) + } + } + }) + +function pollLocations(url, requestedUsers, mode, playerLocations, responseHandler) { + $.ajax({ + type: "POST", + url: url, + data: JSON.stringify({ + requested_users: requestedUsers, + mode: mode, + last_update: moment(get_latest_date(playerLocations)).format("YYYY-MM-DD HH:mm:ss:SSSS") + }), + contentType: "application/json; charset=utf-8", + dataType: 'json', + success: function(data){ + responseHandler( + data.filter(item => item.latitude && item.longitude && item.timestamp_utc && item.username) + ) + }, + error: function(error) { + console.log(error); + } + }); +} + +function get_latest_date(playerLocations){ + return new Date(Math.max.apply(null, playerLocations.map(function(e) { + return new Date(e.timestamp_utc); + }))); +} \ No newline at end of file diff --git a/app/templates/game_owner_dashboard.html b/app/templates/game_owner_dashboard.html index 7c13628..4f5aeb7 100644 --- a/app/templates/game_owner_dashboard.html +++ b/app/templates/game_owner_dashboard.html @@ -4,10 +4,10 @@ {{ super() }} - {% endblock %} {% block app_content %} +

{{ game.name }} Dashboard

@@ -97,25 +97,65 @@ {% block scripts %} {{ super() }} + diff --git a/app/templates/game_player.html b/app/templates/game_player.html index f811419..a12c596 100644 --- a/app/templates/game_player.html +++ b/app/templates/game_player.html @@ -5,7 +5,6 @@ {{ super() }} - {% endblock %} {% block app_content %} @@ -74,6 +73,7 @@ {% block scripts %} {{ super() }} +