Browse Source

- Added in some data augmentation

pull/1/head
Dan 1 year ago
parent
commit
5dff7bfb04
  1. 199
      src/server.ts

199
src/server.ts

@ -4,8 +4,8 @@ dotenv.config();
import express from 'express'; import express from 'express';
import { Request, Response, NextFunction } from 'express'; import { Request, Response, NextFunction } from 'express';
import cors from 'cors'; import cors from 'cors';
import { open, Database } from 'sqlite';
import sqlite3 from 'sqlite3'; import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
import fs from 'fs/promises'; import fs from 'fs/promises';
@ -313,123 +313,156 @@ interface PokemonFamilyEntry {
Any?: number; Any?: number;
Male?: number; Male?: number;
Female?: number; Female?: number;
evolve_to_augmented?: PokemonEntry[]
breed_for_augmented?: PokemonEntry[]
} }
interface PokemonEntry {
pfic: string
name: string
}
const getDbConnection = async (): Promise<Database<sqlite3.Database, sqlite3.Statement>> => {
return open({
filename: './pokemon_forms.db', // Adjust path to your database file
driver: sqlite3.Database
});
};
app.get('/api/plan', authenticateToken, async (req: AuthRequest, res: Response) => { app.get('/api/plan', authenticateToken, async (req: AuthRequest, res: Response) => {
let db: Database<sqlite3.Database, sqlite3.Statement> | null = null;
try { try {
// Get the Pokemon database connection
db = await getDbConnection();
// Read the efficiency plan file // Read the efficiency plan file
const planData = await fs.readFile( const planData = await fs.readFile(
path.join(__dirname, '../plan.json'), path.join(__dirname, '../plan.json'),
'utf-8' 'utf-8'
); );
const efficiencyPlan: GamePlan[] = JSON.parse(planData); const efficiencyPlan: GamePlan[] = JSON.parse(planData);
efficiencyPlan.forEach((game_plan) => {
for (const key in game_plan["pokemon"]) {
game_plan["pokemon"][key]["family_pfic"] = key;
}
});
// Get the Pokemon database connection // Loop through each game plan
const db = await open({ for (let i = 0; i < efficiencyPlan.length; i++) {
filename: '../pokemon_forms.db', const game_plan = efficiencyPlan[i];
driver: sqlite3.Database for (const key in game_plan.pokemon) {
}); if (Object.hasOwnProperty.call(game_plan.pokemon, key)) {
const pokemonFamily: PokemonFamilyEntry = game_plan.pokemon[key];
pokemonFamily.family_pfic = key;
// Get user's caught Pokemon // Merge evolve_to into pfics array
const userDb = await userDbPromise; const pfics: string[] = pokemonFamily.evolve_to.concat(pokemonFamily.evolve_to);
const caughtPokemon = await userDb.all(
'SELECT pfic FROM caught_pokemon WHERE user_id = ?',
[req.user.id]
);
const caughtPfics = new Set(caughtPokemon.map(p => p.pfic));
// Helper function to get evolution methods // Loop through pfics to get details from the database
async function getEvolutionMethods(fromPfic: string, toPfic: string) { for (let j = 0; j < pfics.length; j++) {
// Try direct evolution first const pkmn: string = pfics[j];
const direct = await db.get(`
SELECT method, to_pfic
FROM evolution_chains
WHERE from_pfic = ? AND to_pfic = ?
`, [fromPfic, toPfic]);
if (direct) { try {
return [direct.method]; const details = await db.get<{ PFIC: string; data: string }>(
`SELECT * FROM pokemon_forms WHERE PFIC = ?`,
pkmn
);
if (!details) {
console.log("Details not found for PFIC:", pkmn);
continue;
} }
// Try indirect evolution path const data = JSON.parse(details.data);
const methods = await db.all(` const pokemon = {
WITH RECURSIVE evolution_path AS ( pfic: details.PFIC,
SELECT from_pfic, to_pfic, method, 1 as depth data: data,
FROM evolution_chains };
WHERE from_pfic = ?
UNION ALL if (!pokemonFamily.evolve_to_augmented) {
pokemonFamily.evolve_to_augmented = [];
}
SELECT e.from_pfic, e.to_pfic, e.method, ep.depth + 1 const entry = {
FROM evolution_chains e pfic: key,
JOIN evolution_path ep ON e.from_pfic = ep.to_pfic name: pokemon.data.name,
WHERE ep.depth < 3 };
)
SELECT method
FROM evolution_path
WHERE to_pfic = ?
ORDER BY depth;
`, [fromPfic, toPfic]);
if (methods && methods.length > 0) { pokemonFamily.evolve_to_augmented.push(entry);
return methods.map(m => m.method);
}
return ['Evolution']; } catch (err) {
console.error(`Error fetching details for PFIC ${pkmn}:`, err);
} }
const debug_pfic = "0010-01-000-0";
// Enhance the plan with evolution methods and account for caught Pokemon
/*
for (const game of efficiencyPlan) {
for (const pokemon of game.pokemon.keys()) {
// Set initial catch count
pokemon.catch_count = 1;
if (pokemon.pfic === debug_pfic) {
console.log(`pokemon: ${pokemon.name} - ${pokemon.catch_count}`);
} }
// Add evolution targets to catch count
if (pokemon.evolve_to) {
pokemon.catch_count += pokemon.evolve_to.length;
if (pokemon.pfic === debug_pfic) {
console.log(`pokemon: ${pokemon.name} - ${pokemon.catch_count}`);
} }
}
}
res.json(efficiencyPlan);
// Add evolution methods } catch (err) {
for (const evolution of pokemon.evolve_to) { console.error('Error loading efficiency plan:', err);
const methods = await getEvolutionMethods(pokemon.pfic, evolution.pfic); res.status(500).json({ error: 'Internal server error' });
evolution.method = methods.join(' → '); } finally {
if (db) {
await db.close();
} }
} }
});
/*
app.get('/api/plan', authenticateToken, async (req: AuthRequest, res: Response) => {
try {
// Get the Pokemon database connection
const db = await dbPromise;
// Read the efficiency plan file
const planData = await fs.readFile(
path.join(__dirname, '../plan.json'),
'utf-8'
);
const efficiencyPlan: GamePlan[] = JSON.parse(planData);
for (let game_plan of efficiencyPlan){
for (const key in game_plan["pokemon"]) {
//console.log(key)
game_plan["pokemon"][key]["family_pfic"] = key;
let pfics = []
//console.log(game_plan["pokemon"][key])
// Reduce catch count for already caught Pokemon for (let pkmn of game_plan["pokemon"][key]["evolve_to"]) {
if (caughtPfics.has(pokemon.pfic)) { pfics.push(pkmn)
pokemon.catch_count = Math.max(0, pokemon.catch_count - 1);
if (pokemon.pfic === debug_pfic) {
console.log(`B pokemon: ${pokemon.name} - ${pokemon.catch_count}`);
} }
for (let pkmn of game_plan["pokemon"][key]["evolve_to"]) {
pfics.push(pkmn)
} }
for (let pkmn of pfics) {
console.log(pkmn)
const details = await db.get(`
SELECT *
FROM pokemon_forms
WHERE PFIC = ?
`, pkmn);
// Check evolution targets if(!details) {
if (pokemon.evolve_to) { console.log("oh noes")
for (const evolution of pokemon.evolve_to) { continue;
if (caughtPfics.has(evolution.pfic)) {
pokemon.catch_count = Math.max(0, pokemon.catch_count - 1);
if (pokemon.pfic === debug_pfic) {
console.log(`C pokemon: ${pokemon.name} - ${pokemon.catch_count} (${evolution.pfic})`);
} }
const data = JSON.parse(details.data)
const pokemon = {
"pfic": details.PFIC,
"data": data
}
if (!game_plan["pokemon"][key].evolve_to_augmented) {
game_plan["pokemon"][key].evolve_to_augmented = []
} }
let entry = {
pfic: key,
name: pokemon.data.name
} }
game_plan["pokemon"][key].evolve_to_augmented.push(entry)
} }
} }
} }
*/
await db.close(); await db.close();
res.json(efficiencyPlan); res.json(efficiencyPlan);
@ -439,7 +472,7 @@ app.get('/api/plan', authenticateToken, async (req: AuthRequest, res: Response)
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
} }
}); });
*/
// Update the caught Pokemon routes // Update the caught Pokemon routes
app.get('/api/pokemon/caught', authenticateToken, (req: AuthRequest, res: Response) => { app.get('/api/pokemon/caught', authenticateToken, (req: AuthRequest, res: Response) => {
void userDbPromise.then(async (db) => { void userDbPromise.then(async (db) => {

Loading…
Cancel
Save