Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
8aa159edf7 | |||
1211a98227 | |||
734a5ef2b6 | |||
5c3b405ca8 | |||
75de8fce96 | |||
01e5e44350 |
@ -4,7 +4,8 @@ History
|
|||||||
|
|
||||||
0.23.2 (2020-12-01)
|
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)*
|
* Added concurrency checks to guard against simultaneous fighting/wam'ing/traveling
|
||||||
|
* For concurrency checking use `utils.wait_for_lock` decorator
|
||||||
|
|
||||||
0.23.1 (2020-12-01)
|
0.23.1 (2020-12-01)
|
||||||
-------------------
|
-------------------
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
__author__ = """Eriks Karls"""
|
__author__ = """Eriks Karls"""
|
||||||
__email__ = 'eriks@72.lv'
|
__email__ = 'eriks@72.lv'
|
||||||
__version__ = '0.23.2.1'
|
__version__ = '0.23.2.3'
|
||||||
|
|
||||||
from erepublik import classes, utils, constants
|
from erepublik import classes, utils, constants
|
||||||
from erepublik.citizen import Citizen
|
from erepublik.citizen import Citizen
|
||||||
|
@ -87,9 +87,7 @@ class SlowRequests(Session):
|
|||||||
|
|
||||||
body = "[{dt}]\tURL: '{url}'\tMETHOD: {met}\tARGS: {args}\n".format(dt=utils.now().strftime("%F %T"),
|
body = "[{dt}]\tURL: '{url}'\tMETHOD: {met}\tARGS: {args}\n".format(dt=utils.now().strftime("%F %T"),
|
||||||
url=url, met=method, args=args)
|
url=url, met=method, args=args)
|
||||||
utils.get_file(self.request_log_name)
|
utils.write_file(self.request_log_name, body)
|
||||||
with open(self.request_log_name, 'ab') as file:
|
|
||||||
file.write(body.encode("UTF-8"))
|
|
||||||
|
|
||||||
def _log_response(self, url, resp, redirect: bool = False):
|
def _log_response(self, url, resp, redirect: bool = False):
|
||||||
from erepublik import Citizen
|
from erepublik import Citizen
|
||||||
@ -115,6 +113,7 @@ class SlowRequests(Session):
|
|||||||
filename = 'debug/requests/{time}_{name}{extra}.{ext}'.format(**file_data)
|
filename = 'debug/requests/{time}_{name}{extra}.{ext}'.format(**file_data)
|
||||||
with open(utils.get_file(filename), 'wb') as f:
|
with open(utils.get_file(filename), 'wb') as f:
|
||||||
f.write(resp.text.encode('utf-8'))
|
f.write(resp.text.encode('utf-8'))
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class CitizenBaseAPI:
|
class CitizenBaseAPI:
|
||||||
|
@ -42,7 +42,7 @@ class BaseCitizen(access_points.CitizenAPI):
|
|||||||
politics: classes.Politics = None
|
politics: classes.Politics = None
|
||||||
reporter: classes.Reporter = None
|
reporter: classes.Reporter = None
|
||||||
stop_threads: Event = None
|
stop_threads: Event = None
|
||||||
concurrency_lock: Event = None
|
concurrency_available: Event = None
|
||||||
telegram: classes.TelegramBot = None
|
telegram: classes.TelegramBot = None
|
||||||
|
|
||||||
r: Response = None
|
r: Response = None
|
||||||
@ -59,7 +59,8 @@ class BaseCitizen(access_points.CitizenAPI):
|
|||||||
self.my_companies = classes.MyCompanies(self)
|
self.my_companies = classes.MyCompanies(self)
|
||||||
self.reporter = classes.Reporter(self)
|
self.reporter = classes.Reporter(self)
|
||||||
self.stop_threads = Event()
|
self.stop_threads = Event()
|
||||||
self.concurrency_lock = Event()
|
self.concurrency_available = Event()
|
||||||
|
self.concurrency_available.set()
|
||||||
self.telegram = classes.TelegramBot(stop_event=self.stop_threads)
|
self.telegram = classes.TelegramBot(stop_event=self.stop_threads)
|
||||||
|
|
||||||
self.config.email = email
|
self.config.email = email
|
||||||
@ -416,6 +417,7 @@ class BaseCitizen(access_points.CitizenAPI):
|
|||||||
else:
|
else:
|
||||||
utils.process_error(msg, self.name, sys.exc_info(), self, None, None)
|
utils.process_error(msg, self.name, sys.exc_info(), self, None, None)
|
||||||
|
|
||||||
|
@utils.wait_for_lock
|
||||||
def sleep(self, seconds: int):
|
def sleep(self, seconds: int):
|
||||||
if seconds < 0:
|
if seconds < 0:
|
||||||
seconds = 0
|
seconds = 0
|
||||||
@ -428,15 +430,6 @@ class BaseCitizen(access_points.CitizenAPI):
|
|||||||
self.debug = bool(debug)
|
self.debug = bool(debug)
|
||||||
self._req.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:
|
def to_json(self, indent: bool = False) -> str:
|
||||||
return utils.json.dumps(self, cls=classes.MyJSONEncoder, indent=4 if indent else None)
|
return utils.json.dumps(self, cls=classes.MyJSONEncoder, indent=4 if indent else None)
|
||||||
|
|
||||||
@ -944,12 +937,10 @@ class CitizenCompanies(BaseCitizen):
|
|||||||
def work_as_manager_in_holding(self, holding: classes.Holding) -> Optional[Dict[str, Any]]:
|
def work_as_manager_in_holding(self, holding: classes.Holding) -> Optional[Dict[str, Any]]:
|
||||||
return self._work_as_manager(holding)
|
return self._work_as_manager(holding)
|
||||||
|
|
||||||
|
@utils.wait_for_lock
|
||||||
def _work_as_manager(self, wam_holding: classes.Holding) -> Optional[Dict[str, Any]]:
|
def _work_as_manager(self, wam_holding: classes.Holding) -> Optional[Dict[str, Any]]:
|
||||||
if self.restricted_ip:
|
if self.restricted_ip:
|
||||||
return None
|
return None
|
||||||
if not self._wait_for_concurrency_cleared():
|
|
||||||
return
|
|
||||||
self.concurrency_lock.set()
|
|
||||||
self.update_companies()
|
self.update_companies()
|
||||||
data = {"action_type": "production"}
|
data = {"action_type": "production"}
|
||||||
extra = {}
|
extra = {}
|
||||||
@ -971,7 +962,6 @@ class CitizenCompanies(BaseCitizen):
|
|||||||
data.update(extra)
|
data.update(extra)
|
||||||
if not self.details.current_region == wam_holding.region:
|
if not self.details.current_region == wam_holding.region:
|
||||||
self.write_log("Unable to work as manager because of location - please travel!")
|
self.write_log("Unable to work as manager because of location - please travel!")
|
||||||
self.concurrency_lock.clear()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
employ_factories = self.my_companies.get_employable_factories()
|
employ_factories = self.my_companies.get_employable_factories()
|
||||||
@ -980,7 +970,6 @@ class CitizenCompanies(BaseCitizen):
|
|||||||
|
|
||||||
response = self._post_economy_work("production", wam=[c.id for c in wam_list],
|
response = self._post_economy_work("production", wam=[c.id for c in wam_list],
|
||||||
employ=employ_factories).json()
|
employ=employ_factories).json()
|
||||||
self.concurrency_lock.clear()
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def update_companies(self):
|
def update_companies(self):
|
||||||
@ -1038,7 +1027,8 @@ class CitizenEconomy(CitizenTravel):
|
|||||||
ret.update({house_quality: till})
|
ret.update({house_quality: till})
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def buy_and_activate_house(self, q: int) -> Dict[int, datetime]:
|
@utils.wait_for_lock
|
||||||
|
def buy_and_activate_house(self, q: int) -> Optional[Dict[int, datetime]]:
|
||||||
original_region = self.details.current_country, self.details.current_region
|
original_region = self.details.current_country, self.details.current_region
|
||||||
ok_to_activate = False
|
ok_to_activate = False
|
||||||
inv = self.get_inventory()
|
inv = self.get_inventory()
|
||||||
@ -1051,9 +1041,6 @@ class CitizenEconomy(CitizenTravel):
|
|||||||
|
|
||||||
global_cheapest = self.get_market_offers("House", q)[f"q{q}"]
|
global_cheapest = self.get_market_offers("House", q)[f"q{q}"]
|
||||||
if global_cheapest.price + 200 < local_cheapest.price:
|
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):
|
if self.travel_to_country(global_cheapest.country):
|
||||||
buy = self.buy_from_market(global_cheapest.offer_id, 1)
|
buy = self.buy_from_market(global_cheapest.offer_id, 1)
|
||||||
else:
|
else:
|
||||||
@ -1071,7 +1058,6 @@ class CitizenEconomy(CitizenTravel):
|
|||||||
self.activate_house(q)
|
self.activate_house(q)
|
||||||
if original_region[1] != self.details.current_region:
|
if original_region[1] != self.details.current_region:
|
||||||
self._travel(*original_region)
|
self._travel(*original_region)
|
||||||
self.concurrency_lock.clear()
|
|
||||||
return self.check_house_durability()
|
return self.check_house_durability()
|
||||||
|
|
||||||
def renew_houses(self, forced: bool = False) -> Dict[int, datetime]:
|
def renew_houses(self, forced: bool = False) -> Dict[int, datetime]:
|
||||||
@ -1083,7 +1069,9 @@ class CitizenEconomy(CitizenTravel):
|
|||||||
house_durability = self.check_house_durability()
|
house_durability = self.check_house_durability()
|
||||||
for q, active_till in house_durability.items():
|
for q, active_till in house_durability.items():
|
||||||
if utils.good_timedelta(active_till, - timedelta(hours=48)) <= self.now or forced:
|
if utils.good_timedelta(active_till, - timedelta(hours=48)) <= self.now or forced:
|
||||||
house_durability = self.buy_and_activate_house(q)
|
durability = self.buy_and_activate_house(q)
|
||||||
|
if durability:
|
||||||
|
house_durability = durability
|
||||||
return house_durability
|
return house_durability
|
||||||
|
|
||||||
def activate_house(self, quality: int) -> bool:
|
def activate_house(self, quality: int) -> bool:
|
||||||
@ -1203,14 +1191,12 @@ class CitizenEconomy(CitizenTravel):
|
|||||||
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=json_ret)
|
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=json_ret)
|
||||||
return json_ret
|
return json_ret
|
||||||
|
|
||||||
def buy_market_offer(self, offer: OfferItem, amount: int = None) -> dict:
|
@utils.wait_for_lock
|
||||||
|
def buy_market_offer(self, offer: OfferItem, amount: int = None) -> Optional[Dict[str, Any]]:
|
||||||
if amount is None or amount > offer.amount:
|
if amount is None or amount > offer.amount:
|
||||||
amount = offer.amount
|
amount = offer.amount
|
||||||
traveled = False
|
traveled = False
|
||||||
if not self.details.current_country == offer.country:
|
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
|
traveled = True
|
||||||
self.travel_to_country(offer.country)
|
self.travel_to_country(offer.country)
|
||||||
ret = self._post_economy_marketplace_actions('buy', offer=offer.offer_id, amount=amount)
|
ret = self._post_economy_marketplace_actions('buy', offer=offer.offer_id, amount=amount)
|
||||||
@ -1222,7 +1208,6 @@ class CitizenEconomy(CitizenTravel):
|
|||||||
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=json_ret)
|
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=json_ret)
|
||||||
if traveled:
|
if traveled:
|
||||||
self.travel_to_residence()
|
self.travel_to_residence()
|
||||||
self.concurrency_lock.clear()
|
|
||||||
return json_ret
|
return json_ret
|
||||||
|
|
||||||
def get_market_offers(
|
def get_market_offers(
|
||||||
@ -1702,7 +1687,8 @@ class CitizenMilitary(CitizenTravel):
|
|||||||
def has_battle_contribution(self):
|
def has_battle_contribution(self):
|
||||||
return bool(self.__last_war_update_data.get("citizen_contribution", []))
|
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]:
|
def find_battle_to_fight(self, silent: bool = False) -> Tuple[
|
||||||
|
classes.Battle, classes.BattleDivision, classes.BattleSide]:
|
||||||
self.update_war_info()
|
self.update_war_info()
|
||||||
for battle in self.sorted_battles(self.config.sort_battles_time):
|
for battle in self.sorted_battles(self.config.sort_battles_time):
|
||||||
if not isinstance(battle, classes.Battle):
|
if not isinstance(battle, classes.Battle):
|
||||||
@ -1715,7 +1701,8 @@ class CitizenMilitary(CitizenTravel):
|
|||||||
if self.config.air and div.is_air:
|
if self.config.air and div.is_air:
|
||||||
battle_zone = div
|
battle_zone = div
|
||||||
break
|
break
|
||||||
elif self.config.ground and not div.is_air and (div.div == self.division or (self.maverick and self.config.maverick)):
|
elif self.config.ground and not div.is_air and (
|
||||||
|
div.div == self.division or (self.maverick and self.config.maverick)):
|
||||||
battle_zone = div
|
battle_zone = div
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
@ -1769,18 +1756,15 @@ class CitizenMilitary(CitizenTravel):
|
|||||||
if not self.travel_to_battle(battle, countries_to_travel):
|
if not self.travel_to_battle(battle, countries_to_travel):
|
||||||
break
|
break
|
||||||
|
|
||||||
if not self._wait_for_concurrency_cleared():
|
|
||||||
return
|
|
||||||
self.concurrency_lock.set()
|
|
||||||
if self.change_division(battle, division):
|
if self.change_division(battle, division):
|
||||||
self.set_default_weapon(battle, division)
|
self.set_default_weapon(battle, division)
|
||||||
self.fight(battle, division, side)
|
self.fight(battle, division, side)
|
||||||
self.travel_to_residence()
|
self.travel_to_residence()
|
||||||
self.concurrency_lock.clear()
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@utils.wait_for_lock
|
||||||
def fight(self, battle: classes.Battle, division: classes.BattleDivision, side: classes.BattleSide = None,
|
def fight(self, battle: classes.Battle, division: classes.BattleDivision, side: classes.BattleSide = None,
|
||||||
count: int = None) -> int:
|
count: int = None) -> Optional[int]:
|
||||||
"""Fight in a battle.
|
"""Fight in a battle.
|
||||||
|
|
||||||
Will auto activate booster and travel if allowed to do it.
|
Will auto activate booster and travel if allowed to do it.
|
||||||
@ -1881,7 +1865,8 @@ class CitizenMilitary(CitizenTravel):
|
|||||||
|
|
||||||
return hits, err, damage
|
return hits, err, damage
|
||||||
|
|
||||||
def deploy_bomb(self, battle: classes.Battle, bomb_id: int, inv_side: bool = None, count: int = 1) -> int:
|
@utils.wait_for_lock
|
||||||
|
def deploy_bomb(self, battle: classes.Battle, bomb_id: int, inv_side: bool = None, count: int = 1) -> Optional[int]:
|
||||||
"""Deploy bombs in a battle for given side.
|
"""Deploy bombs in a battle for given side.
|
||||||
|
|
||||||
:param battle: Battle
|
:param battle: Battle
|
||||||
@ -1893,9 +1878,6 @@ class CitizenMilitary(CitizenTravel):
|
|||||||
:rtype: int
|
:rtype: int
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not self._wait_for_concurrency_cleared():
|
|
||||||
return 0
|
|
||||||
self.concurrency_lock.set()
|
|
||||||
if not isinstance(count, int) or count < 1:
|
if not isinstance(count, int) or count < 1:
|
||||||
count = 1
|
count = 1
|
||||||
has_traveled = False
|
has_traveled = False
|
||||||
@ -1929,7 +1911,6 @@ class CitizenMilitary(CitizenTravel):
|
|||||||
self.travel_to_residence()
|
self.travel_to_residence()
|
||||||
|
|
||||||
self._report_action("MILITARY_BOMB", f"Deployed {deployed_count} bombs in battle {battle.id}")
|
self._report_action("MILITARY_BOMB", f"Deployed {deployed_count} bombs in battle {battle.id}")
|
||||||
self.concurrency_lock.clear()
|
|
||||||
return deployed_count
|
return deployed_count
|
||||||
|
|
||||||
def change_division(self, battle: classes.Battle, division: classes.BattleDivision) -> bool:
|
def change_division(self, battle: classes.Battle, division: classes.BattleDivision) -> bool:
|
||||||
|
@ -152,7 +152,8 @@ def get_file(filepath: str) -> str:
|
|||||||
def write_file(filename: str, content: str) -> int:
|
def write_file(filename: str, content: str) -> int:
|
||||||
filename = get_file(filename)
|
filename = get_file(filename)
|
||||||
with open(filename, 'ab') as f:
|
with open(filename, 'ab') as f:
|
||||||
return f.write(content.encode("utf-8"))
|
ret = f.write(content.encode("utf-8"))
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def write_request(response: requests.Response, is_error: bool = False):
|
def write_request(response: requests.Response, is_error: bool = False):
|
||||||
@ -383,3 +384,19 @@ def get_final_hit_dmg(base_dmg: Union[Decimal, float, str], rang: int,
|
|||||||
|
|
||||||
def deprecation(message):
|
def deprecation(message):
|
||||||
warnings.warn(message, DeprecationWarning, stacklevel=2)
|
warnings.warn(message, DeprecationWarning, stacklevel=2)
|
||||||
|
|
||||||
|
|
||||||
|
def wait_for_lock(function):
|
||||||
|
def wrapper(instance, *args, **kwargs):
|
||||||
|
if not instance.concurrency_available.wait(600):
|
||||||
|
e = 'Concurrency not freed in 10min!'
|
||||||
|
instance.write_log(e)
|
||||||
|
if instance.debug:
|
||||||
|
instance.report_error(e)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
instance.concurrency_available.clear()
|
||||||
|
ret = function(instance, *args, **kwargs)
|
||||||
|
instance.concurrency_available.set()
|
||||||
|
return ret
|
||||||
|
return wrapper
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[bumpversion]
|
[bumpversion]
|
||||||
current_version = 0.23.2.1
|
current_version = 0.23.2.3
|
||||||
commit = True
|
commit = True
|
||||||
tag = True
|
tag = True
|
||||||
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.?(?P<dev>\d+)?
|
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.?(?P<dev>\d+)?
|
||||||
|
2
setup.py
2
setup.py
@ -50,6 +50,6 @@ setup(
|
|||||||
test_suite='tests',
|
test_suite='tests',
|
||||||
tests_require=test_requirements,
|
tests_require=test_requirements,
|
||||||
url='https://github.com/eeriks/erepublik/',
|
url='https://github.com/eeriks/erepublik/',
|
||||||
version='0.23.2.1',
|
version='0.23.2.3',
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user