Let's you get a number from the stack to use in dice
This commit is contained in:
parent
df3171b646
commit
e5f3cb0c34
44
lib/dice.ts
44
lib/dice.ts
@ -72,32 +72,36 @@ export class Dice {
|
||||
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> = {};
|
||||
|
||||
// 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;
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user