Skip to content

Commit 2f96d96

Browse files
committed
enhance dieType to know more capabilities, throw error on misconfigure
1 parent 352c8a7 commit 2f96d96

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/src/dice_roller.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class DiceRoller with LoggingMixin {
5151
final results = <RolledDie>[];
5252
final discarded = <RolledDie>[];
5353
for (var i = 0; i < ndice; i++) {
54-
final tensRoll = roll(1, 6);
55-
final onesRoll = roll(1, 6);
54+
final tensRoll = roll(1, 6, 'D66*10 $msg');
55+
final onesRoll = roll(1, 6, 'D66*1 $msg');
5656
final total = tensRoll.total * 10 + onesRoll.total;
5757
final rolled = [
5858
RolledDie.discard(tensRoll.results.first),

lib/src/results.dart

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@ import 'package:collection/collection.dart';
22
import 'package:equatable/equatable.dart';
33
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
44

5-
/// types of die
5+
/// types of die.
66
enum DieType implements Comparable<DieType> {
77
// normal polyhedral (1d6, 1d20, etc)
8-
polyhedral(explodable: true, compoundable: true),
8+
polyhedral(),
99
// fudge dice
10-
fudge(explodable: true, compoundable: true),
10+
fudge(hasPotentialValues: true),
1111
// 1D66 (equivalent to `1d6*10 + 1d6`).
12-
d66(explodable: true, compoundable: true),
12+
d66(hasNSides: false),
1313
// 1d[1,3,5,7,9]
14-
special(explodable: true, compoundable: true),
14+
special(hasPotentialValues: true),
1515
// single value (e.g. a sum or count of dice)
16-
singleVal(explodable: false, compoundable: false);
16+
singleVal(explodable: false, compoundable: false, hasPotentialValues: true);
1717

18-
const DieType({required this.explodable, required this.compoundable});
18+
const DieType({
19+
this.explodable = true,
20+
this.compoundable = true,
21+
this.hasPotentialValues = false,
22+
this.hasNSides = true,
23+
});
1924

25+
/// can the die be exploded?
2026
final bool explodable;
27+
28+
/// can the die be compounded?
2129
final bool compoundable;
2230

31+
/// whether the RolledDie must have non-empty potentialValues
32+
final bool hasPotentialValues;
33+
34+
/// whether the RolledDie must have non-zero nsides
35+
final bool hasNSides;
36+
2337
@override
2438
int compareTo(DieType dieType) => index.compareTo(dieType.index);
2539
}
@@ -180,6 +194,16 @@ class RolledDie extends Equatable implements Comparable<RolledDie> {
180194
this.clampFloor = false,
181195
this.from = const IList.empty(),
182196
}) : potentialValues = IList(potentialValues) {
197+
if (dieType.hasPotentialValues && potentialValues.isEmpty) {
198+
throw ArgumentError(
199+
'Invalid die -- ${dieType.name} must have a potentialValues field',
200+
);
201+
}
202+
if (dieType.hasNSides && nsides == 0) {
203+
throw ArgumentError(
204+
'Invalid die -- ${dieType.name} must have a nsides != 0',
205+
);
206+
}
183207
switch (dieType) {
184208
case DieType.polyhedral:
185209
maxPotentialValue = nsides;
@@ -190,11 +214,6 @@ class RolledDie extends Equatable implements Comparable<RolledDie> {
190214
case DieType.singleVal:
191215
maxPotentialValue = minPotentialValue = result;
192216
case DieType.special || DieType.fudge:
193-
if (potentialValues.isEmpty) {
194-
throw ArgumentError(
195-
'Invalid die -- ${dieType.name} must have a potentialValues field',
196-
);
197-
}
198217
maxPotentialValue = potentialValues.max;
199218
minPotentialValue = potentialValues.min;
200219
}
@@ -215,6 +234,7 @@ class RolledDie extends Equatable implements Comparable<RolledDie> {
215234
Iterable<RolledDie>? from,
216235
}) => RolledDie(
217236
result: result,
237+
nsides: 1,
218238
dieType: DieType.singleVal,
219239
potentialValues: [result],
220240
from: IList.orNull(from) ?? const IList.empty(),
@@ -345,6 +365,7 @@ class RolledDie extends Equatable implements Comparable<RolledDie> {
345365
result,
346366
nsides,
347367
maxPotentialValue,
368+
minPotentialValue,
348369
potentialValues,
349370
dieType,
350371
discarded,

0 commit comments

Comments
 (0)