Browse Source

fix update marker when no playerlocation is registered. Fix situaton when no game endtime is defined

testing
Burathar 4 years ago
parent
commit
1cad73b72b
  1. 2
      app/main/routes.py
  2. 26
      app/static/assets/geolocation_utils.js
  3. 3
      app/static/assets/leaflet/utils.js
  4. 12
      app/templates/game_owner_dashboard.html
  5. 12
      app/templates/game_player.html
  6. 31
      app/templates/player_base.html

2
app/main/routes.py

@ -143,7 +143,7 @@ def get_user_locations(game, username, mode, last_update):
if user is None: if user is None:
return None return None
if mode == 'accumulative': if mode == 'accumulative':
if game.end_time < last_update: if game.end_time or datetime.min < last_update:
return [] return []
locations = user.locations_during_game(game) locations = user.locations_during_game(game)
if not locations: if not locations:

26
app/static/assets/geolocation_utils.js

@ -0,0 +1,26 @@
function getPosition(locationHandler){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locationHandler, showError);
} else {
console.log('Geolocation is not supported by this browser.')
}
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.")
alert("Please refresh page and allow location sharing, otherwise the game won't work :'(")
break;
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.")
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.")
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.")
break;
}
}

3
app/static/assets/leaflet/utils.js

@ -122,6 +122,9 @@ function pollLocations(url, requestedUsers, mode, playerLocations, responseHandl
} }
function get_latest_date(playerLocations){ function get_latest_date(playerLocations){
if (playerLocations.length == 0){
return new Date('0001-01-01T00:00:00Z');
}
return new Date(Math.max.apply(null, playerLocations.map(function(e) { return new Date(Math.max.apply(null, playerLocations.map(function(e) {
return new Date(e.timestamp_utc); return new Date(e.timestamp_utc);
}))); })));

12
app/templates/game_owner_dashboard.html

@ -129,6 +129,15 @@
} }
} }
function sendLocation(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(postCoordinates, showError);
} else {
console.log('Geolocation is not supported by this browser.')
}
}
// Poll Locations // Poll Locations
usernames = JSON.parse('{{ json.dumps(game.usernames())|safe }}') usernames = JSON.parse('{{ json.dumps(game.usernames())|safe }}')
setInterval(function() { setInterval(function() {
@ -138,7 +147,8 @@
'accumulative', 'accumulative',
players, players,
handleResponse handleResponse
)}, 30 * 1000); )
}, 30 * 1000);
function handleResponse(data){ function handleResponse(data){
data.forEach(function (location) { data.forEach(function (location) {

12
app/templates/game_player.html

@ -87,26 +87,29 @@
updateMarkers(); updateMarkers();
var polyline; var polyline;
updatePolyline(); updatePolyline();
// zoom the map to the polyline // zoom the map to the polyline
if(polyline != undefined){
map.fitBounds(polyline.getBounds(), { map.fitBounds(polyline.getBounds(), {
maxZoom : 13 maxZoom : 13
}); });
}
function updateMarkers(){ function updateMarkers(){
if(markers != undefined){ if(markers != undefined){
markers.forEach(function(marker){ markers.forEach(function(marker){
marker.remove() marker.remove()
}); });
}
markers = [] markers = []
}
for (var i = 0; i < locations.length -1; i++) { for (var i = 0; i < locations.length -1; i++) {
markers.push(addPlayerMarker(map, locations[i], bluePlayerIconMini)) markers.push(addPlayerMarker(map, locations[i], bluePlayerIconMini))
} }
if(lastMarker != undefined){ if(lastMarker != undefined){
lastMarker.remove() lastMarker.remove()
}
lastMarker = addPlayerMarker(map, locations[locations.length-1], bluePlayerIcon) lastMarker = addPlayerMarker(map, locations[locations.length-1], bluePlayerIcon)
} }
}
function updatePolyline(){ function updatePolyline(){
if(polyline != undefined){ if(polyline != undefined){
@ -144,13 +147,16 @@
updatePolyline() updatePolyline()
updateMarkers() updateMarkers()
} }
console.log('test1')
//Ajax for Generate Code button //Ajax for Generate Code button
$(function () { $(function () {
console.log('test2')
$('a#generate_auth_hash').bind('click', function () { $('a#generate_auth_hash').bind('click', function () {
console.log('test3')
$.ajax({ $.ajax({
url: "{{ url_for('auth.generate_auth_hash', username=player.user.name) }}", url: "{{ url_for('auth.generate_auth_hash', username=player.user.name) }}",
success: function (result) { success: function (result) {
console.log('test4')
location.reload(); location.reload();
} }
}); });

31
app/templates/player_base.html

@ -8,18 +8,9 @@
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}
<script> <script>
$(document).ready(sendLocation) $(document).ready(getPosition(postCoordinates))
function sendLocation(){ setInterval(function() {getPosition(postCoordinates)}, 65 * 1000);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(postCoordinates, showError);
} else {
console.log('Geolocation is not supported by this browser.')
}
}
setInterval(function() {sendLocation()}, 65 * 1000);
function postCoordinates(position) { function postCoordinates(position) {
$.ajax({ $.ajax({
@ -45,23 +36,5 @@ $.ajaxSetup({
} }
}) })
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.")
alert("Please refresh page and allow location sharing, otherwise the game won't work :'(")
break;
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.")
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.")
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.")
break;
}
}
</script> </script>
{% endblock %} {% endblock %}
Loading…
Cancel
Save