MD handlers for resolvers

This commit is contained in:
2024-08-05 06:25:55 -06:00
parent 3656fc42ac
commit 2b2b88f970
7 changed files with 193 additions and 25 deletions

View File

@@ -147,13 +147,3 @@ export class TTCQueryParser {
}
}
}
// Example usage:
// const
// const parser = new TTCQueryParser(data);
// // Example queries
// console.log(parser.search("$weapon_abilities[name=Rapid Fire].body")); // Example output
// // console.log(parser.search("^weapon_abilities[name=Rapid Fire]", data)); // Example output
// console.log(parser.search("$weapon_abilities[name=Rapid Fire].body", data)); // Example output

View File

@@ -26,12 +26,14 @@ export class TTCQueryResolver {
}
const last = this.stack.at(-1);
if (typeof last === "function" && !onDemand) return last();
if (onDemand) debugger;
return last;
}
private parseResolver(resolver: string) {
if (this.isArithmetic(resolver)) return this.solveArithmetic(resolver);
if (this.isQuery(resolver)) this.runQuery(resolver);
if (this.isQuery(resolver)) return this.runQuery(resolver);
}
private isQuery(resolver: string) {
return (
@@ -56,15 +58,15 @@ export class TTCQueryResolver {
// if (query.startsWith("?") || query.startsWith("_"))
return query.startsWith("_") && this.context
? this.parser.search(query.replace("_", ""), this.context)
: this.parser.search(query.replace(/^[?_].?/, ""));
? this.parser.search(query.replace("_", "^"), this.context).at(0)
: this.parser.search(query.replace(/^[?_].?/, "")).at(0);
}
private handleDice(dice: string, query: string) {
const d = new Dice(dice);
const [method, n] = query.split(":");
let num = Number(n);
if (n.startsWith("$")) num = this.getFromStack(n);
if (n && n.startsWith("$")) num = this.getFromStack(n);
switch (method) {
case "roll":
return () => d.roll();
@@ -91,7 +93,6 @@ export class TTCQueryResolver {
if (n1.startsWith("$")) num1 = this.getFromStack<number>(n1);
if (n2.startsWith("$")) num2 = this.getFromStack<number>(n2);
console.log(num1, num2);
switch (op) {
case "+":
@@ -109,7 +110,7 @@ export class TTCQueryResolver {
}
}
private getFromStack<T>(stackIndex: string): T {
public getFromStack<T>(stackIndex: string): T {
const i = Number(stackIndex.replace("$", ""));
const val = this.stack[i] as T;
return val;