@yipe/dice 0.2.3 → 0.2.5

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.
@@ -2568,6 +2568,7 @@ var RollBuilder = class _RollBuilder {
2568
2568
  /** Apply one-pass reroll threshold (k): reroll faces 1..k once, must keep. */
2569
2569
  reroll(value) {
2570
2570
  if (isNaN(value)) throw new Error("Invalid NaN value for reroll");
2571
+ if (value === this.lastConfig.reroll) return this;
2571
2572
  const newConfigs = this.getSubRollConfigs();
2572
2573
  newConfigs[newConfigs.length - 1].reroll = value;
2573
2574
  return new this.constructor(newConfigs);
@@ -2664,19 +2665,63 @@ var RollBuilder = class _RollBuilder {
2664
2665
  return new this.constructor(newConfigs);
2665
2666
  }
2666
2667
  toExpression() {
2667
- const diceConfigs = this.subRollConfigs.filter(
2668
+ const originalDiceConfigs = this.subRollConfigs.filter(
2668
2669
  (config) => config.sides && config.sides > 0
2669
2670
  );
2671
+ const configGroups = /* @__PURE__ */ new Map();
2672
+ for (const config of originalDiceConfigs) {
2673
+ const keyConfig = { ...config };
2674
+ delete keyConfig.count;
2675
+ delete keyConfig.modifier;
2676
+ const key = JSON.stringify(keyConfig);
2677
+ const existingGroup = configGroups.get(key);
2678
+ if (existingGroup) {
2679
+ existingGroup.totalCount += config.count;
2680
+ } else {
2681
+ configGroups.set(key, { config, totalCount: config.count });
2682
+ }
2683
+ }
2684
+ const rootConfig = this.getRootDieConfig();
2685
+ const groupedConfigs = Array.from(configGroups.values());
2686
+ let rootD20Group;
2687
+ if (rootConfig && rootConfig.sides === 20) {
2688
+ const rootIndex = groupedConfigs.findIndex(
2689
+ ({ 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)
2690
+ );
2691
+ if (rootIndex !== -1) {
2692
+ rootD20Group = groupedConfigs.splice(rootIndex, 1)[0];
2693
+ }
2694
+ }
2695
+ const sortedDiceConfigs = groupedConfigs.map(({ config, totalCount }) => ({
2696
+ ...config,
2697
+ count: totalCount
2698
+ })).sort((a, b) => {
2699
+ const aHasPriority = a.reroll > 0 || a.minimum > 0;
2700
+ const bHasPriority = b.reroll > 0 || b.minimum > 0;
2701
+ if (aHasPriority !== bHasPriority) {
2702
+ return aHasPriority ? -1 : 1;
2703
+ }
2704
+ if (b.sides !== a.sides) return b.sides - a.sides;
2705
+ 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);
2706
+ return complexity(b) - complexity(a);
2707
+ });
2708
+ const diceConfigs = rootD20Group ? [
2709
+ { ...rootD20Group.config, count: rootD20Group.totalCount },
2710
+ ...sortedDiceConfigs
2711
+ ] : sortedDiceConfigs;
2670
2712
  const totalModifier = this.subRollConfigs.reduce(
2671
2713
  (sum, config) => sum + config.modifier,
2672
2714
  0
2673
2715
  );
2674
2716
  if (diceConfigs.length === 0) return totalModifier.toString();
2675
- const rootConfig = this.getRootDieConfig();
2717
+ const rootDieConfig = this.getRootDieConfig();
2718
+ const newRootConfig = rootDieConfig ? diceConfigs.find(
2719
+ (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
2720
+ ) : void 0;
2676
2721
  const diceExpressions = diceConfigs.map(
2677
2722
  (config) => this.configToSingleExpressionWithoutModifier(
2678
2723
  config,
2679
- config === rootConfig
2724
+ config === newRootConfig
2680
2725
  )
2681
2726
  );
2682
2727
  let result = "";
@@ -3277,11 +3322,6 @@ var ACBuilder = class _ACBuilder extends RollBuilder {
3277
3322
  const damageRoll = RollBuilder.fromArgs(...args);
3278
3323
  return new AttackBuilder(this, damageRoll);
3279
3324
  }
3280
- dc(_) {
3281
- throw new Error(
3282
- "Cannot use dc() on an AttackRollBuilder. Use ac() for attack rolls instead."
3283
- );
3284
- }
3285
3325
  get critThreshold() {
3286
3326
  return this.attackConfig.critThreshold;
3287
3327
  }