pokeapi icon indicating copy to clipboard operation
pokeapi copied to clipboard

Add Pokemon strengths and weaknesses

Open bidery opened this issue 4 years ago • 2 comments

Hey,

During my use of the API i also discovered that in a normal Pokémon Request no strengths and weaknesses are provided in the response json.

It would be really cool if you could expand on this.

Best Regards

bidery avatar Jul 18 '22 06:07 bidery

A way to calculate them is explained in a comment on this issue here. But that does seem like an interesting addition to add to the API, perhaps on the pokemon endpoint.

C-Garza avatar Nov 11 '22 04:11 C-Garza

In [1]: import requests
   ...: from collections import defaultdict
   ...:
   ...: def get_matchups(pokemon: str):
   ...:     # 1. Fetch Pokémon types
   ...:     poke = requests.get(f"https://pokeapi.co/api/v2/pokemon/{pokemon}").json()
   ...:
   ...:     # 2. Multiply incoming damage relations across all types
   ...:     damage = defaultdict(lambda: 1.0)
   ...:     for t in poke["types"]:
   ...:         rel = requests.get(t["type"]["url"]).json()["damage_relations"]
   ...:         for x in rel["double_damage_from"]: damage[x["name"]] *= 2.0
   ...:         for x in rel["half_damage_from"]:   damage[x["name"]] *= 0.5
   ...:         for x in rel["no_damage_from"]:     damage[x["name"]] *= 0.0
   ...:
   ...:     return {k: v for k, v in sorted(damage.items()) if v != 1.0}
   ...:
   ...: print(get_matchups("charizard"))
{'bug': 0.25, 'electric': 2.0, 'fairy': 0.5, 'fighting': 0.5, 'fire': 0.5, 'grass': 0.25, 'ground': 0.0, 'rock': 4.0, 'steel': 0.5, 'water': 2.0}

its a simple computation

FallenDeity avatar Sep 01 '26 18:09 FallenDeity