/
DexService.ts
300 lines (254 loc) · 11.8 KB
/
DexService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { Arg, Args } from 'type-graphql';
import FuzzySearch from '../utils/FuzzySearch';
import pokedex from '../assets/pokedex';
import { pokedexAliases } from '../assets/aliases';
import Pokemon from '../utils/pokemon';
import DexDetails from '../structures/DexDetails';
import DexEntry from '../structures/DexEntry';
import { GraphQLJSONObject } from 'graphql-type-json';
import GenderEntry from '../structures/GenderEntry';
import Util, { SimpleFuseOptions } from '../utils/util';
import AbilitiesEntry from '../structures/AbilitiesEntry';
import StatsEntry from '../structures/StatsEntry';
import FlavorEntry from '../structures/FlavorEntry';
import PokemonPaginatedArgs from '../arguments/PokemonPaginatedArgs';
export default class DexService {
private flavors: Record<string, Pokemon.FlavorText[]>;
private tiers: Record<string, string>;
public findByNum(@Arg('num') num: number) {
return pokedex.find((poke) => poke.num === num);
}
public findBySpecies(@Arg('species') species: string) {
return pokedex.get(species);
}
public findByFuzzy(@Args() { pokemon, skip, take }: PokemonPaginatedArgs, @Arg('fuseOptions', () => GraphQLJSONObject) fuseOptions?: SimpleFuseOptions) {
const paginatedFuzzyResult = this.getByFuzzy({ pokemon, skip, take }, fuseOptions);
const queryResults: DexEntry[] = [];
for (const page of paginatedFuzzyResult) {
const dexEntry = new DexEntry();
const genderData = new GenderEntry();
const baseStatsData = new StatsEntry();
const abilitiesData = new AbilitiesEntry();
const pageGenderRatio: Pokemon.DexEntry['genderRatio'] = page.genderRatio || {
male: 0.5,
female: 0.5
};
genderData.male = `${pageGenderRatio.male * 100}%`;
genderData.female = `${pageGenderRatio.female * 100}%`;
baseStatsData.hp = page.baseStats.hp;
baseStatsData.attack = page.baseStats.atk;
baseStatsData.defense = page.baseStats.def;
baseStatsData.specialattack = page.baseStats.spa;
baseStatsData.specialdefense = page.baseStats.spd;
baseStatsData.speed = page.baseStats.spe;
abilitiesData.first = page.abilities.first;
abilitiesData.second = page.abilities.second;
abilitiesData.hidden = page.abilities.hidden;
abilitiesData.special = page.abilities.special;
dexEntry.abilities = abilitiesData;
dexEntry.gender = genderData;
dexEntry.baseStats = baseStatsData;
dexEntry.num = page.num;
dexEntry.species = page.species;
dexEntry.types = page.types;
dexEntry.color = page.color;
dexEntry.eggGroups = page.eggGroups || undefined;
dexEntry.evolutionLevel = page.evoLevel || undefined;
dexEntry.evos = page.evos || undefined;
dexEntry.prevo = page.prevo || undefined;
dexEntry.forme = page.forme || undefined;
dexEntry.formeLetter = page.formeLetter || undefined;
dexEntry.height = page.heightm;
dexEntry.weight = page.weightkg;
dexEntry.baseForme = page.baseForme || undefined;
dexEntry.baseSpecies = page.baseSpecies || undefined;
dexEntry.otherFormes = page.otherFormes || undefined;
queryResults.push(dexEntry);
}
return queryResults;
}
public async findBySpeciesWithDetails(@Args() { pokemon, skip, take, reverse }: PokemonPaginatedArgs, parsingPokemon = '') {
const basePokemonData = this.findBySpecies(pokemon);
if (!basePokemonData) {
throw new Error(`No Pokémon found for ${pokemon}`);
}
if (this.flavors === undefined) {
this.flavors = (await import('../assets/flavorText.json')).default;
}
if (this.tiers === undefined) {
this.tiers = (await import('../assets/formats.json')).default;
}
const pokemonData = new DexDetails();
const genderData = new GenderEntry();
const baseStatsData = new StatsEntry();
const abilitiesData = new AbilitiesEntry();
const evolutionChain: Promise<DexDetails>[] = [];
const preevolutionChain: Promise<DexDetails>[] = [];
const basePokemonGenderRatio: Pokemon.DexEntry['genderRatio'] = basePokemonData.genderRatio || {
male: 0.5,
female: 0.5
};
genderData.male = `${basePokemonGenderRatio.male * 100}%`;
genderData.female = `${basePokemonGenderRatio.female * 100}%`;
baseStatsData.hp = basePokemonData.baseStats.hp;
baseStatsData.attack = basePokemonData.baseStats.atk;
baseStatsData.defense = basePokemonData.baseStats.def;
baseStatsData.specialattack = basePokemonData.baseStats.spa;
baseStatsData.specialdefense = basePokemonData.baseStats.spd;
baseStatsData.speed = basePokemonData.baseStats.spe;
abilitiesData.first = basePokemonData.abilities.first;
abilitiesData.second = basePokemonData.abilities.second;
abilitiesData.hidden = basePokemonData.abilities.hidden;
abilitiesData.special = basePokemonData.abilities.special;
pokemonData.abilities = abilitiesData;
pokemonData.gender = genderData;
pokemonData.baseStats = baseStatsData;
pokemonData.num = basePokemonData.num;
pokemonData.species = basePokemonData.species;
pokemonData.types = basePokemonData.types;
pokemonData.color = basePokemonData.color;
pokemonData.eggGroups = basePokemonData.eggGroups || undefined;
pokemonData.evolutionLevel = basePokemonData.evoLevel || undefined;
pokemonData.evos = basePokemonData.evos;
pokemonData.prevo = basePokemonData.prevo;
pokemonData.smogonTier = this.tiers[pokemon.replace(/([-% ])/gm, '')] || 'Undiscovered';
pokemonData.height = basePokemonData.heightm;
pokemonData.weight = basePokemonData.weightkg;
pokemonData.baseForme = basePokemonData.baseForme;
pokemonData.baseSpecies = basePokemonData.baseSpecies;
pokemonData.otherFormes = basePokemonData.otherFormes;
pokemonData.baseStatsTotal = this.parseBaseStatsTotal(basePokemonData.baseStats);
pokemonData.flavorTexts = [];
pokemonData.serebiiPage = basePokemonData.num >= 1 ? `https://www.serebii.net/pokedex-sm/${basePokemonData.num}.shtml` : '';
pokemonData.bulbapediaPage = basePokemonData.num >= 1 ? this.parseSpeciesForBulbapedia(basePokemonData.species, basePokemonData.baseForme || basePokemonData.baseSpecies) : '';
pokemonData.sprite = this.parseSpeciesForSprite(basePokemonData.species, basePokemonData.baseSpecies, basePokemonData.specialSprite, basePokemonData.specialShinySprite);
pokemonData.shinySprite = this.parseSpeciesForSprite(basePokemonData.species, basePokemonData.baseSpecies, basePokemonData.specialSprite, basePokemonData.specialShinySprite, true);
pokemonData.smogonPage = basePokemonData.num >= 1 ? `https://www.smogon.com/dex/sm/pokemon/${Util.toLowerHyphenCase(basePokemonData.species)}` : '';
if (basePokemonData.num >= 0) {
let shouldParseBaseForme = true;
if (basePokemonData.forme) {
const formFlavors = this.flavors[`${basePokemonData.num}${basePokemonData.forme.toLowerCase()}`];
if (formFlavors) {
shouldParseBaseForme = false;
for (const formFlavor of formFlavors) {
const formFlavorEntry = new FlavorEntry();
formFlavorEntry.game = formFlavor.version_id;
formFlavorEntry.flavor = formFlavor.flavor_text;
pokemonData.flavorTexts.push(formFlavorEntry);
}
}
}
if (shouldParseBaseForme) {
const baseFlavors = this.flavors[basePokemonData.num];
for (const baseFlavor of baseFlavors) {
const formFlavorEntry = new FlavorEntry();
formFlavorEntry.game = baseFlavor.version_id;
formFlavorEntry.flavor = baseFlavor.flavor_text;
pokemonData.flavorTexts.push(formFlavorEntry);
}
}
}
if (reverse) {
pokemonData.flavorTexts.reverse();
}
pokemonData.flavorTexts = pokemonData.flavorTexts.slice(skip, skip + take);
if (basePokemonData.prevo && basePokemonData.prevo !== parsingPokemon) {
const prevoPokemon = this.findBySpecies(basePokemonData.prevo);
if (prevoPokemon) {
preevolutionChain.push(
this.findBySpeciesWithDetails(
{
pokemon: Util.toLowerSingleWordCase(prevoPokemon.species),
skip,
take,
reverse
},
this.parseDataForEvolutionRecursion(basePokemonData, prevoPokemon)
)
);
}
}
if (basePokemonData.evos && basePokemonData.evos[0] !== parsingPokemon) {
for (const evo of basePokemonData.evos) {
const evoPokemon = this.findBySpecies(Util.toLowerSingleWordCase(evo));
if (evoPokemon) {
evolutionChain.push(
this.findBySpeciesWithDetails(
{
pokemon: Util.toLowerSingleWordCase(evoPokemon.species),
skip,
take,
reverse
},
this.parseDataForEvolutionRecursion(basePokemonData, evoPokemon)
)
);
}
}
}
pokemonData.preevolutions = await Promise.all(preevolutionChain);
pokemonData.evolutions = await Promise.all(evolutionChain);
return pokemonData;
}
public getByFuzzy(@Args() { pokemon, skip, take }: PokemonPaginatedArgs, @Arg('fuseOptions', () => GraphQLJSONObject) fuseOptions?: SimpleFuseOptions) {
switch (pokemon.split(' ')[0]) {
case 'mega':
pokemon = `${pokemon.substring(pokemon.split(' ')[0].length + 1)}-mega`;
break;
case 'gigantamax':
case 'gmax':
pokemon = `${pokemon.substring(pokemon.split(' ')[0].length + 1)}-gmax`;
break;
case 'alola':
case 'alolan':
pokemon = `${pokemon.substring(pokemon.split(' ')[0].length + 1)}-alola`;
break;
case 'galar':
case 'galarian':
pokemon = `${pokemon.substring(pokemon.split(' ')[0].length + 1)}-galar`;
break;
default:
break;
}
if (pokemon.length < 5) {
const potentialAlias = pokedexAliases.get(pokemon);
if (potentialAlias) {
pokemon = potentialAlias.name;
}
}
const fuzzyPokemon = new FuzzySearch(pokedex, ['num', 'species'], { threshold: 0.3, ...fuseOptions });
let fuzzyResult = fuzzyPokemon.runFuzzy(pokemon);
if (!fuzzyResult.length) {
const fuzzyAliasResult = new FuzzySearch(pokedexAliases, ['alias', 'name'], { threshold: 0.4 }).runFuzzy(pokemon);
if (fuzzyAliasResult.length) {
fuzzyResult = fuzzyPokemon.runFuzzy(fuzzyAliasResult[0].name);
}
}
if (!fuzzyResult.length) {
throw new Error(`No Pokémon found ${pokemon}`);
}
return fuzzyResult.slice(skip, skip + take);
}
private parseDataForEvolutionRecursion(basePokemonData: Pokemon.DexEntry, evoChainData: Pokemon.DexEntry) {
if (basePokemonData.forme && evoChainData.forme && basePokemonData.forme === evoChainData.forme) {
return Util.toLowerSingleWordCase(basePokemonData.species);
}
return basePokemonData.baseSpecies?.toLowerCase() || basePokemonData.species;
}
private parseSpeciesForBulbapedia(pokemonName: string, baseForme?: string) {
if (baseForme) {
pokemonName = Util.toTitleSnakeCase(baseForme);
}
return `https://bulbapedia.bulbagarden.net/wiki/${pokemonName}_(Pokemon)`;
}
private parseSpeciesForSprite(pokemonName: string, baseSpecies?: string, specialSprite?: string, specialShinySprite?: string, shiny = false) {
if (specialShinySprite && shiny === true) return specialShinySprite;
if (specialSprite && shiny === false) return specialSprite;
if (!baseSpecies) pokemonName = Util.toLowerSingleWordCase(pokemonName);
if (pokemonName.match(/^(.+)-(x|y)$/g)) pokemonName = pokemonName.replace(/^(.+)-(x|y)$/g, '$1$2');
return `https://play.pokemonshowdown.com/sprites/${shiny ? 'ani-shiny' : 'ani'}/${pokemonName}.gif`;
}
private parseBaseStatsTotal(baseStats: Pokemon.Stats) {
return baseStats.hp + baseStats.atk + baseStats.def + baseStats.spa + baseStats.spd + baseStats.spe;
}
}