Skip to content

Commit 4d2f4e5

Browse files
authored
Merge pull request #1310 from PaoloAJ/feature/affecting_items
Feature: affecting_items field to stats #1309 *Rebased to recent master branch*
2 parents 2fe19f6 + a922bdd commit 4d2f4e5

File tree

2 files changed

+90
-10
lines changed

2 files changed

+90
-10
lines changed

Makefile

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,51 @@ docker_config = --settings=config.docker-compose
44
gql_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml
55
gqlv1beta_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml -f Resources/compose/docker-compose-prod-graphql-v1beta.yml
66

7+
# Auto-detect Python and pip commands
8+
PYTHON := $(shell which python3 2>/dev/null || which python 2>/dev/null || echo python3)
9+
PIP := $(shell which pip3 2>/dev/null || which pip 2>/dev/null || echo pip3)
10+
711
.PHONY: help
812
.SILENT:
913

1014
help:
1115
@grep -E '^[a-zA-Z_-]+:.*?# .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?# "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
1216

1317
install: # Install base requirements to run project
14-
pip install -r requirements.txt
18+
$(PIP) install -r requirements.txt
1519

1620
dev-install: # Install developer requirements + base requirements
17-
pip install -r test-requirements.txt
21+
$(PIP) install -r test-requirements.txt
1822

1923
setup: # Set up the project database
20-
python manage.py migrate ${local_config}
24+
$(PYTHON) manage.py migrate ${local_config}
2125

2226
build-db: # Build database
23-
echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${local_config}
27+
echo "from data.v2.build import build_all; build_all()" | $(PYTHON) manage.py shell ${local_config}
2428

2529
wipe-sqlite-db: # Delete's the project database
2630
rm -rf db.sqlite3
2731

2832
serve: # Run the project locally
29-
python manage.py runserver ${local_config}
33+
$(PYTHON) manage.py runserver ${local_config}
3034

3135
test: # Run tests
32-
python manage.py test ${local_config}
36+
$(PYTHON) manage.py test ${local_config}
3337

3438
clean: # Remove any pyc files
3539
find . -type f -name '*.pyc' -delete
3640

3741
migrate: # Run any outstanding migrations
38-
python manage.py migrate ${local_config}
42+
$(PYTHON) manage.py migrate ${local_config}
3943

4044
make-migrations: # Create migrations files if schema has changed
41-
python manage.py makemigrations ${local_config}
45+
$(PYTHON) manage.py makemigrations ${local_config}
4246

4347
shell: # Load a shell
44-
python manage.py shell ${local_config}
48+
$(PYTHON) manage.py shell ${local_config}
4549

4650
openapi-generate:
47-
python manage.py spectacular --color --file openapi.yml ${local_config}
51+
$(PYTHON) manage.py spectacular --color --file openapi.yml ${local_config}
4852

4953
docker-up: # (Docker) Create services/volumes/networks
5054
docker compose up -d

pokemon_v2/serializers.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ class StatDetailSerializer(serializers.ModelSerializer):
14211421
)
14221422
affecting_moves = serializers.SerializerMethodField("get_moves_that_affect")
14231423
affecting_natures = serializers.SerializerMethodField("get_natures_that_affect")
1424+
affecting_items = serializers.SerializerMethodField("get_items_that_affect")
14241425

14251426
class Meta:
14261427
model = Stat
@@ -1431,6 +1432,7 @@ class Meta:
14311432
"is_battle_only",
14321433
"affecting_moves",
14331434
"affecting_natures",
1435+
"affecting_items",
14341436
"characteristics",
14351437
"move_damage_class",
14361438
"names",
@@ -1569,6 +1571,80 @@ def get_natures_that_affect(self, obj):
15691571

15701572
return OrderedDict([("increase", increases), ("decrease", decreases)])
15711573

1574+
@extend_schema_field(
1575+
field={
1576+
"type": "array",
1577+
"items": {
1578+
"type": "object",
1579+
"required": ["name", "url"],
1580+
"properties": {
1581+
"name": {
1582+
"type": "string",
1583+
"examples": ["protein", "x-attack"],
1584+
},
1585+
"url": {
1586+
"type": "string",
1587+
"format": "uri",
1588+
"examples": ["https://pokeapi.co/api/v2/item/46/"],
1589+
},
1590+
},
1591+
},
1592+
}
1593+
)
1594+
def get_items_that_affect(self, obj):
1595+
"""
1596+
Get items that affect this stat (like vitamins, X-items, etc.)
1597+
"""
1598+
# Map stat names to their corresponding vitamin items
1599+
stat_item_mapping = {
1600+
"hp": ["hp-up"],
1601+
"attack": ["protein"],
1602+
"defense": ["iron"],
1603+
"special-attack": ["calcium"],
1604+
"special-defense": ["zinc"],
1605+
"speed": ["carbos"],
1606+
}
1607+
1608+
# Get the stat name (lowercase)
1609+
stat_name = obj.name.lower()
1610+
1611+
# Find items that affect this stat
1612+
affecting_items = []
1613+
1614+
# Check for vitamin items
1615+
if stat_name in stat_item_mapping:
1616+
for item_identifier in stat_item_mapping[stat_name]:
1617+
try:
1618+
item = Item.objects.get(name=item_identifier)
1619+
affecting_items.append(
1620+
ItemSummarySerializer(item, context=self.context).data
1621+
)
1622+
except Item.DoesNotExist:
1623+
pass
1624+
1625+
# Check for X-items (like X Attack, X Defense, etc.)
1626+
x_item_mapping = {
1627+
"attack": ["x-attack"],
1628+
"defense": ["x-defense"],
1629+
"special-attack": ["x-sp-atk"],
1630+
"special-defense": ["x-sp-def"],
1631+
"speed": ["x-speed"],
1632+
"accuracy": ["x-accuracy"],
1633+
"evasion": ["x-evasion"],
1634+
}
1635+
1636+
if stat_name in x_item_mapping:
1637+
for item_identifier in x_item_mapping[stat_name]:
1638+
try:
1639+
item = Item.objects.get(name=item_identifier)
1640+
affecting_items.append(
1641+
ItemSummarySerializer(item, context=self.context).data
1642+
)
1643+
except Item.DoesNotExist:
1644+
pass
1645+
1646+
return affecting_items
1647+
15721648

15731649
#############################
15741650
# ITEM POCKET SERIALIZERS #

0 commit comments

Comments
 (0)