MCPcopy
hub / github.com/PokeAPI/pokeapi / get_encounters

Method get_encounters

pokemon_v2/serializers.py:1193–1249  ·  view source on GitHub ↗
(self, obj)

Source from the content-addressed store, hash-verified

1191 }
1192 )
1193 def get_encounters(self, obj):
1194 # get versions for later use
1195 version_objects = Version.objects.all()
1196 version_data = VersionSummarySerializer(
1197 version_objects, many=True, context=self.context
1198 ).data
1199
1200 # all encounters associated with location area
1201 all_encounters = Encounter.objects.filter(location_area=obj).order_by("pokemon")
1202 encounters_list = []
1203
1204 # break encounters into pokemon groupings
1205 for poke in all_encounters.values("pokemon").distinct():
1206 pokemon_object = Pokemon.objects.get(pk=poke["pokemon"])
1207
1208 pokemon_detail = OrderedDict()
1209 pokemon_detail["pokemon"] = PokemonSummarySerializer(
1210 pokemon_object, context=self.context
1211 ).data
1212 pokemon_detail["version_details"] = []
1213
1214 poke_encounters = all_encounters.filter(pokemon=poke["pokemon"]).order_by(
1215 "version"
1216 )
1217
1218 # each pokemon has multiple versions it could be encountered in
1219 for ver in poke_encounters.values("version").distinct():
1220 version_detail = OrderedDict()
1221 version_detail["version"] = version_data[ver["version"] - 1]
1222 version_detail["max_chance"] = 0
1223 version_detail["encounter_details"] = []
1224
1225 poke_data = EncounterDetailSerializer(
1226 poke_encounters.filter(version=ver["version"]),
1227 many=True,
1228 context=self.context,
1229 ).data
1230
1231 # each version has multiple ways a pokemon can be encountered
1232 for encounter in poke_data:
1233 slot = EncounterSlot.objects.get(pk=encounter["encounter_slot"])
1234 slot_data = EncounterSlotSerializer(slot, context=self.context).data
1235 del encounter["pokemon"]
1236 del encounter["encounter_slot"]
1237 del encounter["location_area"]
1238 del encounter["version"]
1239 encounter["chance"] = slot_data["chance"]
1240 version_detail["max_chance"] += slot_data["chance"]
1241 encounter["method"] = slot_data["encounter_method"]
1242
1243 version_detail["encounter_details"].append(encounter)
1244
1245 pokemon_detail["version_details"].append(version_detail)
1246
1247 encounters_list.append(pokemon_detail)
1248
1249 return encounters_list
1250

Callers

nothing calls this directly

Calls 5

getMethod · 0.80

Tested by

no test coverage detected