mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(#22): Add sprites
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import sprites from '$lib/helpers/sprites.json';
|
||||
|
||||
interface PokemonSpritesData {
|
||||
[key: string]: {
|
||||
forms: {
|
||||
[key: string]: {
|
||||
is_prev_gen_icon?: boolean;
|
||||
is_alias_of?: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export async function GET({ url }) {
|
||||
const pokedexNumber = url.searchParams.get('pokedexNumber');
|
||||
const form = url.searchParams.get('form') ?? undefined;
|
||||
|
||||
if (!pokedexNumber) {
|
||||
return json({
|
||||
status: 400,
|
||||
error: 'Pokédex number is required'
|
||||
});
|
||||
}
|
||||
|
||||
const spriteFileName = getPokemonSpriteFileName(pokedexNumber, form);
|
||||
|
||||
if (spriteFileName) {
|
||||
// If spriteUrl is found, return it with status 200
|
||||
return json({
|
||||
status: 200,
|
||||
spriteFileName: spriteFileName
|
||||
});
|
||||
} else {
|
||||
// If spriteUrl is not found, return 404
|
||||
return json({
|
||||
status: 404,
|
||||
error: `Sprite URL not found for the specified Pokémon with Pokédex number ${pokedexNumber} and form ${form}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getPokemonSpriteFileName(number: string, form?: string): string | undefined {
|
||||
const spritesData: PokemonSpritesData = sprites;
|
||||
|
||||
// Check if the Pokémon number exists in the sprites JSON
|
||||
if (spritesData[number] && spritesData[number]['gen-8']) {
|
||||
const forms = spritesData[number]['gen-8'].forms;
|
||||
let formName = '$';
|
||||
|
||||
// If a form is provided and it exists for the Pokémon, set the form name
|
||||
if (form && forms[form]) {
|
||||
formName = form;
|
||||
}
|
||||
|
||||
// Check if the form has an alias
|
||||
if (forms[formName].is_alias_of) {
|
||||
formName = forms[formName].is_alias_of;
|
||||
}
|
||||
|
||||
const fileName = `${spritesData[number].slug.eng.toLowerCase()}${formName !== '$' ? '-' + formName : ''}.png`;
|
||||
return fileName;
|
||||
}
|
||||
|
||||
// If the Pokémon number doesn't exist or doesn't have gen-8 sprites, return undefined
|
||||
return undefined;
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
import Pagination from '$lib/components/Pagination.svelte';
|
||||
import { browser } from '$app/environment';
|
||||
import type { PokedexEntry } from '$lib/models/PokedexEntry';
|
||||
import PokemonSprite from '$lib/components/PokemonSprite.svelte';
|
||||
|
||||
let combinedData = null as CombinedData[] | null;
|
||||
let failedToLoad = false;
|
||||
@@ -55,6 +56,8 @@
|
||||
<title>Living Dex Tracker - My Boxes</title>
|
||||
</svelte:head>
|
||||
|
||||
<!-- <PokemonSprite pokemonName={'Raichu'} pokedexNumber={26} form={'Alolan'} /> -->
|
||||
|
||||
<div class="container mx-auto">
|
||||
{#if !localUser}
|
||||
<p>Please <a href="/signin" class="underline text-primary hover:text-secondary">sign in</a></p>
|
||||
@@ -65,7 +68,7 @@
|
||||
|
||||
<div class="flex flex-wrap -mx-2">
|
||||
{#each boxNumbers as boxNumber}
|
||||
<div class="mb-8 w-1/2 px-2">
|
||||
<div class="mb-8 md:w-1/2 px-2">
|
||||
<h2 class="text-xl font-bold mb-4">Box {boxNumber}</h2>
|
||||
<div class="grid grid-cols-6">
|
||||
{#each combinedData as { pokedexEntry, catchRecord }}
|
||||
@@ -75,11 +78,17 @@
|
||||
style="grid-column-start: {pokedexEntry[currentPlacement]
|
||||
.column}; grid-row-start: {pokedexEntry[currentPlacement].row}"
|
||||
>
|
||||
<PokemonSprite
|
||||
pokemonName={pokedexEntry.pokemon}
|
||||
pokedexNumber={pokedexEntry.pokedexNumber}
|
||||
form={pokedexEntry.form}
|
||||
/>
|
||||
<!-- {pokedexEntry.form ? `(${pokedexEntry.form})` : ''} -->
|
||||
<!-- <div class="font-bold">
|
||||
{pokedexEntry.pokemon}
|
||||
{pokedexEntry.form ? `(${pokedexEntry.form})` : ''}
|
||||
</div> -->
|
||||
<div>{pokedexEntry.pokedexNumber.toString().padStart(3, '0')}</div>
|
||||
<!-- <div>{pokedexEntry.pokedexNumber.toString().padStart(3, '0')}</div> -->
|
||||
<!-- <div>
|
||||
Caught: {catchRecord.caught ? 'Yes' : 'No'} <br />
|
||||
Needs to Evolve: {catchRecord.haveToEvolve ? 'Yes' : 'No'} <br />
|
||||
|
||||
Reference in New Issue
Block a user