Repository: PokemonGoF/PokemonGo-Bot

Issue:
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
Looks like when the bot is starting up, these fields aren't populated so we get `None` back. We should return 0 if either of these are None

https://app.getsentry.com/pokemongof/pokemongo-bot/issues/147416492/

```
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
  File "pokecli.py", line 490, in <module>
    main()
  File "pokecli.py", line 95, in main
    report_summary(bot)
  File "pokecli.py", line 136, in report_summary
    logger.info('Total XP Earned: {}  Average: {:.2f}/h'.format(metrics.xp_earned(), metrics.xp_per_hour()))
  File "pokemongo_bot/metrics.py", line 28, in xp_earned
    return self.xp['latest'] - self.xp['start']

TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
```



Discussion:
I am having this issue. How to we alter the pokecly.py files so that I no longer get this error?

016-08-07 10:19:24,084 [PokemonGoBot] [INFO] 
2016-08-07 10:19:24,084 [PokemonGoBot] [INFO] 
2016-08-07 10:19:31,577 [       cli] [INFO] 
2016-08-07 10:19:31,577 [       cli] [INFO] Ran for 0:00:11
Traceback (most recent call last):
  File "pokecli.py", line 493, in <module>
    main()
  File "pokecli.py", line 127, in main
    report_summary(bot)
  File "pokecli.py", line 139, in report_summary
    logger.info('Total XP Earned: {}  Average: {:.2f}/h'.format(metrics.xp_earned(), metrics.xp_per_hour()))
  File "/Users/trevor/PokemonGo-Bot/pokemongo_bot/metrics.py", line 28, in xp_earned
    return self.xp['latest'] - self.xp['start']
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

This is still a problem.

Looks like this happens on first login with a fresh new account.
`UpdateLiveStats` tries to get the XP to display the stats and fails at it.
Temporary fix I found is :
- Disabling the worker
- Running the bot once, let it catch a Pokemon, spin a Pokestop, do stuff
- Re-enable the worker
- Watch it work 👍 

Can we make this fix?

yes this is what needs to be done 

but it's not a fix 

account just needs to be level 1 before livestats can be enabled 

@MasterCATZ Yes, it's not a fix. If being level 1 is enough to make it work, we could add a level check in `update_live_stats._should_display` to avoid fetching stats when we're not level 1.

#4088 


Resolving patch:
diff --git a/pokemongo_bot/metrics.py b/pokemongo_bot/metrics.py
--- a/pokemongo_bot/metrics.py
+++ b/pokemongo_bot/metrics.py
@@ -7,15 +7,15 @@ class Metrics(object):
     def __init__(self, bot):
         self.bot = bot
         self.start_time = time.time()
-        self.dust = {'start': 0, 'latest': 0}
-        self.xp = {'start': 0, 'latest': 0}
-        self.distance = {'start': 0, 'latest': 0}
-        self.encounters = {'start': 0, 'latest': 0}
-        self.throws = {'start': 0, 'latest': 0}
-        self.captures = {'start': 0, 'latest': 0}
-        self.visits = {'start': 0, 'latest': 0}
-        self.unique_mons = {'start': 0, 'latest': 0}
-        self.evolutions = {'start': 0, 'latest': 0}
+        self.dust = {'start': -1, 'latest': -1}
+        self.xp = {'start': -1, 'latest': -1}
+        self.distance = {'start': -1, 'latest': -1}
+        self.encounters = {'start': -1, 'latest': -1}
+        self.throws = {'start': -1, 'latest': -1}
+        self.captures = {'start': -1, 'latest': -1}
+        self.visits = {'start': -1, 'latest': -1}
+        self.unique_mons = {'start': -1, 'latest': -1}
+        self.evolutions = {'start': -1, 'latest': -1}
 
         self.releases = 0
         self.highest_cp = {'cp': 0, 'desc': ''}
@@ -84,38 +84,38 @@ def capture_stats(self):
         response_dict = request.call()
         try:
             self.dust['latest'] = response_dict['responses']['GET_PLAYER']['player_data']['currencies'][1]['amount']
-            if self.dust['start'] is None: self.dust['start'] = self.dust['latest']
+            if self.dust['start'] < 0: self.dust['start'] = self.dust['latest']
             for item in response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']:
                 if 'inventory_item_data' in item:
                     if 'player_stats' in item['inventory_item_data']:
                         playerdata = item['inventory_item_data']['player_stats']
 
                         self.xp['latest'] = playerdata.get('experience', 0)
-                        if self.xp['start'] is None: self.xp['start'] = self.xp['latest']
+                        if self.xp['start'] < 0: self.xp['start'] = self.xp['latest']
 
                         self.visits['latest'] = playerdata.get('poke_stop_visits', 0)
-                        if self.visits['start'] is None: self.visits['start'] = self.visits['latest']
+                        if self.visits['start'] < 0: self.visits['start'] = self.visits['latest']
 
                         self.captures['latest'] = playerdata.get('pokemons_captured', 0)
-                        if self.captures['start'] is None: self.captures['start'] = self.captures['latest']
+                        if self.captures['start'] < 0: self.captures['start'] = self.captures['latest']
 
                         self.distance['latest'] = playerdata.get('km_walked', 0)
-                        if self.distance['start'] is None: self.distance['start'] = self.distance['latest']
+                        if self.distance['start'] < 0: self.distance['start'] = self.distance['latest']
 
                         self.encounters['latest'] = playerdata.get('pokemons_encountered', 0)
-                        if self.encounters['start'] is None: self.encounters['start'] = self.encounters['latest']
+                        if self.encounters['start'] < 0: self.encounters['start'] = self.encounters['latest']
 
                         self.throws['latest'] = playerdata.get('pokeballs_thrown', 0)
-                        if self.throws['start'] is None: self.throws['start'] = self.throws['latest']
+                        if self.throws['start'] < 0: self.throws['start'] = self.throws['latest']
 
                         self.unique_mons['latest'] = playerdata.get('unique_pokedex_entries', 0)
-                        if self.unique_mons['start'] is None: self.unique_mons['start'] = self.unique_mons['latest']
+                        if self.unique_mons['start'] < 0: self.unique_mons['start'] = self.unique_mons['latest']
 
                         self.visits['latest'] = playerdata.get('poke_stop_visits', 0)
-                        if self.visits['start'] is None: self.visits['start'] = self.visits['latest']
+                        if self.visits['start'] < 0: self.visits['start'] = self.visits['latest']
 
                         self.evolutions['latest'] = playerdata.get('evolutions', 0)
-                        if self.evolutions['start'] is None: self.evolutions['start'] = self.evolutions['latest']
+                        if self.evolutions['start'] < 0: self.evolutions['start'] = self.evolutions['latest']
         except KeyError:
             # Nothing we can do if there's no player info.
             return


Test patch: