PokemonGo-Bot icon indicating copy to clipboard operation
PokemonGo-Bot copied to clipboard

[dev] Stats not loaded yet

Open Mephistophiles opened this issue 9 years ago • 18 comments

Expected Behavior

When a send /info, I receive a message containing my stats

Actual Behavior

When I send /info, I receive a message: "Stats not loaded yet"

Your FULL config.json (remove your username, password, gmapkey and any other private info)

https://zerobin.net/?5e3c76c169e8bb7a#6+k811VhUdqhGhAk8V0VHuHQ15z1Nyz+47ZTZKBsn+U=

Other Information

OS: ArchLinux

Branch: dev

Git Commit: 5e2338802367c29f1bdb17dc409578ba6feeeb2c

Python Version: 2.7.12

Any other relevant files/configs (eg: path files) Exists: config.json Tasks:

{  
    "type":"TelegramTask",
    "config":{  
        "enabled":true
    }
}

Short Description

Possible solution

How it would help others

Mephistophiles avatar Aug 27 '16 19:08 Mephistophiles

This might be because _get_player_stats(self): is defined after it is used. Can you open pokemongo_bot/cell_workers/telegram_tasks.py and move the last block

def _get_player_stats(self):
    """
    Helper method parsing the bot inventory object and returning the player stats object.
    :return: The player stats object.
    :rtype: dict
    """
    web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)
    with open(web_inventory, "r") as infile:
        json_inventory = json.load(infile)
        infile.close()
    return next((x["inventory_item_data"]["player_stats"]
                 for x in json_inventory
                 if x.get("inventory_item_data", {}).get("player_stats", {})),
                None)

before "work" and report back.

edit, just noticed UpdateLiveStats uses it before definition as well, and that works fine

BreezeRo avatar Aug 27 '16 19:08 BreezeRo

diff --git a/pokemongo_bot/cell_workers/telegram_task.py b/pokemongo_bot/cell_workers/telegram_task.py
index 8dbd5a0..4f86c0d 100644
--- a/pokemongo_bot/cell_workers/telegram_task.py
+++ b/pokemongo_bot/cell_workers/telegram_task.py
@@ -24,6 +24,21 @@ class TelegramTask(BaseTask):
         except IndexError:
             self.update_id = None

+    def _get_player_stats(self):
+        """
+        Helper method parsing the bot inventory object and returning the player stats object.
+        :return: The player stats object.
+        :rtype: dict
+        """
+        web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)
+        with open(web_inventory, "r") as infile:
+            json_inventory = json.load(infile)
+            infile.close()
+        return next((x["inventory_item_data"]["player_stats"]
+                     for x in json_inventory
+                     if x.get("inventory_item_data", {}).get("player_stats", {})),
+                    None)
+ 
     def work(self):
         for update in self.tbot.getUpdates(offset=self.update_id, timeout=10):
             self.update_id = update.update_id+1
@@ -56,17 +71,3 @@ class TelegramTask(BaseTask):
                     )
                     self.tbot.sendMessage(chat_id=update.message.chat_id, parse_mode='Markdown', text="\n".join(res))

-    def _get_player_stats(self):
-        """
-        Helper method parsing the bot inventory object and returning the player stats object.
-        :return: The player stats object.
-        :rtype: dict
-        """
-        web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)
-        with open(web_inventory, "r") as infile:
-            json_inventory = json.load(infile)
-            infile.close()
-        return next((x["inventory_item_data"]["player_stats"]
-                     for x in json_inventory
-                     if x.get("inventory_item_data", {}).get("player_stats", {})),
-                    None)

The same result.

Mephistophiles avatar Aug 27 '16 19:08 Mephistophiles

Yep, move it back, that won't help.

I believe this is to do with when the stats are written to output file. At present, this happens when UpdateLiveStats runs.

Gobberwart avatar Aug 27 '16 19:08 Gobberwart

Hmmm, UpdateLiveStats runs at bot start, so the stats should be there pretty quickly.

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

When I turned on the option bot earned.

Mephistophiles avatar Aug 27 '16 20:08 Mephistophiles

Make sure you have UpdateLiveStats enabled in your config.

@Mephistophiles earned? What option? What happened exactly?

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

UpdateLiveStats turn on

Mephistophiles avatar Aug 27 '16 20:08 Mephistophiles

And then what happens? The bot "earned"???

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

yep

Mephistophiles avatar Aug 27 '16 20:08 Mephistophiles

But yes, the stats will never update if UpdateLiveStats is not turned on.

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

earned what?

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

Telegram bot earned)

Mephistophiles avatar Aug 27 '16 20:08 Mephistophiles

OK so it's working now?

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

Yes

Mephistophiles avatar Aug 27 '16 20:08 Mephistophiles

@Gobberwart the output file is a database, so even when it is in use it would not be a problem to read data.

I think what's happening with the original poster is that he is not collecting stats (config.json, updatelivestat task enabled to true), or he is using a level 1 untouched new pgo account.

If this behavior occurs by default, it may be best to work around it or enable stat collecting by default.

BreezeRo avatar Aug 27 '16 20:08 BreezeRo

@BreezeRo the telegram task gets its player_data info from the web inventory (web/inventory-username.json). If that hasn't had the info written yet (by UpdateLiveStats) there's no data to populate the database with. UpdateLiveStats needs to be enabled for anything related to collection/output of player data.

This is probably undesirable behaviour, but that's how it works currently.

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

If telegram_task.py made an api call instead of relying on UpdateLiveStats / web inventory file, it would work.

eg. replace get_player_stats with:

    def _get_player_stats(self):
        """
        Helper method parsing the bot inventory object and returning the player stats object.
        :return: The player stats object.
        :rtype: dict
        """
        # TODO : find a better solution than calling the api
        inventory_items = self.bot.api.get_inventory() \
            .get('responses', {}) \
            .get('GET_INVENTORY', {}) \
            .get('inventory_delta', {}) \
            .get('inventory_items', {})
        return next((x["inventory_item_data"]["player_stats"]
                     for x in inventory_items
                     if x.get("inventory_item_data", {}).get("player_stats", {})),
                    None)

Not really desirable as it makes an api call, but it's either that, run UpdateLiveStats, or find another way to get this information so it's available throughout the bot without making ridiculous numbers of api calls.

Gobberwart avatar Aug 27 '16 20:08 Gobberwart

@mjmadsen what changes?

k4n30 avatar Oct 05 '16 10:10 k4n30