Skip to content

Commit

Permalink
Consolidate similar meaning configuration keys properly inside anothe…
Browse files Browse the repository at this point in the history
…r key (#1590)

* adding support for embedded config keys and fixing circle prevention

* forgot to fix this config

* refactor nested config system to support flag likes `--forts.something.anything`

This example `--forts.something.anything` would be parsed as
`config.forts_something_anything`. And in the JSON config it should be
like this:

```
‘forts’: {
    ‘something’: {
        ‘anything’: 1
    }
}
```

* add fix_nested_config(config) call

* update missing usage of `config.avoid_circles` -> `config.forts_avoid_circles`
  • Loading branch information
douglascamata authored Jul 29, 2016
1 parent a14323c commit 6154939
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
10 changes: 5 additions & 5 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
"gmapkey": "GOOGLE_MAPS_API_KEY",
"max_steps": 5,
"catch_pokemon": true,
"forts": {
"spin": true,
"avoid_circles": true,
"max_circle_size": 50
},
"websocket_server": false,
"spin_forts": true,
"walk": 4.16,
"action_wait_min": 1,
"action_wait_max": 4,
Expand All @@ -33,10 +37,6 @@
"longer_eggs_first": true,
"evolve_captured": false,
"release_pokemon": true,
"spin_forts": {
"avoid_circles": false,
"max_circle_size": 10
},
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or"},
"// Example of always catching Rattata:": {},
Expand Down
10 changes: 5 additions & 5 deletions configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"gmapkey": "GOOGLE_MAPS_API_KEY",
"max_steps": 5,
"catch_pokemon": true,
"spin_forts": true,
"forts": {
"spin": true,
"avoid_circles": true,
"max_circle_size": 50
},
"walk": 4.16,
"action_wait_min": 1,
"action_wait_max": 4,
Expand All @@ -32,10 +36,6 @@
"longer_eggs_first": true,
"evolve_captured": false,
"release_pokemon": true,
"spin_forts": {
"avoid_circles": false,
"max_circle_size": 10
},
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or" },

Expand Down
34 changes: 27 additions & 7 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ def init_config():
add_config(
parser,
load,
long_flag="--spin_forts",
long_flag="--forts.spin",
help="Enable Spinning Pokestops",
type=bool,
default=True
default=True,
)
add_config(
parser,
Expand Down Expand Up @@ -310,19 +310,19 @@ def init_config():
parser,
load,
short_flag="-ac",
long_flag="--avoid_circles",
long_flag="--forts.avoid_circles",
help="Avoids circles (pokestops) of the max size set in max_circle_size flag",
type=bool,
default=False
default=False,
)
add_config(
parser,
load,
short_flag="-mcs",
long_flag="--max_circle_size",
long_flag="--forts.max_circle_size",
help="If avoid_circles flag is set, this flag specifies the maximum size of circles (pokestops) avoided",
type=int,
default=10
default=10,
)

# Start to parse other attrs
Expand Down Expand Up @@ -364,20 +364,40 @@ def init_config():
if config.evolve_all and isinstance(config.evolve_all, str):
config.evolve_all = [str(pokemon_name) for pokemon_name in config.evolve_all.split(',')]

fix_nested_config(config)
import pdb; pdb.set_trace()
return config

def add_config(parser, json_config, short_flag=None, long_flag=None, **kwargs):
if not long_flag:
raise Exception('add_config calls requires long_flag parameter!')

full_attribute_path = long_flag.split('--')[1]
attribute_name = full_attribute_path.split('.')[-1]

if '.' in full_attribute_path: # embedded config!
embedded_in = full_attribute_path.split('.')[0: -1]
for level in embedded_in:
json_config = json_config.get(level, {})

if 'default' in kwargs:
attribute_name = long_flag.split('--')[1]
kwargs['default'] = json_config.get(attribute_name, kwargs['default'])
if short_flag:
args = (short_flag, long_flag)
else:
args = (long_flag,)
parser.add_argument(*args, **kwargs)


def fix_nested_config(config):
config_dict = config.__dict__

for key, value in config_dict.iteritems():
if '.' in key:
new_key = key.replace('.', '_')
config_dict[new_key] = value
del config_dict[key]

def parse_unicode_str(string):
try:
return string.decode('utf8')
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, config):
self.metrics = Metrics(self)
self.latest_inventory = None
self.cell = None
self.recent_forts = [None] * config.max_circle_size
self.recent_forts = [None] * config.forts_max_circle_size
self.tick_count = 0

# Make our own copy of the workers for this instance
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/cell_workers/move_to_fort_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_nearest_fort(self):
forts = filter(lambda x: x["id"] not in self.fort_timeouts, forts)

# Remove all forts which were spun in the last ticks to avoid circles if set
if self.config.avoid_circles:
if self.config.forts_avoid_circles:
forts = filter(lambda x: x["id"] not in self.recent_forts, forts)

if len(forts) > 0:
Expand Down

0 comments on commit 6154939

Please sign in to comment.