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

Commit

Permalink
Fix TypeError when state is []
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Aug 20, 2016
1 parent e9de499 commit fcd0bce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
21 changes: 19 additions & 2 deletions epicbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import logging
import os
import time
import traceback
import typing

import click
import requests

import epicbot.api
import epicbot.bastion
Expand Down Expand Up @@ -78,8 +80,23 @@ def step(obj: epicbot.utils.Context, with_castle: bool, with_bastion: bool, min_
api.authenticate()
epicbot.bot.Bot(obj, api, library).step()
except Exception as ex:
if not isinstance(ex, click.ClickException):
logging.critical("Critical error.", exc_info=ex)
# Skip expected CLI exceptions.
if isinstance(ex, click.ClickException):
raise
# Log the error since it will be caught by click.
logging.critical("Critical error.", exc_info=ex)
# Send Telegram notification if enabled.
if not obj.telegram_enabled:
raise
requests.get(
"https://api.telegram.org/bot%s/sendMessage" % obj.telegram_token,
params={
"chat_id": obj.telegram_chat_id,
"text": "\N{cross mark} *Critical error*:\n\n```\n%s\n```" % traceback.format_exc(),
"parse_mode": "markdown",
},
)
# Finally propagate it up.
raise


Expand Down
9 changes: 5 additions & 4 deletions epicbot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ def get_buildings(self) -> List[Building]:
if BuildingType.has_value(building["typeId"])
]

def upgrade_building(self, building_id: int) -> Tuple[Error, Counter, None]:
def upgrade_building(self, building_id: int) -> Tuple[Error, Optional[Counter], None]:
"""
Upgrades building to the next level.
"""
result, state = self.post("upgradeBuilding", call_state=True, buildingId=building_id)
# TODO: parse and return updated building.
return self.parse_error(result), self.parse_resource_field(state), None
return self.parse_error(result), (self.parse_resource_field(state) if state else None), None

def destruct_building(self, building_id: int, instant: bool):
"""
Expand Down Expand Up @@ -278,12 +278,12 @@ def add_battle_commands(self, battle_id: str, commands: str) -> Error:
result, _ = self.post("battle_addCommands", battleId=battle_id, commands=commands)
return self.parse_error(result)

def finish_battle(self, battle_id: str, commands: str) -> str:
def finish_battle(self, battle_id: str, commands: str) -> Tuple[str, Optional[Counter]]:
"""
Finishes battle and returns serialized battle result.
"""
result, state = self.post("battle_finish", call_state=True, battleId=battle_id, commands=commands)
return result["battleResult"], self.parse_resource_field(state)
return result["battleResult"], (self.parse_resource_field(state) if state else None)

def open_fair_citadel_gate(self):
"""
Expand Down Expand Up @@ -330,6 +330,7 @@ def parse_resource_field(self, result: Optional[dict]) -> Counter:
"""
Helper method to parse resource collection result.
"""
assert isinstance(result, dict) and "resource" in result
return self.parse_resources(result["resource"])

@staticmethod
Expand Down
4 changes: 3 additions & 1 deletion epicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ 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
logging.info("Cemetery farmed: %s.", amount)
self.notifications.append("Farm \N{MEAT ON BONE} *%s*." % amount)
Expand Down Expand Up @@ -316,8 +317,9 @@ def play_bastion(self):
logging.info("Sleeping…")
time.sleep(self.BASTION_DURATION)
logging.info("Sending commands…")
battle_result, self.resources = self.api.finish_battle(bastion.battle_id, replay.commands)
battle_result, new_resources = self.api.finish_battle(bastion.battle_id, replay.commands)
logging.info("Battle result: %s.", battle_result)
self.resources = new_resources or self.resources

runes_farmed = self.resources[ResourceType.runes] - old_runes_count
logging.info("Farmed %s of %s runes.", runes_farmed, replay.runes)
Expand Down

0 comments on commit fcd0bce

Please sign in to comment.