Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
684b2a6ba4 | |||
447aee8134 | |||
64249fc3d9 | |||
95a78b6e7e | |||
b338ea598a |
13
HISTORY.rst
13
HISTORY.rst
@ -2,8 +2,21 @@
|
||||
History
|
||||
=======
|
||||
|
||||
0.23.2 (2020-12-01)
|
||||
-------------------
|
||||
* Added concurrency checks to guard against simultaneous fighting/wam'ing/traveling *(Note: Probably will make it into a decorator at some time)*
|
||||
|
||||
0.23.1 (2020-12-01)
|
||||
-------------------
|
||||
* Separated battle finding logic from CitizenMilitary.find_battle_and_fight method
|
||||
* Base dmg calculations
|
||||
* Get max hit value for divisions on current side
|
||||
* Added method to get division stats
|
||||
* Wheel of fortune updates
|
||||
|
||||
0.23.0 (2020-11-26)
|
||||
-------------------
|
||||
* ***0.23 - last supported version for Python 3.7.***
|
||||
* Added `Config.maverick` switch, to allow/deny automated fighting in non native divisions if the player has MaverickPack
|
||||
* Added `CitizenMedia.get_article(article_id:int)` method to get article data
|
||||
* Added `CitizenMedia.delete_article(article_id:int)` method to delete article
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
__author__ = """Eriks Karls"""
|
||||
__email__ = 'eriks@72.lv'
|
||||
__version__ = '0.23.1.1'
|
||||
__version__ = '0.23.2'
|
||||
|
||||
from erepublik import classes, utils, constants
|
||||
from erepublik.citizen import Citizen
|
||||
|
@ -35,12 +35,14 @@ class BaseCitizen(access_points.CitizenAPI):
|
||||
eday: int = 0
|
||||
wheel_of_fortune: bool
|
||||
|
||||
debug: bool = False
|
||||
config: classes.Config = None
|
||||
energy: classes.Energy = None
|
||||
details: classes.Details = None
|
||||
politics: classes.Politics = None
|
||||
reporter: classes.Reporter = None
|
||||
stop_threads: Event = None
|
||||
concurrency_lock: Event = None
|
||||
telegram: classes.TelegramBot = None
|
||||
|
||||
r: Response = None
|
||||
@ -57,6 +59,7 @@ class BaseCitizen(access_points.CitizenAPI):
|
||||
self.my_companies = classes.MyCompanies(self)
|
||||
self.reporter = classes.Reporter(self)
|
||||
self.stop_threads = Event()
|
||||
self.concurrency_lock = Event()
|
||||
self.telegram = classes.TelegramBot(stop_event=self.stop_threads)
|
||||
|
||||
self.config.email = email
|
||||
@ -421,6 +424,19 @@ class BaseCitizen(access_points.CitizenAPI):
|
||||
else:
|
||||
sleep(seconds)
|
||||
|
||||
def set_debug(self, debug: bool):
|
||||
self.debug = bool(debug)
|
||||
self._req.debug = bool(debug)
|
||||
|
||||
def __wait_for_concurrency_cleared(self) -> bool:
|
||||
self.concurrency_lock.wait(600)
|
||||
if self.concurrency_lock.is_set():
|
||||
self.write_log('Unable to acquire concurrency lock in 10min!')
|
||||
if self.debug:
|
||||
self.report_error("Lock not released in 10min!")
|
||||
return False
|
||||
return True
|
||||
|
||||
def to_json(self, indent: bool = False) -> str:
|
||||
return utils.json.dumps(self, cls=classes.MyJSONEncoder, indent=4 if indent else None)
|
||||
|
||||
@ -931,6 +947,9 @@ class CitizenCompanies(BaseCitizen):
|
||||
def _work_as_manager(self, wam_holding: classes.Holding) -> Optional[Dict[str, Any]]:
|
||||
if self.restricted_ip:
|
||||
return None
|
||||
if not self.__wait_for_concurrency_cleared():
|
||||
return
|
||||
self.concurrency_lock.set()
|
||||
self.update_companies()
|
||||
data = {"action_type": "production"}
|
||||
extra = {}
|
||||
@ -952,6 +971,7 @@ class CitizenCompanies(BaseCitizen):
|
||||
data.update(extra)
|
||||
if not self.details.current_region == wam_holding.region:
|
||||
self.write_log("Unable to work as manager because of location - please travel!")
|
||||
self.concurrency_lock.clear()
|
||||
return
|
||||
|
||||
employ_factories = self.my_companies.get_employable_factories()
|
||||
@ -960,6 +980,7 @@ class CitizenCompanies(BaseCitizen):
|
||||
|
||||
response = self._post_economy_work("production", wam=[c.id for c in wam_list],
|
||||
employ=employ_factories).json()
|
||||
self.concurrency_lock.clear()
|
||||
return response
|
||||
|
||||
def update_companies(self):
|
||||
@ -1030,6 +1051,9 @@ class CitizenEconomy(CitizenTravel):
|
||||
|
||||
global_cheapest = self.get_market_offers("House", q)[f"q{q}"]
|
||||
if global_cheapest.price + 200 < local_cheapest.price:
|
||||
if not self.__wait_for_concurrency_cleared():
|
||||
return self.check_house_durability()
|
||||
self.concurrency_lock.set()
|
||||
if self.travel_to_country(global_cheapest.country):
|
||||
buy = self.buy_from_market(global_cheapest.offer_id, 1)
|
||||
else:
|
||||
@ -1047,6 +1071,7 @@ class CitizenEconomy(CitizenTravel):
|
||||
self.activate_house(q)
|
||||
if original_region[1] != self.details.current_region:
|
||||
self._travel(*original_region)
|
||||
self.concurrency_lock.clear()
|
||||
return self.check_house_durability()
|
||||
|
||||
def renew_houses(self, forced: bool = False) -> Dict[int, datetime]:
|
||||
@ -1183,6 +1208,9 @@ class CitizenEconomy(CitizenTravel):
|
||||
amount = offer.amount
|
||||
traveled = False
|
||||
if not self.details.current_country == offer.country:
|
||||
if not self.__wait_for_concurrency_cleared():
|
||||
return {'error': True, 'message': 'Concurrency locked for travel'}
|
||||
self.concurrency_lock.set()
|
||||
traveled = True
|
||||
self.travel_to_country(offer.country)
|
||||
ret = self._post_economy_marketplace_actions('buy', offer=offer.offer_id, amount=amount)
|
||||
@ -1194,6 +1222,7 @@ class CitizenEconomy(CitizenTravel):
|
||||
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=json_ret)
|
||||
if traveled:
|
||||
self.travel_to_residence()
|
||||
self.concurrency_lock.clear()
|
||||
return json_ret
|
||||
|
||||
def get_market_offers(
|
||||
@ -1673,46 +1702,57 @@ class CitizenMilitary(CitizenTravel):
|
||||
def has_battle_contribution(self):
|
||||
return bool(self.__last_war_update_data.get("citizen_contribution", []))
|
||||
|
||||
def find_battle_to_fight(self, silent: bool = False) -> Tuple[classes.Battle, classes.BattleDivision, classes.BattleSide]:
|
||||
self.update_war_info()
|
||||
for battle in self.sorted_battles(self.config.sort_battles_time):
|
||||
if not isinstance(battle, classes.Battle):
|
||||
continue
|
||||
battle_zone: Optional[classes.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
|
||||
break
|
||||
elif self.config.ground and not div.is_air and (div.div == self.division or (self.maverick and self.config.maverick)):
|
||||
battle_zone = div
|
||||
break
|
||||
else:
|
||||
continue
|
||||
if not battle_zone:
|
||||
continue
|
||||
allies = battle.invader.deployed + battle.defender.deployed + [battle.invader.country,
|
||||
battle.defender.country]
|
||||
|
||||
travel_needed = self.details.current_country not in allies
|
||||
|
||||
if battle.is_rw:
|
||||
side = battle.defender if self.config.rw_def_side else battle.invader
|
||||
else:
|
||||
defender_side = self.details.current_country in battle.defender.allies + [battle.defender.country, ]
|
||||
side = battle.defender if defender_side else battle.invader
|
||||
|
||||
if not silent:
|
||||
self.write_log(battle)
|
||||
|
||||
travel = (self.config.travel_to_fight and self.should_travel_to_fight() or self.config.force_travel) \
|
||||
if travel_needed else True
|
||||
|
||||
if not travel:
|
||||
continue
|
||||
yield battle, battle_zone, side
|
||||
|
||||
def find_battle_and_fight(self):
|
||||
if self.should_fight()[0]:
|
||||
self.write_log("Checking for battles to fight in...")
|
||||
for battle in self.sorted_battles(self.config.sort_battles_time):
|
||||
if not isinstance(battle, classes.Battle):
|
||||
continue
|
||||
battle_zone: Optional[classes.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
|
||||
break
|
||||
elif self.config.ground and not div.is_air and (div.div == self.division or self.maverick):
|
||||
battle_zone = div
|
||||
break
|
||||
else:
|
||||
continue
|
||||
if not battle_zone:
|
||||
continue
|
||||
for battle, division, side in self.find_battle_to_fight():
|
||||
|
||||
allies = battle.invader.deployed + battle.defender.deployed + [battle.invader.country,
|
||||
battle.defender.country]
|
||||
|
||||
travel_needed = self.details.current_country not in allies
|
||||
|
||||
if battle.is_rw:
|
||||
side = battle.defender if self.config.rw_def_side else battle.invader
|
||||
else:
|
||||
defender_side = self.details.current_country in battle.defender.allies + [battle.defender.country, ]
|
||||
side = battle.defender if defender_side else battle.invader
|
||||
|
||||
self.write_log(battle)
|
||||
|
||||
travel = (self.config.travel_to_fight and self.should_travel_to_fight() or self.config.force_travel) \
|
||||
if travel_needed else True
|
||||
|
||||
if not travel:
|
||||
continue
|
||||
|
||||
if battle.start > self.now:
|
||||
self.sleep(utils.get_sleep_seconds(battle.start))
|
||||
|
||||
@ -1728,10 +1768,15 @@ class CitizenMilitary(CitizenTravel):
|
||||
|
||||
if not self.travel_to_battle(battle, countries_to_travel):
|
||||
break
|
||||
if self.change_division(battle, battle_zone):
|
||||
self.set_default_weapon(battle, battle_zone)
|
||||
self.fight(battle, battle_zone, side)
|
||||
|
||||
if not self.__wait_for_concurrency_cleared():
|
||||
return
|
||||
self.concurrency_lock.set()
|
||||
if self.change_division(battle, division):
|
||||
self.set_default_weapon(battle, division)
|
||||
self.fight(battle, division, side)
|
||||
self.travel_to_residence()
|
||||
self.concurrency_lock.clear()
|
||||
break
|
||||
|
||||
def fight(self, battle: classes.Battle, division: classes.BattleDivision, side: classes.BattleSide = None,
|
||||
@ -1847,6 +1892,10 @@ class CitizenMilitary(CitizenTravel):
|
||||
:return: Deployed count
|
||||
:rtype: int
|
||||
"""
|
||||
|
||||
if not self.__wait_for_concurrency_cleared():
|
||||
return 0
|
||||
self.concurrency_lock.set()
|
||||
if not isinstance(count, int) or count < 1:
|
||||
count = 1
|
||||
has_traveled = False
|
||||
@ -1880,6 +1929,7 @@ class CitizenMilitary(CitizenTravel):
|
||||
self.travel_to_residence()
|
||||
|
||||
self._report_action("MILITARY_BOMB", f"Deployed {deployed_count} bombs in battle {battle.id}")
|
||||
self.concurrency_lock.clear()
|
||||
return deployed_count
|
||||
|
||||
def change_division(self, battle: classes.Battle, division: classes.BattleDivision) -> bool:
|
||||
@ -2382,8 +2432,6 @@ class CitizenTasks(BaseCitizen):
|
||||
|
||||
class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeaderBoard,
|
||||
CitizenMedia, CitizenMilitary, CitizenPolitics, CitizenSocial, CitizenTasks):
|
||||
debug: bool = False
|
||||
|
||||
def __init__(self, email: str = "", password: str = "", auto_login: bool = False):
|
||||
super().__init__(email, password)
|
||||
self._last_full_update = constants.min_datetime
|
||||
@ -2483,10 +2531,6 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
|
||||
for info in data.values():
|
||||
self.reporter.report_action("NEW_MEDAL", info)
|
||||
|
||||
def set_debug(self, debug: bool):
|
||||
self.debug = bool(debug)
|
||||
self._req.debug = bool(debug)
|
||||
|
||||
def set_pin(self, pin: str):
|
||||
self.details.pin = str(pin[:4])
|
||||
|
||||
@ -2563,7 +2607,7 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
|
||||
start_time = utils.good_timedelta(start_time, timedelta(minutes=30))
|
||||
self.send_state_update()
|
||||
self.send_inventory_update()
|
||||
self.reporter.report_action('COMPANIES', json_val=self.my_companies.as_dict)
|
||||
self.send_my_companies_update()
|
||||
sleep_seconds = (start_time - self.now).total_seconds()
|
||||
self.stop_threads.wait(sleep_seconds if sleep_seconds > 0 else 0)
|
||||
except: # noqa
|
||||
@ -2579,6 +2623,9 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
|
||||
def send_inventory_update(self):
|
||||
self.reporter.report_action("INVENTORY", json_val=self.get_inventory(True))
|
||||
|
||||
def send_my_companies_update(self):
|
||||
self.reporter.report_action('COMPANIES', json_val=self.my_companies.as_dict)
|
||||
|
||||
def eat(self):
|
||||
"""
|
||||
Try to eat food
|
||||
|
@ -719,11 +719,11 @@ class BattleSide:
|
||||
|
||||
def __repr__(self):
|
||||
side_text = "Defender" if self.is_defender else "Invader "
|
||||
return f"<BattleSide: {side_text} {self.country.name}|{self.points:02d}p>"
|
||||
return f"<BattleSide: {side_text} {self.country.name}|{self.points:>2d}p>"
|
||||
|
||||
def __str__(self):
|
||||
side_text = "Defender" if self.is_defender else "Invader "
|
||||
return f"{side_text} {self.country.name} - {self.points:02d} points"
|
||||
return f"{side_text} {self.country.name} - {self.points:>2d} points"
|
||||
|
||||
def __format__(self, format_spec):
|
||||
return self.country.iso
|
||||
@ -789,7 +789,7 @@ class BattleDivision:
|
||||
return constants.TERRAINS[self.terrain]
|
||||
|
||||
def __str__(self):
|
||||
base_name = f"Div #{self.id} d{self.div}"
|
||||
base_name = f"D{self.div} #{self.id}"
|
||||
if self.terrain:
|
||||
base_name += f" ({self.terrain_display})"
|
||||
if self.div_end:
|
||||
@ -899,8 +899,8 @@ class Battle:
|
||||
else:
|
||||
time_part = "-{}".format(self.start - time_now)
|
||||
|
||||
return (f"Battle {self.id} for {self.region_name[:16]} | "
|
||||
f"{self.invader} : {self.defender} | Round time {time_part}")
|
||||
return (f"Battle {self.id} for {self.region_name[:16]:16} | "
|
||||
f"{self.invader} : {self.defender} | Round time {time_part} | {'R'+str(self.zone_id):>3}")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Battle #{self.id} {self.invader}:{self.defender} R{self.zone_id}>"
|
||||
|
@ -1,5 +1,5 @@
|
||||
[bumpversion]
|
||||
current_version = 0.23.1.1
|
||||
current_version = 0.23.2
|
||||
commit = True
|
||||
tag = True
|
||||
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.?(?P<dev>\d+)?
|
||||
|
Reference in New Issue
Block a user