@yipe/dice 0.2.2 → 0.2.4

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,43 @@ 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 diceConfigs = Array.from(configGroups.values()).map(({ config, totalCount }) => ({
2685
+ ...config,
2686
+ count: totalCount
2687
+ })).sort((a, b) => {
2688
+ if (b.sides !== a.sides) return b.sides - a.sides;
2689
+ 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);
2690
+ return complexity(b) - complexity(a);
2691
+ });
2670
2692
  const totalModifier = this.subRollConfigs.reduce(
2671
2693
  (sum, config) => sum + config.modifier,
2672
2694
  0
2673
2695
  );
2674
2696
  if (diceConfigs.length === 0) return totalModifier.toString();
2675
2697
  const rootConfig = this.getRootDieConfig();
2698
+ const newRootConfig = rootConfig ? diceConfigs.find(
2699
+ (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
2700
+ ) : void 0;
2676
2701
  const diceExpressions = diceConfigs.map(
2677
2702
  (config) => this.configToSingleExpressionWithoutModifier(
2678
2703
  config,
2679
- config === rootConfig
2704
+ config === newRootConfig
2680
2705
  )
2681
2706
  );
2682
2707
  let result = "";
@@ -3277,11 +3302,6 @@ var ACBuilder = class _ACBuilder extends RollBuilder {
3277
3302
  const damageRoll = RollBuilder.fromArgs(...args);
3278
3303
  return new AttackBuilder(this, damageRoll);
3279
3304
  }
3280
- dc(_) {
3281
- throw new Error(
3282
- "Cannot use dc() on an AttackRollBuilder. Use ac() for attack rolls instead."
3283
- );
3284
- }
3285
3305
  get critThreshold() {
3286
3306
  return this.attackConfig.critThreshold;
3287
3307
  }