예제 #1
0
def asString(gymparticipant):
    first = as_str(gymparticipant["trainer_name"]) + ", " + str(
        gymparticipant["last_modified"]) + ", "
    second = str(gymparticipant["latitude"]) + ", " + str(
        gymparticipant["longitude"]) + ", "
    gym_name = as_str(gymparticipant["name"])
    return first + second + gym_name
예제 #2
0
def log_gym_change(g, previousgym):
    latitude_ = g["latitude"]
    longitude_ = g["longitude"]
    modified_ = g["last_modified"]
    kmh = None
    distance = None
    previous_gym = None
    name_ = as_str(g["name"])
    if not previousgym is None:
        previous_latitude = previousgym["latitude"]
        previous_longitude = previousgym["longitude"]
        previous_lastmodified = previousgym["last_modified"]
        previous_gym = as_str(previousgym["name"])

        elapsed_seconds = (modified_  - previous_lastmodified).total_seconds()

        prevgymcoords = (previous_latitude, previous_longitude)
        thisgymccords = (latitude_, longitude_)
        distance = vincenty(prevgymcoords, thisgymccords).m

        print("Distance between " + previous_gym + str(prevgymcoords) + " and "  + name_ + str(thisgymccords) + " is" + str(distance) +", elapsed is " + str(elapsed_seconds))
        if distance == 0:
            distance = None
        elif elapsed_seconds  > 0 and distance  > 0:
            kmh = distance/elapsed_seconds * 3.6
        else:
            previous_gym = None
            distance = None

    log_gym_change_in_db(g, previous_gym, kmh, distance)
예제 #3
0
def as_map(account):
    res = {
        "username": as_str(account.name()),
        "password": as_str(account.password),
        "provider": account.auth_service
    }
    return res
예제 #4
0
 async def lure_stop(self, pokestop):
     stop_pos = (pokestop.latitude, pokestop.longitude)
     pokestop_details = pokestop_detail(
         await self.worker.do_pokestop_details(pokestop))
     await asyncio.sleep(3)
     lure = await self.worker.do_add_lure(pokestop, stop_pos)
     pokestop_name = as_str(pokestop_details.name)
     return lure, pokestop_name
예제 #5
0
 async def log_first_time_pokestop_info(self, pokestop):
     details = pokestop_detail(await
                               self.worker.do_pokestop_details(pokestop))
     pokestop_name = as_str(details.name)
     self.stop_names[pokestop.id] = pokestop_name
     try:
         log.info(u"Pokestop {} served by {}".format(
             pokestop_name, self.worker.name()))
     except UnicodeDecodeError:
         log.error("Unicode decode error x")
     await asyncio.sleep(2)
예제 #6
0
def log_gym_change_in_db(g, previous_gym_name, kmh, distance):
    connection = __gymmapconnection()

    try:
        latitude_ = g["latitude"]
        longitude_ = g["longitude"]
        name_ = as_str(g["name"])
        modified_ = g["last_modified"]

        sql = "INSERT INTO gymlog(" \
              "gym_id,`name`,trainer_name," \
              "pokemon_uid,latitude,longitude," \
              "last_modified,last_scanned,previous_scan,gym_member_last_scanned,  " \
              "gym_points,team_id, pokemon_id, " \
              "cp, iv_attack, iv_defense, " \
              "iv_stamina, distance, kmh, previous_gym) " \
              "VALUES(" \
              "%s,%s,%s," \
              "%s,%s,%s," \
              "%s,%s,%s,%s," \
              "%s,%s,%s," \
              "%s,%s,%s," \
              "%s,%s,%s,%s)";
        # Read a single record
        trainer_name_ = as_str(g["trainer_name"])
        id_ = as_str(g["gym_id"])
        pokemon_id_ = g["pokemon_id"]
        uid_ = as_str(g["pokemon_uid"])
        args = (id_, name_, trainer_name_, uid_, latitude_, longitude_,
                modified_, g["last_scanned"], g["last_scanned"],
                g["gym_member_last_scanned"],
                g["gym_points"], g["team_id"], pokemon_id_,
                g["cp"], g["iv_attack"], g["iv_defense"], g["iv_stamina"], kmh,
                distance, previous_gym_name)
        connection.cursor().execute(sql, args)
        connection.commit()
    except IntegrityError as e:
        print("Already inserted")
    finally:
        connection.close()
예제 #7
0
async def upsert_account(username, password, provider, system_id):
    async with __account_db_connection() as connection:
        async with connection.cursor() as cursor:
            cursor.execute(*upsert_account_select(username))
            fetchone = cursor.fetchone()
            if fetchone is None:
                cursor.execute(*upsert_account_insert(username, password,
                                                      provider, system_id))
            elif fetchone['system_id'] and fetchone['system_id'] != system_id:
                msg = "Account {} exits in database with other system_id ({}), " \
                      "cannot be created for this system_id".format(username, as_str(fetchone["system_id"]))
                raise ValueError(msg)
        connection.commit()
예제 #8
0
def gym_map(fences):
    result = {}
    for name in gym_names():
        if fences.within_fences(name["latitude"], name["longitude"]):
            result[name["gym_id"]] = as_str(name["name"])
    return result
예제 #9
0
    async def slave_task(self, pos):
        map_objects = await self.worker_with_map_objects(pos=pos, team=1)
        gyms = inrange_gyms(map_objects, pos)
        if len(gyms) == 0:
            if self.worker:
                self.log.info(
                    u"Worker {} not seeing any gyms, skipping".format(
                        self.worker.name()))
            await self.replace_account(pos)
            map_objects = await self.worker_with_map_objects(pos=pos, team=1)
            gyms = inrange_gyms(map_objects, pos)

        if len(gyms) == 0:
            self.log.info(
                u"Worker {} not seeing any gyms, at this coordinate, exiting".
                format(self.worker.name()))
            self.running = False
            return 10

        gym = gyms[0]
        id_ = gym.id

        gym_pos = gym.latitude, gym.longitude
        gym_get_info = self.worker.do_gym_get_info(pos, gym_pos, id_)
        gym_get_info_data = gym_get_info["responses"]["GYM_GET_INFO"]
        self.gym_name = as_str(gym_get_info_data.name)

        while self.running:
            try:
                motivated_pokemon = self.queue.get(block=True, timeout=60)

                berry = self.berry_to_use(self.inventory)
                if not berry:
                    self.log.info(u"No more berries on account, replacing")
                    await self.replace_account(pos)
                count = self.inventory.get(berry)
                ret = feed_pokemon(self.worker.get_raw_api(),
                                   self.worker.account_info(), berry,
                                   motivated_pokemon.pokemon.id, id_, pos,
                                   count)
                if berry in self.inventory:
                    self.inventory[berry] -= 1
                remaining = self.inventory.get(berry, 0)
                log.info(
                    u"Fed pokemon {} berry {} ({} remaining) on gym {}".format(
                        motivated_pokemon.pokemon.id, berry, remaining,
                        self.gym_name))
                if ret["responses"]["GYM_FEED_POKEMON"].SUCCESS != 1:
                    self.log.warning("Not successful " + (str(ret)))
                else:
                    self.feedings[motivated_pokemon.pokemon.id] += 1
                await asyncio.sleep(2)

                self.queue.task_done()

                await asyncio.sleep(10)

                for x in self.feedings:
                    if self.feedings[x] == 2:
                        self.log.info(
                            u"Acount has been feeding enough, changing account for {}"
                            .format(self.gym_name))
                        await self.replace_account(pos)
            except Empty:
                if self.termination_checker():
                    self.running = False
예제 #10
0
    async def berry_gym(self, pos, first_time):
        map_objects = await self.worker_with_map_objects(pos=pos, team=1)
        gyms = list(inrange_gyms(map_objects, pos))
        if len(gyms) == 0:
            if self.worker:
                log.info(u"Worker {} not seeing any gyms, skipping".format(
                    self.worker.name()))
            await self.replace_account(pos)
            map_objects = await self.worker_with_map_objects(pos=pos, team=1)
            gyms = list(inrange_gyms(map_objects, pos))

        if len(gyms) == 0:
            log.info(
                u"Worker {} not seeing any gyms, at this coordinate, exiting".
                format(self.worker.name()))
            self.running = False
            return 10

        gym = gyms[0]
        id_ = gym.id

        await self.spin_stops(map_objects, pos)

        if id_ in self.passives:
            if gym.last_modified_timestamp_ms == self.passives[id_]:
                log.info(u"No change in gym")
                return 120

        gym_pos = gym.latitude, gym.longitude
        gym_get_info = await self.worker.do_gym_get_info(pos, gym_pos, id_)
        gym_get_info_data = gym_get_info["responses"]["GYM_GET_INFO"]
        self.gym_name = as_str(gym_get_info_data.name)

        if gym.owned_by_team != 1:
            if id_ not in self.passives:
                log.info(
                    u"{} is being held by the wrong team. Waiting for the good guys"
                    .format(self.gym_name))
                self.passives[id_] = gym.last_modified_timestamp_ms
            return 120

        if first_time:
            log.info(u"There are {} gyms in range for {} at {}.".format(
                str(len(gyms)), self.worker.name(), str(pos)))

        gym_status_and_defenders = gym_get_info_data.gym_status_and_defenders
        pokemon_for_proto = gym_status_and_defenders.pokemon_fort_proto
        raid_info = pokemon_for_proto.raid_info
        raid_battle = datetime.datetime.fromtimestamp(
            raid_info.raid_battle_ms / 1000)
        raid_end = datetime.datetime.fromtimestamp(raid_info.raid_end_ms /
                                                   1000)

        if raid_battle < datetime.datetime.now() < raid_end:
            diff = (raid_end - datetime.datetime.now()).total_seconds()
            if id_ not in self.passives:
                log.info(
                    u"Gym {} is closed for raid until {}, sleeping {}".format(
                        self.gym_name, str(raid_end), diff))
                self.passives[id_] = gym.last_modified_timestamp_ms
            return diff

        if not self.contains_trainers(gym_get_info):
            if id_ not in self.passives:
                log.info(u"Trainers not in gym {}, waiting".format(
                    self.gym_name))
                self.passives[id_] = gym.last_modified_timestamp_ms
            return 1200 + random.uniform(0, 900)
        elif first_time:
            log.info(u"Trainers in gym {}".format(self.gym_name))

        need_motivation = self.pokemon_needing_motivation(gym_get_info)

        if id_ in self.passives:
            del self.passives[id_]

        if len(need_motivation) == 0:
            return 120

        if len(need_motivation) > 0:
            if id_ not in self.first_berried:
                self.first_berried[id_] = datetime.datetime.now()
            await asyncio.sleep(5)

        filtered_needy = self.filter_motivation(id_, need_motivation)

        under_attack = pokemon_for_proto.is_in_battle
        if len(filtered_needy) > 0 or (under_attack and
                                       (self.defend or self.is_night())):
            if under_attack:
                s2msg("{} is under attack".format(self.gym_name))
            if datetime.datetime.now() > self.next_battle_helper:
                helper_end = datetime.datetime.now() + datetime.timedelta(
                    minutes=self.defense_duration)
                self.next_battle_helper = helper_end
                self.slaves = []
                log.info(u"STARTING FEEDER SLAVES for {} {}".format(
                    self.gym_name, "Under ATTACK" if under_attack else ""))
                await self.add_feed_slave(pos, helper_end)
                await self.add_feed_slave(pos, helper_end)
                await self.add_feed_slave(pos, helper_end)
                await self.add_feed_slave(pos, helper_end)
                if self.heavy_defense:
                    log.info(
                        u"Starting additional battle slaves to work with gym {} under attack"
                        .format(self.gym_name))
                    await self.add_feed_slave(pos, helper_end)
                    await self.add_feed_slave(pos, helper_end)
                    await self.add_feed_slave(pos, helper_end)
                    await self.add_feed_slave(pos, helper_end)
                    await asyncio.sleep(30)  # extra time for heavy defense

                await asyncio.sleep(70)  # give workers time to start

        for motivated_pokemon in filtered_needy:
            for slave_queue in self.slaves:
                slave_queue.put(motivated_pokemon)

            berry = self.berry_to_use(self.inventory)
            if not berry:
                log.info(u"No more berries on account, replacing")
                await self.replace_account(pos)
            count = self.inventory.get(berry)
            ret = feed_pokemon(self.worker.get_raw_api(),
                               self.worker.account_info(), berry,
                               motivated_pokemon.pokemon.id, id_, pos, count)
            self.inventory[berry] -= 1
            log.info(
                u"Fed pokemon {}/{} berry {} ({} remaining) on gym {}".format(
                    motivated_pokemon.pokemon.id,
                    motivated_pokemon.pokemon.pokemon_id, berry,
                    self.inventory[berry], self.gym_name))
            if ret["responses"]["GYM_FEED_POKEMON"].SUCCESS != 1:
                print("Not successful " + (str(ret)))
            else:
                self.feedings[motivated_pokemon.pokemon.id] += 1
            await asyncio.sleep(2)

        return 12 if len(need_motivation) > 0 else 120