Skip to content

Commit

Permalink
Removed api call for player_stats
Browse files Browse the repository at this point in the history
Removed api call for player_stats, and moved output of player_stats to
web to update_live_stats.py
  • Loading branch information
Gobberwart committed Aug 23, 2016
1 parent 2a9f345 commit 32f0c5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
25 changes: 23 additions & 2 deletions pokemongo_bot/cell_workers/update_live_stats.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import ctypes
import json
import os
from sys import stdout, platform as _platform
from datetime import datetime, timedelta

from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.tree_config_builder import ConfigException
from pokemongo_bot.base_dir import _base_dir


class UpdateLiveStats(BaseTask):
Expand Down Expand Up @@ -92,11 +95,15 @@ def work(self):
"""
if not self._should_display():
return WorkerResult.SUCCESS
line = self._get_stats_line(self._get_player_stats())

player_stats = self._get_player_stats()
line = self._get_stats_line(player_stats)
# If line is empty, it couldn't be generated.
if not line:
return WorkerResult.SUCCESS


self.update_web_stats(player_stats)

if self.terminal_title:
self._update_title(line, _platform)

Expand Down Expand Up @@ -356,3 +363,17 @@ def _get_player_stats(self):
for x in inventory_items
if x.get("inventory_item_data", {}).get("player_stats", {})),
None)

def update_web_stats(self,player_data):
web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)

with open(web_inventory, "r") as infile:
json_stats = json.load(infile)

json_stats = [x for x in json_stats if not x.get("inventory_item_data", {}).get("player_stats", None)]

for player_stat in player_data:
json_stats.append({"inventory_item_data": {"player_stats": player_data}})

with open(web_inventory, "w") as outfile:
json.dump(json_stats, outfile)
21 changes: 8 additions & 13 deletions pokemongo_bot/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,20 +1098,15 @@ def refresh(self):

def update_web_inventory(self):
web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)
json_inventory = []

with open(web_inventory, "r") as infile:
json_inventory = json.load(infile)

json_inventory = [x for x in json_inventory if not x.get("inventory_item_data", {}).get("pokedex_entry", None)]
json_inventory = [x for x in json_inventory if not x.get("inventory_item_data", {}).get("candy", None)]
json_inventory = [x for x in json_inventory if not x.get("inventory_item_data", {}).get("item", None)]
json_inventory = [x for x in json_inventory if not x.get("inventory_item_data", {}).get("pokemon_data", None)]

inventory_items = self.bot.api.get_inventory() \
.get('responses', {}) \
.get('GET_INVENTORY', {}) \
.get('inventory_delta', {}) \
.get('inventory_items', {})
player_data = next((x["inventory_item_data"]["player_stats"]
for x in inventory_items
if x.get("inventory_item_data", {}).get("player_stats", {})),
None)
for player_stat in player_data:
json_inventory.append({"inventory_item_data": {"player_stats": player_data}})

for pokedex in self.pokedex.all():
json_inventory.append({"inventory_item_data": {"pokedex_entry": pokedex}})

Expand Down

0 comments on commit 32f0c5d

Please sign in to comment.