Let's you get a number from the stack to use in dice

This commit is contained in:
2024-08-20 19:49:43 -06:00
parent df3171b646
commit e5f3cb0c34
2 changed files with 50 additions and 30 deletions

View File

@@ -23,14 +23,18 @@ export class TTCQueryResolver {
}
public resolve(resolver: string, onDemand?: boolean) {
const resList = resolver.split(",");
for (const res of resList) {
this.stack.push(this.parseResolver(res));
}
const last = this.stack.at(-1);
if (typeof last === "function" && !onDemand) return last();
try {
const resList = resolver.split(",");
for (const res of resList) {
this.stack.push(this.parseResolver(res));
}
const last = this.stack.at(-1);
if (typeof last === "function" && !onDemand) return last();
return last;
return last;
} catch (e) {
return e?.toString();
}
}
private parseResolver(resolver: string) {
@@ -51,8 +55,20 @@ export class TTCQueryResolver {
const [_, idx, q] = query.match(/^(\$\d+)\.(.*)/) || [];
if (!_) throw "Detected stack query but did not match the regex";
const stackItem = this.getFromStack(idx);
if (typeof stackItem === "string" && Dice.isDice(stackItem)) {
return this.handleDice(stackItem, q);
if (
typeof stackItem === "string" &&
Dice.isDice(
stackItem.replace(/\$\d+/g, (e) =>
this.getFromStack<number>(e).toString()
)
)
) {
return this.handleDice(
stackItem.replace(/\$\d+/g, (e) =>
this.getFromStack<number>(e).toString()
),
q
);
}
return this.parser.search(q, stackItem as QueryableObject);
@@ -68,7 +84,7 @@ export class TTCQueryResolver {
const d = new Dice(dice);
const [method, n] = query.split(":");
let num = Number(n);
if (n && n.startsWith("$")) num = this.getFromStack(n);
// if (n && n.startsWith("$")) num = this.getFromStack(n);
switch (method) {
case "roll":
return () => d.roll().total;