From f219922eb8616c4cee84609aae7ee08fcb43342f Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 7 Apr 2026 18:10:11 +0800 Subject: [PATCH] Fix fudge dice value parsing --- src/inline/base/dice.ts | 16 ++++++++-------- src/utils/dice.ts | 13 +++++++++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/inline/base/dice.ts b/src/inline/base/dice.ts index 2b239e8..4b62231 100644 --- a/src/inline/base/dice.ts +++ b/src/inline/base/dice.ts @@ -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+$/)) { diff --git a/src/utils/dice.ts b/src/utils/dice.ts index 5bbfa80..1be019e 100644 --- a/src/utils/dice.ts +++ b/src/utils/dice.ts @@ -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; }