Skip to content

Commit

Permalink
Auto refresh PokemonGoF#2 (PokemonGoF#647)
Browse files Browse the repository at this point in the history
* Added Auto-refresh - Initial

New Pokemons being pushed to the map each 10 seconds.
Old Pokemons being removed from the map during push.

* Update app.py

* Merge fix

bug

* Update map.js

* Update map.html

* Update app.py

* stamp param fix

* Update map.html
  • Loading branch information
Lunat1q authored and 6iz committed Jul 20, 2016
1 parent bfa70a8 commit 74b4609
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
22 changes: 11 additions & 11 deletions pogom/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, import_name, **kwargs):
super(Pogom, self).__init__(import_name, **kwargs)
self.json_encoder = CustomJSONEncoder
self.route("/", methods=['GET'])(self.fullmap)
self.route("/pokemons", methods=['GET'])(self.pokemons)
self.route("/pokemons/<stamp>", methods=['GET'])(self.pokemons)
self.route("/gyms", methods=['GET'])(self.gyms)
self.route("/pokestops", methods=['GET'])(self.pokestops)
self.route("/raw_data", methods=['GET'])(self.raw_data)
Expand All @@ -26,24 +26,24 @@ def fullmap(self):
lng=config['ORIGINAL_LONGITUDE'],
gmaps_key=config['GMAPS_KEY'])

def get_raw_data(self):
def get_raw_data(self, stamp):
return {
'gyms': [g for g in Gym.select().dicts()],
'pokestops': [p for p in Pokestop.select().dicts()],
'pokemons': Pokemon.get_active()
'pokemons': Pokemon.get_active(stamp)
}

def raw_data(self):
return jsonify(self.get_raw_data())
def raw_data(self, stamp):
return jsonify(self.get_raw_data(stamp))

def pokemons(self):
return jsonify(self.get_raw_data()['pokemons'])
def pokemons(self, stamp):
return jsonify(self.get_raw_data(stamp)['pokemons'])

def pokestops(self):
return jsonify(self.get_raw_data()['pokestops'])
def pokestops(self, stamp):
return jsonify(self.get_raw_data(stamp)['pokestops'])

def gyms(self):
return jsonify(self.get_raw_data()['gyms'])
def gyms(self, stamp):
return jsonify(self.get_raw_data(stamp)['gyms'])



Expand Down
12 changes: 8 additions & 4 deletions pogom/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ class Pokemon(BaseModel):
latitude = FloatField()
longitude = FloatField()
disappear_time = DateTimeField()
detect_time = DateTimeField()

@classmethod
def get_active(cls):
def get_active(cls, stamp):
r_stamp = datetime.fromtimestamp(int(stamp)/1e3)
query = (Pokemon
.select()
.where(Pokemon.disappear_time > datetime.now())
.where(Pokemon.disappear_time > datetime.now(), Pokemon.detect_time >= r_stamp)
.dicts())

log.info("Get Pokemons for stamp: {}".format(r_stamp))
pokemons = []
for p in query:
p['pokemon_name'] = get_pokemon_name(p['pokemon_id'])
Expand Down Expand Up @@ -84,6 +86,7 @@ def parse_map(map_dict):
pokestops = {}
gyms = {}

detect_time = datetime.now()
cells = map_dict['responses']['GET_MAP_OBJECTS']['map_cells']
for cell in cells:
for p in cell.get('wild_pokemons', []):
Expand All @@ -95,7 +98,8 @@ def parse_map(map_dict):
'longitude': p['longitude'],
'disappear_time': datetime.fromtimestamp(
(p['last_modified_timestamp_ms'] +
p['time_till_hidden_ms']) / 1000.0)
p['time_till_hidden_ms']) / 1000.0),
'detect_time': detect_time
}

for f in cell.get('forts', []):
Expand Down
5 changes: 3 additions & 2 deletions pogom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ def insert_mock_data(location, num_pokemons):

locations = [l for l in generate_location_steps((latitude, longitude), num_pokemons)]
disappear_time = datetime.now() + timedelta(hours=1)

detect_time = datetime.now()
for i in xrange(num_pokemons):
Pokemon.create(encounter_id=uuid.uuid4(),
spawnpoint_id='sp{}'.format(i),
pokemon_id=(i+1) % 150,
latitude=locations[i][0],
longitude=locations[i][1],
disappear_time=disappear_time)
disappear_time=disappear_time,
detect_time=detect_time)


def get_pokemon_name(pokemon_id):
Expand Down
44 changes: 31 additions & 13 deletions static/map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function pokemonLabel(name, disappear_time, id, disappear_time, latitude, longitude) {
disappear_date = new Date(disappear_time)
var map,
marker,
lastStamp = 0,
requestInterval = 10000;

var markers = [];

pokemonLabel = function(name, disappear_time, id, latitude, longitude) {
disappear_date = new Date(disappear_time + (new Date().getTimezoneOffset() * 60000));

var pad = function pad(number) {
return number <= 99 ? ("0" + number).slice(-2) : number;
Expand All @@ -22,23 +29,23 @@ function pokemonLabel(name, disappear_time, id, disappear_time, latitude, longit
</div>';

return str;
}

};

var map;
var marker;
function initMap() {
initMap = function() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: center_lat, lng: center_lng},
zoom: 16
});
marker = new google.maps.Marker({
var marker = new google.maps.Marker({
position: {lat: center_lat, lng: center_lng},
map: map,
animation: google.maps.Animation.DROP
});
});
GetNewPokemons(lastStamp);
};

$.getJSON("/pokemons", function(result){
GetNewPokemons = function(stamp) {
$.getJSON("/pokemons/"+stamp, function(result){
$.each(result, function(i, item){

var marker = new google.maps.Marker({
Expand All @@ -48,7 +55,7 @@ function initMap() {
});

marker.infoWindow = new google.maps.InfoWindow({
content: pokemonLabel(item.pokemon_name, item.disappear_time, item.pokemon_id, item.disappear_time, item.latitude, item.longitude)
content: pokemonLabel(item.pokemon_name, item.disappear_time, item.pokemon_id, item.latitude, item.longitude)
});

google.maps.event.addListener(marker.infoWindow, 'closeclick', function(){
Expand All @@ -61,6 +68,10 @@ function initMap() {
marker.infoWindow.open(map, marker);
});

markers.push({
m: marker,
disapear: item.disappear_time});

marker.addListener('mouseover', function() {
marker.infoWindow.open(map, marker);
});
Expand All @@ -72,6 +83,13 @@ function initMap() {
});

console.log(item.latitude);
});
});
}).always(function() {setTimeout(function() { GetNewPokemons(lastStamp) }, requestInterval)});
var dObj = new Date();
lastStamp = dObj.getTime();

$.each(markers, function(i, item){
if (item.disapear <= lastStamp - (dObj.getTimezoneOffset() * 60000))
item.m.setMap(null);
});
}
};
2 changes: 1 addition & 1 deletion templates/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ <h1><a href="#">GoMap</a></h1>
var center_lng = {{lng}};
</script>

<script type="text/javascript" src="/static/map.js"></script>
<script type="text/javascript" src="/static/map.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key={{ gmaps_key }}&callback=initMap"></script>
</html>

0 comments on commit 74b4609

Please sign in to comment.