@yipe/dice 0.2.4 → 0.2.6

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.
@@ -2679,22 +2679,42 @@ var RollBuilder = class _RollBuilder {
2679
2679
  configGroups.set(key, { config, totalCount: config.count });
2680
2680
  }
2681
2681
  }
2682
- const diceConfigs = Array.from(configGroups.values()).map(({ config, totalCount }) => ({
2682
+ const rootConfig = this.getRootDieConfig();
2683
+ const groupedConfigs = Array.from(configGroups.values());
2684
+ let rootD20Group;
2685
+ if (rootConfig && rootConfig.sides === 20) {
2686
+ const rootIndex = groupedConfigs.findIndex(
2687
+ ({ config }) => config.sides === rootConfig.sides && config.rollType === rootConfig.rollType && config.reroll === rootConfig.reroll && config.explode === rootConfig.explode && config.minimum === rootConfig.minimum && config.bestOf === rootConfig.bestOf && JSON.stringify(config.keep) === JSON.stringify(rootConfig.keep)
2688
+ );
2689
+ if (rootIndex !== -1) {
2690
+ rootD20Group = groupedConfigs.splice(rootIndex, 1)[0];
2691
+ }
2692
+ }
2693
+ const sortedDiceConfigs = groupedConfigs.map(({ config, totalCount }) => ({
2683
2694
  ...config,
2684
2695
  count: totalCount
2685
2696
  })).sort((a, b) => {
2697
+ const aHasPriority = a.reroll > 0 || a.minimum > 0;
2698
+ const bHasPriority = b.reroll > 0 || b.minimum > 0;
2699
+ if (aHasPriority !== bHasPriority) {
2700
+ return aHasPriority ? -1 : 1;
2701
+ }
2686
2702
  if (b.sides !== a.sides) return b.sides - a.sides;
2687
2703
  const complexity = (c) => (c.reroll > 0 ? 1 : 0) + (c.minimum > 0 ? 1 : 0) + (c.explode > 0 ? 1 : 0) + (c.bestOf > 0 ? 1 : 0) + (c.keep ? 1 : 0);
2688
2704
  return complexity(b) - complexity(a);
2689
2705
  });
2706
+ const diceConfigs = rootD20Group ? [
2707
+ { ...rootD20Group.config, count: rootD20Group.totalCount },
2708
+ ...sortedDiceConfigs
2709
+ ] : sortedDiceConfigs;
2690
2710
  const totalModifier = this.subRollConfigs.reduce(
2691
2711
  (sum, config) => sum + config.modifier,
2692
2712
  0
2693
2713
  );
2694
2714
  if (diceConfigs.length === 0) return totalModifier.toString();
2695
- const rootConfig = this.getRootDieConfig();
2696
- const newRootConfig = rootConfig ? diceConfigs.find(
2697
- (c) => c.sides === rootConfig.sides && c.rollType === rootConfig.rollType && c.reroll === rootConfig.reroll && c.explode === rootConfig.explode && c.minimum === rootConfig.minimum && c.bestOf === rootConfig.bestOf && c.keep === rootConfig.keep
2715
+ 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
2698
2718
  ) : void 0;
2699
2719
  const diceExpressions = diceConfigs.map(
2700
2720
  (config) => this.configToSingleExpressionWithoutModifier(
@@ -2763,7 +2783,18 @@ var RollBuilder = class _RollBuilder {
2763
2783
  case "flat":
2764
2784
  if (config.keep) {
2765
2785
  const mode = config.keep.mode === "highest" ? "kh" : "kl";
2766
- mainExpression = `${config.keep.total}${mode}${config.keep.count}(${baseDie})`;
2786
+ const baseDieExpression = this.configToSingleExpressionWithoutModifier(
2787
+ {
2788
+ ...config,
2789
+ count: 1,
2790
+ modifier: 0,
2791
+ rollType: "flat",
2792
+ keep: void 0,
2793
+ bestOf: 0
2794
+ },
2795
+ false
2796
+ );
2797
+ mainExpression = `${config.keep.total}${mode}${config.keep.count}(${baseDieExpression})`;
2767
2798
  } else {
2768
2799
  const isComplex = baseDie.length > `d${config.sides}`.length;
2769
2800
  const isHalflingShorthand = baseDie === "hd20";