Compare commits
76 Commits
Author | SHA1 | Date | |
---|---|---|---|
f73f2b7b9f | |||
f6433908b4 | |||
2fd317153f | |||
256a180bd6 | |||
c7dbeb2078 | |||
8e5ae0320a | |||
5c258d7aae | |||
75b43fc455 | |||
2362dc51e8 | |||
a2cf479135 | |||
00b87dc832 | |||
0dd1ae9ac5 | |||
76bd40c655 | |||
15e6deebda | |||
69d0e7df0a | |||
4f92894ab6 | |||
9c64bfac0f | |||
1f07f2e270 | |||
71d204843d | |||
d9305214eb | |||
5556d5f772 | |||
1c47d169d2 | |||
ef44787bad | |||
42431134e1 | |||
bedaeeefd1 | |||
bbf304aa99 | |||
a2447959e7 | |||
700bd8d98e | |||
3599dc40fc | |||
6ba727a781 | |||
7f1829a5d7 | |||
e374aa8a54 | |||
7edfa3b004 | |||
12aee23739 | |||
b7f8182ef5 | |||
39093accd0 | |||
aba8c15fd3 | |||
f294506a2d | |||
fd56c6c389 | |||
4f613ee5ac | |||
a7dd528597 | |||
24c755d414 | |||
13b639dc5a | |||
ec1141a46e | |||
77170433c2 | |||
4736f70203 | |||
48a27997ac | |||
90bec82630 | |||
aedfbf4465 | |||
66f459c692 | |||
ef27960ff1 | |||
c48af9a891 | |||
1abfdb71ac | |||
e060f67666 | |||
06d8d1c0b5 | |||
adda8dcb54 | |||
c7f084436d | |||
94a87091a4 | |||
c0b97f112d | |||
3d895bd085 | |||
d548d1bbf1 | |||
b1eefcc662 | |||
41798c446c | |||
074da3adbe | |||
6c9a9e920d | |||
ffa2fc109c | |||
f7f4028f32 | |||
e91705ce90 | |||
ca65a1ffe1 | |||
07c8881092 | |||
25e534f783 | |||
daa071f0f5 | |||
2f8120bd0d | |||
6b2c073abe | |||
c298d66086 | |||
bf971972bf |
@ -19,3 +19,8 @@ insert_final_newline = false
|
|||||||
|
|
||||||
[Makefile]
|
[Makefile]
|
||||||
indent_style = tab
|
indent_style = tab
|
||||||
|
|
||||||
|
[*.py]
|
||||||
|
line_length=120
|
||||||
|
multi_line_output=0
|
||||||
|
balanced_wrapping=True
|
||||||
|
8
.github/ISSUE_TEMPLATE.md
vendored
8
.github/ISSUE_TEMPLATE.md
vendored
@ -1,6 +1,6 @@
|
|||||||
* eRepublik script version:
|
* eRepublik script version:
|
||||||
* Python version:
|
* Python version:
|
||||||
* Operating System:
|
* Operating System:
|
||||||
|
|
||||||
### Description
|
### Description
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ Tell us what happened, what went wrong, and what you expected to happen.
|
|||||||
|
|
||||||
### What I Did
|
### What I Did
|
||||||
|
|
||||||
```
|
``` python
|
||||||
Paste the command(s) you ran and the output.
|
Paste the command(s) you ran and the output.
|
||||||
If there was a crash, please include the traceback here.
|
If there was a crash, please include the traceback here.
|
||||||
```
|
```
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -85,7 +85,7 @@ celerybeat-schedule
|
|||||||
|
|
||||||
# virtualenv
|
# virtualenv
|
||||||
.venv
|
.venv
|
||||||
venv/
|
venv*/
|
||||||
ENV/
|
ENV/
|
||||||
|
|
||||||
# Spyder project settings
|
# Spyder project settings
|
||||||
|
@ -5,7 +5,7 @@ python:
|
|||||||
- 3.7
|
- 3.7
|
||||||
- 3.6
|
- 3.6
|
||||||
|
|
||||||
# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
|
# Command to install dependencies, e.g. pip install -r requirements_dev.txt --use-mirrors
|
||||||
install: pip install -U tox-travis
|
install: pip install -U tox-travis
|
||||||
|
|
||||||
# Command to run tests, e.g. python setup.py test
|
# Command to run tests, e.g. python setup.py test
|
||||||
|
@ -23,12 +23,6 @@ If you are reporting a bug, please include:
|
|||||||
* Any details about your local setup that might be helpful in troubleshooting.
|
* Any details about your local setup that might be helpful in troubleshooting.
|
||||||
* Detailed steps to reproduce the bug.
|
* Detailed steps to reproduce the bug.
|
||||||
|
|
||||||
Fix Bugs
|
|
||||||
~~~~~~~~
|
|
||||||
|
|
||||||
Look through the GitHub issues for bugs. Anything tagged with "bug" and "help
|
|
||||||
wanted" is open to whoever wants to implement it.
|
|
||||||
|
|
||||||
Implement Features
|
Implement Features
|
||||||
~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@ -59,7 +53,7 @@ Get Started!
|
|||||||
|
|
||||||
Ready to contribute? Here's how to set up `erepublik` for local development.
|
Ready to contribute? Here's how to set up `erepublik` for local development.
|
||||||
|
|
||||||
1. Fork the `erepublik_script` repo on GitHub.
|
1. Fork the `erepublik` repo on GitHub.
|
||||||
2. Clone your fork locally::
|
2. Clone your fork locally::
|
||||||
|
|
||||||
$ git clone git@github.com:your_name_here/erepublik.git
|
$ git clone git@github.com:your_name_here/erepublik.git
|
||||||
@ -103,7 +97,7 @@ Before you submit a pull request, check that it meets these guidelines:
|
|||||||
your new functionality into a function with a docstring, and add the
|
your new functionality into a function with a docstring, and add the
|
||||||
feature to the list in README.rst.
|
feature to the list in README.rst.
|
||||||
3. The pull request should work for Python 3.7.1. Check
|
3. The pull request should work for Python 3.7.1. Check
|
||||||
https://travis-ci.org/eeriks/erepublik_script/pull_requests
|
https://travis-ci.org/eeriks/erepublik/pull_requests
|
||||||
and make sure that the tests pass for all supported Python versions.
|
and make sure that the tests pass for all supported Python versions.
|
||||||
|
|
||||||
Tips
|
Tips
|
||||||
@ -112,7 +106,7 @@ Tips
|
|||||||
To run a subset of tests::
|
To run a subset of tests::
|
||||||
|
|
||||||
|
|
||||||
$ python -m unittest tests.test_erepublik_script
|
$ python -m unittest tests.test_erepublik
|
||||||
|
|
||||||
Deploying
|
Deploying
|
||||||
---------
|
---------
|
||||||
|
30
HISTORY.rst
30
HISTORY.rst
@ -2,11 +2,40 @@
|
|||||||
History
|
History
|
||||||
=======
|
=======
|
||||||
|
|
||||||
|
0.19.0 (2020-01-13)
|
||||||
|
-------------------
|
||||||
|
* Created method for current products on sale.
|
||||||
|
* Updated inventory to also include products on sale
|
||||||
|
* set_default_weapon() - eRepublik should return list with all available weapon qualities, but when a battle is just launched, they return only dict with barehands
|
||||||
|
* fight() - no longer calls self.set_default_weapon()
|
||||||
|
* find_battle_and_fight() - now calls self.set_default_weapon() just before fighting
|
||||||
|
* update_war_info() - returns previous battle list if responses 'last_updated' isn't more than 30s old
|
||||||
|
* get_battle_for_war(war_id) - returns Battle instance for specific war, if battle is active for given war
|
||||||
|
* Citizen.get_raw_surplus() fixed and moved to Citizen.my_companies.get_wam_raw_usage()
|
||||||
|
* Implemented division switching
|
||||||
|
* improved multi bomb deploy with auto traveling,
|
||||||
|
* Citizen.fight() simplified battle data gathering logic -> Citizen.shoot logic improved
|
||||||
|
|
||||||
|
|
||||||
|
0.17.0 (2019-11-21)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
* 12th anniversary's endpoints added
|
||||||
|
* Telegram message queue optimisation
|
||||||
|
* WC end fighting energy bugfix
|
||||||
|
* More strict fighting limiting before week change
|
||||||
|
* Improved and fixed ground damage booster usage
|
||||||
|
|
||||||
|
|
||||||
|
0.16.0 (2019-09-29)
|
||||||
|
-------------------
|
||||||
|
|
||||||
* Telegram notification integration
|
* Telegram notification integration
|
||||||
* Improved serialization to JSON
|
* Improved serialization to JSON
|
||||||
* When failing to do WAM because of not enough food - buy food
|
* When failing to do WAM because of not enough food - buy food
|
||||||
* Buy food buys 48h worth instead of 24h energy
|
* Buy food buys 48h worth instead of 24h energy
|
||||||
|
|
||||||
|
|
||||||
0.15.3 (2019-08-24)
|
0.15.3 (2019-08-24)
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
@ -25,6 +54,7 @@ History
|
|||||||
|
|
||||||
* Wall post comment endpoints updated with comment create endpoints.
|
* Wall post comment endpoints updated with comment create endpoints.
|
||||||
|
|
||||||
|
|
||||||
0.1.0 (2019-07-19)
|
0.1.0 (2019-07-19)
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
2
Makefile
2
Makefile
@ -43,6 +43,8 @@ clean-pyc: ## remove Python file artifacts
|
|||||||
find . -name '*.pyo' -exec rm -f {} +
|
find . -name '*.pyo' -exec rm -f {} +
|
||||||
find . -name '*~' -exec rm -f {} +
|
find . -name '*~' -exec rm -f {} +
|
||||||
find . -name '__pycache__' -exec rm -fr {} +
|
find . -name '__pycache__' -exec rm -fr {} +
|
||||||
|
rm -rf debug/
|
||||||
|
rm -rf log/
|
||||||
|
|
||||||
clean-test: ## remove test and coverage artifacts
|
clean-test: ## remove test and coverage artifacts
|
||||||
rm -fr .tox/
|
rm -fr .tox/
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
__author__ = """Eriks Karls"""
|
__author__ = """Eriks Karls"""
|
||||||
__email__ = 'eriks@72.lv'
|
__email__ = 'eriks@72.lv'
|
||||||
__version__ = '0.16.0'
|
__version__ = '0.19.4'
|
||||||
|
|
||||||
from erepublik import classes, utils
|
from erepublik import classes, utils
|
||||||
from erepublik.citizen import Citizen
|
from erepublik.citizen import Citizen
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -2,24 +2,27 @@ import datetime
|
|||||||
import decimal
|
import decimal
|
||||||
import hashlib
|
import hashlib
|
||||||
import random
|
import random
|
||||||
import sys
|
import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
from collections import defaultdict, deque
|
||||||
from collections import deque
|
from typing import Any, Dict, Iterable, List, Mapping, Tuple, Union
|
||||||
from json import JSONDecodeError, loads, JSONEncoder
|
|
||||||
from typing import Any, Dict, List, Union, Mapping, Iterable, Tuple
|
|
||||||
|
|
||||||
from requests import Response, Session, post
|
from requests import Response, Session, post
|
||||||
|
|
||||||
from erepublik import utils
|
from erepublik import utils
|
||||||
|
|
||||||
|
try:
|
||||||
|
import simplejson as json
|
||||||
|
except ImportError:
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
class ErepublikException(Exception):
|
class ErepublikException(Exception):
|
||||||
def __init__(self, message):
|
def __init__(self, message):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|
||||||
|
|
||||||
class ErepublikNetworkException(Exception):
|
class ErepublikNetworkException(ErepublikException):
|
||||||
def __init__(self, message, request):
|
def __init__(self, message, request):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
self.request = request
|
self.request = request
|
||||||
@ -41,7 +44,7 @@ class MyCompanies:
|
|||||||
"""
|
"""
|
||||||
:param holdings: Parsed JSON to dict from en/economy/myCompanies
|
:param holdings: Parsed JSON to dict from en/economy/myCompanies
|
||||||
"""
|
"""
|
||||||
self.holdings = {}
|
self.holdings.clear()
|
||||||
template = dict(id=0, num_factories=0, region_id=0, companies=[])
|
template = dict(id=0, num_factories=0, region_id=0, companies=[])
|
||||||
|
|
||||||
for holding_id, holding in holdings.items():
|
for holding_id, holding in holdings.items():
|
||||||
@ -58,18 +61,20 @@ class MyCompanies:
|
|||||||
"""
|
"""
|
||||||
:param companies: Parsed JSON to dict from en/economy/myCompanies
|
:param companies: Parsed JSON to dict from en/economy/myCompanies
|
||||||
"""
|
"""
|
||||||
self.companies = {}
|
self.companies.clear()
|
||||||
template = dict(id=None, quality=0, is_raw=False, resource_bonus=0, effective_bonus=0, raw_usage=0,
|
template = dict(id=None, quality=0, is_raw=False, resource_bonus=0, effective_bonus=0, raw_usage=0,
|
||||||
production=0, base_production=0, wam_enabled=False, can_work_as_manager=False,
|
base_production=0, wam_enabled=False, can_work_as_manager=False, industry_id=0, todays_works=0,
|
||||||
preset_own_work=0, already_worked=False, can_assign_employees=False, preset_works=0,
|
preset_own_work=0, already_worked=False, can_assign_employees=False, preset_works=0,
|
||||||
todays_works=0, holding_company_id=None, is_assigned_to_holding=False,
|
holding_company_id=None, is_assigned_to_holding=False, cannot_work_as_manager_reason=False)
|
||||||
cannot_work_as_manager_reason=False, industry_id=0)
|
|
||||||
|
|
||||||
for c_id, company in companies.items():
|
for c_id, company in companies.items():
|
||||||
tmp = {}
|
tmp = {}
|
||||||
for key in template.keys():
|
for key in template.keys():
|
||||||
if key in ['id', 'holding_company_id']:
|
if key in ['id', 'holding_company_id']:
|
||||||
company[key] = int(company[key])
|
company[key] = int(company[key])
|
||||||
|
elif key == "raw_usage":
|
||||||
|
if not company.get("is_raw") and company.get('upgrades'):
|
||||||
|
company[key] = company.get('upgrades').get(str(company["quality"])).get('raw_usage')
|
||||||
tmp.update({key: company[key]})
|
tmp.update({key: company[key]})
|
||||||
self.companies.update({int(c_id): tmp})
|
self.companies.update({int(c_id): tmp})
|
||||||
|
|
||||||
@ -77,9 +82,8 @@ class MyCompanies:
|
|||||||
for company_id, company_data in self.companies.items():
|
for company_id, company_data in self.companies.items():
|
||||||
if company_id not in self.holdings[company_data['holding_company_id']]['companies']:
|
if company_id not in self.holdings[company_data['holding_company_id']]['companies']:
|
||||||
self.holdings[company_data['holding_company_id']]['companies'].append(company_id)
|
self.holdings[company_data['holding_company_id']]['companies'].append(company_id)
|
||||||
else:
|
for holding_id in self.holdings:
|
||||||
for holding_id in self.holdings:
|
self.holdings[holding_id]['companies'].sort()
|
||||||
self.holdings[holding_id]['companies'].sort()
|
|
||||||
|
|
||||||
def get_employable_factories(self) -> Dict[int, int]:
|
def get_employable_factories(self) -> Dict[int, int]:
|
||||||
ret = {}
|
ret = {}
|
||||||
@ -161,13 +165,38 @@ class MyCompanies:
|
|||||||
|
|
||||||
raise ErepublikException("Wrong function call")
|
raise ErepublikException("Wrong function call")
|
||||||
|
|
||||||
@property
|
def get_wam_raw_usage(self) -> Dict[str, float]:
|
||||||
def __dict__(self):
|
frm = 0.00
|
||||||
ret = {}
|
wrm = 0.00
|
||||||
for key in dir(self):
|
for company in self.companies.values():
|
||||||
if not key.startswith('_'):
|
if company['wam_enabled']:
|
||||||
ret[key] = getattr(self, key)
|
effective_bonus = float(company["effective_bonus"])
|
||||||
return ret
|
base_prod = float(company["base_production"])
|
||||||
|
raw = base_prod * effective_bonus / 100
|
||||||
|
if not company["is_raw"]:
|
||||||
|
raw *= -company["raw_usage"]
|
||||||
|
if company["industry_id"] in [1, 7, 8, 9, 10, 11]:
|
||||||
|
frm += raw
|
||||||
|
elif company["industry_id"] in [2, 12, 13, 14, 15, 16]:
|
||||||
|
wrm += raw
|
||||||
|
return {'frm': int(frm * 1000) / 1000, 'wrm': int(wrm * 1000) / 1000}
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
name = []
|
||||||
|
for holding_id in sorted(self.holdings.keys()):
|
||||||
|
if not holding_id:
|
||||||
|
name.append(f"Unassigned - {len(self.holdings[0]['companies'])}")
|
||||||
|
else:
|
||||||
|
name.append(f"{holding_id} - {len(self.holdings[holding_id]['companies'])}")
|
||||||
|
return " | ".join(name)
|
||||||
|
|
||||||
|
# @property
|
||||||
|
# def __dict__(self):
|
||||||
|
# ret = {}
|
||||||
|
# for key in dir(self):
|
||||||
|
# if not key.startswith('_'):
|
||||||
|
# ret[key] = getattr(self, key)
|
||||||
|
# return ret
|
||||||
|
|
||||||
|
|
||||||
class SlowRequests(Session):
|
class SlowRequests(Session):
|
||||||
@ -175,19 +204,19 @@ class SlowRequests(Session):
|
|||||||
timeout = datetime.timedelta(milliseconds=500)
|
timeout = datetime.timedelta(milliseconds=500)
|
||||||
uas = [
|
uas = [
|
||||||
# Chrome
|
# Chrome
|
||||||
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36',
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
|
||||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
|
||||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
|
||||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36',
|
|
||||||
# FireFox
|
# FireFox
|
||||||
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0',
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0',
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0',
|
||||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0',
|
'Mozilla/5.0 (X11; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0',
|
||||||
'Mozilla/5.0 (X11; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0',
|
'Mozilla/5.0 (X11; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0',
|
||||||
'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0',
|
'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0',
|
||||||
'Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0',
|
|
||||||
]
|
]
|
||||||
debug = False
|
debug = False
|
||||||
|
|
||||||
@ -202,7 +231,7 @@ class SlowRequests(Session):
|
|||||||
@property
|
@property
|
||||||
def __dict__(self):
|
def __dict__(self):
|
||||||
return dict(last_time=self.last_time, timeout=self.timeout, user_agent=self.headers['User-Agent'],
|
return dict(last_time=self.last_time, timeout=self.timeout, user_agent=self.headers['User-Agent'],
|
||||||
request_log_name=self.request_log_name)
|
request_log_name=self.request_log_name, debug=self.debug)
|
||||||
|
|
||||||
def request(self, method, url, *args, **kwargs):
|
def request(self, method, url, *args, **kwargs):
|
||||||
self._slow_down_requests()
|
self._slow_down_requests()
|
||||||
@ -221,7 +250,10 @@ class SlowRequests(Session):
|
|||||||
def _log_request(self, url, method, data=None, json=None, params=None, **kwargs):
|
def _log_request(self, url, method, data=None, json=None, params=None, **kwargs):
|
||||||
if self.debug:
|
if self.debug:
|
||||||
args = {}
|
args = {}
|
||||||
args.update({'kwargs': kwargs})
|
kwargs.pop('allow_redirects', None)
|
||||||
|
if kwargs:
|
||||||
|
args.update({'kwargs': kwargs})
|
||||||
|
|
||||||
if data:
|
if data:
|
||||||
args.update({"data": data})
|
args.update({"data": data})
|
||||||
|
|
||||||
@ -245,18 +277,17 @@ class SlowRequests(Session):
|
|||||||
self._log_request(hist_resp.request.url, "REDIRECT")
|
self._log_request(hist_resp.request.url, "REDIRECT")
|
||||||
self._log_response(hist_resp.request.url, hist_resp, redirect=True)
|
self._log_response(hist_resp.request.url, hist_resp, redirect=True)
|
||||||
|
|
||||||
# TODO: Must thoroughly check response writing on windows systems
|
|
||||||
file_data = {
|
file_data = {
|
||||||
"path": 'debug/requests',
|
"path": 'debug/requests',
|
||||||
"time": self.last_time.strftime('%Y-%m-%d_%H-%M-%S'),
|
"time": self.last_time.strftime('%Y/%m/%d/%H-%M-%S'),
|
||||||
"name": utils.slugify(url[len(Citizen.url):]),
|
"name": utils.slugify(url[len(Citizen.url):]),
|
||||||
"extra": "_REDIRECT" if redirect else ""
|
"extra": "_REDIRECT" if redirect else ""
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loads(resp.text)
|
json.loads(resp.text)
|
||||||
file_data.update({"ext": "json"})
|
file_data.update({"ext": "json"})
|
||||||
except JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
file_data.update({"ext": "html"})
|
file_data.update({"ext": "html"})
|
||||||
|
|
||||||
filename = 'debug/requests/{time}_{name}{extra}.{ext}'.format(**file_data)
|
filename = 'debug/requests/{time}_{name}{extra}.{ext}'.format(**file_data)
|
||||||
@ -270,7 +301,7 @@ class Config:
|
|||||||
work = True
|
work = True
|
||||||
train = True
|
train = True
|
||||||
wam = False
|
wam = False
|
||||||
auto_sell: List[str] = list()
|
auto_sell: List[str] = None
|
||||||
auto_sell_all = False
|
auto_sell_all = False
|
||||||
employees = False
|
employees = False
|
||||||
fight = False
|
fight = False
|
||||||
@ -294,17 +325,52 @@ class Config:
|
|||||||
telegram_chat_id = 0
|
telegram_chat_id = 0
|
||||||
telegram_token = ""
|
telegram_token = ""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.auto_sell = []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wt(self):
|
def wt(self):
|
||||||
return self.work and self.train
|
return self.work and self.train
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.work = True
|
||||||
|
self.train = True
|
||||||
|
self.wam = False
|
||||||
|
self.auto_sell = list()
|
||||||
|
self.auto_sell_all = False
|
||||||
|
self.employees = False
|
||||||
|
self.fight = False
|
||||||
|
self.air = False
|
||||||
|
self.ground = False
|
||||||
|
self.all_in = False
|
||||||
|
self.next_energy = False
|
||||||
|
self.boosters = False
|
||||||
|
self.travel_to_fight = False
|
||||||
|
self.always_travel = False
|
||||||
|
self.epic_hunt = False
|
||||||
|
self.epic_hunt_ebs = False
|
||||||
|
self.rw_def_side = False
|
||||||
|
self.interactive = True
|
||||||
|
self.continuous_fighting = False
|
||||||
|
self.auto_buy_raw = False
|
||||||
|
self.force_wam = False
|
||||||
|
self.sort_battles_time = True
|
||||||
|
self.force_travel = False
|
||||||
|
self.telegram = True
|
||||||
|
self.telegram_chat_id = 0
|
||||||
|
self.telegram_token = ""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __dict__(self) -> Dict[str, Union[bool, str, List[str]]]:
|
def __dict__(self):
|
||||||
ret = {}
|
return dict(email=self.email, work=self.work, train=self.train, wam=self.wam,
|
||||||
for key in dir(self):
|
auto_sell=self.auto_sell, auto_sell_all=self.auto_sell_all, employees=self.employees,
|
||||||
if not key.startswith('_') and key not in ['email', 'password']:
|
fight=self.fight, air=self.air, ground=self.ground, all_in=self.all_in,
|
||||||
ret[key] = getattr(self, key)
|
next_energy=self.next_energy, boosters=self.boosters, travel_to_fight=self.travel_to_fight,
|
||||||
return ret
|
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,
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
class Energy:
|
class Energy:
|
||||||
@ -351,22 +417,14 @@ class Energy:
|
|||||||
def available(self):
|
def available(self):
|
||||||
return self.recovered + self.recoverable
|
return self.recovered + self.recoverable
|
||||||
|
|
||||||
@property
|
|
||||||
def __dict__(self):
|
|
||||||
ret = {}
|
|
||||||
for key in dir(self):
|
|
||||||
if not key.startswith('_'):
|
|
||||||
ret[key] = getattr(self, key)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
class Details:
|
||||||
class Details(object):
|
|
||||||
xp = 0
|
xp = 0
|
||||||
cc = 0
|
cc = 0
|
||||||
pp = 0
|
pp = 0
|
||||||
pin = None
|
pin = None
|
||||||
gold = 0
|
gold = 0
|
||||||
next_pp: List[int] = []
|
next_pp: List[int] = None
|
||||||
citizen_id = 0
|
citizen_id = 0
|
||||||
citizenship = 0
|
citizenship = 0
|
||||||
current_region = 0
|
current_region = 0
|
||||||
@ -377,6 +435,9 @@ class Details(object):
|
|||||||
daily_task_reward = False
|
daily_task_reward = False
|
||||||
mayhem_skills = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, }
|
mayhem_skills = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, }
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.next_pp = []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def xp_till_level_up(self):
|
def xp_till_level_up(self):
|
||||||
if self.xp >= 10000:
|
if self.xp >= 10000:
|
||||||
@ -403,14 +464,6 @@ class Details(object):
|
|||||||
next_level_up = (1 + (self.xp // 10)) * 10
|
next_level_up = (1 + (self.xp // 10)) * 10
|
||||||
return next_level_up - self.xp
|
return next_level_up - self.xp
|
||||||
|
|
||||||
@property
|
|
||||||
def __dict__(self):
|
|
||||||
ret = {}
|
|
||||||
for key in dir(self):
|
|
||||||
if not key.startswith('_'):
|
|
||||||
ret[key] = getattr(self, key)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
class Politics:
|
class Politics:
|
||||||
is_party_member: bool = False
|
is_party_member: bool = False
|
||||||
@ -421,16 +474,8 @@ class Politics:
|
|||||||
is_congressman: bool = False
|
is_congressman: bool = False
|
||||||
is_country_president: bool = False
|
is_country_president: bool = False
|
||||||
|
|
||||||
@property
|
|
||||||
def __dict__(self):
|
|
||||||
ret = {}
|
|
||||||
for key in dir(self):
|
|
||||||
if not key.startswith('_'):
|
|
||||||
ret[key] = getattr(self, key)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
class House:
|
||||||
class House(object):
|
|
||||||
quality = None
|
quality = None
|
||||||
unactivated_count = 0
|
unactivated_count = 0
|
||||||
active_untill = utils.good_timedelta(utils.now(), -datetime.timedelta(days=1))
|
active_untill = utils.good_timedelta(utils.now(), -datetime.timedelta(days=1))
|
||||||
@ -455,8 +500,8 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
"""
|
"""
|
||||||
self._req = SlowRequests()
|
self._req = SlowRequests()
|
||||||
|
|
||||||
def post(self, url: str, *args, **kwargs) -> Response:
|
def post(self, url: str, data=None, json=None, **kwargs) -> Response:
|
||||||
return self._req.post(url, *args, **kwargs)
|
return self._req.post(url, data, json, **kwargs)
|
||||||
|
|
||||||
def get(self, url: str, **kwargs) -> Response:
|
def get(self, url: str, **kwargs) -> Response:
|
||||||
return self._req.get(url, **kwargs)
|
return self._req.get(url, **kwargs)
|
||||||
@ -467,6 +512,9 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
def _get_military_battlefield_choose_side(self, battle: int, side: int) -> Response:
|
def _get_military_battlefield_choose_side(self, battle: int, side: int) -> Response:
|
||||||
return self.get("{}/military/battlefield-choose-side/{}/{}".format(self.url, battle, side))
|
return self.get("{}/military/battlefield-choose-side/{}/{}".format(self.url, battle, side))
|
||||||
|
|
||||||
|
def _get_military_show_weapons(self, battle: int) -> Response:
|
||||||
|
return self.get("{}/military/show-weapons".format(self.url), params={'_token': self.token, 'battleId': battle})
|
||||||
|
|
||||||
def _get_candidate_party(self, party_slug: str) -> Response:
|
def _get_candidate_party(self, party_slug: str) -> Response:
|
||||||
return self.post("{}/candidate/{}".format(self.url, party_slug))
|
return self.post("{}/candidate/{}".format(self.url, party_slug))
|
||||||
|
|
||||||
@ -476,9 +524,12 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
def _get_main_citizen_profile_json(self, player_id: int) -> Response:
|
def _get_main_citizen_profile_json(self, player_id: int) -> Response:
|
||||||
return self.get("{}/main/citizen-profile-json/{}".format(self.url, player_id))
|
return self.get("{}/main/citizen-profile-json/{}".format(self.url, player_id))
|
||||||
|
|
||||||
def _get_main_citizen_daily_assistant(self) -> Response:
|
def _get_main_citizen_notifications(self) -> Response:
|
||||||
return self.get("{}/main/citizenDailyAssistant".format(self.url))
|
return self.get("{}/main/citizenDailyAssistant".format(self.url))
|
||||||
|
|
||||||
|
def _get_main_citizen_daily_assistant(self) -> Response:
|
||||||
|
return self.get("{}/main/citizenNotifications".format(self.url))
|
||||||
|
|
||||||
def _get_main_city_data_residents(self, city: int, page: int = 1, params: Mapping[str, Any] = None) -> Response:
|
def _get_main_city_data_residents(self, city: int, page: int = 1, params: Mapping[str, Any] = None) -> Response:
|
||||||
if params is None:
|
if params is None:
|
||||||
params = {}
|
params = {}
|
||||||
@ -532,9 +583,8 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
def _get_military_campaigns(self) -> Response:
|
def _get_military_campaigns(self) -> Response:
|
||||||
return self.get("{}/military/campaigns-new/".format(self.url))
|
return self.get("{}/military/campaigns-new/".format(self.url))
|
||||||
|
|
||||||
def _get_military_show_weapons(self, battle_id: int) -> Response:
|
def _get_military_campaigns_json_list(self) -> Response:
|
||||||
params = {"_token": self.token, "battleId": battle_id}
|
return self.get("{}/military/campaignsJson/list".format(self.url))
|
||||||
return self.get("{}/military/show-weapons".format(self.url), params=params)
|
|
||||||
|
|
||||||
def _get_military_unit_data(self, unit_id: int, **kwargs) -> Response:
|
def _get_military_unit_data(self, unit_id: int, **kwargs) -> Response:
|
||||||
params = {"groupId": unit_id, "panel": "members", **kwargs}
|
params = {"groupId": unit_id, "panel": "members", **kwargs}
|
||||||
@ -590,6 +640,10 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
data = dict(_token=self.token, sideCountryId=side_id, battleId=battle_id)
|
data = dict(_token=self.token, sideCountryId=side_id, battleId=battle_id)
|
||||||
return self.post("{}/main/battlefieldTravel".format(self.url), data=data)
|
return self.post("{}/main/battlefieldTravel".format(self.url), data=data)
|
||||||
|
|
||||||
|
def _post_main_battlefield_change_division(self, battle_id: int, division_id: int) -> Response:
|
||||||
|
data = dict(_token=self.token, battleZoneId=division_id, battleId=battle_id)
|
||||||
|
return self.post("{}/main/battlefieldTravel".format(self.url), data=data)
|
||||||
|
|
||||||
def _post_main_buy_gold_items(self, currency: str, item: str, amount: int) -> Response:
|
def _post_main_buy_gold_items(self, currency: str, item: str, amount: int) -> Response:
|
||||||
data = dict(itemId=item, currency=currency, amount=amount, _token=self.token)
|
data = dict(itemId=item, currency=currency, amount=amount, _token=self.token)
|
||||||
return self.post("{}/main/buyGoldItems".format(self.url), data=data)
|
return self.post("{}/main/buyGoldItems".format(self.url), data=data)
|
||||||
@ -622,6 +676,10 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
data = dict(_token=self.token, articleId=article_id, amount=amount)
|
data = dict(_token=self.token, articleId=article_id, amount=amount)
|
||||||
return self.post("{}/main/donate-article".format(self.url), data=data)
|
return self.post("{}/main/donate-article".format(self.url), data=data)
|
||||||
|
|
||||||
|
def _post_main_global_alerts_close(self, alert_id: int) -> Response:
|
||||||
|
data = dict(_token=self.token, alert_id=alert_id)
|
||||||
|
return self.post("{}/main/global-alerts/close".format(self.url), data=data)
|
||||||
|
|
||||||
def _post_delete_message(self, msg_id: list) -> Response:
|
def _post_delete_message(self, msg_id: list) -> Response:
|
||||||
data = {"_token": self.token, "delete_message[]": msg_id}
|
data = {"_token": self.token, "delete_message[]": msg_id}
|
||||||
return self.post("{}/main/messages-delete".format(self.url), data)
|
return self.post("{}/main/messages-delete".format(self.url), data)
|
||||||
@ -760,6 +818,10 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
data = dict(type=kind, quality=quality, duration=duration, battleId=battle, _token=self.token)
|
data = dict(type=kind, quality=quality, duration=duration, battleId=battle, _token=self.token)
|
||||||
return self.post("{}/military/fight-activateBooster".format(self.url), data=data)
|
return self.post("{}/military/fight-activateBooster".format(self.url), data=data)
|
||||||
|
|
||||||
|
def _post_military_change_weapon(self, battle: int, battle_zone: int, weapon_level: int,) -> Response:
|
||||||
|
data = dict(battleId=battle, _token=self.token, battleZoneId=battle_zone, customizationLevel=weapon_level)
|
||||||
|
return self.post("{}/military/change-weapon".format(self.url), data=data)
|
||||||
|
|
||||||
def _post_login(self, email: str, password: str) -> Response:
|
def _post_login(self, email: str, password: str) -> Response:
|
||||||
data = dict(csrf_token=self.token, citizen_email=email, citizen_password=password, remember='on')
|
data = dict(csrf_token=self.token, citizen_email=email, citizen_password=password, remember='on')
|
||||||
return self.post("{}/login".format(self.url), data=data)
|
return self.post("{}/login".format(self.url), data=data)
|
||||||
@ -783,21 +845,16 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
data.update(page=page)
|
data.update(page=page)
|
||||||
return self.post("{}/military/battle-console".format(self.url), data=data)
|
return self.post("{}/military/battle-console".format(self.url), data=data)
|
||||||
|
|
||||||
def _post_military_change_weapon(self, battle_id: int, battle_zone_id: int, customization_level: int) -> Response:
|
|
||||||
data = dict(_token=self.token, battleZoneId=battle_zone_id, battleId=battle_id,
|
|
||||||
customizationLevel=customization_level)
|
|
||||||
return self.post("{}/military/change-weapon".format(self.url), data=data)
|
|
||||||
|
|
||||||
def _post_military_deploy_bomb(self, battle_id: int, bomb_id: int) -> Response:
|
def _post_military_deploy_bomb(self, battle_id: int, bomb_id: int) -> Response:
|
||||||
data = dict(battleId=battle_id, bombId=bomb_id, _token=self.token)
|
data = dict(battleId=battle_id, bombId=bomb_id, _token=self.token)
|
||||||
return self.post("{}/military/deploy-bomb".format(self.url), data=data)
|
return self.post("{}/military/deploy-bomb".format(self.url), data=data)
|
||||||
|
|
||||||
def _post_military_fight_air(self, battle_id: int, side_id: int) -> Response:
|
def _post_military_fight_air(self, battle_id: int, side_id: int, zone_id: int) -> Response:
|
||||||
data = dict(sideId=side_id, battleId=battle_id, _token=self.token)
|
data = dict(sideId=side_id, battleId=battle_id, _token=self.token, battleZoneId=zone_id)
|
||||||
return self.post("{}/military/fight-shoooot/{}".format(self.url, battle_id), data=data)
|
return self.post("{}/military/fight-shoooot/{}".format(self.url, battle_id), data=data)
|
||||||
|
|
||||||
def _post_military_fight_ground(self, battle_id: int, side_id: int) -> Response:
|
def _post_military_fight_ground(self, battle_id: int, side_id: int, zone_id: int) -> Response:
|
||||||
data = dict(sideId=side_id, battleId=battle_id, _token=self.token)
|
data = dict(sideId=side_id, battleId=battle_id, _token=self.token, battleZoneId=zone_id)
|
||||||
return self.post("{}/military/fight-shooot/{}".format(self.url, battle_id), data=data)
|
return self.post("{}/military/fight-shooot/{}".format(self.url, battle_id), data=data)
|
||||||
|
|
||||||
def _post_military_group_missions(self) -> Response:
|
def _post_military_group_missions(self) -> Response:
|
||||||
@ -897,29 +954,61 @@ Class for unifying eRepublik known endpoints and their required/optional paramet
|
|||||||
data = {"_token": self.token, "post_message": body}
|
data = {"_token": self.token, "post_message": body}
|
||||||
return self.post("{}/main/wall-post/create/json".format(self.url), data=data)
|
return self.post("{}/main/wall-post/create/json".format(self.url), data=data)
|
||||||
|
|
||||||
|
def _post_main_wall_post_automatic(self, **kwargs) -> Response:
|
||||||
|
kwargs.update(_token=self.token)
|
||||||
|
return self.post("{}/main/wall-post/create/json".format(self.url), data=kwargs)
|
||||||
|
|
||||||
def _post_main_wall_post_retrieve(self) -> Response:
|
def _post_main_wall_post_retrieve(self) -> Response:
|
||||||
data = {"_token": self.token, "page": 1, "switchedFrom": False}
|
data = {"_token": self.token, "page": 1, "switchedFrom": False}
|
||||||
return self.post("{}/main/wall-post/retrieve/json".format(self.url), data=data)
|
return self.post("{}/main/wall-post/retrieve/json".format(self.url), data=data)
|
||||||
|
|
||||||
|
# 12th anniversary endpoints
|
||||||
|
def _get_anniversary_quest_data(self) -> Response:
|
||||||
|
return self.get("{}/main/anniversaryQuestData".format(self.url))
|
||||||
|
|
||||||
|
def _post_map_rewards_unlock(self, node_id: int) -> Response:
|
||||||
|
data = {'nodeId': node_id, '_token': self.token}
|
||||||
|
return self.post("{}/main/map-rewards-unlock".format(self.url), data=data)
|
||||||
|
|
||||||
|
def _post_map_rewards_speedup(self, node_id: int, currency_amount: int) -> Response:
|
||||||
|
data = {'nodeId': node_id, '_token': self.token, "currencyCost": currency_amount}
|
||||||
|
return self.post("{}/main/map-rewards-speedup".format(self.url), data=data)
|
||||||
|
|
||||||
|
def _post_map_rewards_claim(self, node_id: int) -> Response:
|
||||||
|
data = {'nodeId': node_id, '_token': self.token}
|
||||||
|
return self.post("{}/main/map-rewards-claim".format(self.url), 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,
|
||||||
|
countryNameConfirm=utils.COUNTRY_LINK[attack_country_id])
|
||||||
|
return self.post("{}/{}/new-war".format(self.url, utils.COUNTRY_LINK[self_country_id]), data=data)
|
||||||
|
|
||||||
|
def _post_new_donation(self, country_id: int, amount: int, org_name: str, debate: str = "") -> Response:
|
||||||
|
data = dict(requirments=1, _token=self.token, debate=debate, currency=1, value=amount, commit='Propose',
|
||||||
|
type_name=org_name)
|
||||||
|
return self.post("{}/{}/new-donation".format(self.url, utils.COUNTRY_LINK[country_id]), data=data)
|
||||||
|
|
||||||
|
|
||||||
class Reporter:
|
class Reporter:
|
||||||
__to_update: List[Dict[Any, Any]] = []
|
__to_update: List[Dict[Any, Any]] = None
|
||||||
name: str = ""
|
name: str = ""
|
||||||
email: str = ""
|
email: str = ""
|
||||||
citizen_id: int = 0
|
citizen_id: int = 0
|
||||||
key: str = ""
|
key: str = ""
|
||||||
allowed: bool = False
|
allowed: bool = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __dict__(self):
|
||||||
|
return dict(name=self.name, email=self.email, citizen_id=self.citizen_id, key=self.key, allowed=self.allowed,
|
||||||
|
queue=self.__to_update)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._req = Session()
|
self._req = Session()
|
||||||
self.url = "https://api.erep.lv"
|
self.url = "https://api.erep.lv"
|
||||||
self._req.headers.update({"user-agent": "Bot reporter v2"})
|
self._req.headers.update({"user-agent": "Bot reporter v2"})
|
||||||
|
self.__to_update = []
|
||||||
self.__registered: bool = False
|
self.__registered: bool = False
|
||||||
|
|
||||||
@property
|
|
||||||
def __dict__(self):
|
|
||||||
return dict(allowed=self.allowed, __to_update=self.__to_update)
|
|
||||||
|
|
||||||
def do_init(self, name: str = "", email: str = "", citizen_id: int = 0):
|
def do_init(self, name: str = "", email: str = "", citizen_id: int = 0):
|
||||||
self.name: str = name
|
self.name: str = name
|
||||||
self.email: str = email
|
self.email: str = email
|
||||||
@ -979,16 +1068,16 @@ class Reporter:
|
|||||||
self.__to_update.append(json_data)
|
self.__to_update.append(json_data)
|
||||||
|
|
||||||
|
|
||||||
class MyJSONEncoder(JSONEncoder):
|
class MyJSONEncoder(json.JSONEncoder):
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
from erepublik.citizen import Citizen
|
from erepublik.citizen import Citizen
|
||||||
if isinstance(o, decimal.Decimal):
|
if isinstance(o, decimal.Decimal):
|
||||||
return float("{:.02f}".format(o))
|
return float("{:.02f}".format(o))
|
||||||
elif isinstance(o, datetime.datetime):
|
elif isinstance(o, datetime.datetime):
|
||||||
return dict(__type__='datetime', year=o.year, month=o.month, day=o.day, hour=o.hour, minute=o.minute,
|
return dict(__type__='datetime', date=o.strftime("%Y-%m-%d"), time=o.strftime("%H:%M:%S"),
|
||||||
second=o.second, microsecond=o.microsecond, tzinfo=o.tzinfo.zone if o.tzinfo else None)
|
tzinfo=o.tzinfo.zone if o.tzinfo else None)
|
||||||
elif isinstance(o, datetime.date):
|
elif isinstance(o, datetime.date):
|
||||||
return dict(__type__='date', year=o.year, month=o.month, day=o.day)
|
return dict(__type__='date', date=o.strftime("%Y-%m-%d"))
|
||||||
elif isinstance(o, datetime.timedelta):
|
elif isinstance(o, datetime.timedelta):
|
||||||
return dict(__type__='timedelta', days=o.days, seconds=o.seconds,
|
return dict(__type__='timedelta', days=o.days, seconds=o.seconds,
|
||||||
microseconds=o.microseconds, total_seconds=o.total_seconds())
|
microseconds=o.microseconds, total_seconds=o.total_seconds())
|
||||||
@ -996,7 +1085,7 @@ class MyJSONEncoder(JSONEncoder):
|
|||||||
return dict(headers=o.headers.__dict__, url=o.url, text=o.text)
|
return dict(headers=o.headers.__dict__, url=o.url, text=o.text)
|
||||||
elif hasattr(o, '__dict__'):
|
elif hasattr(o, '__dict__'):
|
||||||
return o.__dict__
|
return o.__dict__
|
||||||
elif isinstance(o, deque):
|
elif isinstance(o, (deque, set)):
|
||||||
return list(o)
|
return list(o)
|
||||||
elif isinstance(o, Citizen):
|
elif isinstance(o, Citizen):
|
||||||
return o.to_json()
|
return o.to_json()
|
||||||
@ -1015,73 +1104,83 @@ class BattleSide:
|
|||||||
self.allies = [int(ally) for ally in allies]
|
self.allies = [int(ally) for ally in allies]
|
||||||
self.deployed = [int(ally) for ally in deployed]
|
self.deployed = [int(ally) for ally in deployed]
|
||||||
|
|
||||||
@property
|
|
||||||
def __dict__(self):
|
|
||||||
ret = {}
|
|
||||||
for key in dir(self):
|
|
||||||
if not key.startswith('_'):
|
|
||||||
ret[key] = getattr(self, key)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
class BattleDivision:
|
class BattleDivision:
|
||||||
end: datetime.datetime
|
end: datetime.datetime
|
||||||
epic: bool
|
epic: bool
|
||||||
dom_pts: Dict[str, int] = None
|
dom_pts: Dict[str, int]
|
||||||
wall: Dict[str, Union[int, float]] = None
|
wall: Dict[str, Union[int, float]]
|
||||||
|
battle_zone_id: int
|
||||||
|
def_medal: Dict[str, int]
|
||||||
|
inv_medal: Dict[str, int]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def div_end(self) -> bool:
|
def div_end(self) -> bool:
|
||||||
return utils.now() >= self.end
|
return utils.now() >= self.end
|
||||||
|
|
||||||
def __init__(self, end: datetime.datetime, epic: bool, inv_pts: int, def_pts: int, wall_for: int, wall_dom: float):
|
def __init__(self, **kwargs):
|
||||||
self.end = end
|
"""Battle division helper class
|
||||||
self.epic = epic
|
|
||||||
self.dom_pts = dict({"inv": inv_pts, "def": def_pts})
|
:param kwargs: must contain keys:
|
||||||
self.wall = dict({"for": wall_for, "dom": wall_dom})
|
div_id: int, end: datetime.datetime, epic: bool, inv_pts: int, def_pts: int,
|
||||||
|
wall_for: int, wall_dom: float, def_medal: Tuple[int, int], inv_medal: Tuple[int, int]
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.battle_zone_id = kwargs.get("div_id", 0)
|
||||||
|
self.end = kwargs.get("end", 0)
|
||||||
|
self.epic = kwargs.get("epic", 0)
|
||||||
|
self.dom_pts = dict({"inv": kwargs.get("inv_pts", 0), "def": kwargs.get("def_pts", 0)})
|
||||||
|
self.wall = dict({"for": kwargs.get("wall_for", 0), "dom": kwargs.get("wall_dom", 0)})
|
||||||
|
self.def_medal = {"id": kwargs.get("def_medal", 0)[0], "dmg": kwargs.get("def_medal", 0)[1]}
|
||||||
|
self.inv_medal = {"id": kwargs.get("inv_medal", 0)[0], "dmg": kwargs.get("inv_medal", 0)[1]}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __dict__(self):
|
def id(self):
|
||||||
ret = {}
|
return self.battle_zone_id
|
||||||
for key in dir(self):
|
|
||||||
if not key.startswith('_'):
|
|
||||||
ret[key] = getattr(self, key)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
class Battle(object):
|
class Battle:
|
||||||
id: int = 0
|
id: int
|
||||||
war_id: int = 0
|
war_id: int
|
||||||
zone_id: int = 0
|
zone_id: int
|
||||||
is_rw: bool = False
|
is_rw: bool
|
||||||
is_dict_lib: bool = False
|
is_dict_lib: bool
|
||||||
start: datetime.datetime = None
|
start: datetime.datetime
|
||||||
invader: BattleSide = None
|
invader: BattleSide
|
||||||
defender: BattleSide = None
|
defender: BattleSide
|
||||||
div: Dict[int, BattleDivision] = None
|
div: Dict[int, BattleDivision]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_air(self) -> bool:
|
def is_air(self) -> bool:
|
||||||
return not bool(self.zone_id % 4)
|
return not bool(self.zone_id % 4)
|
||||||
|
|
||||||
def __init__(self, battle: Dict[str, Any]):
|
def __init__(self, battle: Dict[str, Any]):
|
||||||
self.id = int(battle.get('id', 0))
|
"""Object representing eRepublik battle.
|
||||||
self.war_id = int(battle.get('war_id', 0))
|
|
||||||
self.zone_id = int(battle.get('zone_id', 0))
|
:param battle: Dict object for single battle from '/military/campaignsJson/list' response's 'battles' object
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.id = int(battle.get('id'))
|
||||||
|
self.war_id = int(battle.get('war_id'))
|
||||||
|
self.zone_id = int(battle.get('zone_id'))
|
||||||
self.is_rw = bool(battle.get('is_rw'))
|
self.is_rw = bool(battle.get('is_rw'))
|
||||||
self.is_as = bool(battle.get('is_as'))
|
self.is_as = bool(battle.get('is_as'))
|
||||||
self.is_dict_lib = bool(battle.get('is_dict')) or bool(battle.get('is_lib'))
|
self.is_dict_lib = bool(battle.get('is_dict')) or bool(battle.get('is_lib'))
|
||||||
self.start = datetime.datetime.fromtimestamp(int(battle.get('start', 0)), tz=utils.erep_tz)
|
self.start = datetime.datetime.fromtimestamp(int(battle.get('start', 0)), tz=utils.erep_tz)
|
||||||
|
|
||||||
self.invader = BattleSide(battle.get('inv', {}).get('id'), battle.get('inv', {}).get('points'),
|
self.invader = BattleSide(
|
||||||
[row.get('id') for row in battle.get('inv', {}).get('ally_list')],
|
battle.get('inv', {}).get('id'), battle.get('inv', {}).get('points'),
|
||||||
[row.get('id') for row in battle.get('inv', {}).get('ally_list') if row['deployed']])
|
[row.get('id') for row in battle.get('inv', {}).get('ally_list')],
|
||||||
|
[row.get('id') for row in battle.get('inv', {}).get('ally_list') if row['deployed']]
|
||||||
|
)
|
||||||
|
|
||||||
self.defender = BattleSide(battle.get('def', {}).get('id'), battle.get('def', {}).get('points'),
|
self.defender = BattleSide(
|
||||||
[row.get('id') for row in battle.get('def', {}).get('ally_list')],
|
battle.get('def', {}).get('id'), battle.get('def', {}).get('points'),
|
||||||
[row.get('id') for row in battle.get('def', {}).get('ally_list') if row['deployed']])
|
[row.get('id') for row in battle.get('def', {}).get('ally_list')],
|
||||||
|
[row.get('id') for row in battle.get('def', {}).get('ally_list') if row['deployed']]
|
||||||
|
)
|
||||||
|
|
||||||
self.div = {}
|
self.div = defaultdict(BattleDivision)
|
||||||
for div, data in battle.get('div', {}).items():
|
for div, data in battle.get('div', {}).items():
|
||||||
div = int(data.get('div'))
|
div = int(data.get('div'))
|
||||||
if data.get('end'):
|
if data.get('end'):
|
||||||
@ -1089,11 +1188,18 @@ class Battle(object):
|
|||||||
else:
|
else:
|
||||||
end = utils.localize_dt(datetime.datetime.max - datetime.timedelta(days=1))
|
end = utils.localize_dt(datetime.datetime.max - datetime.timedelta(days=1))
|
||||||
|
|
||||||
battle_div = BattleDivision(
|
if not data['stats']['def']:
|
||||||
end=end, epic=data.get('epic_type') in [1, 5],
|
def_medal = (0, 0)
|
||||||
inv_pts=data.get('dom_pts').get("inv"), def_pts=data.get('dom_pts').get("def"),
|
else:
|
||||||
wall_for=data.get('wall').get("for"), wall_dom=data.get('wall').get("dom")
|
def_medal = (data['stats']['def']['citizenId'], data['stats']['def']['damage'])
|
||||||
)
|
if not data['stats']['inv']:
|
||||||
|
inv_medal = (0, 0)
|
||||||
|
else:
|
||||||
|
inv_medal = (data['stats']['inv']['citizenId'], data['stats']['inv']['damage'])
|
||||||
|
battle_div = BattleDivision(end=end, epic=data.get('epic_type') in [1, 5], div_id=data.get('id'),
|
||||||
|
inv_pts=data.get('dom_pts').get("inv"), def_pts=data.get('dom_pts').get("def"),
|
||||||
|
wall_for=data.get('wall').get("for"), wall_dom=data.get('wall').get("dom"),
|
||||||
|
def_medal=def_medal, inv_medal=inv_medal)
|
||||||
|
|
||||||
self.div.update({div: battle_div})
|
self.div.update({div: battle_div})
|
||||||
|
|
||||||
@ -1101,20 +1207,14 @@ class Battle(object):
|
|||||||
now = utils.now()
|
now = utils.now()
|
||||||
is_started = self.start < utils.now()
|
is_started = self.start < utils.now()
|
||||||
if is_started:
|
if is_started:
|
||||||
time_part = "{}".format(now - self.start)
|
time_part = " {}".format(now - self.start)
|
||||||
else:
|
else:
|
||||||
time_part = "- {}".format(self.start - now)
|
time_part = "-{}".format(self.start - now)
|
||||||
return "Battle {} | {:>21.21}:{:<21.21} | Round {:2} | Start {}".format(
|
|
||||||
self.id, utils.COUNTRIES[self.invader.id], utils.COUNTRIES[self.defender.id], self.zone_id, time_part
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
return f"Battle {self.id} | " \
|
||||||
def __dict__(self):
|
f"{utils.ISO_CC[self.invader.id]} : {utils.ISO_CC[self.defender.id]} | " \
|
||||||
ret = {}
|
f"Round {self.zone_id:2} | " \
|
||||||
for key in dir(self):
|
f"Round time {time_part}"
|
||||||
if not key.startswith('_'):
|
|
||||||
ret[key] = getattr(self, key)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
class EnergyToFight:
|
class EnergyToFight:
|
||||||
@ -1149,34 +1249,52 @@ class EnergyToFight:
|
|||||||
|
|
||||||
|
|
||||||
class TelegramBot:
|
class TelegramBot:
|
||||||
__initialized = False
|
__initialized: bool = False
|
||||||
__queue: List[str] = []
|
__queue: List[str]
|
||||||
chat_id = 0
|
chat_id: int = 0
|
||||||
api_url = ""
|
api_url: str = ""
|
||||||
player_name = ""
|
player_name: str = ""
|
||||||
__last_time: datetime.datetime = None
|
__thread_stopper: threading.Event
|
||||||
|
_last_time: datetime.datetime
|
||||||
|
_last_full_energy_report: datetime.datetime
|
||||||
|
_next_time: datetime.datetime
|
||||||
|
_threads: List[threading.Thread]
|
||||||
|
|
||||||
|
def __init__(self, stop_event: threading.Event = None):
|
||||||
|
self._threads = []
|
||||||
|
self.__queue = []
|
||||||
|
self.__thread_stopper = threading.Event() if stop_event is None else stop_event
|
||||||
|
self._last_full_energy_report = self._next_time = self._last_time = utils.good_timedelta(utils.now(), datetime.timedelta(hours=1))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __dict__(self):
|
||||||
|
return {'chat_id': self.chat_id, 'api_url': self.api_url, 'player': self.player_name,
|
||||||
|
'last_time': self._last_time, 'next_time': self._next_time, 'queue': self.__queue,
|
||||||
|
'initialized': self.__initialized, 'has_threads': bool(len(self._threads))}
|
||||||
|
|
||||||
def do_init(self, chat_id: int, token: str, player_name: str = ""):
|
def do_init(self, chat_id: int, token: str, player_name: str = ""):
|
||||||
self.chat_id = chat_id
|
self.chat_id = chat_id
|
||||||
self.api_url = "https://api.telegram.org/bot{}/sendMessage".format(token)
|
self.api_url = "https://api.telegram.org/bot{}/sendMessage".format(token)
|
||||||
self.player_name = player_name
|
self.player_name = player_name
|
||||||
self.__initialized = True
|
self.__initialized = True
|
||||||
self.__last_time = utils.good_timedelta(utils.now(), datetime.timedelta(minutes=-5))
|
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))
|
||||||
if self.__queue:
|
if self.__queue:
|
||||||
self.send_message("\n\n––––––––––––––––––––––\n\n".join(self.__queue))
|
self.send_message("\n\n––––––––––––––––––––––\n\n".join(self.__queue))
|
||||||
|
|
||||||
def send_message(self, message: str) -> bool:
|
def send_message(self, message: str) -> bool:
|
||||||
|
self.__queue.append(message)
|
||||||
if not self.__initialized:
|
if not self.__initialized:
|
||||||
self.__queue.append(message)
|
|
||||||
return True
|
return True
|
||||||
if self.player_name:
|
self._threads = [t for t in self._threads if t.is_alive()]
|
||||||
message = f"Player *{self.player_name}*\n" + message
|
self._next_time = utils.good_timedelta(utils.now(), datetime.timedelta(minutes=1))
|
||||||
if utils.good_timedelta(utils.now(), datetime.timedelta(seconds=-1)) <= self.__last_time:
|
if not self._threads:
|
||||||
tb = traceback.extract_stack()
|
name = "telegram_{}send".format(f"{self.player_name}_" if self.player_name else "")
|
||||||
message += "\n\n```\n{}\n```".format("\n".join([' File "{}", line {}, in {}\n'.format(l.filename, l.lineno, l.name) for l in tb]))
|
send_thread = threading.Thread(target=self.__send_messages, name=name)
|
||||||
response = post(self.api_url, json=dict(chat_id=self.chat_id, text=message, parse_mode="Markdown"))
|
send_thread.start()
|
||||||
self.__last_time = utils.now()
|
self._threads.append(send_thread)
|
||||||
return response.json().get('ok')
|
|
||||||
|
return True
|
||||||
|
|
||||||
def report_free_bhs(self, battles: List[Tuple[int, int, int, int, datetime.timedelta]]):
|
def report_free_bhs(self, battles: List[Tuple[int, int, int, int, datetime.timedelta]]):
|
||||||
battle_links = []
|
battle_links = []
|
||||||
@ -1195,8 +1313,26 @@ class TelegramBot:
|
|||||||
self.send_message("Free BHs:\n" + "\n".join(battle_links))
|
self.send_message("Free BHs:\n" + "\n".join(battle_links))
|
||||||
|
|
||||||
def report_full_energy(self, available: int, limit: int, interval: int):
|
def report_full_energy(self, available: int, limit: int, interval: int):
|
||||||
message = f"Full energy ({available}hp/{limit}hp +{interval}hp/6min)"
|
if (utils.now() - self._last_full_energy_report).total_seconds() >= 30 * 60:
|
||||||
self.send_message(message)
|
self._last_full_energy_report = utils.now()
|
||||||
|
message = f"Full energy ({available}hp/{limit}hp +{interval}hp/6min)"
|
||||||
|
self.send_message(message)
|
||||||
|
|
||||||
def report_medal(self, msg):
|
def report_medal(self, msg):
|
||||||
self.send_message(f"New award: *{msg}*")
|
self.send_message(f"New award: *{msg}*")
|
||||||
|
|
||||||
|
def __send_messages(self):
|
||||||
|
while self._next_time > utils.now():
|
||||||
|
if self.__thread_stopper.is_set():
|
||||||
|
break
|
||||||
|
self.__thread_stopper.wait(utils.get_sleep_seconds(self._next_time))
|
||||||
|
|
||||||
|
message = "\n\n––––––––––––––––––––––\n\n".join(self.__queue)
|
||||||
|
if self.player_name:
|
||||||
|
message = f"Player *{self.player_name}*\n" + message
|
||||||
|
response = post(self.api_url, json=dict(chat_id=self.chat_id, text=message, parse_mode="Markdown"))
|
||||||
|
self._last_time = utils.now()
|
||||||
|
if response.json().get('ok'):
|
||||||
|
self.__queue = []
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
@ -1,23 +1,33 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
from decimal import Decimal
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Union, Any, List, NoReturn, Mapping
|
from typing import Any, List, Mapping, NoReturn, Optional, Union
|
||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
__all__ = ["FOOD_ENERGY", "COMMIT_ID", "COUNTRIES", "erep_tz",
|
try:
|
||||||
|
import simplejson as json
|
||||||
|
except ImportError:
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["FOOD_ENERGY", "COMMIT_ID", "COUNTRIES", "erep_tz", 'COUNTRY_LINK',
|
||||||
"now", "localize_dt", "localize_timestamp", "good_timedelta", "eday_from_date", "date_from_eday",
|
"now", "localize_dt", "localize_timestamp", "good_timedelta", "eday_from_date", "date_from_eday",
|
||||||
"get_sleep_seconds", "interactive_sleep", "silent_sleep",
|
"get_sleep_seconds", "interactive_sleep", "silent_sleep",
|
||||||
"write_silent_log", "write_interactive_log", "get_file", "write_file",
|
"write_silent_log", "write_interactive_log", "get_file", "write_file",
|
||||||
"send_email", "normalize_html_json", "process_error", ]
|
"send_email", "normalize_html_json", "process_error", "process_warning", 'report_promo', 'calculate_hit']
|
||||||
|
|
||||||
|
if not sys.version_info >= (3, 7):
|
||||||
|
raise AssertionError('This script requires Python version 3.7 and higher\n'
|
||||||
|
'But Your version is v{}.{}.{}'.format(*sys.version_info))
|
||||||
|
|
||||||
FOOD_ENERGY = dict(q1=2, q2=4, q3=6, q4=8, q5=10, q6=12, q7=20)
|
FOOD_ENERGY = dict(q1=2, q2=4, q3=6, q4=8, q5=10, q6=12, q7=20)
|
||||||
COMMIT_ID = "7b92e19"
|
COMMIT_ID = "7b92e19"
|
||||||
@ -93,6 +103,15 @@ COUNTRY_LINK = {1: 'Romania', 9: 'Brazil', 11: 'France', 12: 'Germany', 13: 'Hun
|
|||||||
81: 'Republic-of-China-Taiwan', 166: 'United-Arab-Emirates', 167: 'Albania', 69: 'Bosnia-Herzegovina',
|
81: 'Republic-of-China-Taiwan', 166: 'United-Arab-Emirates', 167: 'Albania', 69: 'Bosnia-Herzegovina',
|
||||||
169: 'Armenia', 83: 'Belarus', 84: 'New-Zealand', 164: 'Saudi-Arabia', 170: 'Nigeria', }
|
169: 'Armenia', 83: 'Belarus', 84: 'New-Zealand', 164: 'Saudi-Arabia', 170: 'Nigeria', }
|
||||||
|
|
||||||
|
ISO_CC = {1: 'ROU', 9: 'BRA', 10: 'ITA', 11: 'FRA', 12: 'DEU', 13: 'HUN', 14: 'CHN', 15: 'ESP', 23: 'CAN', 24: 'USA',
|
||||||
|
26: 'MEX', 27: 'ARG', 28: 'VEN', 29: 'GBR', 30: 'CHE', 31: 'NLD', 32: 'BEL', 33: 'AUT', 34: 'CZE', 35: 'POL',
|
||||||
|
36: 'SVK', 37: 'NOR', 38: 'SWE', 39: 'FIN', 40: 'UKR', 41: 'RUS', 42: 'BGR', 43: 'TUR', 44: 'GRC', 45: 'JPN',
|
||||||
|
47: 'KOR', 48: 'IND', 49: 'IDN', 50: 'AUS', 51: 'ZAF', 52: 'MDA', 53: 'PRT', 54: 'IRL', 55: 'DNK', 56: 'IRN',
|
||||||
|
57: 'PAK', 58: 'ISR', 59: 'THA', 61: 'SVN', 63: 'HRV', 64: 'CHL', 65: 'SRB', 66: 'MYS', 67: 'PHL', 68: 'SGP',
|
||||||
|
69: 'BiH', 70: 'EST', 71: 'LVA', 72: 'LTU', 73: 'PRK', 74: 'URY', 75: 'PRY', 76: 'BOL', 77: 'PER', 78: 'COL',
|
||||||
|
79: 'MKD', 80: 'MNE', 81: 'TWN', 82: 'CYP', 83: 'BLR', 84: 'NZL', 164: 'SAU', 165: 'EGY', 166: 'UAE',
|
||||||
|
167: 'ALB', 168: 'GEO', 169: 'ARM', 170: 'NGA', 171: 'CUB'}
|
||||||
|
|
||||||
|
|
||||||
def now() -> datetime.datetime:
|
def now() -> datetime.datetime:
|
||||||
return datetime.datetime.now(erep_tz).replace(microsecond=0)
|
return datetime.datetime.now(erep_tz).replace(microsecond=0)
|
||||||
@ -113,6 +132,15 @@ def localize_dt(dt: Union[datetime.date, datetime.datetime]) -> datetime.datetim
|
|||||||
|
|
||||||
|
|
||||||
def good_timedelta(dt: datetime.datetime, td: datetime.timedelta) -> datetime.datetime:
|
def good_timedelta(dt: datetime.datetime, td: datetime.timedelta) -> datetime.datetime:
|
||||||
|
"""Normalize timezone aware datetime object after timedelta to correct jumps over DST switches
|
||||||
|
|
||||||
|
:param dt: Timezone aware datetime object
|
||||||
|
:type dt: datetime.datetime
|
||||||
|
:param td: timedelta object
|
||||||
|
:type td: datetime.timedelta
|
||||||
|
:return: datetime object with correct timezone when jumped over DST
|
||||||
|
:rtype: datetime.datetime
|
||||||
|
"""
|
||||||
return erep_tz.normalize(dt + td)
|
return erep_tz.normalize(dt + td)
|
||||||
|
|
||||||
|
|
||||||
@ -288,10 +316,48 @@ def normalize_html_json(js: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def process_error(log_info: str, name: str, exc_info: tuple, citizen=None, commit_id: str = None,
|
def process_error(log_info: str, name: str, exc_info: tuple, citizen=None, commit_id: str = None,
|
||||||
interactive: bool = False):
|
interactive: Optional[bool] = None):
|
||||||
"""
|
"""
|
||||||
Process error logging and email sending to developer
|
Process error logging and email sending to developer
|
||||||
:param interactive: Should print interactively
|
:param interactive: Should print interactively
|
||||||
|
:type interactive: bool
|
||||||
|
:param log_info: String to be written in output
|
||||||
|
:type log_info: str
|
||||||
|
:param name: String Instance name
|
||||||
|
:type name: str
|
||||||
|
:param exc_info: tuple output from sys.exc_info()
|
||||||
|
:type exc_info: tuple
|
||||||
|
:param citizen: Citizen instance
|
||||||
|
:type citizen: Citizen
|
||||||
|
:param commit_id: Code's version by commit id
|
||||||
|
:type commit_id: str
|
||||||
|
"""
|
||||||
|
type_, value_, traceback_ = exc_info
|
||||||
|
content = [log_info]
|
||||||
|
if commit_id:
|
||||||
|
content += ["Commit id: %s" % commit_id]
|
||||||
|
content += [str(value_), str(type_), ''.join(traceback.format_tb(traceback_))]
|
||||||
|
|
||||||
|
if interactive:
|
||||||
|
write_interactive_log(log_info)
|
||||||
|
elif interactive is not None:
|
||||||
|
write_silent_log(log_info)
|
||||||
|
trace = inspect.trace()
|
||||||
|
if trace:
|
||||||
|
trace = trace[-1][0].f_locals
|
||||||
|
if trace.get('__name__') == '__main__':
|
||||||
|
trace = {'commit_id': trace.get('COMMIT_ID'),
|
||||||
|
'interactive': trace.get('INTERACTIVE'),
|
||||||
|
'version': trace.get('__version__'),
|
||||||
|
'config': trace.get('CONFIG')}
|
||||||
|
else:
|
||||||
|
trace = dict()
|
||||||
|
send_email(name, content, citizen, local_vars=trace)
|
||||||
|
|
||||||
|
|
||||||
|
def process_warning(log_info: str, name: str, exc_info: tuple, citizen=None, commit_id: str = None):
|
||||||
|
"""
|
||||||
|
Process error logging and email sending to developer
|
||||||
:param log_info: String to be written in output
|
:param log_info: String to be written in output
|
||||||
:param name: String Instance name
|
:param name: String Instance name
|
||||||
:param exc_info: tuple output from sys.exc_info()
|
:param exc_info: tuple output from sys.exc_info()
|
||||||
@ -299,19 +365,17 @@ def process_error(log_info: str, name: str, exc_info: tuple, citizen=None, commi
|
|||||||
:param commit_id: Code's version by commit id
|
:param commit_id: Code's version by commit id
|
||||||
"""
|
"""
|
||||||
type_, value_, traceback_ = exc_info
|
type_, value_, traceback_ = exc_info
|
||||||
bugtrace = [] if not commit_id else ["Commit id: %s" % commit_id, ]
|
content = [log_info]
|
||||||
bugtrace += [str(value_), str(type_), ''.join(traceback.format_tb(traceback_))]
|
if commit_id:
|
||||||
|
content += ["Commit id: %s" % commit_id]
|
||||||
|
content += [str(value_), str(type_), ''.join(traceback.format_tb(traceback_))]
|
||||||
|
|
||||||
if interactive:
|
|
||||||
write_interactive_log(log_info)
|
|
||||||
else:
|
|
||||||
write_silent_log(log_info)
|
|
||||||
trace = inspect.trace()
|
trace = inspect.trace()
|
||||||
if trace:
|
if trace:
|
||||||
trace = trace[-1][0].f_locals
|
trace = trace[-1][0].f_locals
|
||||||
else:
|
else:
|
||||||
trace = dict()
|
trace = dict()
|
||||||
send_email(name, bugtrace, citizen, local_vars=trace)
|
send_email(name, content, citizen, local_vars=trace)
|
||||||
|
|
||||||
|
|
||||||
def report_promo(kind: str, time_untill: datetime.datetime) -> NoReturn:
|
def report_promo(kind: str, time_untill: datetime.datetime) -> NoReturn:
|
||||||
@ -332,3 +396,44 @@ def slugify(value, allow_unicode=False) -> str:
|
|||||||
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
|
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
|
||||||
value = re.sub(r'[^\w\s-]', '_', value).strip().lower()
|
value = re.sub(r'[^\w\s-]', '_', value).strip().lower()
|
||||||
return re.sub(r'[-\s]+', '-', value)
|
return re.sub(r'[-\s]+', '-', value)
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_hit(strength: float, rang: int, tp: bool, elite: bool, ne: bool, booster: int = 0,
|
||||||
|
weapon: int = 200, is_deploy: bool = False) -> Decimal:
|
||||||
|
dec = 3 if is_deploy else 0
|
||||||
|
base_str = (1 + Decimal(str(round(strength, 3))) / 400)
|
||||||
|
base_rnk = (1 + Decimal(str(rang / 5)))
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def get_ground_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_patriot: bool = False,
|
||||||
|
booster: int = 0, weapon_power: int = 200) -> Decimal:
|
||||||
|
r = requests.get(f"https://www.erepublik.com/en/main/citizen-profile-json/{citizen_id}").json()
|
||||||
|
rang = r['military']['militaryData']['ground']['rankNumber']
|
||||||
|
strength = r['military']['militaryData']['ground']['strength']
|
||||||
|
elite = r['citizenAttributes']['level'] > 100
|
||||||
|
if natural_enemy:
|
||||||
|
true_patriot = True
|
||||||
|
|
||||||
|
return calculate_hit(strength, rang, true_patriot, elite, natural_enemy, booster, weapon_power)
|
||||||
|
|
||||||
|
|
||||||
|
def get_air_hit_dmg_value(citizen_id: int, natural_enemy: bool = False, true_patriot: bool = False, booster: int = 0,
|
||||||
|
weapon_power: int = 0) -> Decimal:
|
||||||
|
r = requests.get(f"https://www.erepublik.com/en/main/citizen-profile-json/{citizen_id}").json()
|
||||||
|
rang = r['military']['militaryData']['aircraft']['rankNumber']
|
||||||
|
elite = r['citizenAttributes']['level'] > 100
|
||||||
|
return calculate_hit(0, rang, true_patriot, elite, natural_enemy, booster, weapon_power)
|
||||||
|
96
examples/battle_launcher.py
Normal file
96
examples/battle_launcher.py
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
import threading
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from erepublik import Citizen, utils
|
||||||
|
|
||||||
|
CONFIG = {
|
||||||
|
'email': 'player@email.com',
|
||||||
|
'password': 'Pa$5w0rd!',
|
||||||
|
'interactive': True,
|
||||||
|
'fight': True,
|
||||||
|
'debug': True,
|
||||||
|
'start_battles': {
|
||||||
|
121672: {"auto_attack": False, "regions": [661]},
|
||||||
|
125530: {"auto_attack": False, "regions": [259]},
|
||||||
|
125226: {"auto_attack": True, "regions": [549]},
|
||||||
|
124559: {"auto_attack": True, "regions": [176]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _battle_launcher(player: Citizen):
|
||||||
|
"""Launch battles. Check every 5th minute (0,5,10...45,50,55) if any battle could be started on specified regions
|
||||||
|
and after launching wait for 90 minutes before starting next attack so that all battles aren't launched at the same
|
||||||
|
time. If player is allowed to fight, do 100 hits on the first round in players division.
|
||||||
|
|
||||||
|
:param player: Logged in Citizen instance
|
||||||
|
":type player: Citizen
|
||||||
|
"""
|
||||||
|
global CONFIG
|
||||||
|
finished_war_ids = {*[]}
|
||||||
|
war_data = CONFIG.get('start_battles', {})
|
||||||
|
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)
|
||||||
|
while not player.stop_threads.is_set():
|
||||||
|
try:
|
||||||
|
attacked = False
|
||||||
|
player.update_war_info()
|
||||||
|
running_wars = {b.war_id for b in player.all_battles.values()}
|
||||||
|
for war_id in war_ids - finished_war_ids - running_wars:
|
||||||
|
war = war_data[str(war_id)]
|
||||||
|
war_regions = set(war.get('regions'))
|
||||||
|
auto_attack = war.get('auto_attack')
|
||||||
|
|
||||||
|
status = player.get_war_status(war_id)
|
||||||
|
if status.get('ended', False):
|
||||||
|
CONFIG['start_battles'].pop(str(war_id), None)
|
||||||
|
finished_war_ids.add(war_id)
|
||||||
|
continue
|
||||||
|
elif not status.get('can_attack'):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if auto_attack or (player.now.hour > 20 or player.now.hour < 2):
|
||||||
|
for reg in war_regions:
|
||||||
|
if attacked:
|
||||||
|
break
|
||||||
|
if reg in status.get('regions', {}).keys():
|
||||||
|
player.launch_attack(war_id, reg, status.get('regions', {}).get(reg))
|
||||||
|
attacked = True
|
||||||
|
hits = 100
|
||||||
|
if player.energy.food_fights >= hits and player.config.fight:
|
||||||
|
for _ in range(120):
|
||||||
|
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)
|
||||||
|
break
|
||||||
|
player.sleep(1)
|
||||||
|
if attacked:
|
||||||
|
break
|
||||||
|
if attacked:
|
||||||
|
break
|
||||||
|
war_ids -= finished_war_ids
|
||||||
|
if attacked:
|
||||||
|
next_attack_time = utils.good_timedelta(next_attack_time, timedelta(hours=1, minutes=30))
|
||||||
|
else:
|
||||||
|
next_attack_time = utils.good_timedelta(next_attack_time, timedelta(minutes=5))
|
||||||
|
player.stop_threads.wait(utils.get_sleep_seconds(next_attack_time))
|
||||||
|
except:
|
||||||
|
player.report_error("Task error: start_battles")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
player = Citizen(email=CONFIG['email'], password=CONFIG['password'], auto_login=False)
|
||||||
|
player.config.interactive = CONFIG['interactive']
|
||||||
|
player.config.fight = CONFIG['fight']
|
||||||
|
player.set_debug(CONFIG.get('debug', False))
|
||||||
|
player.login()
|
||||||
|
if CONFIG.get('start_battles'):
|
||||||
|
name = "{}-start_battles-{}".format(player.name, threading.active_count() - 1)
|
||||||
|
state_thread = threading.Thread(target=_battle_launcher, args=(player,), name=name)
|
||||||
|
state_thread.start()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
102
examples/eat_work_train.py
Normal file
102
examples/eat_work_train.py
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from erepublik import Citizen, utils
|
||||||
|
|
||||||
|
CONFIG = {
|
||||||
|
'email': 'player@email.com',
|
||||||
|
'password': 'Pa$5w0rd!',
|
||||||
|
'interactive': True,
|
||||||
|
'debug': True
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
player = Citizen(email=CONFIG['email'], password=CONFIG['password'], auto_login=False)
|
||||||
|
player.config.interactive = CONFIG['interactive']
|
||||||
|
player.config.fight = CONFIG['fight']
|
||||||
|
player.set_debug(CONFIG.get('debug', False))
|
||||||
|
player.login()
|
||||||
|
now = player.now.replace(second=0, microsecond=0)
|
||||||
|
dt_max = now.replace(year=9999)
|
||||||
|
tasks = {
|
||||||
|
'eat': now,
|
||||||
|
}
|
||||||
|
if player.config.work:
|
||||||
|
tasks.update({'work': now})
|
||||||
|
if player.config.train:
|
||||||
|
tasks.update({'train': now})
|
||||||
|
if player.config.ot:
|
||||||
|
tasks.update({'ot': now})
|
||||||
|
if player.config.wam:
|
||||||
|
tasks.update({'wam': now.replace(hour=14, minute=0)})
|
||||||
|
while True:
|
||||||
|
player.update_all()
|
||||||
|
if tasks.get('work', dt_max) <= now:
|
||||||
|
player.write_log("Doing task: work")
|
||||||
|
player.update_citizen_info()
|
||||||
|
player.work()
|
||||||
|
if player.config.ot:
|
||||||
|
tasks['ot'] = now
|
||||||
|
player.collect_daily_task()
|
||||||
|
next_time = utils.good_timedelta(now.replace(hour=0, minute=0, second=0), timedelta(days=1))
|
||||||
|
tasks.update({'work': next_time})
|
||||||
|
|
||||||
|
if tasks.get('train', dt_max) <= now:
|
||||||
|
player.write_log("Doing task: train")
|
||||||
|
player.update_citizen_info()
|
||||||
|
player.train()
|
||||||
|
player.collect_daily_task()
|
||||||
|
next_time = utils.good_timedelta(now.replace(hour=0, minute=0, second=0), timedelta(days=1))
|
||||||
|
tasks.update({'train': next_time})
|
||||||
|
|
||||||
|
if tasks.get('wam', dt_max) <= now:
|
||||||
|
player.write_log("Doing task: Work as manager")
|
||||||
|
success = player.work_wam()
|
||||||
|
player.eat()
|
||||||
|
if success:
|
||||||
|
next_time = utils.good_timedelta(now.replace(hour=14, minute=0, second=0, microsecond=0),
|
||||||
|
timedelta(days=1))
|
||||||
|
else:
|
||||||
|
next_time = utils.good_timedelta(now.replace(second=0, microsecond=0), timedelta(minutes=30))
|
||||||
|
|
||||||
|
tasks.update({'wam': next_time})
|
||||||
|
|
||||||
|
if tasks.get('eat', dt_max) <= now:
|
||||||
|
player.write_log("Doing task: eat")
|
||||||
|
player.eat()
|
||||||
|
|
||||||
|
if player.energy.food_fights > player.energy.limit // 10:
|
||||||
|
next_minutes = 12
|
||||||
|
else:
|
||||||
|
next_minutes = (player.energy.limit - 5 * player.energy.interval) // player.energy.interval * 6
|
||||||
|
|
||||||
|
next_time = player.energy.reference_time + timedelta(minutes=next_minutes)
|
||||||
|
tasks.update({'eat': next_time})
|
||||||
|
|
||||||
|
if tasks.get('ot', dt_max) <= now:
|
||||||
|
player.write_log("Doing task: ot")
|
||||||
|
if now > player.my_companies.next_ot_time:
|
||||||
|
player.work_ot()
|
||||||
|
next_time = now + timedelta(minutes=60)
|
||||||
|
else:
|
||||||
|
next_time = player.my_companies.next_ot_time
|
||||||
|
tasks.update({'ot': next_time})
|
||||||
|
|
||||||
|
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))
|
||||||
|
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))
|
||||||
|
seconds_to_sleep = sleep_seconds if sleep_seconds > 0 else 0
|
||||||
|
player.sleep(seconds_to_sleep)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -1,14 +1,16 @@
|
|||||||
pip==19.1.1
|
|
||||||
bumpversion==0.5.3
|
bumpversion==0.5.3
|
||||||
wheel==0.33.4
|
coverage==5.0.2
|
||||||
watchdog==0.9.0
|
edx-sphinx-theme==1.5.0
|
||||||
flake8==3.7.8
|
flake8==3.7.9
|
||||||
tox==3.13.2
|
ipython==7.11.1
|
||||||
coverage==4.5.3
|
isort==4.3.21
|
||||||
Sphinx==2.2.0
|
pip==19.3.1
|
||||||
twine==2.0.0
|
PyInstaller==3.6
|
||||||
ipython
|
pytz==2019.3
|
||||||
PyInstaller
|
|
||||||
pytz==2019.1
|
|
||||||
requests==2.22.0
|
requests==2.22.0
|
||||||
edx-sphinx-theme
|
setuptools==44.0.0
|
||||||
|
Sphinx==2.3.1
|
||||||
|
tox==3.14.3
|
||||||
|
twine==3.1.1
|
||||||
|
watchdog==0.9.0
|
||||||
|
wheel==0.33.6
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[bumpversion]
|
[bumpversion]
|
||||||
current_version = 0.16.0
|
current_version = 0.19.4
|
||||||
commit = True
|
commit = True
|
||||||
tag = True
|
tag = True
|
||||||
|
|
||||||
|
9
setup.py
9
setup.py
@ -11,11 +11,11 @@ with open('README.rst') as readme_file:
|
|||||||
with open('HISTORY.rst') as history_file:
|
with open('HISTORY.rst') as history_file:
|
||||||
history = history_file.read()
|
history = history_file.read()
|
||||||
|
|
||||||
requirements = ['pytz>=2019.2', 'requests>=2.22']
|
requirements = ['pytz==2019.3', 'requests==2.22.0']
|
||||||
|
|
||||||
setup_requirements = [ ]
|
setup_requirements = []
|
||||||
|
|
||||||
test_requirements = [ ]
|
test_requirements = []
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
author="Eriks Karls",
|
author="Eriks Karls",
|
||||||
@ -27,6 +27,7 @@ setup(
|
|||||||
'Natural Language :: English',
|
'Natural Language :: English',
|
||||||
'Programming Language :: Python :: 3',
|
'Programming Language :: Python :: 3',
|
||||||
'Programming Language :: Python :: 3.7',
|
'Programming Language :: Python :: 3.7',
|
||||||
|
'Programming Language :: Python :: 3.8',
|
||||||
],
|
],
|
||||||
description="Python package for automated eRepublik playing",
|
description="Python package for automated eRepublik playing",
|
||||||
entry_points={},
|
entry_points={},
|
||||||
@ -42,6 +43,6 @@ setup(
|
|||||||
test_suite='tests',
|
test_suite='tests',
|
||||||
tests_require=test_requirements,
|
tests_require=test_requirements,
|
||||||
url='https://github.com/eeriks/erepublik/',
|
url='https://github.com/eeriks/erepublik/',
|
||||||
version='0.16.0',
|
version='0.19.4',
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
)
|
)
|
||||||
|
@ -79,9 +79,9 @@ class TestErepublik(unittest.TestCase):
|
|||||||
self.citizen.energy.recovered = 3000
|
self.citizen.energy.recovered = 3000
|
||||||
self.citizen.energy.recoverable = 2950
|
self.citizen.energy.recoverable = 2950
|
||||||
self.citizen.energy.interval = 30
|
self.citizen.energy.interval = 30
|
||||||
self.assertEqual(self.citizen.should_fight(), 895)
|
self.assertEqual(self.citizen.should_fight(), 900)
|
||||||
self.citizen.my_companies.ff_lockdown = 160
|
self.citizen.my_companies.ff_lockdown = 160
|
||||||
self.assertEqual(self.citizen.should_fight(), 895)
|
self.assertEqual(self.citizen.should_fight(), 900)
|
||||||
self.citizen.my_companies.ff_lockdown = 0
|
self.citizen.my_companies.ff_lockdown = 0
|
||||||
|
|
||||||
# Level up reachable
|
# Level up reachable
|
||||||
|
Reference in New Issue
Block a user