Browse Source

- Fix up initial get for new db strucutre.

- Remove the sorting into boxes from the server
pull/1/head
Dan 1 year ago
parent
commit
e08e3e5e1f
  1. 21
      .vscode/launch.json
  2. 1
      package-lock.json
  3. 88
      src/server.ts
  4. 2
      tsconfig.json

21
.vscode/launch.json

@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug with ts-node-dev",
"runtimeExecutable": "npx",
"runtimeArgs": ["ts-node-dev", "--inspect", "--respawn", "src/server.ts"],
"skipFiles": ["<node_internals>/**"],
"env": {
"NODE_ENV": "development"
},
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

1
package-lock.json

@ -3048,7 +3048,6 @@
"resolved": "https://registry.npmjs.org/ts-node-dev/-/ts-node-dev-2.0.0.tgz",
"integrity": "sha512-ywMrhCfH6M75yftYvrvNarLEY+SUXtUvU8/0Z6llrHQVBx12GiFk5sStF8UdfE/yfzk9IAq7O5EEbTQsxlBI8w==",
"dev": true,
"license": "MIT",
"dependencies": {
"chokidar": "^3.5.1",
"dynamic-dedupe": "^0.3.0",

88
src/server.ts

@ -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);

2
tsconfig.json

@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"module": "CommonJS",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,

Loading…
Cancel
Save