Skip to content

Commit

Permalink
Optimize Spawns Class
Browse files Browse the repository at this point in the history
Add all known points to `despawn_times` even if out of bounds
or they will always be added to the mystery_sightings table

and fixes the problem that `db_has` only had all points if `MORE_POINTS`
was enabled
  • Loading branch information
evenly-epic-mule committed Feb 8, 2017
1 parent a5e287a commit ced1e3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
21 changes: 9 additions & 12 deletions monocle/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
'RARE_IDS': [],
'REPORT_SINCE': None,
'BOUNDARIES': None,
'STAY_WITHIN_MAP': True,
'MORE_POINTS': True
'STAY_WITHIN_MAP': True
}
for setting_name, default in _optional.items():
if not hasattr(config, setting_name):
Expand Down Expand Up @@ -391,13 +390,15 @@ def session_scope(autoflush=False):

def get_spawns(session):
spawns = session.query(Spawnpoint)
mysteries = set()
mysteries = {}
spawns_dict = {}
despawn_times = {}
altitudes = {}
known_points = set()
for spawn in spawns:
point = (spawn.lat, spawn.lon)
known = (spawn.updated and spawn.updated > config.LAST_MIGRATION)
if known:
despawn_times[spawn.spawn_id] = spawn.despawn_time

# skip if point is not within boundaries (if applicable)
if not Bounds.contain(point):
Expand All @@ -406,19 +407,16 @@ def get_spawns(session):
rounded = utils.round_coords(point, 3)
altitudes[rounded] = spawn.alt

if not spawn.updated or spawn.updated <= config.LAST_MIGRATION:
mysteries.add(point)
if not known:
mysteries[spawn.spawn_id] = point
continue

if spawn.duration == 60:
spawn_time = spawn.despawn_time
else:
spawn_time = (spawn.despawn_time + 1800) % 3600

despawn_times[spawn.spawn_id] = spawn.despawn_time
spawns_dict[spawn.spawn_id] = (point, spawn_time)
if config.MORE_POINTS:
known_points.add(point)

spawns = OrderedDict(sorted(spawns_dict.items(), key=lambda k: k[1][1]))
return spawns, despawn_times, mysteries, altitudes, known_points
Expand Down Expand Up @@ -512,14 +510,13 @@ def add_spawnpoint(session, pokemon):
duration=duration
)
session.add(obj)
shared.SPAWNS.add_known(point)


def add_mystery_spawnpoint(session, pokemon):
# Check if the same entry already exists
spawn_id = pokemon['spawn_id']
point = (pokemon['lat'], pokemon['lon'])
if shared.SPAWNS.db_has(point):
if shared.SPAWNS.db_has(spawn_id):
return
existing = session.query(exists().where(
Spawnpoint.spawn_id == spawn_id)).scalar()
Expand All @@ -539,7 +536,7 @@ def add_mystery_spawnpoint(session, pokemon):
session.add(obj)

if Bounds.contain(point):
shared.SPAWNS.add_mystery(point)
shared.SPAWNS.add_mystery(spawn_id, point)


def add_mystery(session, pokemon):
Expand Down
46 changes: 24 additions & 22 deletions monocle/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
class Spawns:
"""Manage spawn points and times"""
def __init__(self):
self.version = 1
self.spawns = OrderedDict()
self.despawn_times = {}
self.mysteries = set()
self.mysteries = {}
self.cell_points = set()
self.altitudes = {}
self.known_points = set()

def __len__(self):
return len(self.despawn_times)
Expand All @@ -31,15 +31,18 @@ def __bool__(self):
return len(self.despawn_times) > 0

def update(self, loadpickle=False):
if loadpickle:
# this is broken! spawns.pickle is only written if loadpickle is false or failes
# newly learned spawns ware just thrown away and may be readded as mysteries
# besides that newly found mysteries are lost
if loadpickle and False:
try:
self.spawns, self.despawn_times, self.mysteries, self.altitudes, self.known_points = load_pickle('spawns')
if self.mysteries or self.despawn_times:
version, self.spawns, self.despawn_times, self.mysteries, self.altitudes = load_pickle('spawns')
if version == self.version and (self.mysteries or self.despawn_times):
return
except Exception:
pass
with db.session_scope() as session:
self.spawns, self.despawn_times, self.mysteries, a, self.known_points = db.get_spawns(session)
self.spawns, self.despawn_times, self.mysteries, a = db.get_spawns(session)
self.altitudes.update(a)
if not self.altitudes:
self.altitudes = get_point_altitudes()
Expand All @@ -57,7 +60,7 @@ def items(self):
return self.spawns.items()

def get_mysteries(self):
mysteries = deque(self.mysteries | self.cell_points)
mysteries = deque(self.mysteries.values() | self.cell_points)
shuffle(mysteries)
return mysteries

Expand All @@ -72,30 +75,29 @@ def after_last(self):

def add_despawn(self, spawn_id, despawn_time):
self.despawn_times[spawn_id] = despawn_time
self.remove_mystery(spawn_id)

def add_known(self, point):
self.known_points.add(point)
self.remove_mystery(point)

def add_mystery(self, point):
self.mysteries.add(point)
def add_mystery(self, spawn_id, point):
point = round_coords(point, 4)
self.mysteries[spawn_id] = point
self.cell_points.discard(point)

def add_cell_point(self, point):
point = round_coords(point, 4)
self.cell_points.add(point)

def remove_mystery(self, point):
self.mysteries.discard(point)
self.cell_points.discard(point)
def remove_mystery(self, spawn_id):
try:
point = self.mysteries.pop(spawn_id)
self.cell_points.discard(point)
except KeyError:
pass

def get_despawn_seconds(self, spawn_id):
return self.despawn_times.get(spawn_id)

def db_has(self, point):
return point in chain(self.known_points, self.mysteries)

def have_point(self, point):
return point in chain(self.cell_points, self.known_points, self.mysteries)
def db_has(self, spawn_id):
return spawn_id in {**self.despawn_times, **self.mysteries}

def get_despawn_time(self, spawn_id, seen=None):
now = seen or time()
Expand All @@ -115,7 +117,7 @@ def get_time_till_hidden(self, spawn_id):

@property
def pickle_objects(self):
return self.spawns, self.despawn_times, self.mysteries, self.altitudes, self.known_points
return self.version, self.spawns, self.despawn_times, self.mysteries, self.altitudes

@property
def total_length(self):
Expand Down

0 comments on commit ced1e3d

Please sign in to comment.