Compare commits
No commits in common. "ea2285ed1df6bcb73ada808e24d0644de0c4d9b0" and "01cf81f93080433d1221ef684e0a20773d7d941b" have entirely different histories.
ea2285ed1d
...
01cf81f930
3
bot.py
3
bot.py
@ -93,8 +93,7 @@ class BotClient(discord.Client):
|
||||
"Run Showdown damage calculations.\n"
|
||||
"Example format:\n"
|
||||
"` %calc -2 8 SpA Choice Specs Torkoal Overheat vs. 252 HP / 4+ SpD Assault Vest Abomasnow in Sun through Light Screen`\n"
|
||||
"Supported: attacker/defender boosts, EVs, tera, item, ability, species; weather/terrain; screens."
|
||||
"Not supported (popular): non-default multi hits; hazards."
|
||||
"Supported: attacker/defender boosts, EVs, item, species; attacker ability; weather/terrain; screens."
|
||||
)
|
||||
|
||||
async def _calculate(self, args: list[str]) -> str:
|
||||
|
67
calc.js
67
calc.js
@ -12,14 +12,6 @@ import { createToken, Lexer } from "chevrotain";
|
||||
|
||||
const gen = Generations.get(9);
|
||||
|
||||
/**
|
||||
* @param {string} text
|
||||
* @return {string}
|
||||
*/
|
||||
function escapeRegExp(text) {
|
||||
return text.replace(/[\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a lexer.
|
||||
*
|
||||
@ -35,36 +27,20 @@ function buildLexer() {
|
||||
|
||||
const Item = createToken({
|
||||
name: "Item",
|
||||
pattern: new RegExp(
|
||||
[...gen.items].map((i) => escapeRegExp(i.name)).join("|")
|
||||
),
|
||||
pattern: new RegExp([...gen.items].map((i) => i.name).join("|")),
|
||||
});
|
||||
const Ability = createToken({
|
||||
name: "Ability",
|
||||
pattern: new RegExp(
|
||||
[...gen.abilities].map((a) => escapeRegExp(a.name)).join("|")
|
||||
),
|
||||
pattern: new RegExp([...gen.abilities].map((a) => a.name).join("|")),
|
||||
});
|
||||
|
||||
const Pokemon = createToken({
|
||||
name: "Pokemon",
|
||||
pattern: new RegExp(
|
||||
[...gen.species]
|
||||
.flatMap((s) => [
|
||||
// Important: formes are expanded first because the parser takes the
|
||||
// first match, and the original species is always a subset of a
|
||||
// forme.
|
||||
...(s.otherFormes || []).map(escapeRegExp),
|
||||
escapeRegExp(s.name),
|
||||
])
|
||||
.join("|")
|
||||
),
|
||||
pattern: new RegExp([...gen.species].map((s) => s.name).join("|")),
|
||||
});
|
||||
const Move = createToken({
|
||||
name: "Move",
|
||||
pattern: new RegExp(
|
||||
[...gen.moves].map((m) => escapeRegExp(m.name)).join("|")
|
||||
),
|
||||
pattern: new RegExp([...gen.moves].map((m) => m.name).join("|")),
|
||||
});
|
||||
|
||||
const whitespace = createToken({
|
||||
@ -82,19 +58,6 @@ function buildLexer() {
|
||||
pattern: /vs\.?/,
|
||||
});
|
||||
|
||||
const teraEnter = createToken({
|
||||
name: "teraEnter",
|
||||
pattern: "Tera",
|
||||
push_mode: "tera_mode",
|
||||
});
|
||||
const Tera = createToken({
|
||||
name: "Tera",
|
||||
pattern: new RegExp(
|
||||
[...gen.types].map((t) => escapeRegExp(t.name)).join("|")
|
||||
),
|
||||
pop_mode: true,
|
||||
});
|
||||
|
||||
const terrainEnter = createToken({
|
||||
name: "terrainEnter",
|
||||
pattern: "in",
|
||||
@ -103,12 +66,10 @@ function buildLexer() {
|
||||
const Weather = createToken({
|
||||
name: "Weather",
|
||||
pattern: /Sun|Rain|Sand|Snow/,
|
||||
pop_mode: true,
|
||||
});
|
||||
const Terrain = createToken({
|
||||
name: "Terrain",
|
||||
pattern: /(Electric|Grassy|Misty|Psychic) Terrain/,
|
||||
pop_mode: true,
|
||||
});
|
||||
|
||||
const screenEnter = createToken({
|
||||
@ -130,12 +91,10 @@ function buildLexer() {
|
||||
Ability,
|
||||
Pokemon,
|
||||
Move,
|
||||
teraEnter,
|
||||
terrainEnter,
|
||||
screenEnter,
|
||||
],
|
||||
tera_mode: [whitespace, Tera],
|
||||
terrain_mode: [whitespace, Weather, Terrain],
|
||||
terrain_mode: [whitespace, screenEnter, Weather, Terrain],
|
||||
screen_mode: [whitespace, Move],
|
||||
},
|
||||
defaultMode: "default_mode",
|
||||
@ -252,6 +211,8 @@ function parseAndCalculate(line) {
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "Stat":
|
||||
throw Error("Impossible state: bare Stat");
|
||||
case "Item":
|
||||
opts().item = unwrapToken("Item", item);
|
||||
break;
|
||||
@ -268,9 +229,6 @@ function parseAndCalculate(line) {
|
||||
case "Move":
|
||||
move = unwrapToken("Move", item);
|
||||
break;
|
||||
case "teraEnter":
|
||||
opts().teraType = unwrapToken("Tera", it.next().value);
|
||||
break;
|
||||
case "terrainEnter":
|
||||
{
|
||||
let next = it.next().value;
|
||||
@ -326,7 +284,7 @@ function parseAndCalculate(line) {
|
||||
function test() {
|
||||
const text =
|
||||
"-2 8 SpA Choice Specs Torkoal Overheat vs. 252 HP / 4+ SpD Assault Vest Abomasnow in Sun through Light Screen";
|
||||
var res = parseAndCalculate(text);
|
||||
const res = parseAndCalculate(text);
|
||||
|
||||
assert(res.attacker.boosts.spa === -2, "should have -2 SpA");
|
||||
assert(res.attacker.evs.spa === 8, "should have 8 SpA EVs");
|
||||
@ -341,15 +299,6 @@ function test() {
|
||||
res.desc().replace(/:.*/, "") === text,
|
||||
"non-damage text should be equivalent to input"
|
||||
);
|
||||
|
||||
res = parseAndCalculate("Tera Electric Iron Hands Wild Charge vs Basculin");
|
||||
assert(res.attacker.teraType === "Electric", "should parse tera type");
|
||||
|
||||
res = parseAndCalculate("Gallade-Mega Triple Axel vs Gligar");
|
||||
assert(res.attacker.name === "Gallade-Mega", "should parse Mega forme");
|
||||
|
||||
res = parseAndCalculate("Zoroark-Hisui Night Slash vs Golem");
|
||||
assert(res.attacker.name === "Zoroark-Hisui", "should parse regional forme");
|
||||
}
|
||||
|
||||
export { parseAndCalculate, test };
|
||||
|
Loading…
Reference in New Issue
Block a user