From 235d4765851a09d37ab50a04b06fc4842a0bf8f6 Mon Sep 17 00:00:00 2001 From: Quildra Date: Tue, 19 Nov 2024 21:54:38 +0000 Subject: [PATCH] - Add in the graph loading --- src/server.ts | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/server.ts b/src/server.ts index 612baee..8eb3ee1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -31,6 +31,66 @@ const userDbPromise = open({ driver: sqlite3.Database }); +interface Node { + id: string; + [key: string]: any; +} + +interface Link { + source: string; + target: string; + method: string; + [key: string]: any; +} + +interface Graph { + nodes: Node[]; + links: Link[]; +} + +const graph_file_path = "./pokemon_evolution_graph.json" +const loadGraph = async (): Promise => { + const jsonData = await fs.readFile(graph_file_path, 'utf-8'); + return JSON.parse(jsonData); +}; + +async function findNodeEdges(nodeId: string): Promise<{ node: Node; edges: Link[] } | null> { + const graph = await loadGraph(); + const node = graph.nodes.find((n) => n.id === nodeId); + if (!node) { + console.log(`Node with ID ${nodeId} not found.`); + return null; + } + + console.log(`Node found: ${node.id}`); + + const edges = graph.links.filter((link) => link.source === nodeId || link.target === nodeId); + edges.forEach((edge) => { + const relatedNode = edge.source === nodeId ? edge.target : edge.source; + console.log(`Edge found between ${nodeId} and ${relatedNode} with method: ${edge.method}`); + }); + + return { node, edges }; +} + +async function findNodeBackwardEdges(nodeId: string): Promise<{ node: Node; edges: Link[] } | null> { + const graph = await loadGraph(); + const node = graph.nodes.find((n) => n.id === nodeId); + if (!node) { + //console.log(`Node with ID ${nodeId} not found.`); + return null; + } + + //console.log(`Node found: ${node.id}`); + + const edges = graph.links.filter((link) => link.target === nodeId); + edges.forEach((edge) => { + //console.log(`Backward edge found from ${edge.source} to ${nodeId} with method: ${edge.method}`); + }); + + return { node, edges }; +} + // Initialize users table async function initializeDb() { const db = await userDbPromise; @@ -186,6 +246,12 @@ app.get('/api/pokemon/:pfic/details', async (req, res) => { "pfic": details.PFIC, "data": data } + + let cluster = await findNodeBackwardEdges(details.PFIC) + if (cluster && cluster?.edges.length > 0) { + pokemon.data.evolution_method = cluster?.edges[0].method + } + res.json(pokemon); } catch (err) { console.error('Error fetching pokemon details:', err); @@ -193,6 +259,46 @@ app.get('/api/pokemon/:pfic/details', async (req, res) => { } }); +app.post('/api/pokemon/batch/details', async (req, res) => { + try { + const db = await dbPromise; + const { pfics } = req.body; + + if (!Array.isArray(pfics) || pfics.length === 0) { + res.json({ error: 'Invalid input, expected an array of PFICs.' }); + return + } + + const placeholders = pfics.map(() => '?').join(','); + const detailsList = await db.all( + `SELECT * + FROM pokemon_forms + WHERE PFIC IN (${placeholders})`, + pfics + ); + + const pokemonList = await Promise.all(detailsList.map(async (details) => { + const data = JSON.parse(details.data); + const pokemon = { + "pfic": details.PFIC, + "data": data + }; + + let cluster = await findNodeBackwardEdges(details.PFIC); + if (cluster && cluster.edges.length > 0) { + pokemon.data.evolution_method = cluster.edges[0].method; + } + + return pokemon; + })); + + res.json(pokemonList); + } catch (err) { + console.error('Error fetching pokemon details:', err); + res.status(500).json({ error: 'Internal server error' }); + } +}); + app.get('/api/plan', authenticateToken, async (req: AuthRequest, res: Response) => { try { // Read the efficiency plan file