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

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

View File

@ -72,32 +72,36 @@ export class Dice {
return this.computeDistribution(); return this.computeDistribution();
} }
private computeDistribution(): Record<number, number> { public computeDistribution(): Record<number, number> {
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<number, number> = {}; const distribution: Record<number, number> = {};
for (let sum = this.count; sum <= maxSum; sum++) {
// Helper function to compute the sum distribution for given number of dice distribution[sum] = dp[this.count][sum];
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);
return distribution; return distribution;
} }
// STATIC // STATIC
static isDice(d: string) { static isDice(d: string) {
return /\d+[dD]\d+/.test(d); return /\d+[dD]\d+/.test(d);
} }
} }
// globalThis.Dice = Dice;

View File

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