Skip to content

Commit

Permalink
fix: add safe divide for 0 division errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharlock10 committed Mar 30, 2024
1 parent 5e7fbab commit 5700975
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
9 changes: 5 additions & 4 deletions lib/data_classes/players/player_timed_stats.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:freezed_annotation/freezed_annotation.dart";
import "package:paladinsedge/models/index.dart" show Ranked, MatchPlayerStats;
import "package:paladinsedge/utilities/index.dart" as utilities;

part "player_timed_stats.freezed.dart";

Expand Down Expand Up @@ -43,9 +44,9 @@ class PlayerTimedStats with _$PlayerTimedStats {
}) = _PlayerTimedStats;

int get totalMatches => wins + losses;
num get damagePerMinute =>
totalStats.totalDamageDealt / totalMatchesDuration.inMinutes;
num get healingPerMinute =>
totalStats.healingDone / totalMatchesDuration.inMinutes;
num get damagePerMinute => utilities.safeDivide(
totalStats.totalDamageDealt, totalMatchesDuration.inMinutes);
num get healingPerMinute => utilities.safeDivide(
totalStats.healingDone, totalMatchesDuration.inMinutes);
MatchPlayerStats get averageStats => totalStats / totalMatches;
}
25 changes: 13 additions & 12 deletions lib/models/match/match.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "package:hive/hive.dart";
import "package:json_annotation/json_annotation.dart";
import "package:paladinsedge/constants/index.dart" show TypeIds;
import "package:paladinsedge/models/player/player.dart" show Ranked;
import "package:paladinsedge/utilities/index.dart" as utilities;

part "match.g.dart";

Expand Down Expand Up @@ -103,19 +104,19 @@ class MatchPlayerStats {
/// except for the value biggestKillStreak, which remains unchanged
MatchPlayerStats operator /(num divisor) {
return MatchPlayerStats(
kills: kills / divisor,
assists: assists / divisor,
deaths: deaths / divisor,
weaponDamageDealt: weaponDamageDealt / divisor,
totalDamageDealt: totalDamageDealt / divisor,
damageShielded: damageShielded / divisor,
totalDamageTaken: totalDamageTaken / divisor,
selfHealingDone: selfHealingDone / divisor,
healingDone: healingDone / divisor,
kills: utilities.safeDivide(kills, divisor),
assists: utilities.safeDivide(assists, divisor),
deaths: utilities.safeDivide(deaths, divisor),
weaponDamageDealt: utilities.safeDivide(weaponDamageDealt, divisor),
totalDamageDealt: utilities.safeDivide(totalDamageDealt, divisor),
damageShielded: utilities.safeDivide(damageShielded, divisor),
totalDamageTaken: utilities.safeDivide(totalDamageTaken, divisor),
selfHealingDone: utilities.safeDivide(selfHealingDone, divisor),
healingDone: utilities.safeDivide(healingDone, divisor),
biggestKillStreak: biggestKillStreak,
totalMultiKills: totalMultiKills / divisor,
objectiveTime: objectiveTime / divisor,
creditsEarned: creditsEarned / divisor,
totalMultiKills: utilities.safeDivide(totalMultiKills, divisor),
objectiveTime: utilities.safeDivide(objectiveTime, divisor),
creditsEarned: utilities.safeDivide(creditsEarned, divisor),
);
}

Expand Down
58 changes: 39 additions & 19 deletions lib/screens/home/home_player_timed_stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,48 @@ class HomePlayerTimedStats extends HookConsumerWidget {
);

return timedStats == null || isPlayerMatchesLoading
? const widgets.LoadingIndicator(size: 18)
? const widgets.LoadingIndicator(
lineWidth: 2,
size: 28,
label: Text("Loading Stats"),
)
: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
child: Card(
child: SizedBox(
height: widgets.PlayerStatsCard.itemHeight,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: utilities.insertBetween(
[
widgets.PlayerStatsCard(
title: "Matches",
stat: timedStats.totalMatches,
),
widgets.PlayerStatsCard(
title: "Win - Loss",
stat: 0,
statString: "${timedStats.wins} - ${timedStats.losses}",
),
],
const SizedBox(width: 10),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: SizedBox(
height: widgets.PlayerStatsCard.itemHeight,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 10),
children: utilities.insertBetween(
[
widgets.PlayerStatsCard(
title: "Matches",
stat: timedStats.totalMatches,
),
widgets.PlayerStatsCard(
title: "Win - Loss",
stat: 0,
statString:
"${timedStats.wins} - ${timedStats.losses}",
),
widgets.PlayerStatsCard(
title: "Damage/min.",
stat: timedStats.damagePerMinute,
),
widgets.PlayerStatsCard(
title: "Play Time",
stat: 0,
statString: utilities.getFormattedPlayTime(
timedStats.totalMatchesDuration.inMinutes,
),
),
],
const SizedBox(width: 10),
),
),
),
),
Expand Down
6 changes: 6 additions & 0 deletions lib/utilities/math.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ int roundToNearestTenth(
return intResult;
}

// convert a number with decimals in compact format
String humanizeNumber(num number) {
return NumberFormat.compact().format(number);
}

// checks for 0 division, return default value
num safeDivide(num numerator, num denominator, [num defaultValue = 0]) {
return denominator == 0 ? defaultValue : numerator / denominator;
}

0 comments on commit 5700975

Please sign in to comment.