|
|
|
@ -138,85 +138,31 @@ app.post('/api/auth/login', (req: Request, res: Response) => { |
|
|
|
|
|
|
|
// Database connection
|
|
|
|
const dbPromise = open({ |
|
|
|
filename: '../pokemon_forms.db', // Adjust path to your database file
|
|
|
|
filename: './pokemon_forms.db', // Adjust path to your database file
|
|
|
|
driver: sqlite3.Database |
|
|
|
}); |
|
|
|
|
|
|
|
function processPokemonData(pokemonData: any[]): any[][] { |
|
|
|
const pokemonList: any[][] = []; |
|
|
|
let current_group: any[] = []; |
|
|
|
let current_dex_number = 0; |
|
|
|
let current_generation = 0; |
|
|
|
let pokemon_forms: any[] = []; |
|
|
|
|
|
|
|
for (const pokemon of pokemonData) { |
|
|
|
if (pokemon.national_dex !== current_dex_number) { |
|
|
|
if (pokemon_forms.length > 0) { |
|
|
|
for (const form of pokemon_forms) { |
|
|
|
current_group.push(form); |
|
|
|
if (current_group.length === 30) { |
|
|
|
pokemonList.push([...current_group]); |
|
|
|
current_group = []; |
|
|
|
} |
|
|
|
} |
|
|
|
pokemon_forms = []; |
|
|
|
} |
|
|
|
current_dex_number = pokemon.national_dex; |
|
|
|
|
|
|
|
if (!pokemon.form_name) { |
|
|
|
if (current_generation === null || pokemon.generation !== current_generation) { |
|
|
|
if (current_group.length > 0) { |
|
|
|
while (current_group.length < 30) { |
|
|
|
current_group.push(null); |
|
|
|
} |
|
|
|
pokemonList.push([...current_group]); |
|
|
|
current_group = []; |
|
|
|
} |
|
|
|
current_generation = pokemon.generation || 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pokemon_forms.push(pokemon); |
|
|
|
} |
|
|
|
|
|
|
|
// Handle remaining pokemon forms
|
|
|
|
for (const form of pokemon_forms) { |
|
|
|
current_group.push(form); |
|
|
|
if (current_group.length === 30) { |
|
|
|
pokemonList.push([...current_group]); |
|
|
|
current_group = []; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Handle the last group
|
|
|
|
if (current_group.length > 0) { |
|
|
|
while (current_group.length < 30) { |
|
|
|
current_group.push(null); |
|
|
|
} |
|
|
|
pokemonList.push([...current_group]); |
|
|
|
} |
|
|
|
|
|
|
|
return pokemonList; |
|
|
|
} |
|
|
|
|
|
|
|
// Routes
|
|
|
|
app.get('/api/pokemon', async (req, res) => { |
|
|
|
try { |
|
|
|
const db = await dbPromise; |
|
|
|
const pokemon = await db.all(` |
|
|
|
SELECT |
|
|
|
pf.national_dex, pf.name, pf.form_name, pf.PFIC, pf.generation, |
|
|
|
ps.storable_in_home, m.icon_path, m.name as mark_name |
|
|
|
FROM pokemon_forms pf |
|
|
|
JOIN pokemon_storage ps ON pf.PFIC = ps.PFIC |
|
|
|
LEFT JOIN form_marks fm ON pf.PFIC = fm.pfic |
|
|
|
LEFT JOIN marks m ON fm.mark_id = m.id |
|
|
|
WHERE ps.storable_in_home = 1 |
|
|
|
ORDER BY pf.PFIC |
|
|
|
const pokemon_data = await db.all(` |
|
|
|
SELECT * |
|
|
|
FROM pokemon_forms |
|
|
|
WHERE JSON_EXTRACT(data, '$.storable_in_home') == true |
|
|
|
GROUP BY json_extract(data, '$.sprite_url') |
|
|
|
ORDER BY PFIC |
|
|
|
`);
|
|
|
|
|
|
|
|
const processedData = processPokemonData(pokemon); |
|
|
|
|
|
|
|
var processedData: any[] = [] |
|
|
|
for (const db_pokemon of pokemon_data) { |
|
|
|
const data = JSON.parse(db_pokemon.data) |
|
|
|
const pokemon = { |
|
|
|
"pfic": db_pokemon.PFIC, |
|
|
|
"data": data |
|
|
|
} |
|
|
|
processedData.push(pokemon) |
|
|
|
} |
|
|
|
res.json(processedData); |
|
|
|
} catch (err) { |
|
|
|
console.error('Error fetching pokemon:', err); |
|
|
|
|