Skip to content

Commit

Permalink
Merge branch 'trash'
Browse files Browse the repository at this point in the history
  • Loading branch information
modrzew committed Jul 23, 2016
2 parents e217407 + 6885523 commit acd5122
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 93 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ ACCOUNTS = [
('trainer3', 'secret', 'google'),
# ...
]

# Trash Pokemon won't be shown on the live map.
# Their data will still be collected to the database.
TRASH_IDS = [16, 19, 41, 96]
```

### Setting up database
Expand Down
10 changes: 7 additions & 3 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.sql import not_


try:
Expand Down Expand Up @@ -62,6 +63,9 @@ def add_sighting(session, spawn_id, pokemon):


def get_sightings(session):
return session.query(Sighting) \
.filter(Sighting.expire_timestamp > time.time()) \
.all()
query = session.query(Sighting) \
.filter(Sighting.expire_timestamp > time.time())
trash_list = getattr(config, 'TRASH_IDS')
if trash_list:
query = query.filter(not_(Sighting.pokemon_id.in_(config.TRASH_IDS)))
return query.all()
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ gpsoauth==0.3.0
coveralls==1.1
werkzeug==0.11.10
sqlalchemy==1.0.14
mysql-python==1.2.5
221 changes: 221 additions & 0 deletions templates/map.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pokeminer</title>
</head>
<body>
<h1>Loading!</h1>
<div id="fullmap" style='height:100%;width:100%;top:0;left:0;position:absolute;z-index:200;'>
{{ fullmap.html }}
</div>
</body>
{{ fullmap.js }}

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<script type="text/javascript">
var setLabelTime = function(){
$('.label-countdown').each(function (index, element) {
var disappearsAt = new Date(parseInt(element.getAttribute("disappears-at"))*1000);
var now = new Date();

var difference = Math.abs(disappearsAt - now);
var hours = Math.floor(difference / 36e5);
var minutes = Math.floor((difference - (hours * 36e5)) / 6e4);
var seconds = Math.floor((difference - (hours * 36e5) - (minutes * 6e4)) / 1e3);

if(disappearsAt < now){
timestring = "(expired)";
}
else {
timestring = "(";
if(hours > 0)
timestring = hours + "h";

timestring += ("0" + minutes).slice(-2) + "m";
timestring += ("0" + seconds).slice(-2) + "s";
timestring += ")";
}

$(element).text(timestring)
});
};

window.setInterval(setLabelTime, 1000);
</script>

{% if auto_refresh %}
<script type="text/javascript">
var baseURL = location.protocol + "//" + location.hostname + (location.port ? ":"+location.port: "");
var options = {};
var map = null;
var markers = [];
var markerCache = {};
// Adds a marker to the map and push to the array.
function addMarker(options = {}) {
var default_options = {map: map}
for(var prop in options){
if(options.hasOwnProperty(prop)){
default_options[prop] = options[prop];
}
}
var marker = new google.maps.Marker(default_options);
markers.push(marker);
return marker;
}

// Sets the map on all markers in the array.
function setMapOnAll(map, length = null) {
var lastIndex = markers.length -1;
if(length != null){
lastIndex = length;
}
for (var i = lastIndex; i >= 0 ; i--) {
if(!markers[i].persist){
markers[i].setMap(map);
if(map == null){
if(markers[i].timeout != null){
clearTimeout(markers[i].timeout);
}
if(markers[i].key != null){
var cacheIndex = Object.keys(markerCache).indexOf(markers[i].key);
if(cacheIndex >= 0){
delete markerCache[markers[i].key];
}
}
markers.slice(i, 1);
}
}
}
}

// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}

// Shows any markers currently in the array.
function showMarkers() {
setMapOnAll(map);
}

// Deletes all markers in the array by removing references to them.
function deleteMarkers(length) {
setMapOnAll(null, length);
}

$.get(baseURL + "/config", function(response){
var json_obj = $.parseJSON(response);
options["lat"] = json_obj["lat"];
options["lng"] = json_obj["lng"];
options["zoom"] = json_obj["zoom"];
options["identifier"] = json_obj["identifier"];
updateMap();
});

function createMap(){
if(map == null && google != null && google.maps != null){
if(options.identifier != null){
map = new google.maps.Map(
document.getElementById(options["identifier"]), {
center: new google.maps.LatLng(options["lat"], options["lng"]),
zoom: options["zoom"],
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControl: true,
mapTypeControl: true,
scaleControl: true,
streetViewControl: true,
rotateControl: true,
fullscreenControl: true
});
}
}
}

function updateMap(){
// A new map is created because the original one isn't saved
createMap();
// Requests the data and populates the map
$.get(baseURL + "/data", function(response){
var json_obj = $.parseJSON(response);
var now = new Date();
var notifyList = [];

for (var index in json_obj) {
var item = json_obj[index];
var key = item["type"]+item["key"];
if(Object.keys(markerCache).indexOf(key) >= 0){
var needs_replacing = false;
if(item["type"] == "gym" && item["icon"] != markerCache[key].item.icon){
(function(_marker){setTimeout(_marker.setMap(null), 500)})(markerCache[key].marker);
needs_replacing = true;
}
if((markerCache[key].item.lat != item["lat"] && markerCache[key].item.lng != item['lng'])){

console.log("Warning: object with identical key has different coordinates please report bug", key);
needs_replacing = true;
}
if (markerCache[key].item.type != item["type"] || (item["infobox"] != null && markerCache[key].item["infobox"] != null && item["infobox"] != markerCache[key].item["infobox"])) {
(function(_marker){setTimeout(_marker.setMap(null), 500)})(markerCache[key].marker);
needs_replacing = true;
}
if(!needs_replacing){
continue;
}
}
if(markerCache[key] != null && markerCache[key].marker != null){
markerCache[key].marker.setMap(null);
}
var marker = addMarker({
position: new google.maps.LatLng(item["lat"], item["lng"]),
map: map,
icon: item["icon"],
});
markerCache[key] = {item: item, marker: marker};
var disappearsAt;

if(item["disappear_time"] != null){
if(parseInt(item["disappear_time"]) < 0){
disappearsAt = -1;
} else {
disappearsAt = new Date(parseInt(item["disappear_time"] * 1000)) - now;
}
} else {
disappearsAt = {{ auto_refresh }} + 500;
}

if (item["infobox"]) {
(function(_infobox, _map, _marker){
_marker.infoWindow = new google.maps.InfoWindow({
content: _infobox
});
_marker.addListener('click', function() {
_marker.infoWindow.open(_map, _marker);
_marker["persist"] = true;
});

google.maps.event.addListener(_marker.infoWindow,'closeclick',function(){
_marker["persist"] = null;
});
})(item["infobox"], map, marker);
}

(function(_marker, _disappearsAt){
if(_disappearsAt < 0){

} else {
var timeout = setTimeout(function(){_marker.setMap(null);}, Math.ceil(_disappearsAt))
_marker.timeout = timeout;
}
_marker.key = key;
})(marker, disappearsAt);
}
});
}
window.setInterval(updateMap, {{ auto_refresh }});
</script>
{% endif %}
</html>
20 changes: 13 additions & 7 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import config


def get_map_center():
"""Returns center of the map"""
lat = (config.MAP_END[0] + config.MAP_START[0]) / 2
lon = (config.MAP_END[1] + config.MAP_START[1]) / 2
return lat, lon


def get_start_coords(worker_no):
"""
Returns center of square for given worker
"""
total_workers = config.GRID[0] * config.GRID[1]
per_column = total_workers / config.GRID[0]
"""Returns center of square for given worker"""
grid = config.GRID
total_workers = grid[0] * grid[1]
per_column = total_workers / grid[0]
column = worker_no % per_column
row = worker_no / per_column
part_lat = (config.MAP_END[0] - config.MAP_START[0]) / float(config.GRID[0])
part_lon = (config.MAP_END[1] - config.MAP_START[1]) / float(config.GRID[1])
part_lat = (config.MAP_END[0] - config.MAP_START[0]) / float(grid[0])
part_lon = (config.MAP_END[1] - config.MAP_START[1]) / float(grid[1])
start_lat = config.MAP_START[0] + part_lat * row + part_lat / 2
start_lon = config.MAP_START[1] + part_lon * column + part_lon / 2
return start_lat, start_lon
Loading

0 comments on commit acd5122

Please sign in to comment.