support tera

This commit is contained in:
xeals 2023-04-27 11:58:24 +10:00
parent 6aa1cac328
commit 2acbf9945c
Signed by: xeals
GPG Key ID: A498C7AF27EC6B5C
2 changed files with 23 additions and 4 deletions

2
bot.py
View File

@ -93,7 +93,7 @@ class BotClient(discord.Client):
"Run Showdown damage calculations.\n" "Run Showdown damage calculations.\n"
"Example format:\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" "` %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, item, species; attacker ability; weather/terrain; screens." "Supported: attacker/defender boosts, EVs, tera, item, species; attacker ability; weather/terrain; screens."
) )
async def _calculate(self, args: list[str]) -> str: async def _calculate(self, args: list[str]) -> str:

25
calc.js
View File

@ -74,6 +74,19 @@ function buildLexer() {
pattern: /vs\.?/, 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({ const terrainEnter = createToken({
name: "terrainEnter", name: "terrainEnter",
pattern: "in", pattern: "in",
@ -109,9 +122,11 @@ function buildLexer() {
Ability, Ability,
Pokemon, Pokemon,
Move, Move,
teraEnter,
terrainEnter, terrainEnter,
screenEnter, screenEnter,
], ],
tera_mode: [whitespace, Tera],
terrain_mode: [whitespace, Weather, Terrain], terrain_mode: [whitespace, Weather, Terrain],
screen_mode: [whitespace, Move], screen_mode: [whitespace, Move],
}, },
@ -229,8 +244,6 @@ function parseAndCalculate(line) {
} }
} }
break; break;
case "Stat":
throw Error("Impossible state: bare Stat");
case "Item": case "Item":
opts().item = unwrapToken("Item", item); opts().item = unwrapToken("Item", item);
break; break;
@ -247,6 +260,9 @@ function parseAndCalculate(line) {
case "Move": case "Move":
move = unwrapToken("Move", item); move = unwrapToken("Move", item);
break; break;
case "teraEnter":
opts().teraType = unwrapToken("Tera", it.next().value);
break;
case "terrainEnter": case "terrainEnter":
{ {
let next = it.next().value; let next = it.next().value;
@ -302,7 +318,7 @@ function parseAndCalculate(line) {
function test() { function test() {
const text = const text =
"-2 8 SpA Choice Specs Torkoal Overheat vs. 252 HP / 4+ SpD Assault Vest Abomasnow in Sun through Light Screen"; "-2 8 SpA Choice Specs Torkoal Overheat vs. 252 HP / 4+ SpD Assault Vest Abomasnow in Sun through Light Screen";
const res = parseAndCalculate(text); var res = parseAndCalculate(text);
assert(res.attacker.boosts.spa === -2, "should have -2 SpA"); assert(res.attacker.boosts.spa === -2, "should have -2 SpA");
assert(res.attacker.evs.spa === 8, "should have 8 SpA EVs"); assert(res.attacker.evs.spa === 8, "should have 8 SpA EVs");
@ -317,6 +333,9 @@ function test() {
res.desc().replace(/:.*/, "") === text, res.desc().replace(/:.*/, "") === text,
"non-damage text should be equivalent to input" "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");
} }
export { parseAndCalculate, test }; export { parseAndCalculate, test };