From 5700975c4ce76af873b7318e2fd4f4d90fd0f850 Mon Sep 17 00:00:00 2001 From: Tushar Jain Date: Sun, 31 Mar 2024 02:01:15 +0530 Subject: [PATCH] fix: add safe divide for 0 division errors --- .../players/player_timed_stats.dart | 9 +-- lib/models/match/match.dart | 25 ++++---- lib/screens/home/home_player_timed_stats.dart | 58 +++++++++++++------ lib/utilities/math.dart | 6 ++ 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/lib/data_classes/players/player_timed_stats.dart b/lib/data_classes/players/player_timed_stats.dart index 19b5999a..963af14a 100644 --- a/lib/data_classes/players/player_timed_stats.dart +++ b/lib/data_classes/players/player_timed_stats.dart @@ -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"; @@ -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; } diff --git a/lib/models/match/match.dart b/lib/models/match/match.dart index cd9912e4..8484692b 100644 --- a/lib/models/match/match.dart +++ b/lib/models/match/match.dart @@ -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"; @@ -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), ); } diff --git a/lib/screens/home/home_player_timed_stats.dart b/lib/screens/home/home_player_timed_stats.dart index f7c423e9..23a71782 100644 --- a/lib/screens/home/home_player_timed_stats.dart +++ b/lib/screens/home/home_player_timed_stats.dart @@ -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), + ), ), ), ), diff --git a/lib/utilities/math.dart b/lib/utilities/math.dart index 3fcfe956..302c7b42 100644 --- a/lib/utilities/math.dart +++ b/lib/utilities/math.dart @@ -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; +}