Browse Source

Actively fetch new locations and display own location on hunter dashboard

testing
Burathar 4 years ago
parent
commit
fc037da2e6
  1. 18
      app/main/routes.py
  2. 58
      app/templates/game_hunter_dashboard.html
  3. 2
      app/templates/game_owner_dashboard.html

18
app/main/routes.py

@ -127,25 +127,29 @@ def poll_locations(game_name):
requested_users = get_value_if_key_exists(payload, 'requested_users', 'none') requested_users = get_value_if_key_exists(payload, 'requested_users', 'none')
#print(f'mode: {mode}\nlast_request: {last_update}\nrequested_users: {requested_users}') #print(f'mode: {mode}\nlast_request: {last_update}\nrequested_users: {requested_users}')
response_objects = [] response_objects = []
if role == Role.owner: if role in (Role.owner, Role.hunter):
for username in requested_users: for username in requested_users:
user = get_user_locations(game, username, mode, last_update) locations = get_user_locations(game, username, mode, last_update, role == Role.hunter)
if user: #print(locations)
response_objects.append(user) if locations:
response_objects.append(locations)
response_objects = [obj for obj_list in response_objects for obj in obj_list] response_objects = [obj for obj_list in response_objects for obj in obj_list]
return json.dumps(response_objects, cls=LocationEncoder) return json.dumps(response_objects, cls=LocationEncoder)
def get_value_if_key_exists(dictionary, key, default=None): def get_value_if_key_exists(dictionary, key, default=None):
return dictionary[key] if key in dictionary else default return dictionary[key] if key in dictionary else default
def get_user_locations(game, username, mode, last_update): def get_user_locations(game, username, mode, last_update, hunter=False):
user = User.query.filter_by(name=username).first() user = User.query.filter_by(name=username).first()
if user is None: if user is None:
return None return None
if hunter and user.role_in_game(game) != Role.bunny:
return None
if mode == 'accumulative': if mode == 'accumulative':
if game.end_time or datetime.min < last_update: if game.end_time or datetime.max < last_update: # Don't return locations when the game is finished
return [] return []
locations = user.locations_during_game(game) offset = current_app.config['HUNTER_LOCATION_DELAY'] if hunter else 0
locations = user.locations_during_game(game, offset)
if not locations: if not locations:
return None return None
return [location for location in locations if location.timestamp - last_update > timedelta(milliseconds=1)] return [location for location in locations if location.timestamp - last_update > timedelta(milliseconds=1)]

58
app/templates/game_hunter_dashboard.html

@ -4,7 +4,6 @@
{{ super() }} {{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='assets/leaflet/leaflet.css') }}" /> <link rel="stylesheet" href="{{ url_for('static', filename='assets/leaflet/leaflet.css') }}" />
<script src="{{ url_for('static', filename='assets/leaflet/leaflet.js') }}"></script> <script src="{{ url_for('static', filename='assets/leaflet/leaflet.js') }}"></script>
<script src="{{ url_for('static', filename='assets/leaflet/utils.js') }}"></script>
{% endblock %} {% endblock %}
{% block player_app_content %} {% block player_app_content %}
@ -75,25 +74,60 @@
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}
<script src="{{ url_for('static', filename='assets/leaflet/utils.js') }}"></script>
<script type="text/javascript" , crossorigin="anonymous"> <script type="text/javascript" , crossorigin="anonymous">
// Leaflet Map // Leaflet Map
map = getMap() var map = getMap()
markers = [] var bunnieMarkers = []
var bunnies = JSON.parse( var bunnies = JSON.parse(
'{{ json.dumps(game.last_locations(game.bunnies(), offset=hunter_delay), cls=location_encoder)|safe }}') '{{ json.dumps(game.last_locations(game.bunnies(), offset=hunter_delay), cls=location_encoder)|safe }}')
for (var i = 0; i < bunnies.length; i++) { updateBunnieMarkers()
addPlayerMarker(map, bunnies[i])
markers.push([bunnies[i].latitude, bunnies[i].longitude]) if (bunnieMarkers.length > 1) {
map.fitBounds(bunnieMarkers);
} }
var self = JSON.parse('{{ json.dumps(current_user.last_location(), cls=location_encoder)|safe }}') function updateBunnieMarkers(){
if (self) { if(bunnieMarkers != undefined){
addPlayerMarker(map, self, greenPlayerIcon) bunnieMarkers.forEach(function(marker){
markers.push([self.latitude, self.longitude]) marker.remove()
});
}
bunnieMarkers = []
for (var i = 0; i < bunnies.length; i++) {
bunnieMarkers.push(addPlayerMarker(map, bunnies[i], greenPlayerIcon))
}
} }
if (markers.length > 0) { // Poll Locations
map.fitBounds(markers); usernames = JSON.parse('{{ json.dumps(game.usernames())|safe }}').filter(name => name != '{{ current_user.name }}')
setInterval(function() {
pollLocations(
"{{ url_for('main.poll_locations', game_name=game.name) }}",
usernames,
'accumulative',
bunnies,
handleResponse
)
getPosition(updateMyLocation)
}, 10 * 1000);
function handleResponse(data){
data.forEach(function (location) {
bunnie = bunnies.filter(function (bunnie) {
return bunnie.username == location.username;
})[0];
if (new Date(location.timestamp_utc) > new Date(bunnie.timestamp_utc)){
//lastLocation = bunnie[bunnie.length-1] Not necesary because there is just one of each bunnie
if (bunnie.latitude == location.latitude && bunnie.longitude == location.longitude){
bunnie.timestamp_utc = location.timestamp_utc
} else{
bunnies = bunnies.filter(p => p !== bunnie)
bunnies.push(location)
}
}
});
updateBunnieMarkers()
} }
</script> </script>
{% endblock %} {% endblock %}

2
app/templates/game_owner_dashboard.html

@ -134,7 +134,7 @@
} }
// Poll Locations // Poll Locations
usernames = JSON.parse('{{ json.dumps(game.usernames())|safe }}') usernames = JSON.parse('{{ json.dumps(game.usernames())|safe }}').filter(name => name != '{{ current_user.name }}')
setInterval(function() { setInterval(function() {
pollLocations( pollLocations(
"{{ url_for('main.poll_locations', game_name=game.name) }}", "{{ url_for('main.poll_locations', game_name=game.name) }}",

Loading…
Cancel
Save