Browse Source

hide bunny locations younger than 5 minutes from hunter dashboard

testing
Burathar 4 years ago
parent
commit
bf75b07418
  1. 3
      app/main/routes.py
  2. 15
      app/models/game.py
  3. 10
      app/models/game_player.py
  4. 38
      app/models/user.py
  5. 4
      app/templates/game_hunter_dashboard.html
  6. 2
      config.py

3
app/main/routes.py

@ -73,8 +73,9 @@ def game_dashboard(game_name):
return render_template('game_bunny_dashboard.html', title='Game Dashboard', game=game, return render_template('game_bunny_dashboard.html', title='Game Dashboard', game=game,
json=json, location_encoder=LocationEncoder) json=json, location_encoder=LocationEncoder)
if role == Role.hunter: if role == Role.hunter:
hunter_delay = current_app.config['HUNTER_LOCATION_DELAY']
return render_template('game_hunter_dashboard.html', title='Game Dashboard', game=game, return render_template('game_hunter_dashboard.html', title='Game Dashboard', game=game,
json=json, location_encoder=LocationEncoder) hunter_delay=hunter_delay, json=json, location_encoder=LocationEncoder)
if role == Role.none: if role == Role.none:
return render_template('game_hunter_dashboard.html', title='Game Dashboard', game=game, return render_template('game_hunter_dashboard.html', title='Game Dashboard', game=game,
json=json, location_encoder=LocationEncoder) json=json, location_encoder=LocationEncoder)

15
app/models/game.py

@ -29,16 +29,23 @@ class Game(db.Model):
backref=db.backref('game', lazy='joined'), backref=db.backref('game', lazy='joined'),
cascade="save-update, merge, delete, delete-orphan") cascade="save-update, merge, delete, delete-orphan")
def last_player_locations(self): def last_player_locations(self, offset):
# pylint: disable=not-an-iterable # pylint: disable=not-an-iterable
return [player.last_location() for player in self.players if player.user.locations] return [player.last_location(offset=offset) for player in self.players if player.user.locations]
def last_locations(self, players): def last_locations(self, players, offset=None):
'''
Returns last locations for given players within time boundaries of game
Parameters:
players (Player or User list): players for whom the last location is returned
offset (int): Offset in minutes. Only locations older than this amount of minutes will be returned.
'''
locations = [] locations = []
for player in players: for player in players:
if isinstance(player, GamePlayer): if isinstance(player, GamePlayer):
player = player.user player = player.user
location = player.last_location(self) location = player.last_location(self, offset=offset)
if location: if location:
locations.append(location) locations.append(location)
return locations return locations

10
app/models/game_player.py

@ -37,9 +37,15 @@ class GamePlayer(db.Model):
caught_by_players = association_proxy('player_caught_by_players', 'catching_player') caught_by_players = association_proxy('player_caught_by_players', 'catching_player')
caught_players = association_proxy('player_caught_players', 'caught_player') caught_players = association_proxy('player_caught_players', 'caught_player')
def last_location(self): def last_location(self, offset=None):
'''
Returns game_player's last recorded location within game start- and end time.
Parameters:
offset (int): Offset in minutes. Only locations older than this amount of minutes will be returned.
'''
# pylint: disable=no-member # pylint: disable=no-member
return self.user.last_location(self.game) return self.user.last_location(self.game, offset)
def locations_during_game(self): def locations_during_game(self):
# pylint: disable=no-member # pylint: disable=no-member

38
app/models/user.py

@ -1,5 +1,5 @@
from secrets import token_hex from secrets import token_hex
from datetime import datetime from datetime import datetime, timedelta
from flask_login import UserMixin from flask_login import UserMixin
from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.associationproxy import association_proxy
@ -42,23 +42,47 @@ class User(UserMixin, db.Model):
return False return False
return check_password_hash(self.password_hash, password) return check_password_hash(self.password_hash, password)
def locations_during_game(self, game): def locations_during_game(self, game, offset=0):
'''
Returns users locations during game.
Parameters:
game (Game): If specified, only locations within start- and endtime of game wil be returned
offset (int): Offset in minutes. Only locations older than this amount of minutes will be returned.
'''
# pylint: disable=not-an-iterable # pylint: disable=not-an-iterable
if not self.locations: if not self.locations:
return None return None
if game is None: if game is None:
if offset == 0:
return self.locations return self.locations
return [location for location in self.locations
if datetime.utcnow() - location.timestamp > timedelta(minutes=offset)]
game_start = game.start_time or datetime.min game_start = game.start_time or datetime.min
game_end = game.end_time or datetime.max game_end = game.end_time or datetime.max
return [location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end] if offset == 0:
return [location for location in self.locations
def last_location(self, game=None): if location.timestamp > game_start and location.timestamp < game_end]
return [location for location in self.locations
if location.timestamp > game_start and location.timestamp < game_end
and datetime.utcnow() - location.timestamp > timedelta(minutes=offset)]
def last_location(self, game=None, offset=0):
'''
Returns users last recorded location.
Parameters:
game (Game): If specified, only locations within start- and endtime of game wil be returned
offset (int): Offset in minutes. Only locations older than this amount of minutes will be returned.
'''
# pylint: disable=[not-an-iterable, unsubscriptable-object] # pylint: disable=[not-an-iterable, unsubscriptable-object]
if not self.locations: if not self.locations:
return None return None
if game is None: if game is None:
return self.locations[-1] locations = self.locations_during_game(game=None, offset=offset)
locations_during_game = self.locations_during_game(game) return locations[-1] if locations else None
locations_during_game = self.locations_during_game(game, offset=offset)
return locations_during_game[-1] if locations_during_game else None return locations_during_game[-1] if locations_during_game else None
def role_in_game(self, game): def role_in_game(self, game):

4
app/templates/game_hunter_dashboard.html

@ -32,7 +32,7 @@
<span style="color:red;">{{ bunny.player_caught_by_players | selectattr('catching_player', '==', player) | selectattr('review.name', '==', 'denied') |list|length}}</span> / <span style="color:red;">{{ bunny.player_caught_by_players | selectattr('catching_player', '==', player) | selectattr('review.name', '==', 'denied') |list|length}}</span> /
<span style="color:gray;">{{ bunny.player_caught_by_players | selectattr('catching_player', '==', player) | selectattr('review.name', '==', 'none') |list|length}}</span> <span style="color:gray;">{{ bunny.player_caught_by_players | selectattr('catching_player', '==', player) | selectattr('review.name', '==', 'none') |list|length}}</span>
</td> </td>
<td>{% with location = bunny.last_location() %} <td>{% with location = bunny.last_location(offset=hunter_delay) %}
{% if location %}{{ moment(location.timestamp).fromNow()}}: {% endif %} {% if location %}{{ moment(location.timestamp).fromNow()}}: {% endif %}
{{ location }} {{ location }}
{% endwith %}</td> {% endwith %}</td>
@ -81,7 +81,7 @@
attribution: 'Kaartgegevens &copy; <a href="https://kadaster.nl">Kadaster</a>' attribution: 'Kaartgegevens &copy; <a href="https://kadaster.nl">Kadaster</a>'
}).addTo( map ); }).addTo( map );
var bunnies = JSON.parse('{{ json.dumps(game.last_locations(game.bunnies()), cls=location_encoder)|safe }}') var bunnies = JSON.parse('{{ json.dumps(game.last_locations(game.bunnies(), offset=hunter_delay), cls=location_encoder)|safe }}')
for (var i = 0; i < bunnies.length; i++){ for (var i = 0; i < bunnies.length; i++){
addPlayerMarker(map, bunnies[i]) addPlayerMarker(map, bunnies[i])
} }

2
config.py

@ -24,3 +24,5 @@ class Config(object):
UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER') or 'uploads' UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER') or 'uploads'
PLAYER_CAUGHT_PLAYER_PHOTO_DIR_NAME = os.environ.get('PLAYER_CAUGHT_PLAYER_PHOTO_DIR_NAME') or 'caught_bunny_photos' PLAYER_CAUGHT_PLAYER_PHOTO_DIR_NAME = os.environ.get('PLAYER_CAUGHT_PLAYER_PHOTO_DIR_NAME') or 'caught_bunny_photos'
ALLOWED_PHOTO_EXTENSIONS = os.environ.get('ALLOWED_EXTENSIONS') or {'png', 'jpg', 'jpeg', 'gif', 'tiff', 'heif', 'heic'} ALLOWED_PHOTO_EXTENSIONS = os.environ.get('ALLOWED_EXTENSIONS') or {'png', 'jpg', 'jpeg', 'gif', 'tiff', 'heif', 'heic'}
HUNTER_LOCATION_DELAY = os.environ.get('HUNTER_LOCATION_DELAY') or 5

Loading…
Cancel
Save