Repository: PokemonGoF/PokemonGo-Bot

Issue:
keep_best_iv should order by iv then cp
### Expected Behavior

If we have 3 pokemon of a type and wanted to keep the 2 best IV and two pokemon have the same low IV, we would like to keep the one with max CP
### Actual Behavior

[20:25:39] Keep 2 best Slowpoke, based on iv
[20:25:39] Slowpoke [CP 274] [Potential 0.56]
[20:25:39] Slowpoke [CP 172] [Potential 0.51]
[20:25:39] Exchange 1 pokemon(s)
[20:25:39] Exchanging Slowpoke [CP 392] [Potential 0.51] for candy!
### Steps to Reproduce

Farm pokemon and have some luck :)
### Other Information

OS: OSX 
Git Commit: (run 'git log -n 1 --pretty=format:"%H"' and paste it here)
34caa002a464b7665cdcfb531ea3d302632898ac



Discussion:

Resolving patch:
diff --git a/pokemongo_bot/cell_workers/pokemon_transfer_worker.py b/pokemongo_bot/cell_workers/pokemon_transfer_worker.py
--- a/pokemongo_bot/cell_workers/pokemon_transfer_worker.py
+++ b/pokemongo_bot/cell_workers/pokemon_transfer_worker.py
@@ -3,6 +3,7 @@
 from pokemongo_bot.human_behaviour import sleep, action_delay
 from pokemongo_bot import logger
 
+
 class PokemonTransferWorker(object):
 
     def __init__(self, bot):
@@ -24,21 +25,31 @@ def work(self):
                 keep_best, keep_best_cp, keep_best_iv = self._validate_keep_best_config(pokemon_name)
 
                 if keep_best:
-                    order_criteria = 'cp'
-                    limit = keep_best_cp
+                    best_pokemon_ids = set()
+                    order_criteria = 'none'
+                    if keep_best_cp >= 1:
+                        cp_limit = keep_best_cp
+                        best_cp_pokemons = sorted(group, key=lambda x: (x['cp'], x['iv']), reverse=True)[:cp_limit]
+                        best_pokemon_ids = set(pokemon['pokemon_data']['id'] for pokemon in best_cp_pokemons)
+                        order_criteria = 'cp'
 
                     if keep_best_iv >= 1:
-                        order_criteria = 'iv'
-                        limit = keep_best_iv
-
-                    best_pokemons = sorted(group, key=lambda x: x[order_criteria], reverse=True)[:limit]
+                        iv_limit = keep_best_iv
+                        best_iv_pokemons = sorted(group, key=lambda x: (x['iv'], x['cp']), reverse=True)[:iv_limit]
+                        best_pokemon_ids |= set(pokemon['pokemon_data']['id'] for pokemon in best_iv_pokemons)
+                        if order_criteria == 'cp':
+                            order_criteria = 'cp and iv'
+                        else:
+                            order_criteria = 'iv'
 
                     # remove best pokemons from all pokemons array
                     all_pokemons = group
-                    for best_pokemon in best_pokemons:
+                    best_pokemons = []
+                    for best_pokemon_id in best_pokemon_ids:
                         for pokemon in all_pokemons:
-                            if best_pokemon['pokemon_data']['id'] == pokemon['pokemon_data']['id']:
+                            if best_pokemon_id == pokemon['pokemon_data']['id']:
                                 all_pokemons.remove(pokemon)
+                                best_pokemons.append(pokemon)
 
                     if best_pokemons and all_pokemons:
                         logger.log("Keep {} best {}, based on {}".format(len(best_pokemons),
@@ -190,10 +201,6 @@ def _validate_keep_best_config(self, pokemon_name):
             except ValueError:
                 keep_best_iv = 0
 
-            if keep_best_cp > 1 and keep_best_iv > 1:
-                logger.log("keep_best_cp and keep_best_iv can't be > 0 at the same time. Ignore it.", "red")
-                keep_best = False
-
             if keep_best_cp < 0 or keep_best_iv < 0:
                 logger.log("Keep best can't be < 0. Ignore it.", "red")
                 keep_best = False


Test patch: