Skip to content
This repository has been archived by the owner on Nov 12, 2018. It is now read-only.

Commit

Permalink
#31 Expose only needed values
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Aug 20, 2016
1 parent 5fdf31d commit 3e31ecc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions epicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def destruct_extended_areas(self):
if (
building.type in BuildingType.extended_areas() and
building.is_completed and
self.buildings.castle.level >= self.library.destroy_levels[building.type] and
self.buildings.castle_level >= self.library.destroy_levels[building.type] and
self.can_upgrade(building.type, building.level)
):
logging.info("Destructing %s #%s…", building.type.name, building.id)
Expand All @@ -216,7 +216,7 @@ def upgrade_units(self):
if unit_type not in UnitType.upgradable() or not self.can_upgrade(unit_type, level + 1):
continue
logging.info("Upgrading unit %s to level %s…", unit_type.name, level + 1)
error, new_resources = self.api.start_research(unit_type.value, level + 1, self.buildings.forge.id)
error, new_resources = self.api.start_research(unit_type.value, level + 1, self.buildings.forge_id)
if error == Error.ok:
self.resources = new_resources
self.notifications.append("Upgrade *{}*.".format(unit_type.name))
Expand Down
15 changes: 10 additions & 5 deletions epicbot/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging

from collections import OrderedDict
from operator import itemgetter
from typing import Any, Callable, Iterable, Iterator

Expand All @@ -16,10 +17,14 @@ class Buildings:
Building manager.
"""
def __init__(self, buildings: Iterable[Building], library: Library):
self.buildings = sorted(buildings, key=self.sorting_key(library))
# Special buildings.
self.castle = next(building for building in self.buildings if building.type == BuildingType.castle)
self.forge = next(building for building in self.buildings if building.type == BuildingType.forge)
# Keep track of all buildings by building ID.
self.buildings = OrderedDict(
(building.id, building)
for building in sorted(buildings, key=self.sorting_key(library))
)
# Cache some frequently used values.
self.castle_level = next(building.level for building in buildings if building.type == BuildingType.castle)
self.forge_id = next(building.id for building in buildings if building.type == BuildingType.forge)
# Build caches.
self.max_level = dict(sorted(
[(building.type, building.level) for building in buildings],
Expand All @@ -36,7 +41,7 @@ def __init__(self, buildings: Iterable[Building], library: Library):
)

def __iter__(self) -> Iterator[Building]:
return iter(self.buildings)
return iter(self.buildings.values())

@staticmethod
def sorting_key(library: Library) -> Callable[[Building], Any]:
Expand Down

0 comments on commit 3e31ecc

Please sign in to comment.