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. 60
      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): @@ -127,25 +127,29 @@ def poll_locations(game_name):
requested_users = get_value_if_key_exists(payload, 'requested_users', 'none')
#print(f'mode: {mode}\nlast_request: {last_update}\nrequested_users: {requested_users}')
response_objects = []
if role == Role.owner:
if role in (Role.owner, Role.hunter):
for username in requested_users:
user = get_user_locations(game, username, mode, last_update)
if user:
response_objects.append(user)
locations = get_user_locations(game, username, mode, last_update, role == Role.hunter)
#print(locations)
if locations:
response_objects.append(locations)
response_objects = [obj for obj_list in response_objects for obj in obj_list]
return json.dumps(response_objects, cls=LocationEncoder)
def get_value_if_key_exists(dictionary, key, default=None):
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()
if user is None:
return None
if hunter and user.role_in_game(game) != Role.bunny:
return None
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 []
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:
return None
return [location for location in locations if location.timestamp - last_update > timedelta(milliseconds=1)]

60
app/templates/game_hunter_dashboard.html

@ -4,7 +4,6 @@ @@ -4,7 +4,6 @@
{{ super() }}
<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/utils.js') }}"></script>
{% endblock %}
{% block player_app_content %}
@ -75,25 +74,60 @@ @@ -75,25 +74,60 @@
{% block scripts %}
{{ super() }}
<script src="{{ url_for('static', filename='assets/leaflet/utils.js') }}"></script>
<script type="text/javascript" , crossorigin="anonymous">
// Leaflet Map
map = getMap()
markers = []
var map = getMap()
var bunnieMarkers = []
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++) {
addPlayerMarker(map, bunnies[i])
markers.push([bunnies[i].latitude, bunnies[i].longitude])
updateBunnieMarkers()
if (bunnieMarkers.length > 1) {
map.fitBounds(bunnieMarkers);
}
var self = JSON.parse('{{ json.dumps(current_user.last_location(), cls=location_encoder)|safe }}')
if (self) {
addPlayerMarker(map, self, greenPlayerIcon)
markers.push([self.latitude, self.longitude])
function updateBunnieMarkers(){
if(bunnieMarkers != undefined){
bunnieMarkers.forEach(function(marker){
marker.remove()
});
}
bunnieMarkers = []
for (var i = 0; i < bunnies.length; i++) {
bunnieMarkers.push(addPlayerMarker(map, bunnies[i], greenPlayerIcon))
}
}
if (markers.length > 0) {
map.fitBounds(markers);
// Poll Locations
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>
{% endblock %}

2
app/templates/game_owner_dashboard.html

@ -134,7 +134,7 @@ @@ -134,7 +134,7 @@
}
// 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() {
pollLocations(
"{{ url_for('main.poll_locations', game_name=game.name) }}",

Loading…
Cancel
Save