From e5f3cb0c34f98d4156ed9999f53e5b185e96c1cf Mon Sep 17 00:00:00 2001 From: Emma Date: Tue, 20 Aug 2024 19:49:43 -0600 Subject: [PATCH] Let's you get a number from the stack to use in dice --- lib/dice.ts | 44 ++++++++++++++++++++---------------- lib/ttcQuery/TTCResolvers.ts | 36 +++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/lib/dice.ts b/lib/dice.ts index 6603019..dd056fc 100644 --- a/lib/dice.ts +++ b/lib/dice.ts @@ -72,32 +72,36 @@ export class Dice { return this.computeDistribution(); } - private computeDistribution(): Record { + public computeDistribution(): Record { + const maxSum = this.count * this.sides; + const dp: number[][] = Array.from({ length: this.count + 1 }, () => + Array(maxSum + 1).fill(0) + ); + + dp[0][0] = 1; + + for (let dice = 1; dice <= this.count; dice++) { + for (let sum = 0; sum <= maxSum; sum++) { + for (let face = 1; face <= this.sides; face++) { + if (sum >= face) { + dp[dice][sum] += dp[dice - 1][sum - face]; + } + } + } + } + const distribution: Record = {}; - - // Helper function to compute the sum distribution for given number of dice - const computeSumDistribution = ( - dice: number, - sides: number, - currentSum: number, - currentDice: number - ): void => { - if (currentDice === dice) { - distribution[currentSum] = (distribution[currentSum] || 0) + 1; - return; - } - for (let i = 1; i <= sides; i++) { - computeSumDistribution(dice, sides, currentSum + i, currentDice + 1); - } - }; - - // Compute distribution - computeSumDistribution(this.count, this.sides, 0, 0); + for (let sum = this.count; sum <= maxSum; sum++) { + distribution[sum] = dp[this.count][sum]; + } return distribution; } + // STATIC static isDice(d: string) { return /\d+[dD]\d+/.test(d); } } + +// globalThis.Dice = Dice; diff --git a/lib/ttcQuery/TTCResolvers.ts b/lib/ttcQuery/TTCResolvers.ts index 2751b08..f48ac4f 100644 --- a/lib/ttcQuery/TTCResolvers.ts +++ b/lib/ttcQuery/TTCResolvers.ts @@ -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(e).toString() + ) + ) + ) { + return this.handleDice( + stackItem.replace(/\$\d+/g, (e) => + this.getFromStack(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;