From 6c8dbcd99e3d3738b5ec811e1307f7f0f9da28b0 Mon Sep 17 00:00:00 2001 From: Eriks K Date: Sat, 4 Jul 2020 12:28:02 +0300 Subject: [PATCH] Updated fighting division chooser --- erepublik/__init__.py | 2 +- erepublik/citizen.py | 26 ++++++++++++++++---------- erepublik/classes.py | 15 ++++++++++----- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/erepublik/__init__.py b/erepublik/__init__.py index 52ee1f7..e2a8083 100644 --- a/erepublik/__init__.py +++ b/erepublik/__init__.py @@ -5,7 +5,7 @@ __author__ = """Eriks Karls""" __email__ = 'eriks@72.lv' __version__ = '0.20.2' -__commit_id__ = "bcb27da" +__commit_id__ = "d07334f" from erepublik import classes, utils from erepublik.citizen import Citizen diff --git a/erepublik/citizen.py b/erepublik/citizen.py index 8db89de..d78f172 100644 --- a/erepublik/citizen.py +++ b/erepublik/citizen.py @@ -13,7 +13,7 @@ from requests import HTTPError, RequestException, Response from erepublik import utils from erepublik.access_points import CitizenAPI from erepublik.classes import Battle, Config, Details, Energy, \ - ErepublikException, MyCompanies, MyJSONEncoder, OfferItem, Politics, Reporter, TelegramBot + ErepublikException, MyCompanies, MyJSONEncoder, OfferItem, Politics, Reporter, TelegramBot, BattleDivision class BaseCitizen(CitizenAPI): @@ -1395,7 +1395,7 @@ class CitizenMilitary(CitizenTravel): available_weapons = self._get_military_show_weapons(battle_id).json() weapon_quality = -1 weapon_damage = 0 - if not battle.div[battle_zone].div == 11: + if not battle.div[battle_zone].is_air: for weapon in available_weapons: try: if weapon['weaponQuantity'] > 30 and weapon['weaponInfluence'] > weapon_damage: @@ -1540,17 +1540,21 @@ class CitizenMilitary(CitizenTravel): battle = self.all_battles.get(battle_id) if not isinstance(battle, Battle): continue - battle_zone = 0 + battle_zone: Optional[BattleDivision] = None for div in battle.div.values(): if div.terrain == 0: + if div.div_end: + continue if self.config.air and div.is_air: - battle_zone = div.id + battle_zone = div break elif self.config.ground and not div.is_air and div.div == self.division: - battle_zone = div.id + battle_zone = div break else: - return + continue + if not battle_zone: + continue allies = battle.invader.deployed + battle.defender.deployed + [battle.invader.id, battle.defender.id] travel_needed = self.details.current_country not in allies @@ -1584,8 +1588,9 @@ class CitizenMilitary(CitizenTravel): if not self.travel_to_battle(battle_id, country_ids_to_travel): break - self.set_default_weapon(battle_id, battle_zone) - self.fight(battle_id, battle_zone, side_id) + self.change_division(battle_id, battle_zone.id) + self.set_default_weapon(battle_id, battle_zone.id) + self.fight(battle_id, battle_zone.id, side_id) self.travel_to_residence() break @@ -1734,8 +1739,9 @@ class CitizenMilitary(CitizenTravel): :return: """ battle = self.all_battles.get(battle_id) - self._post_main_battlefield_change_division(battle_id, battle.div[division_to].battle_zone_id) - self._report_action("MILITARY_DIV_SWITCH", f"Switched to d{division_to} in battle {battle_id}") + division = battle.div[division_to] + self._post_main_battlefield_change_division(battle_id, division_to) + self._report_action("MILITARY_DIV_SWITCH", f"Switched to d{division.div} in battle {battle_id}") def get_ground_hit_dmg_value(self, rang: int = None, strength: float = None, elite: bool = None, ne: bool = False, booster_50: bool = False, booster_100: bool = False, tp: bool = True) -> Decimal: diff --git a/erepublik/classes.py b/erepublik/classes.py index cbed805..c22cc12 100644 --- a/erepublik/classes.py +++ b/erepublik/classes.py @@ -621,8 +621,10 @@ class BattleSide: points: int deployed: List[int] = None allies: List[int] = None + battle: "Battle" - def __init__(self, country_id: int, points: int, allies: List[int], deployed: List[int]): + def __init__(self, battle: "Battle", country_id: int, points: int, allies: List[int], deployed: List[int]): + self.battle = battle self.id = country_id self.points = points self.allies = [int(ally) for ally in allies] @@ -639,6 +641,7 @@ class BattleDivision: inv_medal: Dict[str, int] terrain: int div: int + battle: "Battle" @property def is_air(self): @@ -648,7 +651,7 @@ class BattleDivision: def div_end(self) -> bool: return utils.now() >= self.end - def __init__(self, div_id: int, end: datetime.datetime, epic: bool, div: int, wall_for: int, wall_dom: float, + def __init__(self, battle: "Battle", div_id: int, end: datetime.datetime, epic: bool, div: int, wall_for: int, wall_dom: float, terrain_id: int = 0): """Battle division helper class @@ -660,7 +663,7 @@ class BattleDivision: :type wall_for: int :type wall_dom: float """ - + self.battle = battle self.battle_zone_id = div_id self.end = end self.epic = epic @@ -713,12 +716,14 @@ class Battle: self.start = datetime.datetime.fromtimestamp(int(battle.get('start', 0)), tz=utils.erep_tz) self.invader = BattleSide( + self, battle.get('inv', {}).get('id'), battle.get('inv', {}).get('points'), [row.get('id') for row in battle.get('inv', {}).get('ally_list')], [row.get('id') for row in battle.get('inv', {}).get('ally_list') if row['deployed']] ) self.defender = BattleSide( + self, battle.get('def', {}).get('id'), battle.get('def', {}).get('points'), [row.get('id') for row in battle.get('def', {}).get('ally_list')], [row.get('id') for row in battle.get('def', {}).get('ally_list') if row['deployed']] @@ -726,13 +731,13 @@ class Battle: self.div = {} for div, data in battle.get('div', {}).items(): - div = int(data.get('div')) + div = int(div) if data.get('end'): end = datetime.datetime.fromtimestamp(data.get('end'), tz=utils.erep_tz) else: end = utils.localize_dt(datetime.datetime.max - datetime.timedelta(days=1)) - battle_div = BattleDivision(div_id=data.get('id'), div=data.get('dev'), end=end, + battle_div = BattleDivision(self, div_id=data.get('id'), div=data.get('div'), end=end, epic=data.get('epic_type') in [1, 5], wall_for=data.get('wall').get("for"), wall_dom=data.get('wall').get("dom"),