From e885e3fa62de0f2fa6ba0c9c243e505a7ba8fcdd Mon Sep 17 00:00:00 2001 From: gabe Date: Sun, 28 Jul 2024 20:04:30 -0400 Subject: [PATCH] feat: add new `sprites` field to `type` via adding `TypeSprites` to db containing type name icons from all games (excluding tera types, for now) --- data/v2/build.py | 39 +++++++++++++++++++++ data/v2/sprites | 2 +- pokemon_v2/migrations/0016_typesprites.py | 41 +++++++++++++++++++++++ pokemon_v2/models.py | 4 +++ pokemon_v2/serializers.py | 12 +++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 pokemon_v2/migrations/0016_typesprites.py diff --git a/data/v2/build.py b/data/v2/build.py index 9e6459eb..cfabe10a 100644 --- a/data/v2/build.py +++ b/data/v2/build.py @@ -629,6 +629,45 @@ def _build_types(): build_generic((TypeEfficacyPast,), "type_efficacy_past.csv", csv_record_to_objects) + def csv_record_to_objects(info): + game_map = { + "generation-iii": [ + "colosseum", + "emerald", + "firered-leafgreen", + "ruby-saphire", + "xd", + ], + "generation-iv": ["diamond-pearl", "heartgold-soulsilver", "platinum"], + "generation-v": ["black-2-white-2", "black-white"], + "generation-vi": ["omega-ruby-alpha-sapphire", "x-y"], + "generation-vii": [ + "lets-go-pikachu-lets-go-eevee", + "sun-moon", + "ultra-sun-ultra-moon", + ], + "generation-viii": [ + "brilliant-diamond-and-shining-pearl", + "legends-arceus", + "sword-shield", + ], + "generation-ix": ["scarlet-violet"], + } + sprites = {} + for generation in game_map.keys(): + for game in game_map[generation]: + if generation not in sprites: + sprites[generation] = {} + sprites[generation][game] = { + "name_icon": file_path_or_none( + f"types/{generation}/{game}/{info[0]}.png" + ) + } + + yield TypeSprites(type_id=int(info[0]), sprites=sprites) + + build_generic((TypeSprites,), "types.csv", csv_record_to_objects) + ############# # CONTEST # diff --git a/data/v2/sprites b/data/v2/sprites index ca5a7886..d607f0df 160000 --- a/data/v2/sprites +++ b/data/v2/sprites @@ -1 +1 @@ -Subproject commit ca5a7886c10753144e6fae3b69d45a4d42a449b4 +Subproject commit d607f0df04218c564167f654802262ef23a3cac1 diff --git a/pokemon_v2/migrations/0016_typesprites.py b/pokemon_v2/migrations/0016_typesprites.py new file mode 100644 index 00000000..a9e752d6 --- /dev/null +++ b/pokemon_v2/migrations/0016_typesprites.py @@ -0,0 +1,41 @@ +# Generated by Django 3.2.23 on 2024-07-29 02:09 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("pokemon_v2", "0015_pokemoncries"), + ] + + operations = [ + migrations.CreateModel( + name="TypeSprites", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("sprites", models.JSONField()), + ( + "type", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="typesprites", + to="pokemon_v2.type", + ), + ), + ], + options={ + "abstract": False, + }, + ), + ] diff --git a/pokemon_v2/models.py b/pokemon_v2/models.py index d7d0e48a..dfe6bc0a 100644 --- a/pokemon_v2/models.py +++ b/pokemon_v2/models.py @@ -773,6 +773,10 @@ class TypeEfficacyPast(HasTypeEfficacy, HasGeneration): pass +class TypeSprites(HasType): + sprites = models.JSONField() + + ################# # STAT MODELS # ################# diff --git a/pokemon_v2/serializers.py b/pokemon_v2/serializers.py index 3b5df51f..c59bb3e5 100644 --- a/pokemon_v2/serializers.py +++ b/pokemon_v2/serializers.py @@ -2340,6 +2340,12 @@ class TypeNameSerializer(serializers.ModelSerializer): fields = ("name", "language") +class TypeSpriteSerializer(serializers.ModelSerializer): + class Meta: + model = TypeSprites + fields = ("sprites",) + + class TypeDetailSerializer(serializers.ModelSerializer): """ Serializer for the Type resource @@ -2357,6 +2363,7 @@ class TypeDetailSerializer(serializers.ModelSerializer): ) pokemon = serializers.SerializerMethodField("get_type_pokemon") moves = MoveSummarySerializer(many=True, read_only=True, source="move") + sprites = serializers.SerializerMethodField("get_type_sprites") class Meta: model = Type @@ -2371,8 +2378,13 @@ class TypeDetailSerializer(serializers.ModelSerializer): "names", "pokemon", "moves", + "sprites", ) + def get_type_sprites(self, obj): + sprites_object = TypeSprites.objects.get(type_id=obj) + return sprites_object.sprites + # adds an entry for the given type with the given damage # factor in the given direction to the set of relations