diff --git a/erepublik/citizen.py b/erepublik/citizen.py index 412065e..988f536 100644 --- a/erepublik/citizen.py +++ b/erepublik/citizen.py @@ -829,7 +829,8 @@ class Citizen(CitizenAPI): if not isinstance(battle_id, int): self.report_error(f"WARNINNG! Parameter battle_id should be 'int', but it is '{type(battle_id).__name__}'") battle_id = int(battle_id) - + if battle_id not in self.all_battles: + self.update_war_info() battle = self.all_battles[battle_id] zone_id = battle.div[11 if battle.is_air else self.division].battle_zone_id if not battle.is_air and self.config.boosters: @@ -2045,7 +2046,7 @@ class Citizen(CitizenAPI): def report_error(self, msg: str = "", is_warning: bool = False): if is_warning: - process_warning(msg, self.name, sys.exc_info(), self, self.commit_id, None) + process_warning(msg, self.name, sys.exc_info(), self, self.commit_id) else: process_error(msg, self.name, sys.exc_info(), self, self.commit_id, None) diff --git a/erepublik/classes.py b/erepublik/classes.py index 0f4585d..c6b4ebc 100644 --- a/erepublik/classes.py +++ b/erepublik/classes.py @@ -4,7 +4,7 @@ import hashlib import random import threading import time -from collections import deque +from collections import deque, defaultdict from json import JSONDecodeError, loads, JSONEncoder from typing import Any, Dict, List, Union, Mapping, Iterable, Tuple @@ -1099,17 +1099,25 @@ class BattleDivision: def div_end(self) -> bool: return utils.now() >= self.end - def __init__( - self, div_id: int, end: datetime.datetime, epic: bool, inv_pts: int, def_pts: int, - wall_for: int, wall_dom: float, def_medal: Tuple[int, int], inv_medal: Tuple[int, int] - ): - self.battle_zone_id = div_id - self.end = end - self.epic = epic - self.dom_pts = dict({"inv": inv_pts, "def": def_pts}) - self.wall = dict({"for": wall_for, "dom": wall_dom}) - self.def_medal = {"id": def_medal[0], "dmg": def_medal[1]} - self.inv_medal = {"id": inv_medal[0], "dmg": inv_medal[1]} + def __init__(self, **kwargs): + """Battle division helper class + + :param kwargs: must contain keys: + div_id: int, end: datetime.datetime, epic: bool, inv_pts: int, def_pts: int, + wall_for: int, wall_dom: float, def_medal: Tuple[int, int], inv_medal: Tuple[int, int] + """ + + self.battle_zone_id = kwargs.get("div_id", 0) + self.end = kwargs.get("end", 0) + self.epic = kwargs.get("epic", 0) + self.dom_pts = dict({"inv": kwargs.get("inv_pts", 0), "def": kwargs.get("def_pts", 0)}) + self.wall = dict({"for": kwargs.get("wall_for", 0), "dom": kwargs.get("wall_dom", 0)}) + self.def_medal = {"id": kwargs.get("def_medal", 0)[0], "dmg": kwargs.get("def_medal", 0)[1]} + self.inv_medal = {"id": kwargs.get("inv_medal", 0)[0], "dmg": kwargs.get("inv_medal", 0)[1]} + + @property + def id(self): + return self.battle_zone_id class Battle: @@ -1164,7 +1172,7 @@ class Battle: [row.get('id') for row in battle.get('def', {}).get('ally_list') if row['deployed']] ) - self.div = {} + self.div = defaultdict(BattleDivision) for div, data in battle.get('div', {}).items(): div = int(data.get('div')) if data.get('end'): diff --git a/erepublik/utils.py b/erepublik/utils.py index f3199da..06dbcea 100644 --- a/erepublik/utils.py +++ b/erepublik/utils.py @@ -292,11 +292,17 @@ def process_error(log_info: str, name: str, exc_info: tuple, citizen=None, commi """ Process error logging and email sending to developer :param interactive: Should print interactively + :type interactive: bool :param log_info: String to be written in output + :type log_info: str :param name: String Instance name + :type name: str :param exc_info: tuple output from sys.exc_info() + :type exc_info: tuple :param citizen: Citizen instance + :type citizen: Citizen :param commit_id: Code's version by commit id + :type commit_id: str """ type_, value_, traceback_ = exc_info content = [log_info]