Merge pull request #1310 from PaoloAJ/feature/affecting_items

Feature: affecting_items field to stats #1309 *Rebased to recent master branch*
This commit is contained in:
Alessandro Pezzè 2025-10-15 21:48:26 +09:00 committed by GitHub
commit 4d2f4e564b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 90 additions and 10 deletions

View File

@ -4,6 +4,10 @@ docker_config = --settings=config.docker-compose
gql_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml
gqlv1beta_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml -f Resources/compose/docker-compose-prod-graphql-v1beta.yml
# Auto-detect Python and pip commands
PYTHON := $(shell which python3 2>/dev/null || which python 2>/dev/null || echo python3)
PIP := $(shell which pip3 2>/dev/null || which pip 2>/dev/null || echo pip3)
.PHONY: help
.SILENT:
@ -11,40 +15,40 @@ help:
@grep -E '^[a-zA-Z_-]+:.*?# .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?# "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
install: # Install base requirements to run project
pip install -r requirements.txt
$(PIP) install -r requirements.txt
dev-install: # Install developer requirements + base requirements
pip install -r test-requirements.txt
$(PIP) install -r test-requirements.txt
setup: # Set up the project database
python manage.py migrate ${local_config}
$(PYTHON) manage.py migrate ${local_config}
build-db: # Build database
echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${local_config}
echo "from data.v2.build import build_all; build_all()" | $(PYTHON) manage.py shell ${local_config}
wipe-sqlite-db: # Delete's the project database
rm -rf db.sqlite3
serve: # Run the project locally
python manage.py runserver ${local_config}
$(PYTHON) manage.py runserver ${local_config}
test: # Run tests
python manage.py test ${local_config}
$(PYTHON) manage.py test ${local_config}
clean: # Remove any pyc files
find . -type f -name '*.pyc' -delete
migrate: # Run any outstanding migrations
python manage.py migrate ${local_config}
$(PYTHON) manage.py migrate ${local_config}
make-migrations: # Create migrations files if schema has changed
python manage.py makemigrations ${local_config}
$(PYTHON) manage.py makemigrations ${local_config}
shell: # Load a shell
python manage.py shell ${local_config}
$(PYTHON) manage.py shell ${local_config}
openapi-generate:
python manage.py spectacular --color --file openapi.yml ${local_config}
$(PYTHON) manage.py spectacular --color --file openapi.yml ${local_config}
docker-up: # (Docker) Create services/volumes/networks
docker compose up -d

View File

@ -1421,6 +1421,7 @@ class StatDetailSerializer(serializers.ModelSerializer):
)
affecting_moves = serializers.SerializerMethodField("get_moves_that_affect")
affecting_natures = serializers.SerializerMethodField("get_natures_that_affect")
affecting_items = serializers.SerializerMethodField("get_items_that_affect")
class Meta:
model = Stat
@ -1431,6 +1432,7 @@ class StatDetailSerializer(serializers.ModelSerializer):
"is_battle_only",
"affecting_moves",
"affecting_natures",
"affecting_items",
"characteristics",
"move_damage_class",
"names",
@ -1569,6 +1571,80 @@ class StatDetailSerializer(serializers.ModelSerializer):
return OrderedDict([("increase", increases), ("decrease", decreases)])
@extend_schema_field(
field={
"type": "array",
"items": {
"type": "object",
"required": ["name", "url"],
"properties": {
"name": {
"type": "string",
"examples": ["protein", "x-attack"],
},
"url": {
"type": "string",
"format": "uri",
"examples": ["https://pokeapi.co/api/v2/item/46/"],
},
},
},
}
)
def get_items_that_affect(self, obj):
"""
Get items that affect this stat (like vitamins, X-items, etc.)
"""
# Map stat names to their corresponding vitamin items
stat_item_mapping = {
"hp": ["hp-up"],
"attack": ["protein"],
"defense": ["iron"],
"special-attack": ["calcium"],
"special-defense": ["zinc"],
"speed": ["carbos"],
}
# Get the stat name (lowercase)
stat_name = obj.name.lower()
# Find items that affect this stat
affecting_items = []
# Check for vitamin items
if stat_name in stat_item_mapping:
for item_identifier in stat_item_mapping[stat_name]:
try:
item = Item.objects.get(name=item_identifier)
affecting_items.append(
ItemSummarySerializer(item, context=self.context).data
)
except Item.DoesNotExist:
pass
# Check for X-items (like X Attack, X Defense, etc.)
x_item_mapping = {
"attack": ["x-attack"],
"defense": ["x-defense"],
"special-attack": ["x-sp-atk"],
"special-defense": ["x-sp-def"],
"speed": ["x-speed"],
"accuracy": ["x-accuracy"],
"evasion": ["x-evasion"],
}
if stat_name in x_item_mapping:
for item_identifier in x_item_mapping[stat_name]:
try:
item = Item.objects.get(name=item_identifier)
affecting_items.append(
ItemSummarySerializer(item, context=self.context).data
)
except Item.DoesNotExist:
pass
return affecting_items
#############################
# ITEM POCKET SERIALIZERS #