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

Pokemon optimizer improvements #4446

Merged
merged 5 commits into from
Aug 21, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 3 deletions configs/config.json.optimizer.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"transfer": true,
"// 'transfer_wait_min' and 'transfer_wait_max' are the minimum and maximum": {},
"// time to wait when transferring a pokemon": {},
"transfer_wait_min": 1,
"transfer_wait_min": 2,
"transfer_wait_max": 4,
"// the 'evolve' parameter activate or deactivate the evolution of pokemons": {},
"// at false, no pokemon is going to be evolved, ever": {},
Expand All @@ -83,13 +83,13 @@
"evolve_for_xp": true,
"// the 'evolve_only_with_lucky_egg' parameter let you choose if you want the optimizer": {},
"// to only Evolve Pokemons when a lucky egg is available": {},
"evolve_only_with_lucky_egg": true,
"evolve_only_with_lucky_egg": false,
"// the 'evolve_count_for_lucky_egg' parameter let you define the minimum": {},
"// number of Pokemons that must evolve before using a lucky egg": {},
"// If that number is not reached, and evolve_only_with_lucky_egg is true, evolution will be skipped": {},
"// If that number is not reached, and evolve_only_with_lucky_egg is false,": {},
"// evolution will be performed without using a lucky egg": {},
"evolve_count_for_lucky_egg": 90,
"evolve_count_for_lucky_egg": 92,
"// the 'may_use_lucky_egg' parameter let you choose if you want the optimizer": {},
"// to use a lucky egg right before evolving Pokemons. At false; the optimizer": {},
"// is free to evolve Pokemons even if you do not have any lucky egg.": {},
Expand Down
47 changes: 34 additions & 13 deletions pokemongo_bot/cell_workers/pokemon_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def initialize(self):
self.max_pokemon_storage = inventory.get_pokemon_inventory_size()
self.last_pokemon_count = 0

self.config_transfer = self.config.get("transfer", False)
self.config_evolve = self.config.get("evolve", False)
self.config_transfer = self.config.get("transfer", True)
Copy link
Contributor

@mjmadsen mjmadsen Aug 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer things to be off by default.

If we use the default value, they are probably missing important stuff in their config.

self.config_evolve = self.config.get("evolve", True)
self.config_evolve_time = self.config.get("evolve_time", 20)
self.config_evolve_for_xp = self.config.get("evolve_for_xp", True)
self.config_evolve_only_with_lucky_egg = self.config.get("evolve_only_with_lucky_egg", False)
self.config_evolve_count_for_lucky_egg = self.config.get("evolve_count_for_lucky_egg", 90)
self.config_may_use_lucky_egg = self.config.get("may_use_lucky_egg", False)
self.config_evolve_count_for_lucky_egg = self.config.get("evolve_count_for_lucky_egg", 92)
self.config_may_use_lucky_egg = self.config.get("may_use_lucky_egg", True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we revert this as well?

self.config_keep = self.config.get("keep", [{"top": 1, "evolve": True, "sort": ["iv"]},
{"top": 1, "evolve": True, "sort": ["ncp"]},
{"top": 1, "evolve": False, "sort": ["cp"]}])
Expand All @@ -49,6 +49,7 @@ def get_pokemon_slot_left(self):
if pokemon_count != self.last_pokemon_count:
self.last_pokemon_count = pokemon_count
self.logger.info("Pokemon Bag: %s/%s", pokemon_count, self.max_pokemon_storage)
self.save_web_inventory()

return inventory.Pokemons.get_space_left()

Expand All @@ -57,7 +58,6 @@ def work(self):
return WorkerResult.SUCCESS

self.open_inventory()
self.save_web_inventory()

transfer_all = []
evo_all_best = []
Expand All @@ -76,6 +76,7 @@ def work(self):
evo_all = evo_all_best + evo_all_crap

self.apply_optimization(transfer_all, evo_all)
self.save_web_inventory()

return WorkerResult.SUCCESS

Expand All @@ -92,11 +93,30 @@ def open_inventory(self):
self.family_by_family_id.setdefault(family_id, []).append(pokemon)

def save_web_inventory(self):
inventory_items = self.bot.api.get_inventory()["responses"]["GET_INVENTORY"]["inventory_delta"]["inventory_items"]
web_inventory = os.path.join(_base_dir, "web", "inventory-%s.json" % self.bot.config.username)

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

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

for pokedex in inventory.pokedex().all():
ii.append({"inventory_item_data": {"pokedex_entry": pokedex}})

for family_id, candy in inventory.candies()._data.items():
ii.append({"inventory_item_data": {"candy": {"family_id": family_id, "candy": candy.quantity}}})

for item_id, item in inventory.items()._data.items():
ii.append({"inventory_item_data": {"item": {"item_id": item_id, "count": item.count}}})

for pokemon in inventory.pokemons().all():
ii.append({"inventory_item_data": {"pokemon_data": pokemon._data}})

with open(web_inventory, "w") as outfile:
json.dump(inventory_items, outfile)
json.dump(ii, outfile)

def get_family_optimized(self, family_id, family):
evolve_best = []
Expand Down Expand Up @@ -206,6 +226,7 @@ def get_evolution_plan(self, family_id, family, evolve_best, keep_best):
crap = [p for p in crap if not p.in_fort and not p.is_favorite]
crap.sort(key=lambda p: p.iv * p.ncp, reverse=True)

# We will gain a candy whether we choose to transfer or evolve these Pokemon
candies += len(crap)

# Let's see if we can evolve our best Pokemon
Expand All @@ -218,8 +239,9 @@ def get_evolution_plan(self, family_id, family, evolve_best, keep_best):
candies -= pokemon.evolution_cost

if candies < 0:
continue
break

candies += 1
can_evolve_best.append(pokemon)

# Not sure if the evo keep the same id
Expand All @@ -236,13 +258,12 @@ def get_evolution_plan(self, family_id, family, evolve_best, keep_best):

# transfer + keep_for_evo = len(crap)
# leftover_candies = candies - len(crap) + transfer * 1
# keep_for_evo = leftover_candies / junior_evolution_cost
#
# keep_for_evo = (candies - len(crap) + transfer) / junior_evolution_cost
# keep_for_evo = (candies - keep_for_evo) / junior_evolution_cost
# keep_for_evo = (leftover_candies - 1) / (junior_evolution_cost - 1)
# keep_for_evo = (candies - len(crap) + transfer - 1) / (junior_evolution_cost - 1)
# keep_for_evo = (candies - keep_for_evo - 1) / (junior_evolution_cost - 1)

if (candies > 0) and junior_evolution_cost:
keep_for_evo = int(candies / (junior_evolution_cost + 1))
keep_for_evo = int((candies - 1) / junior_evolution_cost)
else:
keep_for_evo = 0

Expand Down