@yipe/dice 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/builder/index.cjs +23 -20
- package/dist/builder/index.cjs.map +1 -1
- package/dist/builder/index.js +23 -20
- package/dist/builder/index.js.map +1 -1
- package/package.json +1 -1
package/dist/builder/index.js
CHANGED
|
@@ -2427,6 +2427,12 @@ var defaultConfig = {
|
|
|
2427
2427
|
keep: void 0,
|
|
2428
2428
|
rollType: "flat"
|
|
2429
2429
|
};
|
|
2430
|
+
var rollConfigsEqual = (a, b) => {
|
|
2431
|
+
return a.count === b.count && a.sides === b.sides && a.modifier === b.modifier && a.reroll === b.reroll && a.explode === b.explode && a.minimum === b.minimum && a.bestOf === b.bestOf && a.keep === b.keep && a.rollType === b.rollType;
|
|
2432
|
+
};
|
|
2433
|
+
var configComplexityScore = (config) => {
|
|
2434
|
+
return (config.reroll > 0 ? 1 : 0) + (config.explode > 0 ? 1 : 0) + (config.minimum > 0 ? 1 : 0) + (config.bestOf > 0 ? 1 : 0) + (config.keep !== void 0 ? 1 : 0) + (config.rollType !== "flat" ? 1 : 0);
|
|
2435
|
+
};
|
|
2430
2436
|
var RollBuilder = class _RollBuilder {
|
|
2431
2437
|
constructor(countOrConfigs = 1) {
|
|
2432
2438
|
__publicField(this, "subRollConfigs");
|
|
@@ -2696,12 +2702,9 @@ var RollBuilder = class _RollBuilder {
|
|
|
2696
2702
|
})).sort((a, b) => {
|
|
2697
2703
|
const aHasPriority = a.reroll > 0 || a.minimum > 0;
|
|
2698
2704
|
const bHasPriority = b.reroll > 0 || b.minimum > 0;
|
|
2699
|
-
if (aHasPriority !== bHasPriority)
|
|
2700
|
-
return aHasPriority ? -1 : 1;
|
|
2701
|
-
}
|
|
2705
|
+
if (aHasPriority !== bHasPriority) return aHasPriority ? -1 : 1;
|
|
2702
2706
|
if (b.sides !== a.sides) return b.sides - a.sides;
|
|
2703
|
-
|
|
2704
|
-
return complexity(b) - complexity(a);
|
|
2707
|
+
return configComplexityScore(b) - configComplexityScore(a);
|
|
2705
2708
|
});
|
|
2706
2709
|
const diceConfigs = rootD20Group ? [
|
|
2707
2710
|
{ ...rootD20Group.config, count: rootD20Group.totalCount },
|
|
@@ -2713,9 +2716,7 @@ var RollBuilder = class _RollBuilder {
|
|
|
2713
2716
|
);
|
|
2714
2717
|
if (diceConfigs.length === 0) return totalModifier.toString();
|
|
2715
2718
|
const rootDieConfig = this.getRootDieConfig();
|
|
2716
|
-
const newRootConfig = rootDieConfig ? diceConfigs.find(
|
|
2717
|
-
(c) => c.sides === rootDieConfig.sides && c.rollType === rootDieConfig.rollType && c.reroll === rootDieConfig.reroll && c.explode === rootDieConfig.explode && c.minimum === rootDieConfig.minimum && c.bestOf === rootDieConfig.bestOf && c.keep === rootDieConfig.keep
|
|
2718
|
-
) : void 0;
|
|
2719
|
+
const newRootConfig = rootDieConfig ? diceConfigs.find((c) => rollConfigsEqual(c, rootDieConfig)) : void 0;
|
|
2719
2720
|
const diceExpressions = diceConfigs.map(
|
|
2720
2721
|
(config) => this.configToSingleExpressionWithoutModifier(
|
|
2721
2722
|
config,
|
|
@@ -2761,7 +2762,11 @@ var RollBuilder = class _RollBuilder {
|
|
|
2761
2762
|
}
|
|
2762
2763
|
}
|
|
2763
2764
|
if (config.minimum > 0) {
|
|
2764
|
-
|
|
2765
|
+
if (config.reroll > 0 && !config.explode) {
|
|
2766
|
+
baseDie = `${config.minimum}>(${baseDie})`;
|
|
2767
|
+
} else {
|
|
2768
|
+
baseDie = `${config.minimum}>${baseDie}`;
|
|
2769
|
+
}
|
|
2765
2770
|
if (config.reroll > 0 && config.explode > 0) {
|
|
2766
2771
|
for (let i = 1; i <= config.reroll; i++) {
|
|
2767
2772
|
baseDie += ` reroll ${i}`;
|
|
@@ -2786,11 +2791,10 @@ var RollBuilder = class _RollBuilder {
|
|
|
2786
2791
|
const baseDieExpression = this.configToSingleExpressionWithoutModifier(
|
|
2787
2792
|
{
|
|
2788
2793
|
...config,
|
|
2789
|
-
count:
|
|
2794
|
+
count: config.count,
|
|
2790
2795
|
modifier: 0,
|
|
2791
2796
|
rollType: "flat",
|
|
2792
|
-
keep: void 0
|
|
2793
|
-
bestOf: 0
|
|
2797
|
+
keep: void 0
|
|
2794
2798
|
},
|
|
2795
2799
|
false
|
|
2796
2800
|
);
|
|
@@ -2799,21 +2803,20 @@ var RollBuilder = class _RollBuilder {
|
|
|
2799
2803
|
const isComplex = baseDie.length > `d${config.sides}`.length;
|
|
2800
2804
|
const isHalflingShorthand = baseDie === "hd20";
|
|
2801
2805
|
const isD20Shorthand = baseDie === "d20" && isRootDie;
|
|
2802
|
-
const hasMinimum =
|
|
2803
|
-
const hasReroll =
|
|
2806
|
+
const hasMinimum = config.minimum > 0;
|
|
2807
|
+
const hasReroll = config.reroll > 0;
|
|
2804
2808
|
const effectiveCount = config.isSubtraction ? Math.abs(config.count) : config.count < 0 ? 1 : Math.abs(config.count);
|
|
2805
2809
|
if (effectiveCount > 1) {
|
|
2806
|
-
const shouldAddParentheses = isComplex
|
|
2807
|
-
!hasMinimum && !hasReroll || // Simple complex case
|
|
2808
|
-
hasReroll && !hasMinimum);
|
|
2810
|
+
const shouldAddParentheses = isComplex;
|
|
2809
2811
|
mainExpression = shouldAddParentheses ? `${effectiveCount}(${baseDie})` : `${effectiveCount}${baseDie}`;
|
|
2810
2812
|
} else if (effectiveCount === 1) {
|
|
2813
|
+
const needsParens = hasReroll && hasMinimum;
|
|
2811
2814
|
if (config.isSubtraction) {
|
|
2812
|
-
mainExpression = `1${baseDie}`;
|
|
2815
|
+
mainExpression = needsParens ? `1(${baseDie})` : `1${baseDie}`;
|
|
2813
2816
|
} else if (isComplex || isHalflingShorthand || isD20Shorthand || config.count < 0) {
|
|
2814
|
-
mainExpression = baseDie;
|
|
2817
|
+
mainExpression = needsParens ? `1(${baseDie})` : baseDie;
|
|
2815
2818
|
} else {
|
|
2816
|
-
mainExpression = `1${baseDie}`;
|
|
2819
|
+
mainExpression = needsParens ? `1(${baseDie})` : `1${baseDie}`;
|
|
2817
2820
|
}
|
|
2818
2821
|
} else {
|
|
2819
2822
|
mainExpression = baseDie;
|