Skip to content

Commit 0b06d69

Browse files
committed
refactor: reduce cognitive complexity for sprite-parser
[skip publish]
1 parent f7130f2 commit 0b06d69

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

src/lib/utils/sprite-parser.ts

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface ParseSpeciesForSpriteParams {
2222
specialSprite?: string;
2323
}
2424

25-
const MegaSpriteRegex = /^(.+)-(x|y)$/g;
25+
const MegaSpriteRegex = /^(.+)-([xy])$/g;
2626
const SpriteUrls = {
2727
baseUrl: 'https://play.pokemonshowdown.com/sprites/',
2828
animatedShinyBackSprites: 'ani-back-shiny/',
@@ -36,6 +36,12 @@ const Gen9SpriteUrls = {
3636
animatedSprites: 'gen5/',
3737
animatedShinySprites: 'gen5-shiny/'
3838
};
39+
const afdSpriteUrls = {
40+
animatedShinyBackSprites: 'afd-back-shiny/',
41+
animatedBackSprites: 'afd-back/',
42+
animatedSprites: 'afd/',
43+
animatedShinySprites: 'afd-shiny/'
44+
};
3945

4046
/**
4147
* Parses the species information to determine the appropriate sprite URL based on the given parameters.
@@ -53,10 +59,12 @@ export function parseSpeciesForSprite({
5359
shiny = false,
5460
backSprite = false
5561
}: ParseSpeciesForSpriteParams): string {
56-
if (shiny && backSprite && specialShinyBackSprite) return specialShinyBackSprite;
57-
if (backSprite && specialBackSprite) return specialBackSprite;
58-
if (shiny && specialShinySprite) return specialShinySprite;
59-
if (specialSprite) return specialSprite;
62+
if (isAprilFools()) {
63+
return parseAsAfd(pokemonName, baseSpecies, shiny, backSprite);
64+
}
65+
66+
const parsedSpecialSprite = parseSpecialSprites(specialSprite, specialShinySprite, specialBackSprite, specialShinyBackSprite, shiny, backSprite);
67+
if (parsedSpecialSprite) return parsedSpecialSprite;
6068

6169
if (!baseSpecies) {
6270
pokemonName = toLowerSingleWordCase(pokemonName);
@@ -68,19 +76,65 @@ export function parseSpeciesForSprite({
6876

6977
// TODO: Remove when Showdown adds GIFs of Gen 9 Pokémon
7078
// Parse differently for generation 9 (Number 906 is sprigatito)
71-
if (pokemonNumber >= 906 || pokemonName === 'ursaluna-bloodmoon') {
72-
const pokemonPng = `${pokemonName}.png`;
79+
if (pokemonNumber >= 906 || pokemonName === 'ursaluna-bloodmoon') return parsePngSprite(pokemonName, shiny, backSprite);
7380

74-
if (shiny && backSprite) return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedShinyBackSprites + pokemonPng;
75-
if (backSprite) return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedBackSprites + pokemonPng;
76-
if (shiny) return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedShinySprites + pokemonPng;
77-
return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedSprites + pokemonPng;
78-
}
81+
return parseGifSprite(pokemonName, shiny, backSprite);
82+
}
83+
84+
function parseSpecialSprites(
85+
specialSprite: string | undefined,
86+
specialShinySprite: string | undefined,
87+
specialBackSprite: string | undefined,
88+
specialShinyBackSprite: string | undefined,
89+
shiny: boolean,
90+
backSprite: boolean
91+
) {
92+
if (shiny && backSprite && specialShinyBackSprite) return specialShinyBackSprite;
93+
if (backSprite && specialBackSprite) return specialBackSprite;
94+
if (shiny && specialShinySprite) return specialShinySprite;
95+
if (specialSprite) return specialSprite;
96+
97+
return null;
98+
}
7999

100+
function parsePngSprite(pokemonName: string, shiny: boolean, backSprite: boolean) {
101+
const pokemonPng = `${pokemonName}.png`;
102+
103+
if (shiny && backSprite) return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedShinyBackSprites + pokemonPng;
104+
if (backSprite) return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedBackSprites + pokemonPng;
105+
if (shiny) return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedShinySprites + pokemonPng;
106+
return SpriteUrls.baseUrl + Gen9SpriteUrls.animatedSprites + pokemonPng;
107+
}
108+
109+
function parseGifSprite(pokemonName: string, shiny: boolean, backSprite: boolean) {
80110
const pokemonGif = `${pokemonName}.gif`;
81111

82112
if (shiny && backSprite) return SpriteUrls.baseUrl + SpriteUrls.animatedShinyBackSprites + pokemonGif;
83113
if (backSprite) return SpriteUrls.baseUrl + SpriteUrls.animatedBackSprites + pokemonGif;
84114
if (shiny) return SpriteUrls.baseUrl + SpriteUrls.animatedShinySprites + pokemonGif;
85115
return SpriteUrls.baseUrl + SpriteUrls.animatedSprites + pokemonGif;
86116
}
117+
118+
const APRIL = 3 as const;
119+
const FIRST = 1 as const;
120+
function isAprilFools() {
121+
const currentDate = new Date();
122+
return currentDate.getMonth() === APRIL && currentDate.getDate() === FIRST;
123+
}
124+
125+
function parseAsAfd(pokemonName: string, baseSpecies: string | undefined, shiny: boolean, backSprite: boolean) {
126+
if (!baseSpecies) {
127+
pokemonName = toLowerSingleWordCase(pokemonName);
128+
}
129+
130+
if (pokemonName.match(MegaSpriteRegex)) {
131+
pokemonName = pokemonName.replace(MegaSpriteRegex, '$1$2');
132+
}
133+
134+
const pokemonGif = `${pokemonName}.png`;
135+
136+
if (shiny && backSprite) return SpriteUrls.baseUrl + afdSpriteUrls.animatedShinyBackSprites + pokemonGif;
137+
if (backSprite) return SpriteUrls.baseUrl + afdSpriteUrls.animatedBackSprites + pokemonGif;
138+
if (shiny) return SpriteUrls.baseUrl + afdSpriteUrls.animatedShinySprites + pokemonGif;
139+
return SpriteUrls.baseUrl + afdSpriteUrls.animatedSprites + pokemonGif;
140+
}

0 commit comments

Comments
 (0)