Fix fudge dice value parsing

This commit is contained in:
Alex 2026-04-07 18:10:11 +08:00
parent ef0e9822c7
commit f219922eb8
2 changed files with 17 additions and 12 deletions

View file

@ -91,8 +91,15 @@ export class DiceWidgetBase implements BaseWidget {
this.max = cMax || 20;
this.add = cAdd || 0;
if (cMatch?.[4] === "F") {
this.type = "fudge";
this.min = -1;
this.max = 1;
this.value = parseInt(value || "") || 0;
}
const valStr = value ? value.trim() : "";
const detailMatch = valStr.match(/^((?:-?\d+, )*-?\d+) \((\d+)\)$/);
const detailMatch = valStr.match(/^((?:-?\d+, )*-?\d+) \((-?\d+)\)$/);
if (detailMatch) {
this.rolls = detailMatch[1].split(", ").map((n) => parseInt(n));
@ -102,13 +109,6 @@ export class DiceWidgetBase implements BaseWidget {
this.value = parseInt(valStr) || maxVal + this.add;
}
if (cMatch?.[4] === "F") {
this.type = "fudge";
this.min = -1;
this.max = 1;
this.value = parseInt(value || "") || 0;
}
if (params) {
for (const param of params) {
if (param.match(/^\d+$/)) {

View file

@ -14,7 +14,7 @@ export const nrandom = (
quantity: number,
min: number,
max: number,
not = NaN
not = NaN,
): number => {
let result = not;
while (result === not || Number.isNaN(result)) {
@ -26,10 +26,15 @@ export const nrandom = (
return result;
};
export const nroll = (quantity: number, max: number, not = -1): number =>
export const nroll = (quantity: number, max: number, not = -1): number =>
nrandom(quantity, 1, max, not);
export const nrollDetails = (quantity: number, min: number, max: number, not = NaN): { sum: number; rolls: number[] } => {
export const nrollDetails = (
quantity: number,
min: number,
max: number,
not = NaN,
): { sum: number; rolls: number[] } => {
let sum = not;
let rolls: number[] = [];
@ -37,7 +42,7 @@ export const nrollDetails = (quantity: number, min: number, max: number, not = N
sum = 0;
rolls = [];
for (let i = 0; i < quantity; i++) {
const val = nrandom(1, min, max, not);
const val = Math.floor(Math.random() * (max - min + 1)) + min;
rolls.push(val);
sum += val;
}