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

Commit

Permalink
#31 Get rid of update_resources method 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Aug 20, 2016
1 parent fcd0bce commit 5fdf31d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
19 changes: 9 additions & 10 deletions epicbot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,19 @@ def farm_gift(self, user_id: str) -> Error:
result, _ = self.post("giftFarm", userId=user_id)
return self.parse_error(result)

def collect_resource(self, building_id: int) -> Counter:
def collect_resource(self, building_id: int) -> Tuple[Counter, Counter]:
"""
Collects resource from the building.
"""
result, state = self.post("collectResource", call_state=True, buildingId=building_id)
return self.parse_resource_field(result["reward"]), self.parse_resource_field(state)

def farm_cemetery(self) -> Counter:
def farm_cemetery(self) -> Tuple[Counter, Counter]:
"""
Collects died enemy army.
"""
result, _ = self.post("cemeteryFarm")
return self.parse_resource_field(result["reward"])
result, state = self.post("cemeteryFarm")
return self.parse_resource_field(result["reward"]), self.parse_resource_field(state)

def get_buildings(self) -> List[Building]:
"""
Expand Down Expand Up @@ -176,21 +176,20 @@ def upgrade_building(self, building_id: int) -> Tuple[Error, Optional[Counter],
# TODO: parse and return updated building.
return self.parse_error(result), (self.parse_resource_field(state) if state else None), None

def destruct_building(self, building_id: int, instant: bool):
def destruct_building(self, building_id: int, instant: bool) -> Tuple[Error, Optional[Counter], None]:
"""
Destructs building. Used to clean extended areas.
"""
result, state = self.post("destructBuilding", call_state=True, buildingId=building_id, instant=instant)
logging.info("destructBuilding state: %s", state)
return self.parse_error(result)
# TODO: parse and return updated building.
return self.parse_error(result), (self.parse_resource_field(state) if state else None), None

def start_research(self, unit_id: int, level: int, forge_building_id: int):
def start_research(self, unit_id: int, level: int, forge_building_id: int) -> Tuple[Error, Optional[Counter]]:
"""
Start unit research.
"""
result, state = self.post("startResearch", call_state=True, level=level, unitId=unit_id, buildingId=forge_building_id)
logging.info("startResearch state: %s", state)
return self.parse_error(result)
return self.parse_error(result), (self.parse_resource_field(state) if state else None)

def click_alliance_daily_gift(self):
"""
Expand Down
28 changes: 10 additions & 18 deletions epicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,12 @@ def step(self):
self.log_resources()
logging.info("Made %s requests. Bye!", self.api.request_id)

def update_resources(self):
"""
Updates resources.
"""
# TODO: Deprecated. Should be removed.
self.resources = self.api.get_self_info().resources

def check_cemetery(self):
"""
Farms cemetery.
"""
amount = self.api.farm_cemetery().get(ResourceType.food, 0)
# FIXME: update resources from state.
self.resources[ResourceType.food] += amount
reward, self.resources = self.api.farm_cemetery()
amount = reward.get(ResourceType.food, 0)
logging.info("Cemetery farmed: %s.", amount)
self.notifications.append("Farm \N{MEAT ON BONE} *%s*." % amount)

Expand Down Expand Up @@ -203,16 +195,16 @@ def destruct_extended_areas(self):
self.buildings.castle.level >= self.library.destroy_levels[building.type] and
self.can_upgrade(building.type, building.level)
):
logging.info("Cleaning %s #%s…", building.type.name, building.id)
error = self.api.destruct_building(building.id, False)
logging.info("Destructing %s #%s…", building.type.name, building.id)
error, new_resources, _ = self.api.destruct_building(building.id, False)
if error == Error.ok:
self.update_resources()
self.resources = new_resources
self.buildings = epicbot.managers.Buildings(self.api.get_buildings(), self.library)
self.notifications.append("Destruct *{}*.".format(building.type.name))
# Only one area can be simultaneously destroyed.
# Only one area can be simultaneously destructed.
return
else:
logging.error("Failed to clean extended area.")
logging.error("Failed to destruct extended area.")

def upgrade_units(self):
"""
Expand All @@ -224,11 +216,11 @@ 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 = 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.update_resources()
self.resources = new_resources
self.notifications.append("Upgrade *{}*.".format(unit_type.name))
# One research per time and we've just started a one.
# Only one research can be simultaneously performed.
break
else:
logging.warning("Failed to upgrade: %s.", error.name)
Expand Down

0 comments on commit 5fdf31d

Please sign in to comment.