-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
66 lines (52 loc) · 2.01 KB
/
server.ts
File metadata and controls
66 lines (52 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import express, { type Request, type Response } from "express";
import { Dex, type GenerationNum } from "@pkmn/dex";
import { Generations } from "@pkmn/data";
import * as dotenv from "dotenv";
dotenv.config();
const app = express();
const gens = new Generations(Dex);
const NUM_GENERATIONS: GenerationNum = 9;
const getLearnset = async (pokemonName: string, formeLevel: number = 0) => {
let learnset = new Set<string>();
for (let i: GenerationNum = 1; i <= NUM_GENERATIONS; i++) {
const gen = gens.get(i);
let pokemon = gen.species.get(pokemonName);
if (!pokemon) continue;
if (pokemon.changesFrom) {
const learnsetChangesFrom = await getLearnset(
pokemon.changesFrom,
formeLevel + 1
);
if (learnsetChangesFrom) {
learnsetChangesFrom.forEach((move) => learnset.add(move));
}
}
const learnsetMon = await gen.learnsets.get(pokemonName);
if (!(learnsetMon && learnsetMon.learnset)) continue;
Object.keys(learnsetMon.learnset).forEach((move) => learnset.add(move));
if (pokemon.prevo) {
const learnsetPrevo = await getLearnset(pokemon.prevo);
if (!learnsetPrevo) continue;
learnsetPrevo.forEach((move) => learnset.add(move));
}
}
return learnset;
};
// Home. Doesn't do anything.
app.get("/", (req: Request, res: Response) => {
res.send(
"This is the home of learnsets. To get the learnset of a mon, put the name of a pokemon (lowercase) after the forward slash in the url."
);
});
// URL slug to get the name of the Pokemon
app.get("/:pokemon", async (req, res) => {
const pokemon = req.params.pokemon;
if (pokemon !== "favicon.ico") {
const learnsetMon = await getLearnset(pokemon);
console.log("Got learnset for: " + pokemon);
res.json(Array.from(learnsetMon));
}
});
app.listen(process.env.PORT, () => {
console.log("Listening on " + process.env.PORT);
});