Skip to content

Commit

Permalink
migrated to null safety (#2)
Browse files Browse the repository at this point in the history
* migrated for null safety

* Update CHANGELOG.md

Co-authored-by: Igor Borges <[email protected]>
  • Loading branch information
csells and comigor authored Mar 14, 2021
1 parent 7037449 commit cd14db4
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 0.2.0-nullsafety.0
- Migrate to nullsafety.

## 0.2.0-1
- Add codespaces configuration and GitHub Actions workflows.

Expand Down
4 changes: 2 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ linter:
- parameter_assignments
- prefer_adjacent_string_concatenation
- prefer_asserts_in_initializer_lists
- prefer_bool_in_asserts
# - prefer_bool_in_asserts
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_const_constructors
Expand Down Expand Up @@ -110,7 +110,7 @@ linter:
- slash_for_doc_comments
- sort_constructors_first
- sort_unnamed_constructors_first
- super_goes_last
# - super_goes_last
- test_types_in_equals
- throw_in_finally
- type_annotate_public_apis
Expand Down
4 changes: 2 additions & 2 deletions example/main_statistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import 'package:d20/roll_statistics.dart';
void main() {
final D20 d20 = D20();
final RollStatistics stats = d20.rollWithStatistics('2d8+5+5d6');
print(stats.results[0].faces);
print(stats.results[1].results);
print(stats.results![0].faces);
print(stats.results![1].results);
}
24 changes: 14 additions & 10 deletions lib/d20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ class D20 {
/// Creates a dice roller.
///
/// Optionally receives a [Random].
D20({Random random}) {
D20({Random? random}) {
_random = random == null ? Random() : random;
}

final Parser _parser = Parser();
final ContextModel _context = ContextModel();

/// A [Random] instance that can be customized for seeding.
Random _random;
late Random _random;

RollResult _rollSingleDie(Match match) {
final int numberOfRolls = match[1] == null ? 1 : int.parse(match[1]);
final int faces = int.parse(match[2]);
final int numberOfRolls = match[1] == null ? 1 : int.parse(match[1]!);
final int faces = int.parse(match[2]!);
final List<int> results = List<int>.filled(numberOfRolls, faces)
.map((int die) => _random.nextInt(die) + 1)
.toList();

int sum = results.fold(0, (int sum, int roll) => sum + roll);

if (match[3] == '-l') {
sum -= results.fold(faces, min);
sum -= results.fold<int>(faces, min);
} else if (match[3] == '-h') {
sum -= results.fold(0, max);
sum -= results.fold<int>(0, max);
}

return RollResult(
Expand Down Expand Up @@ -83,8 +83,10 @@ class D20 {
matches.elementAt(i).end, result.finalResult.toString());
}

final double exactResult =
_parser.parse(newRoll).evaluate(EvaluationType.REAL, _context);
final double exactResult = _parser
.parse(newRoll)
// ignore: avoid_as
.evaluate(EvaluationType.REAL, _context) as double;

return RollStatistics(
rollNotation: sanitizedRoll,
Expand All @@ -108,8 +110,10 @@ class D20 {
RegExp(r'(\d+)?d(\d+)(-[l|h])?'),
(Match m) => _rollSingleDie(m).finalResult.toString());

final double exactResult =
_parser.parse(newRoll).evaluate(EvaluationType.REAL, _context);
final double exactResult = _parser
.parse(newRoll)
// ignore: avoid_as
.evaluate(EvaluationType.REAL, _context) as double;

return exactResult.round();
}
Expand Down
10 changes: 5 additions & 5 deletions lib/roll_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class RollResult {
});

/// The string notation from this particular roll.
String rollNotation;
String? rollNotation;

/// How many faces the die on the roll has.
int faces;
int? faces;

/// The number of times the die has been rolled.
int numberOfRolls;
int? numberOfRolls;

/// A list of dice results from this roll.
List<int> results;
List<int>? results;

/// The final result from this roll (considering multiple throws and L/H notation).
int finalResult;
int? finalResult;
}
6 changes: 3 additions & 3 deletions lib/roll_statistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class RollStatistics {
});

/// The sanitized string notation from the roll.
String rollNotation;
String? rollNotation;

/// A list of [RollResult] from the roll.
List<RollResult> results;
List<RollResult>? results;

/// The final result from the roll.
int finalResult;
int? finalResult;
}
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name: d20
version: 0.2.0-1
version: 0.2.0-nullsafety.0
authors:
- Igor Borges <[email protected]>
description: >
D20 is a Dart library for RPG dice rolling. Supports standard notation (like "2d12", "d6+5" and "2d20-L").
homepage: https://github.com/Igor1201/d20

environment:
sdk: ">=2.0.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
math_expressions: ^1.0.0
math_expressions: ^2.1.0-nullsafety.0

dev_dependencies:
test: ^1.5.1+1
20 changes: 10 additions & 10 deletions test/d20_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ void main() {
test('rolls are computed from last to first (for optimization)', () {
final D20 die = D20();
final RollStatistics statistics = die.rollWithStatistics('d8 + d100');
expect(statistics.results[0].faces, 100);
expect(statistics.results[1].faces, 8);
expect(statistics.results![0].faces, 100);
expect(statistics.results![1].faces, 8);
});

test('it has the correct amount of rolls and rolls within', () {
final D20 die = D20();
final RollStatistics statistics = die.rollWithStatistics('2d20-L + 6d6');
expect(statistics.results.length, 2);
expect(statistics.results[0].results.length, 6);
expect(statistics.results[1].results.length, 2);
expect(statistics.results!.length, 2);
expect(statistics.results![0].results!.length, 6);
expect(statistics.results![1].results!.length, 2);
});

test('it correctly parses a single result', () {
final D20 die = D20(random: Random(0));
final RollStatistics statistics =
die.rollWithStatistics('1d1 + 7d6-L + 1d1');
expect(statistics.results[1].faces, 6);
expect(statistics.results[1].numberOfRolls, 7);
expect(statistics.results[1].rollNotation, '7d6-l');
expect(statistics.results[1].results, <int>[6, 5, 2, 1, 4, 3, 2]);
expect(statistics.results[1].finalResult, 22);
expect(statistics.results![1].faces, 6);
expect(statistics.results![1].numberOfRolls, 7);
expect(statistics.results![1].rollNotation, '7d6-l');
expect(statistics.results![1].results, <int>[6, 5, 2, 1, 4, 3, 2]);
expect(statistics.results![1].finalResult, 22);
});
});
}

0 comments on commit cd14db4

Please sign in to comment.