Skip to content

Commit

Permalink
Allow to collect level up rewards (#2004)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihisil authored and solderzzc committed Jul 31, 2016
1 parent dc2fa60 commit 5629590
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{
"type": "HandleSoftBan"
},
{
"type": "CollectLevelUpReward"
},
{
"type": "IncubateEggs",
"config": {
Expand Down
3 changes: 3 additions & 0 deletions configs/config.json.path.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{
"type": "HandleSoftBan"
},
{
"type": "CollectLevelUpReward"
},
{
"type": "IncubateEggs",
"config": {
Expand Down
3 changes: 3 additions & 0 deletions configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{
"type": "HandleSoftBan"
},
{
"type": "CollectLevelUpReward"
},
{
"type": "IncubateEggs",
"config": {
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
from handle_soft_ban import HandleSoftBan
from follow_path import FollowPath
from follow_spiral import FollowSpiral
from collect_level_up_reward import CollectLevelUpReward
from base_task import BaseTask
64 changes: 64 additions & 0 deletions pokemongo_bot/cell_workers/collect_level_up_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from pokemongo_bot import logger
from pokemongo_bot.cell_workers.base_task import BaseTask


class CollectLevelUpReward(BaseTask):
current_level = 0
previous_level = 0

def initialize(self):
self.current_level = self._get_current_level()
self.previous_level = 0

def work(self):
self.current_level = self._get_current_level()

# let's check level reward on bot initialization
# to be able get rewards for old bots
if self.previous_level == 0:
self._collect_level_reward()
# level up situation
elif self.current_level > self.previous_level:
logger.log('Level up from {} to {}!'.format(self.previous_level, self.current_level), 'green')
self._collect_level_reward()

self.previous_level = self.current_level

def _collect_level_reward(self):
self.bot.api.level_up_rewards(level=self.current_level)
response_dict = self.bot.api.call()
if 'status_code' in response_dict and response_dict['status_code'] == 1:
data = (response_dict
.get('responses', {})
.get('LEVEL_UP_REWARDS', {})
.get('items_awarded', []))

if data:
logger.log('Collected level up rewards:', 'green')

for item in data:
if 'item_id' in item and str(item['item_id']) in self.bot.item_list:
got_item = self.bot.item_list[str(item['item_id'])]
count = 'item_count' in item and item['item_count'] or 0
logger.log('{} x {}'.format(got_item, count), 'green')

def _get_current_level(self):
level = 0
response_dict = self.bot.get_inventory()
data = (response_dict
.get('responses', {})
.get('GET_INVENTORY', {})
.get('inventory_delta', {})
.get('inventory_items', {}))

for item in data:
level = (item
.get('inventory_item_data', {})
.get('player_stats', {})
.get('level', 0))

# we found a level, no need to continue iterate
if level:
break

return level

0 comments on commit 5629590

Please sign in to comment.