Skip to content

Commit

Permalink
Merge pull request #97 from WatWowMap/dyn-gen-info
Browse files Browse the repository at this point in the history
feat: dynamically set generation info
  • Loading branch information
TurtIeSocks committed Jan 24, 2024
2 parents 4bcc4ec + 8bfe99d commit 905448c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pogo-data-generator",
"version": "1.16.8",
"version": "1.16.9",
"description": "Pokemon GO project data generator",
"author": "TurtIeSocks",
"license": "Apache-2.0",
Expand Down
31 changes: 30 additions & 1 deletion src/classes/PokeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
TempEvolutions,
} from '../typings/dataTypes'
import { TypeProto, PokemonIdProto, MoveProto } from '../typings/protos'
import { PokeApiStats, PokeApiTypes } from '../typings/pokeapi'
import {
BasePokeApiStruct,
PokeApiStats,
PokeApiTypes,
} from '../typings/pokeapi'
import { SpeciesApi } from '../typings/general'

export default class PokeApi extends Masterfile {
Expand Down Expand Up @@ -541,4 +545,29 @@ export default class PokeApi extends Masterfile {
}),
)
}

async getGenerations() {
const generations: { results: BasePokeApiStruct[] } = await this.fetch(
'https://pokeapi.co/api/v2/generation',
)
const results = await Promise.all(
generations.results.map(async (gen, index) => {
const {
main_region,
pokemon_species,
}: {
main_region: BasePokeApiStruct
pokemon_species: BasePokeApiStruct[]
} = await this.fetch(gen.url)
const name = this.capitalize(main_region.name)
const pokemonIds = pokemon_species.map(
(pokemon) => +pokemon.url.split('/').at(-2),
)
const min = Math.min(...pokemonIds)
const max = Math.max(...pokemonIds)
return { id: index + 1, name, range: [min, max] }
}),
)
return Object.fromEntries(results.map(({ id, ...rest}) => [id, rest]))
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export async function generate({
)
const AllPokeApi = new PokeApi()
await AllPokeApi.setMaxPokemonId()
const generations = await AllPokeApi.getGenerations()
AllPokemon.generations = generations
const AllMisc = new Misc()
const apk = new ApkReader()

Expand Down
59 changes: 16 additions & 43 deletions src/typings/pokeapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,42 @@ import { AllPokemon, AllTypes } from './dataTypes'

export interface PokeApiStats {
abilities: {
ability: {
name: string
url: string
}
ability: BasePokeApiStruct
is_hidden: boolean
slot: number
}[]
base_experience: number
forms: {
name: string
url: string
}[]
forms: BasePokeApiStruct[]
game_indices: {
game_index: number
version: {
name: string
url: string
}
version: BasePokeApiStruct
}[]
height: number
held_items: []
id: number
is_default: boolean
location_area_encounters: string
moves: {
move: {
name: string
url: string
}
move: BasePokeApiStruct
version_group_details: {
level_learned_at: number
move_learn_method: {
name: string
url: string
}
version_group: {
name: string
url: string
}
move_learn_method: BasePokeApiStruct
version_group: BasePokeApiStruct
}[]
}[]
name: string
order: number
past_types: []
species: {
name: string
url: string
}
species: BasePokeApiStruct
sprites: Sprites
stats: {
base_stat: number
effort: number
stat: {
name: string
url: string
}
stat: BasePokeApiStruct
}[]
types: {
slot: number
type: {
name: string
url: string
}
type: BasePokeApiStruct
}[]
weight: number
}
Expand Down Expand Up @@ -101,19 +74,19 @@ type Sprite = {
front_shiny_female?: string
}

type PokeApiType = {
export type BasePokeApiStruct = {
name: string
url: string
}

export interface PokeApiTypes {
damage_relations: {
double_damage_from: PokeApiType[]
double_damage_to: PokeApiType[]
half_damage_from: PokeApiType[]
half_damage_to: PokeApiType[]
no_damage_from: PokeApiType[]
no_damage_to: PokeApiType[]
double_damage_from: BasePokeApiStruct[]
double_damage_to: BasePokeApiStruct[]
half_damage_from: BasePokeApiStruct[]
half_damage_to: BasePokeApiStruct[]
no_damage_from: BasePokeApiStruct[]
no_damage_to: BasePokeApiStruct[]
}
}

Expand Down

0 comments on commit 905448c

Please sign in to comment.