Skip to content

Commit 352c8a7

Browse files
committed
consistent logging for rerolls. cleanup min/max
1 parent f1b2513 commit 352c8a7

File tree

6 files changed

+44
-143
lines changed

6 files changed

+44
-143
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ as a RolledDie object, rather than an integer. The RolledDie object incorporates
77
a lot of the metadata/scoring in pre-8.0.0 results.
88

99
Having a more elaborate result will make it easier for client applications
10-
to render the outcome of a roll. Each RolledDie knows its result, the # of sides on the die,
11-
whether it was counted as a success/failure, and
10+
to render the outcome of a roll. Each RolledDie knows its result, the # of sides on the die, and
11+
whether it was counted as a success/failure.
1212

1313
## 📈 Enhancements
1414

1515
- upgrade to dart 3.8.0
1616
- upgrade to petitparser 7.0.0
1717
- added dependency on fast_immutable_collections
1818
- remove metadata & score from RollResult
19+
- allow compounding, exploding, and rerolls for 'odd' die (`dF, D66, and d[vals]`)
1920
- rather than simple list of Integers representing the output of a roll or operation, now each
2021
die rolled is a 'RolledDie' object which has the metadata/score.
2122
- the RollResult's results field changes from List<int> to IList<RolledDie>. RolledDie represents not just the

lib/src/ast_core.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
2-
31
import 'dice_expression.dart';
42
import 'dice_roller.dart';
53
import 'results.dart';

lib/src/ast_ops.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CountOp extends Binary {
3434
final lhs = left();
3535
final rhs = right();
3636

37-
bool test(RolledDie rolledDie) {
37+
bool shouldCount(RolledDie rolledDie) {
3838
var rhsEmptyAndSimpleCount = false;
3939
final target = rhs.totalOrDefault(() {
4040
// if missing RHS, we can make assumptions depending on operator.
@@ -44,11 +44,11 @@ class CountOp extends Binary {
4444
rhsEmptyAndSimpleCount = true;
4545
return 0;
4646
case '#s' || '#cs':
47-
// example: '3d6#s' -- assume target is nsides (maximum)
48-
return rolledDie.nsides;
47+
// example: '3d6#s' should match 6, or '3D66' should match 66
48+
return rolledDie.maxPotentialValue;
4949
case '#f' || '#cf':
50-
// example: '3d6#f' -- assume target is 1 (minimum)
51-
return 1;
50+
// generally should be 1.
51+
return rolledDie.minPotentialValue;
5252
default:
5353
throw FormatException(
5454
'Invalid count operation. Missing count target',
@@ -93,7 +93,7 @@ class CountOp extends Binary {
9393
}
9494
}
9595

96-
final scoredResults = lhs.results.where(test);
96+
final scoredResults = lhs.results.where(shouldCount);
9797

9898
if (countType == CountType.count) {
9999
// if counting, the count becomes the new result
@@ -111,7 +111,7 @@ class CountOp extends Binary {
111111
} else {
112112
// if counting success/failures, the results are updated w/ scoring
113113

114-
final nonScoredResults = lhs.results.whereNot(test);
114+
final nonScoredResults = lhs.results.whereNot(shouldCount);
115115

116116
return RollResult(
117117
expression: toString(),

lib/src/dice_roller.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class DiceRoller with LoggingMixin {
3333
case DieType.polyhedral:
3434
return roll(1, rolledDie.nsides, msg);
3535
case DieType.fudge:
36-
return rollFudge(1);
36+
return rollFudge(1, msg);
3737
case DieType.d66:
38-
return rollD66(1);
38+
return rollD66(1, msg);
3939
case DieType.special:
40-
return rollVals(1, rolledDie.potentialValues);
40+
return rollVals(1, rolledDie.potentialValues, msg);
4141
default:
4242
return RollResult(
4343
expression: rolledDie.result.toString(),
@@ -47,7 +47,7 @@ class DiceRoller with LoggingMixin {
4747
}
4848
}
4949

50-
RollResult rollD66(int ndice) {
50+
RollResult rollD66(int ndice, [String msg = '']) {
5151
final results = <RolledDie>[];
5252
final discarded = <RolledDie>[];
5353
for (var i = 0; i < ndice; i++) {
@@ -61,6 +61,9 @@ class DiceRoller with LoggingMixin {
6161
discarded.addAll(rolled);
6262
results.add(RolledDie.d66(result: total, from: rolled));
6363
}
64+
logger.finest(
65+
() => 'roll ${ndice}D66 => $results {discarded: $discarded} $msg',
66+
);
6467
return RollResult(
6568
expression: toString(),
6669
opType: OpType.rollD66,
@@ -93,11 +96,11 @@ class DiceRoller with LoggingMixin {
9396
];
9497

9598
/// Roll N fudge dice, return results
96-
RollResult rollFudge(int ndice) {
99+
RollResult rollFudge(int ndice, [String msg = '']) {
97100
RangeError.checkValueInInterval(ndice, minDice, maxDice, 'ndice');
98101
final results = selectN(ndice, RolledDie.defaultFudgeVals);
99102

100-
logger.finest(() => 'roll ${ndice}dF => $results');
103+
logger.finest(() => 'roll ${ndice}dF => $results $msg');
101104

102105
return RollResult(
103106
expression: '${ndice}dF',
@@ -107,12 +110,12 @@ class DiceRoller with LoggingMixin {
107110
}
108111

109112
/// Roll N fudge dice, return results
110-
RollResult rollVals(int ndice, IList<int> sideVals) {
113+
RollResult rollVals(int ndice, IList<int> sideVals, [String msg = '']) {
111114
RangeError.checkValueInInterval(ndice, minDice, maxDice, 'ndice');
112115
final results = selectN(ndice, sideVals);
113116

114117
logger.finest(
115-
() => 'roll ${ndice}d${sideVals.toString(false)} => $results',
118+
() => 'roll ${ndice}d${sideVals.toString(false)} => $results $msg',
116119
);
117120

118121
return RollResult(

lib/src/results.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ class RolledDie extends Equatable implements Comparable<RolledDie> {
191191
maxPotentialValue = minPotentialValue = result;
192192
case DieType.special || DieType.fudge:
193193
if (potentialValues.isEmpty) {
194-
throw new ArgumentError(
195-
'Invalid die -- ${dieType} must have a potentialValues field',
194+
throw ArgumentError(
195+
'Invalid die -- ${dieType.name} must have a potentialValues field',
196196
);
197197
}
198198
maxPotentialValue = potentialValues.max;
@@ -367,7 +367,6 @@ class RolledDie extends Equatable implements Comparable<RolledDie> {
367367
'result': result,
368368
'nsides': nsides,
369369
'potentialValues': potentialValues.toList(growable: false),
370-
'maxPotentialValue': maxPotentialValue,
371370
'dieType': dieType.name,
372371
'discarded': discarded,
373372
'success': success,

test/dart_dice_parser_test.dart

Lines changed: 21 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,9 @@ void main() {
596596
(((4d(3 + 3)) ! ) + ((2 + 2)d6)) =add=> RollResult(total: 33, results: [6(d6💣), 3(d6🔥), 2(d6), 1(d6), 5(d6), 5(d6), 1(d6), 4(d6), 6(d6)])
597597
((4d(3 + 3)) ! ) =explode=> RollResult(total: 17, results: [6(d6💣), 3(d6🔥), 2(d6), 1(d6), 5(d6)])
598598
(4d(3 + 3)) =rollDice=> RollResult(total: 14, results: [6(d6), 2(d6), 1(d6), 5(d6)])
599-
(3 + 3) =add=> RollResult(total: 6, results: [3(), 3()])
599+
(3 + 3) =add=> RollResult(total: 6, results: [3(val), 3(val)])
600600
((2 + 2)d6) =rollDice=> RollResult(total: 16, results: [5(d6), 1(d6), 4(d6), 6(d6)])
601-
(2 + 2) =add=> RollResult(total: 4, results: [2(), 2()])
601+
(2 + 2) =add=> RollResult(total: 4, results: [2(val), 2(val)])
602602
'''
603603
.trim(),
604604
),
@@ -614,59 +614,19 @@ void main() {
614614
'expression': '(4d6)',
615615
'total': 14,
616616
'results': [
617-
{
618-
'result': 6,
619-
'nsides': 6,
620-
'maxPotentialValue': 6,
621-
'dieType': 'polyhedral',
622-
},
623-
{
624-
'result': 2,
625-
'nsides': 6,
626-
'maxPotentialValue': 6,
627-
'dieType': 'polyhedral',
628-
},
629-
{
630-
'result': 1,
631-
'nsides': 6,
632-
'maxPotentialValue': 6,
633-
'dieType': 'polyhedral',
634-
},
635-
{
636-
'result': 5,
637-
'nsides': 6,
638-
'maxPotentialValue': 6,
639-
'dieType': 'polyhedral',
640-
},
617+
{'result': 6, 'nsides': 6, 'dieType': 'polyhedral'},
618+
{'result': 2, 'nsides': 6, 'dieType': 'polyhedral'},
619+
{'result': 1, 'nsides': 6, 'dieType': 'polyhedral'},
620+
{'result': 5, 'nsides': 6, 'dieType': 'polyhedral'},
641621
],
642622
'detailedResults': {
643623
'expression': '(4d6)',
644624
'opType': 'rollDice',
645625
'results': [
646-
{
647-
'result': 6,
648-
'nsides': 6,
649-
'maxPotentialValue': 6,
650-
'dieType': 'polyhedral',
651-
},
652-
{
653-
'result': 2,
654-
'nsides': 6,
655-
'maxPotentialValue': 6,
656-
'dieType': 'polyhedral',
657-
},
658-
{
659-
'result': 1,
660-
'nsides': 6,
661-
'maxPotentialValue': 6,
662-
'dieType': 'polyhedral',
663-
},
664-
{
665-
'result': 5,
666-
'nsides': 6,
667-
'maxPotentialValue': 6,
668-
'dieType': 'polyhedral',
669-
},
626+
{'result': 6, 'nsides': 6, 'dieType': 'polyhedral'},
627+
{'result': 2, 'nsides': 6, 'dieType': 'polyhedral'},
628+
{'result': 1, 'nsides': 6, 'dieType': 'polyhedral'},
629+
{'result': 5, 'nsides': 6, 'dieType': 'polyhedral'},
670630
],
671631
'total': 14,
672632
},
@@ -689,29 +649,17 @@ void main() {
689649
{
690650
'result': 6,
691651
'nsides': 6,
692-
'maxPotentialValue': 6,
693652
'dieType': 'polyhedral',
694653
'critSuccess': true,
695654
},
696655
{
697656
'result': 1,
698657
'nsides': 6,
699-
'maxPotentialValue': 6,
700658
'dieType': 'polyhedral',
701659
'critFailure': true,
702660
},
703-
{
704-
'result': 2,
705-
'nsides': 6,
706-
'maxPotentialValue': 6,
707-
'dieType': 'polyhedral',
708-
},
709-
{
710-
'result': 5,
711-
'nsides': 6,
712-
'maxPotentialValue': 6,
713-
'dieType': 'polyhedral',
714-
},
661+
{'result': 2, 'nsides': 6, 'dieType': 'polyhedral'},
662+
{'result': 5, 'nsides': 6, 'dieType': 'polyhedral'},
715663
],
716664
'detailedResults': {
717665
'expression': '(((4d6) #cf ) #cs )',
@@ -720,29 +668,17 @@ void main() {
720668
{
721669
'result': 6,
722670
'nsides': 6,
723-
'maxPotentialValue': 6,
724671
'dieType': 'polyhedral',
725672
'critSuccess': true,
726673
},
727674
{
728675
'result': 1,
729676
'nsides': 6,
730-
'maxPotentialValue': 6,
731677
'dieType': 'polyhedral',
732678
'critFailure': true,
733679
},
734-
{
735-
'result': 2,
736-
'nsides': 6,
737-
'maxPotentialValue': 6,
738-
'dieType': 'polyhedral',
739-
},
740-
{
741-
'result': 5,
742-
'nsides': 6,
743-
'maxPotentialValue': 6,
744-
'dieType': 'polyhedral',
745-
},
680+
{'result': 2, 'nsides': 6, 'dieType': 'polyhedral'},
681+
{'result': 5, 'nsides': 6, 'dieType': 'polyhedral'},
746682
],
747683
'left': {
748684
'expression': '((4d6) #cf )',
@@ -751,57 +687,21 @@ void main() {
751687
{
752688
'result': 1,
753689
'nsides': 6,
754-
'maxPotentialValue': 6,
755690
'dieType': 'polyhedral',
756691
'critFailure': true,
757692
},
758-
{
759-
'result': 6,
760-
'nsides': 6,
761-
'maxPotentialValue': 6,
762-
'dieType': 'polyhedral',
763-
},
764-
{
765-
'result': 2,
766-
'nsides': 6,
767-
'maxPotentialValue': 6,
768-
'dieType': 'polyhedral',
769-
},
770-
{
771-
'result': 5,
772-
'nsides': 6,
773-
'maxPotentialValue': 6,
774-
'dieType': 'polyhedral',
775-
},
693+
{'result': 6, 'nsides': 6, 'dieType': 'polyhedral'},
694+
{'result': 2, 'nsides': 6, 'dieType': 'polyhedral'},
695+
{'result': 5, 'nsides': 6, 'dieType': 'polyhedral'},
776696
],
777697
'left': {
778698
'expression': '(4d6)',
779699
'opType': 'rollDice',
780700
'results': [
781-
{
782-
'result': 6,
783-
'nsides': 6,
784-
'maxPotentialValue': 6,
785-
'dieType': 'polyhedral',
786-
},
787-
{
788-
'result': 2,
789-
'nsides': 6,
790-
'maxPotentialValue': 6,
791-
'dieType': 'polyhedral',
792-
},
793-
{
794-
'result': 1,
795-
'nsides': 6,
796-
'maxPotentialValue': 6,
797-
'dieType': 'polyhedral',
798-
},
799-
{
800-
'result': 5,
801-
'nsides': 6,
802-
'maxPotentialValue': 6,
803-
'dieType': 'polyhedral',
804-
},
701+
{'result': 6, 'nsides': 6, 'dieType': 'polyhedral'},
702+
{'result': 2, 'nsides': 6, 'dieType': 'polyhedral'},
703+
{'result': 1, 'nsides': 6, 'dieType': 'polyhedral'},
704+
{'result': 5, 'nsides': 6, 'dieType': 'polyhedral'},
805705
],
806706
'total': 14,
807707
},

0 commit comments

Comments
 (0)