summaryrefslogtreecommitdiff
path: root/app_types
diff options
context:
space:
mode:
authorDmitrii Morozov <snoopdesigns@gmail.com>2024-07-29 18:46:53 +0200
committerDmitrii Morozov <snoopdesigns@gmail.com>2024-07-29 18:46:53 +0200
commitc2c3c022cce2faec962be9a6b875efba0c569e7a (patch)
tree81880cc8bb9f21e740f3c2df9eea152580bedd65 /app_types
parent72efe8976dceb9cd0d998def404dd2b6852b1c25 (diff)
Error handling for non public stats
Diffstat (limited to 'app_types')
-rw-r--r--app_types/__init__.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/app_types/__init__.py b/app_types/__init__.py
index 12e9adf..a4cf271 100644
--- a/app_types/__init__.py
+++ b/app_types/__init__.py
@@ -40,22 +40,33 @@ class User:
return instance
async def fetch_stats(self) -> UserStats:
- stats = await self.__fortnite_user.fetch_br_stats()
- bp_level: float = await self.__fortnite_user.fetch_battlepass_level(season=__season__)
- combined_stats = stats.get_combined_stats()
- device_stats = {}
- if 'keyboardmouse' in combined_stats:
- device_stats = combined_stats['keyboardmouse']
- else:
- device_stats = combined_stats['gamepad']
-
- stats = UserStats()
-
- stats.user_id = self.id
- stats.user_display_name = self.display_name
- stats.level = int(bp_level//1) if bp_level is not None else 0
- stats.matches_played = device_stats['matchesplayed']
- stats.kills = device_stats['kills']
- stats.wins = device_stats['wins']
-
- return stats \ No newline at end of file
+ user_stats = UserStats()
+ user_stats.user_id = self.id
+ user_stats.user_display_name = self.display_name
+
+ try:
+ br_stats = await self.__fortnite_user.fetch_br_stats()
+ bp_level: float = await self.__fortnite_user.fetch_battlepass_level(season=__season__)
+ combined_stats = br_stats.get_combined_stats()
+ user_stats.level = int(bp_level//1) if bp_level is not None else 0
+ user_stats.matches_played = self.get_stats_value(combined_stats, 'matchesplayed')
+ user_stats.kills = self.get_stats_value(combined_stats, 'kills')
+ user_stats.wins = self.get_stats_value(combined_stats, 'wins')
+ except:
+ user_stats.level = 0
+ user_stats.matches_played = 0
+ user_stats.kills = 0
+ user_stats.wins = 0
+ return user_stats
+
+ def get_stats_value(self, combined_stats: dict, key: str):
+ value = 0
+ value += self.get_stats_value_null_safe(combined_stats, 'keyboardmouse', key)
+ value += self.get_stats_value_null_safe(combined_stats, 'gamepad', key)
+ return value
+
+ def get_stats_value_null_safe(self, combined_stats: dict, device_key: str, key: str):
+ if device_key in combined_stats:
+ if key in combined_stats[device_key]:
+ return combined_stats[device_key][key]
+ return 0 \ No newline at end of file