def mon_iv(self, origin: str, timestamp: float, encounter_proto: dict, mitm_mapper): """ Update/Insert a mon with IVs """ cache = get_cache(self._args) origin_logger = get_origin_logger(logger, origin=origin) wild_pokemon = encounter_proto.get("wild_pokemon", None) if wild_pokemon is None or wild_pokemon.get( "encounter_id", 0) == 0 or not str(wild_pokemon["spawnpoint_id"]): return origin_logger.debug3("Updating IV sent for encounter at {}", timestamp) now = datetime.utcfromtimestamp( time.time()).strftime("%Y-%m-%d %H:%M:%S") spawnid = int(str(wild_pokemon["spawnpoint_id"]), 16) getdetspawntime = self._get_detected_endtime(str(spawnid)) despawn_time_unix = gen_despawn_timestamp(getdetspawntime, timestamp) despawn_time = datetime.utcfromtimestamp(despawn_time_unix).strftime( "%Y-%m-%d %H:%M:%S") latitude = wild_pokemon.get("latitude") longitude = wild_pokemon.get("longitude") pokemon_data = wild_pokemon.get("pokemon_data") encounter_id = wild_pokemon["encounter_id"] shiny = wild_pokemon["pokemon_data"]["display"].get("is_shiny", 0) pokemon_display = pokemon_data.get("display", {}) weather_boosted = pokemon_display.get('weather_boosted_value', None) if encounter_id < 0: encounter_id = encounter_id + 2**64 cache_key = "moniv{}{}".format(encounter_id, weather_boosted) if cache.exists(cache_key): return mitm_mapper.collect_mon_iv_stats(origin, encounter_id, int(shiny)) if getdetspawntime is None: origin_logger.debug3( "updating IV mon #{} at {}, {}. Despawning at {} (init)", pokemon_data["id"], latitude, longitude, despawn_time) else: origin_logger.debug3( "updating IV mon #{} at {}, {}. Despawning at {} (non-init)", pokemon_data["id"], latitude, longitude, despawn_time) capture_probability = encounter_proto.get("capture_probability") capture_probability_list = capture_probability.get( "capture_probability_list") if capture_probability_list is not None: capture_probability_list = capture_probability_list.replace( "[", "").replace("]", "").split(",") # ditto detector if is_mon_ditto(origin_logger, pokemon_data): # mon must be a ditto :D mon_id = 132 gender = 3 move_1 = 242 move_2 = 133 form = 0 else: mon_id = pokemon_data.get("id") gender = pokemon_display.get("gender_value", None) move_1 = pokemon_data.get("move_1") move_2 = pokemon_data.get("move_2") form = pokemon_display.get("form_value", None) query = ( "INSERT INTO pokemon (encounter_id, spawnpoint_id, pokemon_id, latitude, longitude, disappear_time, " "individual_attack, individual_defense, individual_stamina, move_1, move_2, cp, cp_multiplier, " "weight, height, gender, catch_prob_1, catch_prob_2, catch_prob_3, rating_attack, rating_defense, " "weather_boosted_condition, last_modified, costume, form) " "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, " "%s, %s, %s, %s, %s) " "ON DUPLICATE KEY UPDATE last_modified=VALUES(last_modified), disappear_time=VALUES(disappear_time), " "individual_attack=VALUES(individual_attack), individual_defense=VALUES(individual_defense), " "individual_stamina=VALUES(individual_stamina), move_1=VALUES(move_1), move_2=VALUES(move_2), " "cp=VALUES(cp), cp_multiplier=VALUES(cp_multiplier), weight=VALUES(weight), height=VALUES(height), " "gender=VALUES(gender), catch_prob_1=VALUES(catch_prob_1), catch_prob_2=VALUES(catch_prob_2), " "catch_prob_3=VALUES(catch_prob_3), rating_attack=VALUES(rating_attack), " "rating_defense=VALUES(rating_defense), weather_boosted_condition=VALUES(weather_boosted_condition), " "costume=VALUES(costume), form=VALUES(form), pokemon_id=VALUES(pokemon_id)" ) insert_values = (encounter_id, spawnid, mon_id, latitude, longitude, despawn_time, pokemon_data.get("individual_attack"), pokemon_data.get("individual_defense"), pokemon_data.get("individual_stamina"), move_1, move_2, pokemon_data.get("cp"), pokemon_data.get("cp_multiplier"), pokemon_data.get("weight"), pokemon_data.get("height"), gender, float(capture_probability_list[0]), float(capture_probability_list[1]), float(capture_probability_list[2]), None, None, weather_boosted, now, pokemon_display.get("costume_value", None), form) self._db_exec.execute(query, insert_values, commit=True) cache_time = int(despawn_time_unix - datetime.now().timestamp()) if cache_time > 0: cache.set(cache_key, 1, ex=int(cache_time)) origin_logger.debug3("Done updating mon in DB") return True
def mons(self, origin: str, timestamp: float, map_proto: dict, mitm_mapper): """ Update/Insert mons from a map_proto dict """ cache = get_cache(self._args) origin_logger = get_origin_logger(logger, origin=origin) origin_logger.debug3( "DbPogoProtoSubmit::mons called with data received") cells = map_proto.get("cells", None) if cells is None: return False query_mons = ( "INSERT INTO pokemon (encounter_id, spawnpoint_id, pokemon_id, latitude, longitude, disappear_time, " "individual_attack, individual_defense, individual_stamina, move_1, move_2, cp, cp_multiplier, " "weight, height, gender, catch_prob_1, catch_prob_2, catch_prob_3, rating_attack, rating_defense, " "weather_boosted_condition, last_modified, costume, form) " "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, " "%s, %s, %s, %s, %s) " "ON DUPLICATE KEY UPDATE last_modified=VALUES(last_modified), disappear_time=VALUES(disappear_time)" ) mon_args = [] for cell in cells: for wild_mon in cell["wild_pokemon"]: spawnid = int(str(wild_mon["spawnpoint_id"]), 16) lat = wild_mon["latitude"] lon = wild_mon["longitude"] mon_id = wild_mon["pokemon_data"]["id"] encounter_id = wild_mon["encounter_id"] if encounter_id < 0: encounter_id = encounter_id + 2**64 mitm_mapper.collect_mon_stats(origin, str(encounter_id)) now = datetime.utcfromtimestamp( time.time()).strftime("%Y-%m-%d %H:%M:%S") # get known spawn end time and feed into despawn time calculation getdetspawntime = self._get_detected_endtime(str(spawnid)) despawn_time_unix = gen_despawn_timestamp( getdetspawntime, timestamp) despawn_time = datetime.utcfromtimestamp( despawn_time_unix).strftime("%Y-%m-%d %H:%M:%S") if getdetspawntime is None: origin_logger.debug3( "adding mon (#{}) at {}, {}. Despawns at {} (init) ({})", mon_id, lat, lon, despawn_time, spawnid) else: origin_logger.debug3( "adding mon (#{}) at {}, {}. Despawns at {} (non-init) ({})", mon_id, lat, lon, despawn_time, spawnid) cache_key = "mon{}".format(encounter_id) if cache.exists(cache_key): continue mon_args.append(( encounter_id, spawnid, mon_id, lat, lon, despawn_time, # TODO: consider .get("XXX", None) # noqa: E800 None, None, None, None, None, None, None, None, None, wild_mon["pokemon_data"]["display"]["gender_value"], None, None, None, None, None, wild_mon["pokemon_data"]["display"] ["weather_boosted_value"], now, wild_mon["pokemon_data"]["display"]["costume_value"], wild_mon["pokemon_data"]["display"]["form_value"])) cache_time = int(despawn_time_unix - int(datetime.now().timestamp())) if cache_time > 0: cache.set(cache_key, 1, ex=cache_time) self._db_exec.executemany(query_mons, mon_args, commit=True) return True
def mon_iv(self, origin: str, timestamp: float, encounter_proto: dict, mitm_mapper): """ Update/Insert a mon with IVs """ wild_pokemon = encounter_proto.get("wild_pokemon", None) if wild_pokemon is None or wild_pokemon.get( "encounter_id", 0) == 0 or not str(wild_pokemon["spawnpoint_id"]): return logger.debug("Updating IV sent by {} for encounter at {}".format( str(origin), str(timestamp))) now = datetime.utcfromtimestamp( time.time()).strftime("%Y-%m-%d %H:%M:%S") spawnid = int(str(wild_pokemon["spawnpoint_id"]), 16) getdetspawntime = self._get_detected_endtime(str(spawnid)) despawn_time_unix = gen_despawn_timestamp(getdetspawntime) despawn_time = datetime.utcfromtimestamp(despawn_time_unix).strftime( "%Y-%m-%d %H:%M:%S") latitude = wild_pokemon.get("latitude") longitude = wild_pokemon.get("longitude") pokemon_data = wild_pokemon.get("pokemon_data") encounter_id = wild_pokemon["encounter_id"] shiny = wild_pokemon["pokemon_data"]["display"].get("is_shiny", 0) if encounter_id < 0: encounter_id = encounter_id + 2**64 mitm_mapper.collect_mon_iv_stats(origin, encounter_id, int(shiny)) if getdetspawntime is None: logger.debug( "{}: updating IV mon #{} at {}, {}. Despawning at {} (init)", str(origin), pokemon_data["id"], latitude, longitude, despawn_time) else: logger.debug( "{}: updating IV mon #{} at {}, {}. Despawning at {} (non-init)", str(origin), pokemon_data["id"], latitude, longitude, despawn_time) capture_probability = encounter_proto.get("capture_probability") capture_probability_list = capture_probability.get( "capture_probability_list") if capture_probability_list is not None: capture_probability_list = capture_probability_list.replace( "[", "").replace("]", "").split(",") pokemon_display = pokemon_data.get("display") if pokemon_display is None: pokemon_display = {} # initialize to not run into nullpointer # ditto detector if pokemon_data.get("id") in (13, 46, 163, 165, 167, 187, 223, 293, 316, 322, 399, 590) and \ ((pokemon_display.get("weather_boosted_value", None) is not None and pokemon_display.get("weather_boosted_value", None) > 0) \ and (pokemon_data.get("individual_attack") < 4 or pokemon_data.get( "individual_defense") < 4 or pokemon_data.get("individual_stamina") < 4 or pokemon_data.get("cp_multiplier") < .3) or \ (pokemon_display.get("weather_boosted_value", None) is not None and pokemon_display.get("weather_boosted_value", None) == 0) \ and pokemon_data.get("cp_multiplier") > .733): # mon must be a ditto :D mon_id = 132 gender = 3 move_1 = 242 move_2 = 133 form = 0 else: mon_id = pokemon_data.get("id") gender = pokemon_display.get("gender_value", None) move_1 = pokemon_data.get("move_1") move_2 = pokemon_data.get("move_2") form = pokemon_display.get("form_value", None) query = ( "INSERT INTO pokemon (encounter_id, spawnpoint_id, pokemon_id, latitude, longitude, disappear_time, " "individual_attack, individual_defense, individual_stamina, move_1, move_2, cp, cp_multiplier, " "weight, height, gender, catch_prob_1, catch_prob_2, catch_prob_3, rating_attack, rating_defense, " "weather_boosted_condition, last_modified, costume, form) " "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, " "%s, %s, %s, %s, %s) " "ON DUPLICATE KEY UPDATE last_modified=VALUES(last_modified), disappear_time=VALUES(disappear_time), " "individual_attack=VALUES(individual_attack), individual_defense=VALUES(individual_defense), " "individual_stamina=VALUES(individual_stamina), move_1=VALUES(move_1), move_2=VALUES(move_2), " "cp=VALUES(cp), cp_multiplier=VALUES(cp_multiplier), weight=VALUES(weight), height=VALUES(height), " "gender=VALUES(gender), catch_prob_1=VALUES(catch_prob_1), catch_prob_2=VALUES(catch_prob_2), " "catch_prob_3=VALUES(catch_prob_3), rating_attack=VALUES(rating_attack), " "rating_defense=VALUES(rating_defense), weather_boosted_condition=VALUES(weather_boosted_condition), " "costume=VALUES(costume), form=VALUES(form), pokemon_id=VALUES(pokemon_id)" ) vals = (encounter_id, spawnid, mon_id, latitude, longitude, despawn_time, pokemon_data.get("individual_attack"), pokemon_data.get("individual_defense"), pokemon_data.get("individual_stamina"), move_1, move_2, pokemon_data.get("cp"), pokemon_data.get("cp_multiplier"), pokemon_data.get("weight"), pokemon_data.get("height"), gender, float(capture_probability_list[0]), float(capture_probability_list[1]), float(capture_probability_list[2]), None, None, pokemon_display.get('weather_boosted_value', None), now, pokemon_display.get("costume_value", None), form) self._db_exec.execute(query, vals, commit=True) logger.debug("Done updating mon in DB") return True