Skip to content

Commit

Permalink
feat(stat): filter dnf solves from best and worst when not every solv…
Browse files Browse the repository at this point in the history
…e is dnf
  • Loading branch information
GabinL21 committed Aug 10, 2023
1 parent 78e6be0 commit 06911af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/solve/model/solve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Solve {
}

Duration getEffectiveTime() {
if (dnf) return const Duration(minutes: 10);
if (plusTwo) return time + plusTwoDuration;
return time;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/stats/utils/stats_calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class StatsCalculator {
static Stat computeWorst(List<Solve> solves) {
if (solves.isEmpty) return WorstStat.empty();
if (solves.every((s) => s.dnf)) return WorstStat.dnf();
final times = _getTimes(solves);
final nonDnfSolves = solves.where((s) => !s.dnf);
final times = _getTimes(nonDnfSolves);
return WorstStat(times.max);
}

Expand Down
24 changes: 20 additions & 4 deletions test/stats/utils/stats_calculator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ void main() {
_buildSolve(11000),
];

final dnfSolves = List.generate(5, (_) => _buildDnfSolve(60000));

final solvesWithDnf = <Solve>[
...solvesWithPlusTwo,
_buildDnfSolve(5000),
_buildDnfSolve(15000),
];

final sevenSolves = <Solve>[
...solvesWithPlusTwo,
_buildPlusTwoSolve(12000),
Expand All @@ -30,8 +38,6 @@ void main() {
_buildSolve(10009),
];

final dnfSolves = List.generate(5, (_) => _buildDnfSolve());

group('BestStat', () {
test('returns empty best when solves are empty', () {
final bestStat = StatsCalculator.computeBest(List.empty());
Expand All @@ -43,6 +49,11 @@ void main() {
expect(bestStat, BestStat.dnf());
});

test('filters DNF solves when not every solve is DNF', () {
final bestStat = StatsCalculator.computeBest(solvesWithDnf);
expect(bestStat, BestStat(7000));
});

test('computes best with +2 correctly', () {
final bestStat = StatsCalculator.computeBest(solvesWithPlusTwo);
expect(bestStat, BestStat(7000));
Expand All @@ -60,6 +71,11 @@ void main() {
expect(worstStat, WorstStat.dnf());
});

test('filters DNF solves when not every solve is DNF', () {
final worstStat = StatsCalculator.computeWorst(solvesWithDnf);
expect(worstStat, WorstStat(11000));
});

test('computes worst with +2 correctly', () {
final worstStat = StatsCalculator.computeWorst(solvesWithPlusTwo);
expect(worstStat, WorstStat(11000));
Expand Down Expand Up @@ -142,11 +158,11 @@ Solve _buildPlusTwoSolve(int timeInMillis) {
);
}

Solve _buildDnfSolve() {
Solve _buildDnfSolve(int timeInMillis) {
return Solve(
uid: '',
timestamp: DateTime(2000),
time: const Duration(minutes: 10),
time: Duration(milliseconds: timeInMillis),
scramble: '',
dnf: true,
);
Expand Down

0 comments on commit 06911af

Please sign in to comment.