def update_device_locations(self): LOG.debug('Running device controller task') session = database.Session() raids = database.get_raids_for_forts(session, self.forts) session.close() for fort_time in self.locked_forts: if fort_time.time <= time.time(): self.locked_forts.remove(fort_time) forts_no_raid = [] forts_no_boss = [] for fort in self.forts: if fort in [fort_time.fort for fort_time in self.locked_forts]: continue hasRaid = False for raid in raids: if fort.id == raid.fort_id: hasRaid = True if (raid.pokemon_id is None or raid.pokemon_id == 0) and raid.time_battle <= time.time(): forts_no_boss.append(fort) break if not hasRaid: forts_no_raid.append(fort) for device in self.devices: fort = None if len(forts_no_boss) > 0: random.shuffle(forts_no_boss) fort = forts_no_boss.pop() elif len(forts_no_raid) > 0: random.shuffle(forts_no_raid) fort = forts_no_raid.pop() session = database.Session() if fort is not None: self.locked_forts.append(FortTime(fort, time.time() + 120)) self.teleport(device, fort.lat, fort.lon, session) session.close()
def update_raids_and_forts(t_obj, lock, forts_static): time.sleep(1) while True: is_locked = False try: LOG.debug('Updating Raids and checking Gyms') session = database.Session() raids = database.get_raids_for_forts(session, forts_static) db_raids = [] for raid in raids: db_raids.append(DBRaid(raid.id,raid.fort_id,raid.level,raid.pokemon_id,raid.time_spawn,raid.time_battle,raid.time_end)) session.commit() session.close() lock.acquire() is_locked = True locked_forts = t_obj.get_locked_forts() for fort_time in locked_forts: if fort_time.time <= time.time(): locked_forts.remove(fort_time) t_obj.set_locked_forts(locked_forts) forts = t_obj.get_forts() forts_no_raid = [] forts_no_raid_priority = [] forts_no_boss = [] for fort in forts: if fort.id in [fort_time.fort_id for fort_time in locked_forts]: continue hasRaid = False for raid in db_raids: if raid.time_end <= time.time(): continue if fort.id == raid.fort_id: hasRaid = True if (raid.pokemon_id is None or raid.pokemon_id == 0) and raid.time_battle <= time.time(): if raid.time_battle + 1200 >= time.time(): forts_no_boss.append(fort) else: hasRaid = False break if not hasRaid: if time.time() - fort.updated >= 300: forts_no_raid_priority.append(fort) else: forts_no_raid.append(fort) random.shuffle(forts_no_boss) random.shuffle(forts_no_raid_priority) random.shuffle(forts_no_raid) t_obj.set_forts_no_raid(forts_no_raid) t_obj.set_forts_no_raid_priority(forts_no_raid_priority) t_obj.set_forts_no_boss(forts_no_boss) lock.release() time.sleep(5) except KeyboardInterrupt: sys.exit(0) except Exception as e: LOG.error('Failed to update Raids and Gyms: {}'.format(e)) if is_locked: lock.release() time.sleep(1)