Pretend you just got into Pokemon. You watch a few episodes of the anime and maybe dabbled into the card game. You now decide to buy my first Pokemon game. You want to optimally play the game by knowing every type and what each type is super-effective or not-very effective. However, you am too lazy to actually look up stuff on Bulbapedia or blisy.net (both of which have extensive use of PokeAPI funnily enough), and you want to feel “smart” by looking them up using PokeAPI. It would be very useful to use the GET function in R to get each type and its relationships to optimally pick moves in battle.
First API Usage: Number of Types
The main question I want to know before the type advantages is what types are there in Pokemon? We will find out together.
We first can load these libraries.
library(tidyverse) # Quality of Life
Warning: package 'tidyverse' was built under R version 4.5.3
Warning: package 'tidyr' was built under R version 4.5.3
Warning: package 'readr' was built under R version 4.5.3
Warning: package 'purrr' was built under R version 4.5.3
Warning: package 'dplyr' was built under R version 4.5.3
Warning: package 'forcats' was built under R version 4.5.3
Warning: package 'lubridate' was built under R version 4.5.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.1 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(jsonlite) # JSON interactions
Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':
flatten
library(magrittr) # Advanced Pipe Operator
Attaching package: 'magrittr'
The following object is masked from 'package:purrr':
set_names
The following object is masked from 'package:tidyr':
extract
library(httr) # HTTP interactions
Warning: package 'httr' was built under R version 4.5.3
Then, I want to work with getting the start website of our API call. It is very easy to start, all you need is “https://pokeapi.co/api/v2”, with arguments afterwards. I use the “/type/” argument as that is the main category we want out of PokeAPI.
— THERE IS NO API KEY NEEDED. USING A URL IS FINE. —
startlink <-"https://pokeapi.co/api/v2/type/"
I use this link in a GET() request to get all data in the “type” argument, and we can get the “results” DataFrame, as within it holds the name and url of each type (the number).
name url
1 normal https://pokeapi.co/api/v2/type/1/
2 fighting https://pokeapi.co/api/v2/type/2/
3 flying https://pokeapi.co/api/v2/type/3/
4 poison https://pokeapi.co/api/v2/type/4/
5 ground https://pokeapi.co/api/v2/type/5/
6 rock https://pokeapi.co/api/v2/type/6/
7 bug https://pokeapi.co/api/v2/type/7/
8 ghost https://pokeapi.co/api/v2/type/8/
9 steel https://pokeapi.co/api/v2/type/9/
10 fire https://pokeapi.co/api/v2/type/10/
11 water https://pokeapi.co/api/v2/type/11/
12 grass https://pokeapi.co/api/v2/type/12/
13 electric https://pokeapi.co/api/v2/type/13/
14 psychic https://pokeapi.co/api/v2/type/14/
15 ice https://pokeapi.co/api/v2/type/15/
16 dragon https://pokeapi.co/api/v2/type/16/
17 dark https://pokeapi.co/api/v2/type/17/
18 fairy https://pokeapi.co/api/v2/type/18/
19 stellar https://pokeapi.co/api/v2/type/19/
20 unknown https://pokeapi.co/api/v2/type/10001/
From the call, I see that there are 20 types, corresponding with 20 rows. However, we are going to disregard Stellar and Unknown types (Stellar is specifically for a Generation IX mechanic that extremely complicated, and Unknown only deals with eggs, the move “Curse” if they user is a Ghost type prior to Generation V, and a hacked Arceus in Generation IV). This leaves us with 18.
Second API Usage: Type Effectiveness
Now that we know how many types there are. I have this code here that prints every single type, that checks if there is more than 0 super-effective, not-very-effective, or no-effective, it prints the whole list.
for(x in1:18){ actual_type =paste0(startlink,x,sep="") alltypes <- actual_type %>%GET() %>%content(as ="text",encoding ="UTF-8") %>%fromJSON()# Double Damage: Super-Effectiveif(length(alltypes$damage_relations$double_damage_to$name)!=0) {print(paste0(alltypes$name," deals double damage to ", alltypes$damage_relations$double_damage_to$name)) }# Half Damage: Not-Very-Effectiveif(length(alltypes$damage_relations$half_damage_to$name)!=0) {print(paste0(alltypes$name," deals half damage to ", alltypes$damage_relations$half_damage_to$name)) }# No Damage: Immuneif(length(alltypes$damage_relations$no_damage_to$name)!=0) {print(paste(alltypes$name," deals no damage to ", alltypes$damage_relations$no_damage_to$name)) }Sys.sleep(1) # We do not want to bombard the API with too many requests too soon.}
[1] "normal deals half damage to rock" "normal deals half damage to steel"
[1] "normal deals no damage to ghost"
[1] "fighting deals double damage to normal"
[2] "fighting deals double damage to rock"
[3] "fighting deals double damage to steel"
[4] "fighting deals double damage to ice"
[5] "fighting deals double damage to dark"
[1] "fighting deals half damage to flying"
[2] "fighting deals half damage to poison"
[3] "fighting deals half damage to bug"
[4] "fighting deals half damage to psychic"
[5] "fighting deals half damage to fairy"
[1] "fighting deals no damage to ghost"
[1] "flying deals double damage to fighting"
[2] "flying deals double damage to bug"
[3] "flying deals double damage to grass"
[1] "flying deals half damage to rock"
[2] "flying deals half damage to steel"
[3] "flying deals half damage to electric"
[1] "poison deals double damage to grass" "poison deals double damage to fairy"
[1] "poison deals half damage to poison" "poison deals half damage to ground"
[3] "poison deals half damage to rock" "poison deals half damage to ghost"
[1] "poison deals no damage to steel"
[1] "ground deals double damage to poison"
[2] "ground deals double damage to rock"
[3] "ground deals double damage to steel"
[4] "ground deals double damage to fire"
[5] "ground deals double damage to electric"
[1] "ground deals half damage to bug" "ground deals half damage to grass"
[1] "ground deals no damage to flying"
[1] "rock deals double damage to flying" "rock deals double damage to bug"
[3] "rock deals double damage to fire" "rock deals double damage to ice"
[1] "rock deals half damage to fighting" "rock deals half damage to ground"
[3] "rock deals half damage to steel"
[1] "bug deals double damage to grass" "bug deals double damage to psychic"
[3] "bug deals double damage to dark"
[1] "bug deals half damage to fighting" "bug deals half damage to flying"
[3] "bug deals half damage to poison" "bug deals half damage to ghost"
[5] "bug deals half damage to steel" "bug deals half damage to fire"
[7] "bug deals half damage to fairy"
[1] "ghost deals double damage to ghost"
[2] "ghost deals double damage to psychic"
[1] "ghost deals half damage to dark"
[1] "ghost deals no damage to normal"
[1] "steel deals double damage to rock" "steel deals double damage to ice"
[3] "steel deals double damage to fairy"
[1] "steel deals half damage to steel" "steel deals half damage to fire"
[3] "steel deals half damage to water" "steel deals half damage to electric"
[1] "fire deals double damage to bug" "fire deals double damage to steel"
[3] "fire deals double damage to grass" "fire deals double damage to ice"
[1] "fire deals half damage to rock" "fire deals half damage to fire"
[3] "fire deals half damage to water" "fire deals half damage to dragon"
[1] "water deals double damage to ground" "water deals double damage to rock"
[3] "water deals double damage to fire"
[1] "water deals half damage to water" "water deals half damage to grass"
[3] "water deals half damage to dragon"
[1] "grass deals double damage to ground" "grass deals double damage to rock"
[3] "grass deals double damage to water"
[1] "grass deals half damage to flying" "grass deals half damage to poison"
[3] "grass deals half damage to bug" "grass deals half damage to steel"
[5] "grass deals half damage to fire" "grass deals half damage to grass"
[7] "grass deals half damage to dragon"
[1] "electric deals double damage to flying"
[2] "electric deals double damage to water"
[1] "electric deals half damage to grass"
[2] "electric deals half damage to electric"
[3] "electric deals half damage to dragon"
[1] "electric deals no damage to ground"
[1] "psychic deals double damage to fighting"
[2] "psychic deals double damage to poison"
[1] "psychic deals half damage to steel"
[2] "psychic deals half damage to psychic"
[1] "psychic deals no damage to dark"
[1] "ice deals double damage to flying" "ice deals double damage to ground"
[3] "ice deals double damage to grass" "ice deals double damage to dragon"
[1] "ice deals half damage to steel" "ice deals half damage to fire"
[3] "ice deals half damage to water" "ice deals half damage to ice"
[1] "dragon deals double damage to dragon"
[1] "dragon deals half damage to steel"
[1] "dragon deals no damage to fairy"
[1] "dark deals double damage to ghost" "dark deals double damage to psychic"
[1] "dark deals half damage to fighting" "dark deals half damage to dark"
[3] "dark deals half damage to fairy"
[1] "fairy deals double damage to fighting"
[2] "fairy deals double damage to dragon"
[3] "fairy deals double damage to dark"
[1] "fairy deals half damage to poison" "fairy deals half damage to steel"
[3] "fairy deals half damage to fire"
That is a lot of type relationships. But I feel that we do not need every single one. Instead, what if I want to put up the type of the user and the type of the opponent and see if there is a relationship if I attack them. PokeAPI also allows for inputting the type instead of its ID, so we can take advantage of that. We can now make a function to use later. (I am using the “types” API call above, so please get that before trying this).
With this, we can also add a test to see if both a and b are valid, as the user might not know the correct Pokemon types and incorrectly put in a type. It well even help if the model freaks out and inputs an error, as it would automatically funnel through the invalid type conditional.
effectiveness <-function(a,b) {# a is the user, b is the opponent. a is using an offensive move link <-paste0(startlink,a,sep ="") # Combines for the full URL relationship <- link %>%GET() %>%content(as ="text",encoding ="UTF-8") %>%fromJSON()if(b %in% relationship$damage_relations$double_damage_to$name) # Checks if 2x {print(paste0(toupper(a)," attacks are super-effective (2x) against ",toupper(b)," types")) }elseif(b %in% relationship$damage_relations$half_damage_to$name) # Checks if 1/2x {print(paste0(toupper(a)," attacks are not-very-effective (1/2x) against ",toupper(b)," types")) }elseif(b %in% relationship$damage_relations$no_damage_to$name) # Checks if 0x {print(paste0(toupper(a)," attacks do no damage (0x) against ",toupper(b)," types")) }elseif(b %in% types$results$name & a %in% types$results$name ) # 1x {print(paste0(toupper(a)," attacks do neutral damage (1x) against ",toupper(b)," types")) }else# Invalid {print("At least one of the types is invalid") }Sys.sleep(1) # I do not trust that people will wait to use the API, so I make them.}
Now that we can categorize each, I am going to use the “ghost” type to test it as it has all 4 of these characteristics to other types, and I can just test something against “ghost” for if the first input is invalid.
effectiveness("ghost","fighting") # Neutral
[1] "GHOST attacks do neutral damage (1x) against FIGHTING types"
effectiveness("ghost","ghost") # Super-Effective
[1] "GHOST attacks are super-effective (2x) against GHOST types"
[1] "GHOST attacks are not-very-effective (1/2x) against DARK types"
effectiveness("ghost","normal") # Immune
[1] "GHOST attacks do no damage (0x) against NORMAL types"
effectiveness("ghost","a") # Invalid Type (2)
[1] "At least one of the types is invalid"
effectiveness("a","ghost") # Invalid Type (1)
[1] "At least one of the types is invalid"
effectiveness("ghost","psychic") # Test after the error (Super-Effective)
[1] "GHOST attacks are super-effective (2x) against PSYCHIC types"
Conclusion
It works! And we can use this to test every type advantage combination between 2 types. Now it is up to you to use this to choose your perfect move or switch to another Pokemon. Just do not take this too seriously if you lose.