Compare commits

...

93 Commits

Author SHA1 Message Date
c458eb4b1c Bump version: 0.23.3.1 → 0.23.3.2 2020-12-11 11:12:34 +02:00
4af4d284c9 Updated dev requirement versions 2020-12-11 11:12:27 +02:00
104c1a0b16 Bump required python version 2020-12-11 11:12:01 +02:00
86f820771b Hits done bugfix 2020-12-11 11:11:25 +02:00
2a7af0cb7d Bump version: 0.23.3 → 0.23.3.1 2020-12-10 14:37:12 +02:00
94de509026 __all__ definitions 2020-12-10 14:36:34 +02:00
82d913bc47 Bump version: 0.23.2.11 → 0.23.3 2020-12-10 13:26:11 +02:00
c462eac369 Bomb deploy update 2020-12-10 13:25:47 +02:00
d522a4faeb Bump version: 0.23.2.10 → 0.23.2.11 2020-12-09 13:33:26 +02:00
084a47b07a TelegramBot renamed to TelegramReporter 2020-12-09 13:30:18 +02:00
5082855440 Default Telegram's token moved to TelegramReporter 2020-12-09 13:29:28 +02:00
c9971f3570 str.format() -> f-string 2020-12-09 13:28:56 +02:00
c0122eccdf Bump version: 0.23.2.9 → 0.23.2.10 2020-12-08 18:05:21 +02:00
2524173da0 Telegram bot's token can be reused 2020-12-08 18:05:18 +02:00
4a4b991faf Telegram bot's token can be reused 2020-12-08 18:05:01 +02:00
e87c48124c Bump version: 0.23.2.8 → 0.23.2.9 2020-12-07 14:10:52 +02:00
f0f47566a0 Bugfix: Don't travel to renew house if You can't afford the house 2020-12-07 14:10:40 +02:00
f88eaccf67 Bump version: 0.23.2.7 → 0.23.2.8 2020-12-07 14:01:29 +02:00
7be129a781 Quickfix for forbidding Dictatorship/Liberation wars 2020-12-07 14:01:22 +02:00
32546505b9 Bump version: 0.23.2.6 → 0.23.2.7 2020-12-04 13:13:17 +02:00
00ceabf8e3 cleanup 2020-12-04 13:13:10 +02:00
19113da8e6 cleanup 2020-12-04 13:11:11 +02:00
3ad7172925 cleanup 2020-12-04 13:08:42 +02:00
179f1a0892 Only init TelegramReporter if both chat id and token have been supplied 2020-12-04 13:08:16 +02:00
65adf707e2 Only init TelegramReporter if both chat id and token have been supplied 2020-12-04 13:08:04 +02:00
29f9ce5ccc Bump version: 0.23.2.5 → 0.23.2.6 2020-12-02 18:40:41 +02:00
fa3881bf10 Updated classes.OfferItem default price to be higher considerably higher than max price for cheapest most expensive items on sale (Q5 houses), Updated house renewal 2020-12-02 18:40:33 +02:00
c1e8e94cba Bump version: 0.23.2.4 → 0.23.2.5 2020-12-02 13:00:51 +02:00
8133461fd7 pep8 2020-12-02 13:00:33 +02:00
c87333411a Hotfix 2020-12-02 12:56:06 +02:00
d679006b34 small fix 2020-12-01 18:56:14 +02:00
fa308db074 Bump version: 0.23.2.3 → 0.23.2.4 2020-12-01 18:49:35 +02:00
a080163af9 Bugfix 2020-12-01 18:49:33 +02:00
8aa159edf7 Bump version: 0.23.2.2 → 0.23.2.3 2020-12-01 18:49:01 +02:00
1211a98227 Bugfix 2020-12-01 18:48:52 +02:00
734a5ef2b6 Bump version: 0.23.2.1 → 0.23.2.2 2020-12-01 18:11:09 +02:00
5c3b405ca8 Added utils.wait_for_lock decorator to wait for concurrency release 2020-12-01 18:11:00 +02:00
75de8fce96 File closing before return 2020-12-01 15:09:45 +02:00
01e5e44350 File closing before return 2020-12-01 15:07:17 +02:00
500409f74a Bump version: 0.23.2 → 0.23.2.1 2020-12-01 15:01:00 +02:00
9d79bb713c naming bugfix 2020-12-01 15:00:55 +02:00
684b2a6ba4 Bump version: 0.23.1.2 → 0.23.2 2020-12-01 14:22:40 +02:00
447aee8134 Concurrency flag to guard against 2020-12-01 14:22:26 +02:00
64249fc3d9 History 2020-12-01 13:11:45 +02:00
95a78b6e7e Bump version: 0.23.1.1 → 0.23.1.2 2020-12-01 13:02:29 +02:00
b338ea598a Seperated battle finding logic from CitizenMilitary.find_battle_and_fight method 2020-12-01 13:02:10 +02:00
b88e7973e8 Bump version: 0.23.1 → 0.23.1.1 2020-11-30 18:54:44 +02:00
c671425531 Base dmg calculations 2020-11-30 18:54:35 +02:00
08937d06e4 Bump version: 0.23.0 → 0.23.1 2020-11-30 18:20:48 +02:00
cec4510831 Requirement update 2020-11-30 18:20:42 +02:00
cfb9501647 PEP8 2020-11-30 18:16:13 +02:00
d419211955 Get max hit value for divisions on current side 2020-11-30 18:12:36 +02:00
0ea144db17 Added method to get division stats 2020-11-30 17:44:23 +02:00
1418f580cd Spin wheel of fortune updates 2020-11-30 17:43:42 +02:00
49575bddf5 History update for v0.23.0 2020-11-26 18:39:25 +02:00
c874247335 Bump version: 0.22.3.3 → 0.23.0 2020-11-26 18:32:46 +02:00
6e9def4394 Added .get_article(int) and .delete_article(int) methods to CitizenMedia class 2020-11-26 18:32:31 +02:00
d6fbaa7945 Maverick fighting should be explicitly enabled through Citizen.config 2020-11-26 16:16:15 +02:00
eb740c60c7 Bump version: 0.22.3.2 → 0.22.3.3 2020-11-20 18:54:20 +02:00
240c409739 Travel to region bugfix 2020-11-20 18:54:12 +02:00
f348a315c5 Bump version: 0.22.3.1 → 0.22.3.2 2020-11-20 16:57:43 +02:00
7a09eea2b4 CitizenAnniversary.collect_map_quest_node() now accepts argument extra, to collect extra reward 2020-11-20 16:57:36 +02:00
81b1069cf0 Bump version: 0.22.3 → 0.22.3.1 2020-11-20 14:46:04 +02:00
429d43df15 CitizenTasks.work() bugfix: When employer out of money - resign 2020-11-20 14:45:58 +02:00
c81986d65e CitizenEconomy.post_market_offer() bugfix 2020-11-20 14:31:16 +02:00
7bb988f716 PEP8 2020-11-20 14:24:11 +02:00
16cae24712 Bump version: 0.22.2.1 → 0.22.3 2020-11-16 12:12:30 +02:00
3e051fe906 History update 2020-11-16 12:07:20 +02:00
aa9cda9314 eRepublik temp technical difficulties 2020-11-16 12:06:59 +02:00
fc66db8cab Config file generator update 2020-11-16 11:56:37 +02:00
6bbc4f8768 eRepublik has both airplaneRaw and aircraftRaw. Possible future bugs because of this! 2020-11-16 11:56:13 +02:00
160b32a914 Buy market offer directly 2020-11-16 11:52:55 +02:00
a51c3c620e Python round to even bugfix.
`{"stock":10.494533,"consume":10.5}` rounds to 0
2020-11-16 11:52:27 +02:00
a1c26468eb UA updates 2020-11-12 11:47:20 +02:00
4895ae3663 Bump version: 0.22.2 → 0.22.2.1 2020-11-10 15:39:15 +02:00
b8d7cc8d7c Example updates 2020-11-10 15:38:30 +02:00
1d0645b490 Bump version: 0.22.1.5 → 0.22.2 2020-11-09 16:34:52 +02:00
30cf6203b7 History update 2020-11-09 16:34:41 +02:00
a32e88218d CitizenEconomy.get_market_offers now accepts searching tickets and the new aircraft qualities 2020-11-09 16:28:21 +02:00
a031da0ee7 Bump version: 0.22.1.4 → 0.22.1.5 2020-11-04 17:22:44 +02:00
bdb13fa4ae History update, WAM actions are now more verbose 2020-11-04 17:22:35 +02:00
e1e3b33d46 requirement update 2020-11-03 18:10:43 +02:00
e09ca143b1 Bump version: 0.22.1.3 → 0.22.1.4 2020-11-03 14:31:43 +02:00
61d0599295 House renewal bugfix 2020-11-03 14:31:37 +02:00
1ef600492a House renewal bugfix 2020-11-03 14:30:20 +02:00
377eda6445 Inventory booster bugfix 2020-11-03 14:29:42 +02:00
fd13667ca8 Bump version: 0.22.1.2 → 0.22.1.3 2020-10-30 12:58:26 +02:00
0479afcabe Report if out of money to work as manager 2020-10-30 12:58:18 +02:00
486f022f35 Unified Item namig from constants.INDUSTRIES 2020-10-30 12:43:45 +02:00
638373e452 Bump version: 0.22.1.1 → 0.22.1.2 2020-10-27 17:04:40 +02:00
04f357cc70 Bugfix 2020-10-27 17:04:37 +02:00
ed4ffe5af6 Bump version: 0.22.1 → 0.22.1.1 2020-10-22 18:04:13 +03:00
8aa90a7dbf Lint 2020-10-22 18:04:06 +03:00
14 changed files with 492 additions and 296 deletions

View File

@ -2,6 +2,48 @@
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
* Fixed `CitizenTravel.travel_to_region(region_id:int)` method
* Added `CitizenAnniversary.collect_map_quest_node(node_id:int, extra:bool=False)` to collect also extra rewards
* Fixed `CitizenTasks.work()` when employer out of money - resign and find a new job
* Fixed `CitizenEconomy.post_market_offer()`
0.22.3 (2020-11-16)
-------------------
* Fixed round to even bug when doing wam and not enough raw.
* Added meta industry airplaneRaw
* Added method `Citizen.buy_market_offer(OfferItem, amount=None)` to directly buy market offer with included travel to country and back.
0.22.2 (2020-11-09)
-------------------
* Allow querying market offers for q2-q5 aircrafts
* Added "Ticket" industry
0.22.1 (2020-11-04)
-------------------
* Requirement update
* Unified product naming in inventory and other places based on `erepublik.constants.INDUSTRIES` values
* `erepublik.Citizen` parameter `auto_login` now defaults to `False`
* Continued work on more verbose action and result logging
0.22.0 (2020-10-22)
-------------------
* Ability to dump session and restore from file

View File

@ -93,8 +93,8 @@
<label class="custom-control-label" for="wam">Work as manager</label>
</div>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" onchange="updateJson()" id="employ">
<label class="custom-control-label" for="employ">Employ employees</label>
<input type="checkbox" class="custom-control-input" onchange="updateJson()" id="employees">
<label class="custom-control-label" for="employees">Employ employees</label>
</div>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" onchange="updateJson()" id="auto_buy_raw" checked>
@ -236,8 +236,8 @@
config.debug = debug.checked;
let wam = document.getElementById('wam'); // Generated
config.wam = wam.checked;
let employ = document.getElementById('employ'); // Generated
config.employ = employ.checked;
let employees = document.getElementById('employees'); // Generated
config.employees = employees.checked;
let auto_buy_raw = document.getElementById('auto_buy_raw'); // Generated
let auto_sell_all = document.getElementById('auto_sell_all'); // Generated
@ -249,7 +249,7 @@
let auto_sell_house = document.getElementById('auto_sell_house'); // Generated
let auto_sell_arm = document.getElementById('auto_sell_arm'); // Generated
let auto_sell_air = document.getElementById('auto_sell_air'); // Generated
if (config.wam || config.employ) {
if (config.wam || config.employees) {
auto_buy_raw.disabled = false;
auto_sell_all.disabled = false;
auto_sell_frm.disabled = false;
@ -359,7 +359,7 @@
"interactive": true,
"debug": true,
"wam": true,
"employ": true,
"employees": true,
"auto_buy_raw": true,
"auto_sell_all": true,
"auto_sell": [

View File

@ -4,9 +4,9 @@
__author__ = """Eriks Karls"""
__email__ = 'eriks@72.lv'
__version__ = '0.22.1'
__version__ = '0.23.3.2'
from erepublik import classes, utils, constants
from erepublik.citizen import Citizen
__all__ = ["classes", "utils", "Citizen", ]
__all__ = ["classes", "utils", "Citizen", 'constants']

View File

@ -15,9 +15,11 @@ class SlowRequests(Session):
timeout: datetime.timedelta = datetime.timedelta(milliseconds=500)
uas: List[str] = [
# Chrome
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36', # noqa
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', # noqa
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36', # noqa
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36',
@ -27,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
@ -83,11 +81,10 @@ class SlowRequests(Session):
if params:
args.update({"params": params})
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)
body = f"[{utils.now().strftime('%F %T')}]\tURL: '{url}'\tMETHOD: {method}\tARGS: {args}\n"
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
@ -97,22 +94,20 @@ class SlowRequests(Session):
self._log_request(hist_resp.request.url, "REDIRECT")
self._log_response(hist_resp.request.url, hist_resp, redirect=True)
file_data = {
"path": 'debug/requests',
"time": self.last_time.strftime('%Y/%m/%d/%H-%M-%S'),
"name": utils.slugify(url[len(Citizen.url):]),
"extra": "_REDIRECT" if redirect else ""
}
fd_path = 'debug/requests'
fd_time = self.last_time.strftime('%Y/%m/%d/%H-%M-%S')
fd_name = utils.slugify(url[len(Citizen.url):])
fd_extra = "_REDIRECT" if redirect else ""
try:
utils.json.loads(resp.text)
file_data.update({"ext": "json"})
fd_ext = 'json'
except utils.json.JSONDecodeError:
file_data.update({"ext": "html"})
fd_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'))
filename = f'{fd_path}/{fd_time}_{fd_name}{fd_extra}.{fd_ext}'
utils.write_file(filename, resp.text)
pass
class CitizenBaseAPI:
@ -159,8 +154,10 @@ class ErepublikAnniversaryAPI(CitizenBaseAPI):
data = {'nodeId': node_id, '_token': self.token, "currencyCost": currency_amount}
return self.post(f"{self.url}/main/map-rewards-speedup", data=data)
def _post_map_rewards_claim(self, node_id: int) -> Response:
def _post_map_rewards_claim(self, node_id: int, extra: bool = False) -> Response:
data = {'nodeId': node_id, '_token': self.token}
if extra:
data['claimExtra'] = 1
return self.post(f"{self.url}/main/map-rewards-claim", data=data)
def _post_main_wheel_of_fortune_spin(self, cost) -> Response:
@ -174,6 +171,9 @@ class ErepublikArticleAPI(CitizenBaseAPI):
def _get_main_article_json(self, article_id: int) -> Response:
return self.get(f"{self.url}/main/articleJson/{article_id}")
def _get_main_delete_article(self, article_id: int) -> Response:
return self.get(f"{self.url}/main/delete-article/{article_id}/1")
def _post_main_article_comments(self, article_id: int, page: int = 1) -> Response:
data = dict(_token=self.token, articleId=article_id, page=page)
if page:
@ -285,7 +285,6 @@ class ErepublikCountryAPI(CitizenBaseAPI):
def _post_main_country_donate(self, country_id: int, action: str, value: Union[int, float],
quality: int = None) -> Response:
data = dict(countryId=country_id, action=action, _token=self.token, value=value, quality=quality)
return self.post(f"{self.url}/main/country-donate", data=data,
headers={"Referer": f"{self.url}/country/economy/Latvia"})
@ -346,11 +345,12 @@ class ErepublikEconomyAPI(CitizenBaseAPI):
def _post_economy_marketplace_actions(self, action: str, **kwargs) -> Response:
if action == 'buy':
data = dict(_token=self.token, offerId=kwargs['offer'], amount=kwargs['amount'], orderBy="price_asc", currentPage=1,
buyAction=1)
data = dict(_token=self.token, offerId=kwargs['offer'], amount=kwargs['amount'],
orderBy="price_asc", currentPage=1, buyAction=1)
elif action == 'sell':
data = dict(_token=self.token, countryId=kwargs["country_id"], price=kwargs["price"],
industryId=kwargs["industry"], quality=kwargs["quality"], amount=kwargs['amount'], sellAction='postOffer')
industryId=kwargs["industry"], quality=kwargs["quality"], amount=kwargs['amount'],
sellAction='postOffer')
elif action == 'delete':
data = dict(_token=self.token, offerId=kwargs["offer_id"], sellAction='deleteOffer')
else:
@ -359,16 +359,20 @@ class ErepublikEconomyAPI(CitizenBaseAPI):
class ErepublikLeaderBoardAPI(CitizenBaseAPI):
def _get_main_leaderboards_damage_aircraft_rankings(self, country_id: int, weeks: int = 0, mu_id: int = 0) -> Response:
def _get_main_leaderboards_damage_aircraft_rankings(self, country_id: int, weeks: int = 0,
mu_id: int = 0) -> Response: # noqa
return self.get(f"{self.url}/main/leaderboards-damage-aircraft-rankings/{country_id}/{weeks}/{mu_id}/0")
def _get_main_leaderboards_damage_rankings(self, country_id: int, weeks: int = 0, mu_id: int = 0, div: int = 0) -> Response:
def _get_main_leaderboards_damage_rankings(self, country_id: int, weeks: int = 0, mu_id: int = 0,
div: int = 0) -> Response: # noqa
return self.get(f"{self.url}/main/leaderboards-damage-rankings/{country_id}/{weeks}/{mu_id}/{div}")
def _get_main_leaderboards_kills_aircraft_rankings(self, country_id: int, weeks: int = 0, mu_id: int = 0) -> Response:
def _get_main_leaderboards_kills_aircraft_rankings(self, country_id: int, weeks: int = 0,
mu_id: int = 0) -> Response: # noqa
return self.get(f"{self.url}/main/leaderboards-kills-aircraft-rankings/{country_id}/{weeks}/{mu_id}/0")
def _get_main_leaderboards_kills_rankings(self, country_id: int, weeks: int = 0, mu_id: int = 0, div: int = 0) -> Response:
def _get_main_leaderboards_kills_rankings(self, country_id: int, weeks: int = 0, mu_id: int = 0,
div: int = 0) -> Response: # noqa
return self.get(f"{self.url}/main/leaderboards-kills-rankings/{country_id}/{weeks}/{mu_id}/{div}")
@ -380,6 +384,9 @@ class ErepublikLocationAPI(CitizenBaseAPI):
class ErepublikMilitaryAPI(CitizenBaseAPI):
def _get_military_battle_stats(self, battle_id: int, division: int, division_id: int):
return self.get(f"{self.url}/military/battle-stats/{battle_id}/{division}/{division_id}")
def _get_military_battlefield_choose_side(self, battle_id: int, side_id: int) -> Response:
return self.get(f"{self.url}/military/battlefield-choose-side/{battle_id}/{side_id}")
@ -431,8 +438,9 @@ class ErepublikMilitaryAPI(CitizenBaseAPI):
data.update(page=page)
return self.post(f"{self.url}/military/battle-console", data=data)
def _post_military_deploy_bomb(self, battle_id: int, bomb_id: int) -> Response:
data = dict(battleId=battle_id, bombId=bomb_id, _token=self.token)
def _post_military_deploy_bomb(self, battle_id: int, division_id: int, side_id: int, bomb_id: int) -> Response:
data = dict(battleId=battle_id, battleZoneId=division_id, sideId=side_id, sideCountryId=side_id,
bombId=bomb_id, _token=self.token)
return self.post(f"{self.url}/military/deploy-bomb", data=data)
def _post_military_fight_air(self, battle_id: int, side_id: int, zone_id: int) -> Response:
@ -476,7 +484,7 @@ class ErepublikPoliticsAPI(CitizenBaseAPI):
class ErepublikPresidentAPI(CitizenBaseAPI):
def _post_wars_attack_region(self, war_id: int, region_id: int, region_name: str) -> Response:
data = {'_token': self.token, 'warId': war_id, 'regionName': region_name, 'regionNameConfirm': region_name}
return self.post('{}/wars/attack-region/{}/{}'.format(self.url, war_id, region_id), data=data)
return self.post(f'{self.url}/wars/attack-region/{war_id}/{region_id}', data=data)
def _post_new_war(self, self_country_id: int, attack_country_id: int, debate: str = "") -> Response:
data = dict(requirments=1, _token=self.token, debate=debate,

View File

@ -12,6 +12,7 @@ from typing import Any, Dict, List, NoReturn, Optional, Set, Tuple, Union
from requests import HTTPError, RequestException, Response
from . import utils, classes, access_points, constants
from .classes import OfferItem
class BaseCitizen(access_points.CitizenAPI):
@ -32,14 +33,17 @@ class BaseCitizen(access_points.CitizenAPI):
maverick: bool = False
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
telegram: classes.TelegramBot = None
concurrency_available: Event = None
telegram: classes.TelegramReporter = None
r: Response = None
name: str = "Not logged in!"
@ -55,12 +59,15 @@ class BaseCitizen(access_points.CitizenAPI):
self.my_companies = classes.MyCompanies(self)
self.reporter = classes.Reporter(self)
self.stop_threads = Event()
self.telegram = classes.TelegramBot(stop_event=self.stop_threads)
self.concurrency_available = Event()
self.concurrency_available.set()
self.telegram = classes.TelegramReporter(stop_event=self.stop_threads)
self.config.email = email
self.config.password = password
self.inventory = {}
self.inventory_status = dict(used=0, total=0)
self.wheel_of_fortune = False
def get_csrf_token(self):
"""
@ -229,6 +236,9 @@ class BaseCitizen(access_points.CitizenAPI):
self.politics.is_party_president = bool(party.get('is_party_president'))
self.politics.party_slug = f"{party.get('stripped_title')}-{party.get('party_id')}"
self.wheel_of_fortune = bool(
re.search(r'<a id="launch_wof" class="powerspin_sidebar( show_free)?" href="javascript:">', html))
def update_inventory(self):
"""
Updates class properties and returns structured inventory.
@ -264,6 +274,8 @@ class BaseCitizen(access_points.CitizenAPI):
kind = re.sub(r'_q\d\d*', "", item_data.get('token'))
else:
kind = item_data.get('type')
if constants.INDUSTRIES[kind]:
kind = constants.INDUSTRIES[constants.INDUSTRIES[kind]]
if kind not in active_items:
active_items[kind] = {}
icon = item_data['icon'] if item_data[
@ -316,11 +328,15 @@ class BaseCitizen(access_points.CitizenAPI):
self.eb_small += amount
elif q == 15:
self.eb_small += amount
item_data.update(token='energy_bar')
kind = re.sub(r'_q\d\d*', "", item_data.get('token'))
if item_data.get('token', "") == "house_q100":
self.ot_points = item_data['amount']
if constants.INDUSTRIES[kind]:
kind = constants.INDUSTRIES[constants.INDUSTRIES[kind]]
if kind not in final_items:
final_items[kind] = {}
@ -339,10 +355,10 @@ class BaseCitizen(access_points.CitizenAPI):
icon = "/images/modules/pvp/ghost_boosters/icon_booster_30_60.png"
else:
icon = "//www.erepublik.net/images/modules/manager/tab_storage.png"
item_data = dict(kind=kind, quality=item_data.get('quality', 0), amount=item_data.get('amount', 0),
durability=item_data.get('duration', 0), icon=icon, name=name)
_item_data = dict(kind=kind, quality=item_data.get('quality', 0), amount=item_data.get('amount', 0),
durability=item_data.get('duration', 0), icon=icon, name=name)
if item_data.get('type') in ('damageBoosters', "aircraftDamageBoosters"):
item_data = {item_data['durability']: item_data}
_item_data = {_item_data['durability']: _item_data}
else:
if item_data.get('type') == 'bomb':
firepower = 0
@ -351,9 +367,9 @@ class BaseCitizen(access_points.CitizenAPI):
except AttributeError:
pass
finally:
item_data.update(fire_power=firepower)
item_data = {item_data['quality']: item_data}
final_items[kind].update(item_data)
_item_data.update(fire_power=firepower)
_item_data = {_item_data['quality']: _item_data}
final_items[kind].update(_item_data)
raw_materials: Dict[str, Dict[int, Dict[str, Union[str, int]]]] = {}
if data.get("rawMaterials", {}).get("items", {}):
@ -409,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)
@ -587,8 +607,8 @@ class BaseCitizen(access_points.CitizenAPI):
except AttributeError:
continue
if data:
msgs = ["{count} x {kind}, totaling {} {currency}\n"
"{about}".format(d["count"] * d["reward"], **d) for d in data.values()]
msgs = [f"{d['count']} x {d['kind']}, totaling {d['count'] * d['reward']} "
f"{d['currency']}" for d in data.values()]
msgs = "\n".join(msgs)
if self.config.telegram:
@ -682,9 +702,15 @@ class BaseCitizen(access_points.CitizenAPI):
if re.search(r'Occasionally there are a couple of things which we need to check or to implement in order make '
r'your experience in eRepublik more pleasant. <strong>Don\'t worry about ongoing battles, timer '
r'will be stopped during maintenance.</strong>', response.text):
self.write_log("eRepublik ss having maintenance. Sleeping for 5 minutes")
self.write_log("eRepublik is having maintenance. Sleeping for 5 minutes")
self.sleep(5 * 60)
return True
if re.search('We are experiencing some tehnical dificulties', response.text):
self.write_log("eRepublik is having technical difficulties. Sleeping for 5 minutes")
self.sleep(5 * 60)
return True
return bool(re.search(r'body id="error"|Internal Server Error|'
r'CSRF attack detected|meta http-equiv="refresh"|'
r'not_authenticated', response.text))
@ -718,31 +744,41 @@ class CitizenAnniversary(BaseCitizen):
def start_unlocking_map_quest_node(self, node_id: int):
return self._post_map_rewards_unlock(node_id)
def collect_map_quest_node(self, node_id: int):
return self._post_map_rewards_claim(node_id)
def collect_map_quest_node(self, node_id: int, extra: bool = False):
return self._post_map_rewards_claim(node_id, extra)
def speedup_map_quest_node(self, node_id: int):
node = self.get_anniversary_quest_data().get('cities', {}).get(str(node_id), {})
return self._post_map_rewards_speedup(node_id, node.get("skipCost", 0))
def spin_wheel_of_fortune(self, max_cost=0, spin_count=0):
def _write_spin_data(cost, cc, prize):
self._report_action("WHEEL_SPIN", f"Cost: {cost:4d} | Currency left: {cc:,} | Prize: {prize}")
if not self.config.spin_wheel_of_fortune:
self.write_log("Unable to spin wheel of fortune because 'config.spin_wheel_of_fortune' is False")
return
def _write_spin_data(cost: int, prize: str):
self._report_action("WHEEL_SPIN", f"Cost: {cost:4d} | Currency left: {self.details.cc:,} | Prize: {prize}")
if not self.wheel_of_fortune:
self.update_citizen_info()
base = self._post_main_wheel_of_fortune_build().json()
current_cost = 0 if base.get('progress').get('free_spin') else base.get('cost')
current_count = base.get('progress').get('spins')
prizes = base.get('prizes')
if not max_cost and not spin_count:
r = self._post_main_wheel_of_fortune_spin(current_cost).json()
_write_spin_data(current_cost, r.get('account'),
base.get('prizes').get('prizes').get(str(r.get('result'))).get('tooltip'))
_write_spin_data(current_cost, prizes.get('prizes').get(str(r.get('result'))).get('tooltip'))
else:
while max_cost >= current_cost if max_cost else spin_count >= current_count if spin_count else False:
r = self._spin_wheel_of_loosing(current_cost)
current_count += 1
prize_name = prizes.get('prizes').get(str(r.get('result'))).get('tooltip')
if r.get('result') == 7:
prize_name += f" - {prizes.get('jackpot').get(str(r.get('jackpot'))).get('tooltip')}"
_write_spin_data(current_cost, prize_name)
current_cost = r.get('cost')
_write_spin_data(current_cost, r.get('account'),
base.get('prizes').get('prizes').get(str(r.get('result'))).get('tooltip'))
if r.get('jackpot', 0) == 3:
return
def _spin_wheel_of_loosing(self, current_cost: int) -> Dict[str, Any]:
r = self._post_main_wheel_of_fortune_spin(current_cost).json()
@ -795,7 +831,14 @@ class CitizenTravel(BaseCitizen):
if data.get('alreadyInRegion'):
return True
else:
country = constants.COUNTRIES[data.get('preselectCountryId')]
country = None
for country_data in data.get('countries').values():
if region_id in country_data.get('regions'):
country = constants.COUNTRIES[country_data.get('id')]
break
if country is None:
raise classes.ErepublikException('Region not found!')
if self._travel(country, region_id):
self._report_action("TRAVEL", "Traveled to region")
@ -893,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
@ -908,6 +952,7 @@ class CitizenCompanies(BaseCitizen):
if int(free_inventory * 0.75) < self.my_companies.get_needed_inventory_usage(wam_list):
self.update_inventory()
free_inventory = self.inventory_status["total"] - self.inventory_status["used"]
while wam_list and free_inventory < self.my_companies.get_needed_inventory_usage(wam_list):
wam_list.pop(-1)
@ -976,32 +1021,40 @@ class CitizenEconomy(CitizenTravel):
def check_house_durability(self) -> Dict[int, datetime]:
ret = {}
inv = self.get_inventory()
for house_quality, active_house in inv['active'].get('house', {}).items():
for house_quality, active_house in inv['active'].get('House', {}).items():
till = utils.good_timedelta(self.now, timedelta(seconds=active_house['time_left']))
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()
if not inv['final'].get('house', {}).get(q, {}):
if not inv['final'].get('House', {}).get(q, {}):
countries = [self.details.citizenship, ]
if self.details.current_country != self.details.citizenship:
countries.append(self.details.current_country)
offers = [self.get_market_offers("house", q, country)[f"q{q}"] for country in countries]
offers = [self.get_market_offers("House", q, country)[f"q{q}"] for country in countries]
local_cheapest = sorted(offers, key=lambda o: o.price)[0]
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 self.travel_to_country(global_cheapest.country):
buy = self.buy_from_market(global_cheapest.offer_id, 1)
if global_cheapest.price + 2000 < self.details.cc:
if self.travel_to_country(global_cheapest.country):
buy = self.buy_market_offer(global_cheapest, 1)
else:
buy = dict(error=True, message='Unable to travel!')
else:
buy = {'error': True, 'message': 'Unable to travel!'}
buy = dict(error=True, message='Not enough money to buy house!')
else:
buy = self.buy_from_market(local_cheapest.offer_id, 1)
if buy["error"]:
msg = f"Unable to buy q{q} house! \n{buy['message']}"
if local_cheapest.price < self.details.cc:
buy = self.buy_market_offer(local_cheapest, 1)
else:
buy = dict(error=True, message='Not enough money to buy house!')
if buy is None:
pass
elif buy["error"]:
msg = f'Unable to buy q{q} house! \n{buy["message"]}'
self.write_log(msg)
else:
ok_to_activate = True
@ -1022,14 +1075,16 @@ 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:
r = self._post_economy_activate_house(quality).json()
r: Dict[str, Any] = self._post_economy_activate_house(quality).json()
self._update_inventory_data(r)
if r.get("status") and not r.json().get("error"):
house: Dict[str, Union[int, str]] = self.get_inventory()['active']['house'][quality]
if r.get("status") and not r.get("error"):
house: Dict[str, Union[int, str]] = self.get_inventory()['active']['House'][quality]
time_left = timedelta(seconds=house["time_left"])
active_until = utils.good_timedelta(self.now, time_left)
self._report_action(
@ -1085,7 +1140,7 @@ class CitizenEconomy(CitizenTravel):
offers = self.get_my_market_offers()
for offer in offers:
if offer['id'] == offer_id:
industry = constants.INDUSTRIES[offer['industry']]
industry = constants.INDUSTRIES[offer['industryId']]
amount = offer['amount']
q = offer['quality']
price = offer['price']
@ -1097,23 +1152,27 @@ class CitizenEconomy(CitizenTravel):
self._report_action("ECONOMY_DELETE_OFFER",
f"Removed offer for {amount} x {industry} q{q} for {price}cc/each",
kwargs=offer)
return ret.get('error')
return not ret.get('error')
else:
self._report_action("ECONOMY_DELETE_OFFER", f"Unable to find offer id{offer_id}", kwargs={'offers': offers})
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}")
final = industry in [1, 2, 4, 23]
items = self.inventory['final' if final else 'raw'][constants.INDUSTRIES[industry]]
if items[quality if final else 0]['amount'] < amount:
self.update_inventory()
items = self.inventory['final' if final else 'raw'][constants.INDUSTRIES[industry]]
if items[quality if final else 0]['amount'] < amount:
_inv_qlt = quality if industry in [1, 2, 3, 4, 23] else 0
_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:
inventory = self.get_inventory(True)
items = inventory[_kind].get(constants.INDUSTRIES[industry], {_inv_qlt: {'amount': 0}})
if items[_inv_qlt]['amount'] < amount:
self._report_action("ECONOMY_SELL_PRODUCTS", "Unable to sell! Not enough items in storage!",
kwargs=dict(inventory=items[quality if final else 0], amount=amount))
kwargs=dict(inventory=items[_inv_qlt], amount=amount))
return False
data = {
@ -1128,25 +1187,37 @@ class CitizenEconomy(CitizenTravel):
message = (f"Posted market offer for {amount}q{quality} "
f"{constants.INDUSTRIES[industry]} for price {price}cc")
self._report_action("ECONOMY_SELL_PRODUCTS", message, kwargs=ret)
return bool(ret.get('error', True))
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 json_ret.get('error'):
return json_ret
else:
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", "", kwargs=json_ret)
self._report_action("BOUGHT_PRODUCTS", json_ret.get('message'), kwargs=json_ret)
return json_ret
@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)
json_ret = self.buy_from_market(offer.offer_id, amount)
if traveled:
self.travel_to_residence()
return json_ret
def get_market_offers(
self, product_name: str, quality: int = None, country: constants.Country = None
) -> Dict[str, classes.OfferItem]:
raw_short_names = dict(frm="foodRaw", wrm="weaponRaw", hrm="houseRaw", arm="airplaneRaw")
q1_industries = ["aircraft"] + list(raw_short_names.values())
q1_industries = list(raw_short_names.values())
q5_industries = ['house', 'aircraft', 'ticket']
if product_name in raw_short_names:
quality = 1
product_name = raw_short_names[product_name]
@ -1160,7 +1231,7 @@ class CitizenEconomy(CitizenTravel):
if quality:
offers[f"q{quality}"] = classes.OfferItem()
else:
max_quality = 1 if product_name in q1_industries else 5 if product_name == 'house' else 7
max_quality = 1 if product_name in q1_industries else 5 if product_name.lower() in q5_industries else 7
for q in range(max_quality):
offers[f"q{q + 1}"] = classes.OfferItem()
@ -1400,9 +1471,21 @@ class CitizenMedia(BaseCitizen):
article_id = 0
return article_id
else:
raise classes.ErepublikException("Article kind must be one of:\n{}\n'{}' is not supported".format(
"\n".join(["{}: {}".format(k, v) for k, v in kinds.items()]), kind
))
kinds = "\n".join([f"{k}: {v}" for k, v in kinds.items()])
raise classes.ErepublikException(f"Article kind must be one of:\n{kinds}\n'{kind}' is not supported")
def get_article(self, article_id: int) -> Dict[str, Any]:
return self._get_main_article_json(article_id).json()
def delete_article(self, article_id: int) -> NoReturn:
article_data = self.get_article(article_id)
if article_data and article_data['articleData']['canDelete']:
self._report_action("ARTICLE_DELETE",
f"Attempting to delete article '{article_data['article']['title']}' (#{article_id})",
kwargs=article_data)
self._get_main_delete_article(article_id)
else:
self.write_log(f"Unable to delete article (#{article_id})!")
class CitizenMilitary(CitizenTravel):
@ -1494,51 +1577,6 @@ class CitizenMilitary(CitizenTravel):
f" new influence {influence}", kwargs=r.json())
return influence
# def check_epic_battles(self):
# active_fs = False
# for battle_id in self.sorted_battles(self.config.sort_battles_time):
# battle = self.all_battles.get(battle_id)
# if not battle.is_air:
# my_div: BattleDivision = battle.div.get(self.division)
# if my_div.epic and my_div.end > self.now:
# if self.energy.food_fights > 50:
# inv_allies = battle.invader.deployed + [battle.invader.id]
# def_allies = battle.defender.deployed + [battle.defender.id]
# all_allies = inv_allies + def_allies
# if self.details.current_country not in all_allies:
# if self.details.current_country in battle.invader.allies:
# allies = battle.invader.deployed
# side = battle.invader.id
# else:
# allies = battle.defender.deployed
# side = battle.defender.id
#
# self.travel_to_battle(battle.id, allies)
#
# else:
# if self.details.current_country in inv_allies:
# side = battle.invader.id
# elif self.details.current_country in def_allies:
# side = battle.defender.id
# else:
# self.write_log(
# f"Country {self.details.current_country} not in all allies list ({all_allies}) and "
# f"also not in inv allies ({inv_allies}) nor def allies ({def_allies})")
# break
# error_count = 0
# while self.energy.food_fights > 5 and error_count < 20:
# errors = self.fight(battle_id, side_id=side, count=self.energy.food_fights - 5)
# if errors:
# error_count += errors
# if self.config.epic_hunt_ebs:
# self._eat('orange')
# self.travel_to_residence()
# break
# elif bool(my_div.epic):
# active_fs = True
#
# self.active_fs = active_fs
def sorted_battles(self, sort_by_time: bool = True, only_tp=False) -> List[classes.Battle]:
cs_battles_priority_air: List[classes.Battle] = []
cs_battles_priority_ground: List[classes.Battle] = []
@ -1620,6 +1658,7 @@ class CitizenMilitary(CitizenTravel):
def get_cheap_tp_divisions(self) -> Dict[str, List[Tuple[int, classes.BattleDivision]]]:
air_divs: List[Tuple[int, classes.BattleDivision]] = []
ground_divs: List[Tuple[int, classes.BattleDivision]] = []
check_maverick = self.maverick and self.config.maverick
for battle in reversed(self.sorted_battles(True, True)):
for division in battle.div.values():
is_start_ok = utils.good_timedelta(division.battle.start, timedelta(minutes=-1)) < self.now
@ -1632,7 +1671,7 @@ class CitizenMilitary(CitizenTravel):
else:
air_divs.append((medal.get('1').get('raw_value'), division))
elif not division.is_air and self.config.ground:
if not division.div == self.division and not self.maverick:
if not division.div == self.division and not check_maverick:
continue
division_medals = self.get_battle_round_data(division)
medal = division_medals[self.details.citizenship == division.battle.defender.country]
@ -1649,46 +1688,62 @@ 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
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 maverick_ok):
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))
@ -1704,14 +1759,16 @@ 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 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.
@ -1754,7 +1811,7 @@ class CitizenMilitary(CitizenTravel):
else:
self._eat('blue')
if self.energy.recovered < 50 or error_count >= 10 or count <= 0:
self.write_log("Hits: {:>4} | Damage: {}".format(total_hits, total_damage))
self.write_log(f"Hits: {total_hits:>4} | Damage: {total_damage}")
ok_to_fight = False
if total_damage:
self.reporter.report_fighting(battle, not side.is_defender, division, total_damage, total_hits)
@ -1791,6 +1848,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"}')
@ -1803,7 +1863,18 @@ class CitizenMilitary(CitizenTravel):
self.travel_to_battle(battle, countries)
err = True
elif r_json.get("message") == "ENEMY_KILLED":
hits = (self.energy.recovered - r_json["details"]["wellness"]) // 10
# Non-InfantryKit players
if r_json['user']['earnedXp']:
hits = r_json['user']['earnedXp']
# InfantryKit player
# The almost always safe way (breaks on levelup hit)
elif self.energy.recovered >= r_json["details"]["wellness"]: # Haven't reached levelup
hits = (self.energy.recovered - r_json["details"]["wellness"]) // 10
else:
hits = r_json['hits']
if r_json['user']['epicBattle']:
hits /= 1+r_json['user']['epicBattle']
self.energy.recovered = r_json["details"]["wellness"]
self.details.xp = int(r_json["details"]["points"])
damage = r_json["user"]["givenDamage"] * (1.1 if r_json["oldEnemy"]["isNatural"] else 1)
@ -1812,17 +1883,24 @@ 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, division: classes.BattleDivision, bomb_id: int, inv_side: bool, count: int = 1) -> Optional[int]:
"""Deploy bombs in a battle for given side.
:param battle: Battle
:type battle: Battle
:type battle: classes.Battle
:param division: BattleDivision
:type division: classes.BattleDivision
:param bomb_id: int bomb id
:param inv_side: should deploy on invader side, if None then will deploy in currently available side
:param count: int how many bombs to deploy
:type bomb_id: int
:param inv_side: should deploy on invader side
:type inv_side: bool
:param count: how many bombs to deploy
:type count: int
:return: Deployed count
:rtype: int
"""
if not isinstance(count, int) or count < 1:
count = 1
has_traveled = False
@ -1833,18 +1911,15 @@ class CitizenMilitary(CitizenTravel):
good_countries = [battle.invader.country] + battle.invader.deployed
if self.details.current_country not in good_countries:
has_traveled = self.travel_to_battle(battle, good_countries)
elif inv_side is not None:
good_countries = [battle.defender.country] + battle.defender.deployed
if self.details.current_country not in good_countries:
has_traveled = self.travel_to_battle(battle, good_countries)
else:
involved = [battle.invader.country,
battle.defender.country] + battle.invader.deployed + battle.defender.deployed
if self.details.current_country not in involved:
count = 0
side = battle.invader if inv_side else battle.defender
errors = deployed_count = 0
while (not deployed_count == count) and errors < 10:
r = self._post_military_deploy_bomb(battle.id, bomb_id).json()
r = self._post_military_deploy_bomb(battle.id, division.id, side.id, bomb_id).json()
if not r.get('error'):
deployed_count += 1
elif r.get('message') == 'LOCKED':
@ -1986,13 +2061,8 @@ class CitizenMilitary(CitizenTravel):
count = self.energy.food_fights
msg = "Fighting all-in. Doing %i hits" % count
# All-in for AIR battles
elif all([self.config.air, self.config.all_in, self.energy.available >= self.energy.limit]):
count = self.energy.food_fights
msg = "Fighting all-in in AIR. Doing %i hits" % count
# Get to next Energy +1
elif self.next_reachable_energy and self.config.next_energy:
elif self.config.next_energy and self.next_reachable_energy:
count = self.next_reachable_energy
msg = "Fighting for +1 energy. Doing %i hits" % count
@ -2014,6 +2084,21 @@ class CitizenMilitary(CitizenTravel):
return (r_json.get(str(battle.invader.id)).get("fighterData"),
r_json.get(str(battle.defender.id)).get("fighterData"))
def get_battle_division_stats(self, division: classes.BattleDivision) -> Dict[str, Any]:
battle = division.battle
r = self._get_military_battle_stats(battle.id, division.div, division.id)
return r.json()
def get_division_max_hit(self, division: classes.BattleDivision) -> int:
""" Returns max hit in division for current side (if not on either side returns 0)
:param division: BattleDivision for which to get max hit value
:type division: classes.BattleDivision
:return: max hit value
:rtype: int
"""
return self.get_battle_division_stats(division).get('maxHit', -1)
def schedule_attack(self, war_id: int, region_id: int, region_name: str, at_time: datetime):
if at_time:
self.sleep(utils.get_sleep_seconds(at_time))
@ -2233,7 +2318,8 @@ class CitizenTasks(BaseCitizen):
js = response.json()
good_msg = ["already_worked", "captcha"]
if not js.get("status") and not js.get("message") in good_msg:
if js.get('message') == 'employee':
if js.get('message') in ['employee', 'money']:
self.resign_from_employer()
self.find_new_job()
self.update_citizen_info()
self.work()
@ -2243,7 +2329,7 @@ class CitizenTasks(BaseCitizen):
self._eat("blue")
if self.energy.food_fights < 1:
seconds = (self.energy.reference_time - self.now).total_seconds()
self.write_log("I don't have energy to work. Will sleep for {}s".format(seconds))
self.write_log(f"I don't have energy to work. Will sleep for {seconds}s")
self.sleep(seconds)
self._eat("blue")
self.work()
@ -2347,8 +2433,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
@ -2376,11 +2460,10 @@ 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 and self.config.telegram_chat_id:
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)
@ -2401,7 +2484,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:
@ -2438,8 +2520,8 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
data[(title, reward)]['count'] += count
self._post_main_global_alerts_close(medal.get('id'))
if data:
msgs = ["{count} x {kind},"
" totaling {} {currency}".format(d["count"] * d["reward"], **d) for d in data.values()]
msgs = [f"{d['count']} x {d['kind']}, totaling {d['count'] * d['reward']} "
f"{d['currency']}" for d in data.values()]
msgs = "\n".join(msgs)
if self.config.telegram:
@ -2448,10 +2530,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])
@ -2528,7 +2606,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
@ -2544,6 +2622,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
@ -2573,7 +2654,7 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
if not amount:
inv_resp = self._get_economy_inventory_items().json()
category = "rawMaterials" if kind.endswith("Raw") else "finalProducts"
item = "{}_{}".format(constants.INDUSTRIES[kind], quality)
item = f"{constants.INDUSTRIES[kind]}_{quality}"
amount = inv_resp.get("inventoryItems").get(category).get("items").get(item).get("amount", 0)
if amount >= 1:
@ -2602,13 +2683,17 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
elif kind.endswith("Raw"):
self.sell_produced_product(kind, 1)
else:
raise classes.ErepublikException("Unknown kind produced '{kind}'".format(kind=kind))
raise classes.ErepublikException(f"Unknown kind produced '{kind}'")
elif self.config.auto_buy_raw and re.search(r"not_enough_[^_]*_raw", response.get("message")):
raw_kind = re.search(r"not_enough_(\w+)_raw", response.get("message"))
if raw_kind:
raw_kind = raw_kind.group(1)
result = response.get("result", {})
amount_needed = round(result.get("consume", 0) - result.get("stock", 0) + 0.49)
amount_needed = round(result.get("consume", 0) - result.get("stock", 0) + 0.5)
self._report_action(
'WORK_AS_MANAGER', f"Unable to wam! Missing {amount_needed} {raw_kind}, will try to buy.",
kwargs=response
)
start_place = (self.details.current_country, self.details.current_region)
while amount_needed > 0:
amount = amount_needed
@ -2617,22 +2702,34 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
if not best_offer.country == self.details.current_country:
self.travel_to_country(best_offer.country)
self._report_action("ECONOMY_BUY",
f"Attempting to buy {amount} {raw_kind} for {best_offer.price * amount}cc")
rj = self.buy_from_market(amount=amount, offer=best_offer.offer_id)
if not rj.get('error'):
amount_needed -= amount
else:
self.write_log(rj.get('message', ""))
self._report_action(
"ECONOMY_BUY", f"Unable to buy products! Reason: {rj.get('message')}", kwargs=rj
)
break
else:
if not start_place == (self.details.current_country, self.details.current_region):
self.travel_to_holding(holding)
self._wam(holding)
return
if not start_place == (self.details.current_country, self.details.current_region):
self.travel_to_residence()
return
elif response.get("message") == "not_enough_health_food":
self.buy_food()
self._wam(holding)
elif response.get("message") == "tax_money":
self._report_action("WORK_AS_MANAGER", "Not enough money to work as manager!", kwargs=response)
self.write_log("Not enough money to work as manager!")
else:
msg = "I was not able to wam and or employ because:\n{}".format(response)
msg = f"I was not able to wam and or employ because:\n{response}"
self._report_action("WORK_AS_MANAGER", f"Worked as manager failed: {msg}", kwargs=response)
self.write_log(msg)
@ -2671,7 +2768,7 @@ class Citizen(CitizenAnniversary, CitizenCompanies, CitizenEconomy, CitizenLeade
wam_count = self.my_companies.get_total_wam_count()
if wam_count:
self.write_log("Wam ff lockdown is now {}, was {}".format(wam_count, self.my_companies.ff_lockdown))
self.write_log(f"Wam ff lockdown is now {wam_count}, was {self.my_companies.ff_lockdown}")
self.my_companies.ff_lockdown = wam_count
self.travel_to_residence()
return bool(wam_count)

View File

@ -3,14 +3,15 @@ import hashlib
import threading
import weakref
from decimal import Decimal
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, Union, NoReturn, Generator, Iterable
from typing import Any, Dict, List, NamedTuple, Tuple, Union, NoReturn, Generator, Iterable
from requests import Response, Session, post
from . import utils, constants
__all__ = ['Battle', 'BattleDivision', 'BattleSide', 'Company', 'Config', 'Details', 'Energy', 'ErepublikException',
'Holding', 'MyCompanies', 'MyJSONEncoder', 'OfferItem', 'Politics', 'Reporter', 'TelegramBot']
'ErepublikNetworkException', 'EnergyToFight',
'Holding', 'MyCompanies', 'MyJSONEncoder', 'OfferItem', 'Politics', 'Reporter', 'TelegramReporter']
class ErepublikException(Exception):
@ -314,7 +315,9 @@ class MyCompanies:
self._companies.clear()
@property
def as_dict(self) -> Dict[str, Union[str, int, datetime.datetime, Dict[str, Dict[str, Union[str, int, List[Dict[str, Union[str, int, bool, float, Decimal]]]]]]]]:
def as_dict(self) -> Dict[str, Union[str, int, datetime.datetime, Dict[str, Dict[str, Union[
str, int, List[Dict[str, Union[str, int, bool, float, Decimal]]]]
]]]]:
return dict(name=str(self), work_units=self.work_units, next_ot_time=self.next_ot_time,
ff_lockdown=self.ff_lockdown,
holdings={str(hi): h.as_dict for hi, h in self.holdings.items()},
@ -355,6 +358,8 @@ class Config:
telegram = True
telegram_chat_id = 0
telegram_token = ""
maverick = False
spin_wheel_of_fortune = False
def __init__(self):
self.auto_sell = []
@ -387,6 +392,8 @@ class Config:
self.telegram = True
self.telegram_chat_id = 0
self.telegram_token = ""
self.maverick = False
self.spin_wheel_of_fortune = False
@property
def as_dict(self):
@ -395,10 +402,11 @@ class Config:
fight=self.fight, air=self.air, ground=self.ground, all_in=self.all_in,
next_energy=self.next_energy, boosters=self.boosters, travel_to_fight=self.travel_to_fight,
always_travel=self.always_travel, epic_hunt=self.epic_hunt, epic_hunt_ebs=self.epic_hunt_ebs,
rw_def_side=self.rw_def_side, interactive=self.interactive,
rw_def_side=self.rw_def_side, interactive=self.interactive, maverick=self.maverick,
continuous_fighting=self.continuous_fighting, auto_buy_raw=self.auto_buy_raw,
force_wam=self.force_wam, sort_battles_time=self.sort_battles_time, force_travel=self.force_travel,
telegram=self.telegram, telegram_chat_id=self.telegram_chat_id, telegram_token=self.telegram_token)
telegram=self.telegram, telegram_chat_id=self.telegram_chat_id, telegram_token=self.telegram_token,
spin_wheel_of_fortune=self.spin_wheel_of_fortune)
class Energy:
@ -412,7 +420,7 @@ class Energy:
self._recovery_time = utils.now()
def __repr__(self):
return "{:4}/{:4} + {:4}, {:3}hp/6min".format(self.recovered, self.limit, self.recoverable, self.interval)
return f"{self.recovered:4}/{self.limit:4} + {self.recoverable:4}, {self.interval:3}hp/6min"
def set_reference_time(self, recovery_time: datetime.datetime):
self._recovery_time = recovery_time.replace(microsecond=0)
@ -591,10 +599,10 @@ class Reporter:
for unreported_data in self.__to_update:
unreported_data.update(player_id=self.citizen_id, key=self.key)
unreported_data = utils.json.loads(utils.json.dumps(unreported_data, cls=MyJSONEncoder))
self._req.post("{}/bot/update".format(self.url), json=unreported_data)
self._req.post(f"{self.url}/bot/update", json=unreported_data)
self.__to_update.clear()
data = utils.json.loads(utils.json.dumps(data, cls=MyJSONEncoder))
r = self._req.post("{}/bot/update".format(self.url), json=data)
r = self._req.post(f"{self.url}/bot/update", json=data)
return r
def register_account(self):
@ -602,8 +610,8 @@ class Reporter:
try:
r = self.__bot_update(dict(key=self.key, check=True, player_id=self.citizen_id))
if not r.json().get("status"):
self._req.post("{}/bot/register".format(self.url), json=dict(name=self.name, email=self.email,
player_id=self.citizen_id))
self._req.post(f"{self.url}/bot/register", json=dict(name=self.name, email=self.email,
player_id=self.citizen_id))
finally:
self.__registered = True
self.allowed = True
@ -712,11 +720,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
@ -782,7 +790,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:
@ -857,13 +865,15 @@ class Battle:
self.invader = BattleSide(
self, constants.COUNTRIES[battle.get('inv', {}).get('id')], battle.get('inv', {}).get('points'),
[constants.COUNTRIES[row.get('id')] for row in battle.get('inv', {}).get('ally_list')],
[constants.COUNTRIES[row.get('id')] for row in battle.get('inv', {}).get('ally_list') if row['deployed']], False
[constants.COUNTRIES[row.get('id')] for row in battle.get('inv', {}).get('ally_list') if row['deployed']],
False
)
self.defender = BattleSide(
self, constants.COUNTRIES[battle.get('def', {}).get('id')], battle.get('def', {}).get('points'),
[constants.COUNTRIES[row.get('id')] for row in battle.get('def', {}).get('ally_list')],
[constants.COUNTRIES[row.get('id')] for row in battle.get('def', {}).get('ally_list') if row['deployed']], True
[constants.COUNTRIES[row.get('id')] for row in battle.get('def', {}).get('ally_list') if row['deployed']],
True
)
self.div = {}
@ -886,11 +896,12 @@ class Battle:
time_now = utils.now()
is_started = self.start < utils.now()
if is_started:
time_part = " {}".format(time_now - self.start)
time_part = f" {time_now - self.start}"
else:
time_part = "-{}".format(self.start - time_now)
time_part = f"-{self.start - time_now}"
return f"Battle {self.id} for {self.region_name[:16]} | {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}>"
@ -927,7 +938,7 @@ class EnergyToFight:
return self.energy
class TelegramBot:
class TelegramReporter:
__initialized: bool = False
__queue: List[str]
chat_id: int = 0
@ -952,10 +963,12 @@ class TelegramBot:
'last_time': self._last_time, 'next_time': self._next_time, 'queue': self.__queue,
'initialized': self.__initialized, 'has_threads': not self._threads}
def do_init(self, chat_id: int, token: str, player_name: str = ""):
def do_init(self, chat_id: int, token: str = None, player_name: str = None):
if token is None:
token = "864251270:AAFzZZdjspI-kIgJVk4gF3TViGFoHnf8H4o"
self.chat_id = chat_id
self.api_url = "https://api.telegram.org/bot{}/sendMessage".format(token)
self.player_name = player_name
self.api_url = f"https://api.telegram.org/bot{token}/sendMessage"
self.player_name = player_name or ""
self.__initialized = True
self._last_time = utils.good_timedelta(utils.now(), datetime.timedelta(minutes=-5))
self._last_full_energy_report = utils.good_timedelta(utils.now(), datetime.timedelta(minutes=-30))
@ -971,7 +984,7 @@ class TelegramBot:
self._threads = [t for t in self._threads if t.is_alive()]
self._next_time = utils.good_timedelta(utils.now(), datetime.timedelta(seconds=20))
if not self._threads:
name = "telegram_{}send".format(f"{self.player_name}_" if self.player_name else "")
name = f"telegram_{f'{self.player_name}_' if self.player_name else ''}send"
send_thread = threading.Thread(target=self.__send_messages, name=name)
send_thread.start()
self._threads.append(send_thread)
@ -1006,7 +1019,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

View File

@ -53,14 +53,14 @@ class Country:
class Industries:
__by_name = {'food': 1, 'weapon': 2, 'house': 4, 'aircraft': 23,
'foodraw': 7, 'weaponraw': 12, 'houseraw': 18, 'aircraftraw': 24,
__by_name = {'food': 1, 'weapon': 2, 'ticket': 3, 'house': 4, 'aircraft': 23,
'foodraw': 7, 'weaponraw': 12, 'houseraw': 18, 'aircraftraw': 24, 'airplaneraw': 24,
'frm': 7, 'wrm': 12, 'hrm': 18, 'arm': 24,
'frm q1': 7, 'frm q2': 8, 'frm q3': 9, 'frm q4': 10, 'frm q5': 11,
'wrm q1': 12, 'wrm q2': 13, 'wrm q3': 14, 'wrm q4': 15, 'wrm q5': 16,
'hrm q1': 18, 'hrm q2': 19, 'hrm q3': 20, 'hrm q4': 21, 'hrm q5': 22,
'arm q1': 24, 'arm q2': 25, 'arm q3': 26, 'arm q4': 27, 'arm q5': 28}
__by_id = {1: "Food", 2: "Weapon", 4: "House", 23: "Aircraft",
__by_id = {1: "Food", 2: "Weapon", 3: "Ticket", 4: "House", 23: "Aircraft",
7: "foodRaw", 8: "FRM q2", 9: "FRM q3", 10: "FRM q4", 11: "FRM q5",
12: "weaponRaw", 13: "WRM q2", 14: "WRM q3", 15: "WRM q4", 16: "WRM q5",
17: "houseRaw", 18: "houseRaw", 19: "HRM q2", 20: "HRM q3", 21: "HRM q4", 22: "HRM q5",

View File

@ -21,11 +21,11 @@ try:
except ImportError:
import json
__all__ = ['VERSION', 'calculate_hit', 'caught_error', 'date_from_eday', 'eday_from_date',
__all__ = ['VERSION', 'calculate_hit', 'caught_error', 'date_from_eday', 'eday_from_date', 'deprecation',
'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']
'process_error', 'process_warning', 'send_email', 'silent_sleep', 'slugify', 'write_file', 'write_request',
'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'
@ -93,7 +93,7 @@ def interactive_sleep(sleep_seconds: int):
# seconds = seconds % 30 if seconds % 30 else 30
else:
seconds = 1
sys.stdout.write("\rSleeping for {:4} more seconds".format(sleep_seconds))
sys.stdout.write(f"\rSleeping for {sleep_seconds:4} more seconds")
sys.stdout.flush()
time.sleep(seconds)
sleep_seconds -= seconds
@ -105,7 +105,7 @@ silent_sleep = time.sleep
def _write_log(msg, timestamp: bool = True, should_print: bool = False):
erep_time_now = now()
txt = "[{}] {}".format(erep_time_now.strftime('%F %T'), msg) if timestamp else msg
txt = f"[{erep_time_now.strftime('%F %T')}] {msg}" if timestamp else msg
txt = "\n".join(["\n".join(textwrap.wrap(line, 120)) for line in txt.splitlines()])
if not os.path.isdir('log'):
os.mkdir('log')
@ -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):
@ -171,10 +172,10 @@ def write_request(response: requests.Response, is_error: bool = False):
ext = "html"
if not is_error:
filename = "debug/requests/{}_{}.{}".format(now().strftime('%F_%H-%M-%S'), name, ext)
filename = f"debug/requests/{now().strftime('%F_%H-%M-%S')}_{name}.{ext}"
write_file(filename, html)
else:
return {"name": "{}_{}.{}".format(now().strftime('%F_%H-%M-%S'), name, ext),
return {"name": f"{now().strftime('%F_%H-%M-%S')}_{name}.{ext}",
"content": html.encode('utf-8'),
"mimetype": "application/json" if ext == "json" else "text/html"}
@ -195,14 +196,14 @@ def send_email(name: str, content: List[Any], player=None, local_vars: Dict[str,
if promo:
resp = {"name": "%s.html" % name, "mimetype": "text/html",
"content": file_content_template.format(title="Promo", body="<br/>".join(content))}
subject = "[eBot][{}] Promos: {}".format(now().strftime('%F %T'), name)
subject = f"[eBot][{now().strftime('%F %T')}] Promos: {name}"
elif captcha:
resp = {"name": "%s.html" % name, "mimetype": "text/html",
"content": file_content_template.format(title="ReCaptcha", body="<br/>".join(content))}
subject = "[eBot][{}] RECAPTCHA: {}".format(now().strftime('%F %T'), name)
subject = f"[eBot][{now().strftime('%F %T')}] RECAPTCHA: {name}"
else:
subject = "[eBot][%s] Bug trace: %s" % (now().strftime('%F %T'), name)
subject = f"[eBot][{now().strftime('%F %T')}] Bug trace: {name}"
body = "".join(traceback.format_stack()) + \
"\n\n" + \
@ -338,17 +339,8 @@ def calculate_hit(strength: float, rang: int, tp: bool, elite: bool, ne: bool, b
base_wpn = (1 + Decimal(str(weapon / 100)))
dmg = 10 * base_str * base_rnk * base_wpn
if elite:
dmg = dmg * 11 / 10
if tp and rang >= 70:
dmg = dmg * (1 + Decimal((rang - 69) / 10))
dmg = dmg * (100 + booster) / 100
if ne:
dmg = dmg * 11 / 10
return round(dmg, dec)
dmg = get_final_hit_dmg(dmg, rang, tp=tp, elite=elite, ne=ne, booster=booster)
return Decimal(round(dmg, dec))
def get_ground_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_patriot: bool = False,
@ -371,6 +363,19 @@ def get_air_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_pat
return calculate_hit(0, rang, true_patriot, elite, natural_enemy, booster, weapon_power)
def get_final_hit_dmg(base_dmg: Union[Decimal, float, str], rang: int,
tp: bool = False, elite: bool = False, ne: bool = False, booster: int = 0) -> Decimal:
dmg = Decimal(str(base_dmg))
if elite:
dmg = dmg * 11 / 10
if tp and rang >= 70:
dmg = dmg * (1 + Decimal((rang - 69) / 10))
dmg = dmg * (100 + booster) / 100
if ne:
dmg = dmg * 11 / 10
return Decimal(dmg)
# def _clear_up_battle_memory(battle):
# del battle.invader._battle, battle.defender._battle
# for div_id, division in battle.div.items():
@ -379,3 +384,19 @@ def get_air_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_pat
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

View File

@ -29,7 +29,7 @@ def _battle_launcher(player: Citizen):
"""
global CONFIG
finished_war_ids = {*[]}
war_data = CONFIG.get('start_battles', {})
war_data = CONFIG.get('battle_launcher', {})
war_ids = {int(war_id) for war_id in war_data.keys()}
next_attack_time = player.now
next_attack_time = next_attack_time.replace(minute=next_attack_time.minute // 5 * 5, second=0)
@ -45,7 +45,7 @@ def _battle_launcher(player: Citizen):
status = player.get_war_status(war_id)
if status.get('ended', False):
CONFIG['start_battles'].pop(war_id, None)
CONFIG['battle_launcher'].pop(war_id, None)
finished_war_ids.add(war_id)
continue
elif not status.get('can_attack'):
@ -64,7 +64,15 @@ def _battle_launcher(player: Citizen):
player.update_war_info()
battle_id = player.get_war_status(war_id).get("battle_id")
if battle_id is not None and battle_id in player.all_battles:
player.fight(battle_id, player.details.citizenship, hits)
battle = player.all_battles.get(battle_id)
for division in battle.div.values():
if division.div == player.division:
div = division
break
else:
player.report_error("Players division not found in the first round!")
break
player.fight(battle, div, battle.invader, hits)
break
player.sleep(1)
if attacked:
@ -89,7 +97,7 @@ def main():
player.set_debug(CONFIG.get('debug', False))
player.login()
if CONFIG.get('battle_launcher'):
name = "{}-battle_launcher-{}".format(player.name, threading.active_count() - 1)
name = f"{player.name}-battle_launcher-{threading.active_count() - 1}"
state_thread = threading.Thread(target=_battle_launcher, args=(player,), name=name)
state_thread.start()

View File

@ -6,7 +6,11 @@ CONFIG = {
'email': 'player@email.com',
'password': 'Pa$5w0rd!',
'interactive': True,
'debug': True
'debug': True,
'work': True,
'ot': True, # Work OverTime
'wam': True, # WorkAsManager
'train': True
}
@ -14,7 +18,10 @@ CONFIG = {
def main():
player = Citizen(email=CONFIG['email'], password=CONFIG['password'], auto_login=False)
player.config.interactive = CONFIG['interactive']
player.config.fight = CONFIG['fight']
player.config.work = CONFIG['work']
player.config.train = CONFIG['train']
player.config.ot = CONFIG['ot']
player.config.wam = CONFIG['wam']
player.set_debug(CONFIG.get('debug', False))
player.login()
now = player.now.replace(second=0, microsecond=0)
@ -76,7 +83,7 @@ def main():
tasks.update({'eat': next_time})
if tasks.get('ot', dt_max) <= now:
player.write_log("Doing task: ot")
player.write_log("Doing task: work overtime")
if now > player.my_companies.next_ot_time:
player.work_ot()
next_time = now + timedelta(minutes=60)
@ -87,15 +94,15 @@ def main():
closest_next_time = dt_max
next_tasks = []
for task, next_time in sorted(tasks.items(), key=lambda s: s[1]):
next_tasks.append("{}: {}".format(next_time.strftime('%F %T'), task))
next_tasks.append(f"{next_time.strftime('%F %T')}: {task}")
if next_time < closest_next_time:
closest_next_time = next_time
sleep_seconds = int(utils.get_sleep_seconds(closest_next_time))
if sleep_seconds <= 0:
player.write_log(f"Loop detected! Offending task: '{next_tasks[0]}'")
player.write_log("My next Tasks and there time:\n" + "\n".join(sorted(next_tasks)))
player.write_log("Sleeping until (eRep): {} (sleeping for {}s)".format(
closest_next_time.strftime("%F %T"), sleep_seconds))
player.write_log(f"Sleeping until (eRep): {closest_next_time.strftime('%F %T')}"
f" (sleeping for {sleep_seconds}s)")
seconds_to_sleep = sleep_seconds if sleep_seconds > 0 else 0
player.sleep(seconds_to_sleep)
except Exception as e:

View File

@ -2,18 +2,18 @@ bump2version==1.0.1
coverage==5.3
edx-sphinx-theme==1.5.0
flake8==3.8.4
ipython>=7.18.1
ipython>=7.19.0
isort==5.6.4
pip==20.2.4
PyInstaller==4.0
pytz==2020.1
pytest==6.1.1
responses==0.12.0
setuptools==50.3.2
Sphinx==3.2.1
requests==2.24.0
pip==20.3.1
PyInstaller==4.1
pytz==2020.4
pytest==6.1.2
responses==0.12.1
setuptools==51.0.0
Sphinx==3.3.1
requests==2.25.0
PySocks==1.7.1
tox==3.20.1
twine==3.2.0
watchdog==0.10.3
wheel==0.35.1
watchdog==1.0.0
wheel==0.36.1

View File

@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.22.1
current_version = 0.23.3.2
commit = True
tag = True
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.?(?P<dev>\d+)?

View File

@ -12,16 +12,16 @@ with open('HISTORY.rst') as history_file:
history = history_file.read()
requirements = [
'pytz==2020.1',
'requests==2.24.0',
'pytz==2020.4',
'requests==2.25.0',
'PySocks==1.7.1'
]
setup_requirements = []
test_requirements = [
"pytest==6.1.0",
"responses==0.12.0"
"pytest==6.1.2",
"responses==0.12.1"
]
setup(
@ -45,11 +45,11 @@ setup(
keywords='erepublik',
name='eRepublik',
packages=find_packages(include=['erepublik']),
python_requires='>=3.6, <4',
python_requires='>=3.7, <4',
setup_requires=setup_requirements,
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/eeriks/erepublik/',
version='0.22.1',
version='0.23.3.2',
zip_safe=False,
)

View File

@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-
"""Tests for `erepublik` package."""
from typing import Callable
from erepublik import Citizen
@ -66,7 +65,8 @@ class TestErepublik(unittest.TestCase):
self.assertEqual(self.citizen.next_reachable_energy, 0)
def test_should_fight(self):
is_wc_close: Callable[[], bool] = lambda: self.citizen.max_time_till_full_ff > self.citizen.time_till_week_change
def is_wc_close():
return self.citizen.max_time_till_full_ff > self.citizen.time_till_week_change
self.citizen.config.fight = False
self.assertEqual(self.citizen.should_fight(), (0, "Fighting not allowed!", False))