| 978 | ) |
| 979 | class PokemonEncounterView(APIView): |
| 980 | def get(self, request, pokemon_id): |
| 981 | self.context = dict(request=request) |
| 982 | |
| 983 | try: |
| 984 | pokemon = Pokemon.objects.get(pk=pokemon_id) |
| 985 | except Pokemon.DoesNotExist: |
| 986 | raise Http404 |
| 987 | |
| 988 | encounter_objects = Encounter.objects.filter(pokemon=pokemon) |
| 989 | |
| 990 | area_ids = ( |
| 991 | encounter_objects.order_by("location_area") |
| 992 | .distinct("location_area") |
| 993 | .values_list("location_area", flat=True) |
| 994 | ) |
| 995 | |
| 996 | location_area_objects = LocationArea.objects.filter(pk__in=area_ids) |
| 997 | version_objects = Version.objects |
| 998 | |
| 999 | encounters_list = [] |
| 1000 | |
| 1001 | for area_id in area_ids: |
| 1002 | location_area = location_area_objects.get(pk=area_id) |
| 1003 | |
| 1004 | area_encounters = encounter_objects.filter(location_area_id=area_id) |
| 1005 | |
| 1006 | version_ids = ( |
| 1007 | area_encounters.order_by("version_id") |
| 1008 | .distinct("version_id") |
| 1009 | .values_list("version_id", flat=True) |
| 1010 | ) |
| 1011 | |
| 1012 | version_details_list = [] |
| 1013 | |
| 1014 | for version_id in version_ids: |
| 1015 | version = version_objects.get(pk=version_id) |
| 1016 | |
| 1017 | version_encounters = area_encounters.filter( |
| 1018 | version_id=version_id |
| 1019 | ).order_by("encounter_slot_id") |
| 1020 | |
| 1021 | encounters_data = EncounterDetailSerializer( |
| 1022 | version_encounters, many=True, context=self.context |
| 1023 | ).data |
| 1024 | |
| 1025 | max_chance = 0 |
| 1026 | encounter_details_list = [] |
| 1027 | |
| 1028 | for encounter in encounters_data: |
| 1029 | slot = EncounterSlot.objects.get(pk=encounter["encounter_slot"]) |
| 1030 | slot_data = EncounterSlotSerializer(slot, context=self.context).data |
| 1031 | |
| 1032 | del encounter["pokemon"] |
| 1033 | del encounter["encounter_slot"] |
| 1034 | del encounter["location_area"] |
| 1035 | del encounter["version"] |
| 1036 | encounter["chance"] = slot_data["chance"] |
| 1037 | max_chance += slot_data["chance"] |