Skip to content

Commit

Permalink
fix: add back sprite support to learnset queries
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed May 29, 2021
1 parent 9e70b74 commit c02f2fc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 13 deletions.
102 changes: 91 additions & 11 deletions src/services/LearnsetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ import type Pokemon from '#utils/pokemon';
import Util from '#utils/util';
import { Args } from 'type-graphql';

interface ParseSpeciesForSpriteParams {
pokemonName: string;
baseSpecies?: string;
specialSprite?: string;
specialShinySprite?: string;
specialBackSprite?: string;
specialShinyBackSprite?: string;
shiny?: boolean;
backSprite?: boolean;
}

const SpriteRegexReplaceMatch = /^(.+)-(x|y)$/g;
const SpriteUrls = {
baseUrl: 'https://play.pokemonshowdown.com/sprites/',
animatedShinyBackSprites: 'ani-back-shiny/',
animatedBackSprites: 'ani-back/',
animatedSprites: 'ani/',
animatedShinySprites: 'ani-shiny/'
};

export default class LearnsetService {
public findLearnsets(@Args() { pokemon, moves, generation }: LearnsetArgs, requestedFields: GraphQLSet<keyof LearnsetEntry>): LearnsetEntry {
const learnset = learnsets.get(pokemon);
Expand Down Expand Up @@ -92,13 +112,57 @@ export default class LearnsetService {
addPropertyToClass(
learnsetEntry,
'sprite',
this.parseSpeciesForSprite(pokemonEntry.species, pokemonEntry.baseSpecies, pokemonEntry.specialSprite, pokemonEntry.specialShinySprite),
this.parseSpeciesForSprite({
pokemonName: pokemonEntry.species,
baseSpecies: pokemonEntry.baseSpecies,
specialSprite: pokemonEntry.specialSprite,
specialShinySprite: pokemonEntry.specialShinySprite,
specialBackSprite: pokemonEntry.specialBackSprite,
specialShinyBackSprite: pokemonEntry.specialShinyBackSprite
}),
requestedFields
);
addPropertyToClass(
learnsetEntry,
'shinySprite',
this.parseSpeciesForSprite(pokemonEntry.species, pokemonEntry.baseSpecies, pokemonEntry.specialSprite, pokemonEntry.specialShinySprite, true),
this.parseSpeciesForSprite({
pokemonName: pokemonEntry.species,
baseSpecies: pokemonEntry.baseSpecies,
specialSprite: pokemonEntry.specialSprite,
specialShinySprite: pokemonEntry.specialShinySprite,
specialBackSprite: pokemonEntry.specialBackSprite,
specialShinyBackSprite: pokemonEntry.specialShinyBackSprite,
shiny: true
}),
requestedFields
);
addPropertyToClass(
learnsetEntry,
'backSprite',
this.parseSpeciesForSprite({
pokemonName: pokemonEntry.species,
baseSpecies: pokemonEntry.baseSpecies,
specialSprite: pokemonEntry.specialSprite,
specialShinySprite: pokemonEntry.specialShinySprite,
specialBackSprite: pokemonEntry.specialBackSprite,
specialShinyBackSprite: pokemonEntry.specialShinyBackSprite,
backSprite: true
}),
requestedFields
);
addPropertyToClass(
learnsetEntry,
'shinyBackSprite',
this.parseSpeciesForSprite({
pokemonName: pokemonEntry.species,
baseSpecies: pokemonEntry.baseSpecies,
specialSprite: pokemonEntry.specialSprite,
specialShinySprite: pokemonEntry.specialShinySprite,
specialBackSprite: pokemonEntry.specialBackSprite,
specialShinyBackSprite: pokemonEntry.specialShinyBackSprite,
shiny: true,
backSprite: true
}),
requestedFields
);

Expand Down Expand Up @@ -139,15 +203,31 @@ export default class LearnsetService {
return method.slice(1, 2) as MethodTypes;
}

private parseSpeciesForSprite(pokemonName: string, baseForme?: string, specialSprite?: string, specialShinySprite?: string, shiny = false) {
if (specialShinySprite && shiny) return specialShinySprite;
if (specialSprite && !shiny) return specialSprite;

if (!baseForme) 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 parseSpeciesForSprite({
pokemonName,
baseSpecies,
specialSprite,
specialShinySprite,
specialBackSprite,
specialShinyBackSprite,
shiny = false,
backSprite = false
}: ParseSpeciesForSpriteParams) {
if (shiny && backSprite && specialShinyBackSprite) return specialShinyBackSprite;
if (backSprite && specialBackSprite) return specialBackSprite;
if (shiny && specialShinySprite) return specialShinySprite;
if (specialSprite) return specialSprite;

if (!baseSpecies) pokemonName = Util.toLowerSingleWordCase(pokemonName);

if (pokemonName.match(SpriteRegexReplaceMatch)) pokemonName = pokemonName.replace(SpriteRegexReplaceMatch, '$1$2');

const pokemonGif = `${pokemonName}.gif`;

if (shiny && backSprite) return SpriteUrls.baseUrl + SpriteUrls.animatedShinyBackSprites + pokemonGif;
if (backSprite) return SpriteUrls.baseUrl + SpriteUrls.animatedBackSprites + pokemonGif;
if (shiny) return SpriteUrls.baseUrl + SpriteUrls.animatedShinySprites + pokemonGif;
return SpriteUrls.baseUrl + SpriteUrls.animatedSprites + pokemonGif;
}

private shouldIncludePokemonDetails(requestedFields: GraphQLSet<keyof LearnsetEntry>) {
Expand Down
19 changes: 17 additions & 2 deletions src/structures/LearnsetEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,27 @@ export default class LearnsetEntry {
@Field(() => String, { description: 'The species name for a Pokémon' })
public species!: string;

@Field(() => String, { description: 'The sprite for the Pokémon' })
@Field({
description: 'The sprite for a Pokémon. For most Pokémon this will be the animated gif, with some exceptions that were older-gen exclusive'
})
public sprite!: string;

@Field(() => String, { description: 'The shiny sprite for the Pokémon' })
@Field({
description: 'The shiny sprite for a Pokémon. For most Pokémon this will be the animated gif, with some exceptions that were older-gen exclusive'
})
public shinySprite!: string;

@Field({
description: 'The back sprite for a Pokémon. For most Pokémon this will be the animated gif, with some exceptions that were older-gen exclusive'
})
public backSprite!: string;

@Field({
description:
'The shiny back sprite for a Pokémon. For most Pokémon this will be the animated gif, with some exceptions that were older-gen exclusive'
})
public shinyBackSprite!: string;

@Field(() => String, { description: 'The PokéDex colour for the Pokémon' })
public color!: string;
}

0 comments on commit c02f2fc

Please sign in to comment.