Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stops bot after a set level limit #4656

Merged
merged 1 commit into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion configs/config.json.cluster.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
}
},
{
"type": "CollectLevelUpReward"
"type": "LevelUp",
"config": {
"collect_reward": true,
"level_limit": -1
}
},
{
"type": "IncubateEggs",
Expand Down
6 changes: 5 additions & 1 deletion configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
}
},
{
"type": "CollectLevelUpReward"
"type": "LevelUp",
"config": {
"collect_reward": true,
"level_limit": -1
}
},
{
"type": "IncubateEggs",
Expand Down
6 changes: 5 additions & 1 deletion configs/config.json.map.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
}
},
{
"type": "CollectLevelUpReward"
"type": "LevelUp",
"config": {
"collect_reward": true,
"level_limit": -1
}
},
{
"type": "IncubateEggs",
Expand Down
6 changes: 5 additions & 1 deletion configs/config.json.optimizer.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
}
},
{
"type": "CollectLevelUpReward"
"type": "LevelUp",
"config": {
"collect_reward": true,
"level_limit": -1
}
},
{
"type": "IncubateEggs",
Expand Down
6 changes: 5 additions & 1 deletion configs/config.json.path.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
}
},
{
"type": "CollectLevelUpReward"
"type": "LevelUp",
"config": {
"collect_reward": true,
"level_limit": -1
}
},
{
"type": "IncubateEggs",
Expand Down
6 changes: 5 additions & 1 deletion configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
}
},
{
"type": "CollectLevelUpReward"
"type": "LevelUp",
"config": {
"collect_reward": true,
"level_limit": -1
}
},
{
"type": "IncubateEggs",
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/cell_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from handle_soft_ban import HandleSoftBan
from follow_path import FollowPath
from follow_spiral import FollowSpiral
from collect_level_up_reward import CollectLevelUpReward
from level_up import LevelUp
from follow_cluster import FollowCluster
from sleep_schedule import SleepSchedule
from update_live_stats import UpdateLiveStats
Expand Down
92 changes: 92 additions & 0 deletions pokemongo_bot/cell_workers/level_up.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import sys

from pokemongo_bot.base_task import BaseTask
from pokemongo_bot import inventory

class LevelUp(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

current_level = 0
previous_level = 0

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

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

if self.collect_reward:
# 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:
self.emit_event(
'level_up',
formatted='Level up from {previous_level} to {current_level}',
data={
'previous_level': self.previous_level,
'current_level': self.current_level
}
)
self._collect_level_reward()

if self.level_limit != -1 and self.current_level >= self.level_limit:
sys.exit("You have reached your target level! Exiting now.")

self.previous_level = self.current_level

def _process_config(self):
self.level_limit = self.config.get('level_limit', -1)
self.collect_reward = self.config.get('collect_reward', True)

def _should_run(self):
return self.level_limit != -1 or self.collect_reward

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

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'])]
item['name'] = got_item
count = 'item_count' in item and item['item_count'] or 0
inventory.items().get(item['item_id']).add(count)

self.emit_event(
'level_up_reward',
formatted='Received level up reward: {items}',
data={
'items': data
}
)

def _get_current_level(self):
level = 0
response_dict = self.bot.api.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