Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
f88eaccf67 | |||
7be129a781 | |||
32546505b9 | |||
00ceabf8e3 | |||
19113da8e6 | |||
3ad7172925 | |||
179f1a0892 | |||
65adf707e2 | |||
29f9ce5ccc | |||
fa3881bf10 | |||
c1e8e94cba | |||
8133461fd7 | |||
c87333411a | |||
d679006b34 | |||
fa308db074 | |||
a080163af9 | |||
8aa159edf7 | |||
1211a98227 | |||
734a5ef2b6 | |||
5c3b405ca8 | |||
75de8fce96 | |||
01e5e44350 | |||
500409f74a | |||
9d79bb713c | |||
684b2a6ba4 | |||
447aee8134 | |||
64249fc3d9 |
14
HISTORY.rst
14
HISTORY.rst
@ -2,8 +2,22 @@
|
||||
History
|
||||
=======
|
||||
|
||||
0.23.2 (2020-12-01)
|
||||
-------------------
|
||||
* 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)
|
||||
-------------------
|
||||
* 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.2'
|
||||
__version__ = '0.23.2.8'
|
||||
|
||||
from erepublik import classes, utils, constants
|
||||
from erepublik.citizen import Citizen
|
||||
|
@ -29,14 +29,10 @@ class SlowRequests(Session):
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
|
||||
'Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0',
|
||||
'Mozilla/5.0 (X11; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0',
|
||||
'Mozilla/5.0 (X11; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0',
|
||||
'Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0',
|
||||
'Mozilla/5.0 (X11; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0',
|
||||
'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0',
|
||||
]
|
||||
debug: bool = False
|
||||
|
||||
@ -87,9 +83,9 @@ class SlowRequests(Session):
|
||||
|
||||
body = "[{dt}]\tURL: '{url}'\tMETHOD: {met}\tARGS: {args}\n".format(dt=utils.now().strftime("%F %T"),
|
||||
url=url, met=method, args=args)
|
||||
utils.get_file(self.request_log_name)
|
||||
with open(self.request_log_name, 'ab') as file:
|
||||
file.write(body.encode("UTF-8"))
|
||||
pass
|
||||
|
||||
def _log_response(self, url, resp, redirect: bool = False):
|
||||
from erepublik import Citizen
|
||||
@ -113,8 +109,8 @@ class SlowRequests(Session):
|
||||
file_data.update({"ext": "html"})
|
||||
|
||||
filename = 'debug/requests/{time}_{name}{extra}.{ext}'.format(**file_data)
|
||||
with open(utils.get_file(filename), 'wb') as f:
|
||||
f.write(resp.text.encode('utf-8'))
|
||||
utils.write_file(filename, resp.text)
|
||||
pass
|
||||
|
||||
|
||||
class CitizenBaseAPI:
|
||||
|
@ -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_available: Event = None
|
||||
telegram: classes.TelegramBot = None
|
||||
|
||||
r: Response = None
|
||||
@ -57,6 +59,8 @@ class BaseCitizen(access_points.CitizenAPI):
|
||||
self.my_companies = classes.MyCompanies(self)
|
||||
self.reporter = classes.Reporter(self)
|
||||
self.stop_threads = Event()
|
||||
self.concurrency_available = Event()
|
||||
self.concurrency_available.set()
|
||||
self.telegram = classes.TelegramBot(stop_event=self.stop_threads)
|
||||
|
||||
self.config.email = email
|
||||
@ -421,6 +425,10 @@ class BaseCitizen(access_points.CitizenAPI):
|
||||
else:
|
||||
sleep(seconds)
|
||||
|
||||
def set_debug(self, debug: bool):
|
||||
self.debug = bool(debug)
|
||||
self._req.debug = bool(debug)
|
||||
|
||||
def to_json(self, indent: bool = False) -> str:
|
||||
return utils.json.dumps(self, cls=classes.MyJSONEncoder, indent=4 if indent else None)
|
||||
|
||||
@ -928,6 +936,7 @@ class CitizenCompanies(BaseCitizen):
|
||||
def work_as_manager_in_holding(self, holding: classes.Holding) -> Optional[Dict[str, Any]]:
|
||||
return self._work_as_manager(holding)
|
||||
|
||||
@utils.wait_for_lock
|
||||
def _work_as_manager(self, wam_holding: classes.Holding) -> Optional[Dict[str, Any]]:
|
||||
if self.restricted_ip:
|
||||
return None
|
||||
@ -1017,7 +1026,7 @@ class CitizenEconomy(CitizenTravel):
|
||||
ret.update({house_quality: till})
|
||||
return ret
|
||||
|
||||
def buy_and_activate_house(self, q: int) -> Dict[int, datetime]:
|
||||
def buy_and_activate_house(self, q: int) -> Optional[Dict[int, datetime]]:
|
||||
original_region = self.details.current_country, self.details.current_region
|
||||
ok_to_activate = False
|
||||
inv = self.get_inventory()
|
||||
@ -1031,12 +1040,14 @@ class CitizenEconomy(CitizenTravel):
|
||||
global_cheapest = self.get_market_offers("House", q)[f"q{q}"]
|
||||
if global_cheapest.price + 200 < local_cheapest.price:
|
||||
if self.travel_to_country(global_cheapest.country):
|
||||
buy = self.buy_from_market(global_cheapest.offer_id, 1)
|
||||
buy = self.buy_market_offer(global_cheapest, 1)
|
||||
else:
|
||||
buy = {'error': True, 'message': 'Unable to travel!'}
|
||||
else:
|
||||
buy = self.buy_from_market(local_cheapest.offer_id, 1)
|
||||
if buy["error"]:
|
||||
buy = self.buy_market_offer(local_cheapest, 1)
|
||||
if buy is None:
|
||||
pass
|
||||
elif buy["error"]:
|
||||
msg = f"Unable to buy q{q} house! \n{buy['message']}"
|
||||
self.write_log(msg)
|
||||
else:
|
||||
@ -1058,7 +1069,9 @@ class CitizenEconomy(CitizenTravel):
|
||||
house_durability = self.check_house_durability()
|
||||
for q, active_till in house_durability.items():
|
||||
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
|
||||
|
||||
def activate_house(self, quality: int) -> bool:
|
||||
@ -1139,11 +1152,13 @@ class CitizenEconomy(CitizenTravel):
|
||||
return False
|
||||
|
||||
def post_market_offer(self, industry: int, quality: int, amount: int, price: float) -> bool:
|
||||
if isinstance(industry, str):
|
||||
industry = constants.INDUSTRIES[industry]
|
||||
if not constants.INDUSTRIES[industry]:
|
||||
self.write_log(f"Trying to sell unsupported industry {industry}")
|
||||
|
||||
_inv_qlt = quality if industry in [1, 2, 3, 4, 23] else 0
|
||||
_kind = 'final' if industry in [1, 2, 4, 4, 23] else 'raw'
|
||||
_kind = 'final' if industry in [1, 2, 4, 23] else 'raw'
|
||||
inventory = self.get_inventory()
|
||||
items = inventory[_kind].get(constants.INDUSTRIES[industry], {_inv_qlt: {'amount': 0}})
|
||||
if items[_inv_qlt]['amount'] < amount:
|
||||
@ -1168,7 +1183,7 @@ class CitizenEconomy(CitizenTravel):
|
||||
self._report_action("ECONOMY_SELL_PRODUCTS", message, kwargs=ret)
|
||||
return not bool(ret.get('error', True))
|
||||
|
||||
def buy_from_market(self, offer: int, amount: int) -> dict:
|
||||
def buy_from_market(self, offer: int, amount: int) -> Dict[str, Any]:
|
||||
ret = self._post_economy_marketplace_actions('buy', offer=offer, amount=amount)
|
||||
json_ret = ret.json()
|
||||
if not json_ret.get('error', True):
|
||||
@ -1178,20 +1193,15 @@ class CitizenEconomy(CitizenTravel):
|
||||
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=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:
|
||||
amount = offer.amount
|
||||
traveled = False
|
||||
if not self.details.current_country == offer.country:
|
||||
traveled = True
|
||||
self.travel_to_country(offer.country)
|
||||
ret = self._post_economy_marketplace_actions('buy', offer=offer.offer_id, amount=amount)
|
||||
json_ret = ret.json()
|
||||
if not json_ret.get('error', True):
|
||||
self.details.cc = ret.json()['currency']
|
||||
self.details.gold = ret.json()['gold']
|
||||
json_ret.pop("offerUpdate", None)
|
||||
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=json_ret)
|
||||
json_ret = self.buy_from_market(offer.offer_id, amount)
|
||||
if traveled:
|
||||
self.travel_to_residence()
|
||||
return json_ret
|
||||
@ -1673,20 +1683,25 @@ 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]:
|
||||
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
|
||||
if battle.is_dict_lib:
|
||||
continue
|
||||
battle_zone: Optional[classes.BattleDivision] = None
|
||||
for div in battle.div.values():
|
||||
if div.terrain == 0:
|
||||
if div.div_end:
|
||||
continue
|
||||
maverick_ok = self.maverick and self.config.maverick
|
||||
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)):
|
||||
elif self.config.ground and not div.is_air and (div.div == self.division or maverick_ok):
|
||||
battle_zone = div
|
||||
break
|
||||
else:
|
||||
@ -1739,14 +1754,16 @@ class CitizenMilitary(CitizenTravel):
|
||||
|
||||
if not self.travel_to_battle(battle, countries_to_travel):
|
||||
break
|
||||
|
||||
if self.change_division(battle, division):
|
||||
self.set_default_weapon(battle, division)
|
||||
self.fight(battle, division, side)
|
||||
self.travel_to_residence()
|
||||
break
|
||||
|
||||
@utils.wait_for_lock
|
||||
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.
|
||||
|
||||
Will auto activate booster and travel if allowed to do it.
|
||||
@ -1826,6 +1843,9 @@ class CitizenMilitary(CitizenTravel):
|
||||
elif r_json.get("message") == "ZONE_INACTIVE":
|
||||
self.write_log("Wrong division!!")
|
||||
return 0, 10, 0
|
||||
elif r_json.get("message") == "NON_BELLIGERENT":
|
||||
self.write_log("Dictatorship/Liberation wars are not supported!")
|
||||
return 0, 10, 0
|
||||
elif r_json.get("message") in ["FIGHT_DISABLED", "DEPLOYMENT_MODE"]:
|
||||
self._post_main_profile_update('options',
|
||||
params='{"optionName":"enable_web_deploy","optionValue":"off"}')
|
||||
@ -1847,7 +1867,8 @@ class CitizenMilitary(CitizenTravel):
|
||||
|
||||
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.
|
||||
|
||||
:param battle: Battle
|
||||
@ -1858,6 +1879,7 @@ class CitizenMilitary(CitizenTravel):
|
||||
:return: Deployed count
|
||||
:rtype: int
|
||||
"""
|
||||
|
||||
if not isinstance(count, int) or count < 1:
|
||||
count = 1
|
||||
has_traveled = False
|
||||
@ -2393,8 +2415,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
|
||||
@ -2423,10 +2443,8 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
|
||||
self.update_citizen_info()
|
||||
self.reporter.do_init()
|
||||
if self.config.telegram:
|
||||
# noinspection SpellCheckingInspection
|
||||
self.telegram.do_init(self.config.telegram_chat_id or 620981703,
|
||||
self.config.telegram_token or "864251270:AAFzZZdjspI-kIgJVk4gF3TViGFoHnf8H4o",
|
||||
"" if self.config.telegram_chat_id or self.config.telegram_token else self.name)
|
||||
if self.config.telegram_chat_id and self.config.telegram_token:
|
||||
self.telegram.do_init(self.config.telegram_chat_id, self.config.telegram_token, self.name)
|
||||
self.telegram.send_message(f"*Started* {utils.now():%F %T}")
|
||||
|
||||
self.update_all(True)
|
||||
@ -2447,7 +2465,6 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
|
||||
if self.details.gold >= 54:
|
||||
self.buy_tg_contract()
|
||||
else:
|
||||
|
||||
self.write_log(f"Training ground contract active but "
|
||||
f"don't have enough gold ({self.details.gold}g {self.details.cc}cc)")
|
||||
if self.energy.is_energy_full and self.config.telegram:
|
||||
@ -2494,10 +2511,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])
|
||||
|
||||
@ -2574,7 +2587,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
|
||||
@ -2590,6 +2603,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
|
||||
|
@ -1016,7 +1016,7 @@ class TelegramBot:
|
||||
|
||||
|
||||
class OfferItem(NamedTuple):
|
||||
price: float = 99_999.
|
||||
price: float = 999_999_999.
|
||||
country: constants.Country = constants.Country(0, "", "", "")
|
||||
amount: int = 0
|
||||
offer_id: int = 0
|
||||
|
@ -25,7 +25,7 @@ __all__ = ['VERSION', 'calculate_hit', 'caught_error', 'date_from_eday', 'eday_f
|
||||
'get_air_hit_dmg_value', 'get_file', 'get_ground_hit_dmg_value', 'get_sleep_seconds', 'good_timedelta',
|
||||
'interactive_sleep', 'json', 'localize_dt', 'localize_timestamp', 'normalize_html_json', 'now',
|
||||
'process_error', 'process_warning', 'send_email', 'silent_sleep', 'slugify', 'write_file',
|
||||
'write_interactive_log', 'write_silent_log']
|
||||
'write_interactive_log', 'write_silent_log', 'get_final_hit_dmg', 'wait_for_lock']
|
||||
|
||||
if not sys.version_info >= (3, 6):
|
||||
raise AssertionError('This script requires Python version 3.6 and higher\n'
|
||||
@ -152,7 +152,8 @@ def get_file(filepath: str) -> str:
|
||||
def write_file(filename: str, content: str) -> int:
|
||||
filename = get_file(filename)
|
||||
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):
|
||||
@ -383,3 +384,19 @@ def get_final_hit_dmg(base_dmg: Union[Decimal, float, str], rang: int,
|
||||
|
||||
def deprecation(message):
|
||||
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]
|
||||
current_version = 0.23.1.2
|
||||
current_version = 0.23.2.8
|
||||
commit = True
|
||||
tag = True
|
||||
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.?(?P<dev>\d+)?
|
||||
|
Reference in New Issue
Block a user