-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcheckRarity.js
57 lines (49 loc) · 2.12 KB
/
checkRarity.js
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
// Importing the pokemon and their rarities
const pokemon = require("../data/main/pokemon.json");
const legendaries = require("../data/main/legendary.json");
const mythicals = require("../data/main/mythical.json");
const ultraBeasts = require("../data/main/ultra-beast.json");
const regionals = require("../data/main/regional.json");
const events = require("../data/main/event.json");
const gigantamaxes = require("../data/main/gigantamax.json");
// Convert arrays to Sets for faster lookups and reduced memory usage
const pokemonSet = new Set(pokemon.map(p => p.toLowerCase()));
const legendariesSet = new Set(legendaries.map(p => p.toLowerCase()));
const mythicalsSet = new Set(mythicals.map(p => p.toLowerCase()));
const ultraBeastsSet = new Set(ultraBeasts.map(p => p.toLowerCase()));
const regionalsSet = new Set(regionals.map(p => p.toLowerCase()));
const eventsSet = new Set(events.map(p => p.toLowerCase()));
const gigantamaxSet = new Set(gigantamaxes.map(p => p.toLowerCase()));
/**
* Checks the rarity of a given Pokémon.
* @param {string} pokemonName - The name of the Pokémon to check.
* @returns {Promise<string>} The rarity of the Pokémon.
* @throws {Error} If the Pokémon name is not recognized.
*/
async function checkRarity(pokemonName) {
const normalizedName = pokemonName.toLowerCase();
// Check if the Pokémon exists in our data
if (!pokemonSet.has(normalizedName)) {
throw new Error(
`[PokeHint] Unable to identify the rarity of that Pokémon (${pokemonName}).`
);
}
// Array of rarity checks, ordered from most rare to least rare
const rarityChecks = [
{ set: legendariesSet, rarity: "Legendary" },
{ set: mythicalsSet, rarity: "Mythical" },
{ set: ultraBeastsSet, rarity: "Ultra Beast" },
{ set: regionalsSet, rarity: "Regional" },
{ set: eventsSet, rarity: "Event" },
{ set: gigantamaxSet, rarity: "Gigantamax" }
];
// Check each rarity set
for (const { set, rarity } of rarityChecks) {
if (set.has(normalizedName)) {
return rarity;
}
}
// If not found in any special category, it's a regular Pokémon
return "Regular";
}
module.exports = checkRarity;