mirror of
https://github.com/alexkurowski/solo-toolkit.git
synced 2026-07-22 10:10:32 +00:00
Fix fudge dice value parsing
This commit is contained in:
parent
ef0e9822c7
commit
f219922eb8
2 changed files with 17 additions and 12 deletions
|
|
@ -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+$/)) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue