Traveling improved for Citizen class

This commit is contained in:
Eriks Karls 2019-07-31 21:38:34 +03:00
parent 7927c162f8
commit 8911adb81c

View File

@ -541,8 +541,15 @@ class Citizen(classes.CitizenAPI):
def_allies = battle.defender.deployed + [battle.defender.id] def_allies = battle.defender.deployed + [battle.defender.id]
all_allies = inv_allies + def_allies all_allies = inv_allies + def_allies
if self.details.current_country not in all_allies: if self.details.current_country not in all_allies:
self._travel(battle.defender.id, self.get_country_travel_region(battle.defender.id)) if self.details.current_country in battle.invader.allies:
side = battle.defender.id 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: else:
if self.details.current_country in inv_allies: if self.details.current_country in inv_allies:
side = battle.invader.id side = battle.invader.id
@ -675,14 +682,16 @@ class Citizen(classes.CitizenAPI):
if travel_needed: if travel_needed:
if battle.is_rw: if battle.is_rw:
self._travel(battle.defender.id, self.get_country_travel_region(battle.defender.id)) country_ids_to_travel = [battle.defender.id]
elif self.details.current_country not in battle.invader.allies: elif self.details.current_country in battle.invader.allies:
self.travel(battle_id=battle.id) country_ids_to_travel = battle.invader.deployed + [battle.invader.id]
side_id = battle.invader.id side_id = battle.invader.id
else: else:
self._travel(battle.defender.id, self.get_country_travel_region(battle.defender.id)) country_ids_to_travel = battle.defender.deployed + [battle.defender.id]
side_id = battle.defender.id side_id = battle.defender.id
if not self.travel_to_battle(battle_id, country_ids_to_travel):
break
self.fight(battle_id, side_id, battle.is_air) self.fight(battle_id, side_id, battle.is_air)
self.travel_to_residence() self.travel_to_residence()
self.collect_weekly_reward() self.collect_weekly_reward()
@ -903,7 +912,8 @@ class Citizen(classes.CitizenAPI):
if wam_list: if wam_list:
wam_holding = self.my_companies.holdings.get(wam_holding_id) wam_holding = self.my_companies.holdings.get(wam_holding_id)
if not self.details.current_region == wam_holding['region_id']: if not self.details.current_region == wam_holding['region_id']:
self.travel(holding_id=wam_holding_id, region_id=wam_holding['region_id']) if not self.travel_to_region(wam_holding['region_id']):
return False
response = self._post_economy_work("production", wam=wam_list, employ=employee_companies).json() response = self._post_economy_work("production", wam=wam_list, employ=employee_companies).json()
self.reporter.report_action("WORK_WAM_EMPLOYEES", response) self.reporter.report_action("WORK_WAM_EMPLOYEES", response)
if response.get("status"): if response.get("status"):
@ -966,15 +976,8 @@ class Citizen(classes.CitizenAPI):
self.post_market_offer(industry=self.available_industries[kind], amount=int(amount), self.post_market_offer(industry=self.available_industries[kind], amount=int(amount),
quality=int(quality), price=price) quality=int(quality), price=price)
def travel_to_residence(self):
self.update_citizen_info()
res_r = self.details.residence_region
if self.details.residence_country and res_r and not res_r == self.details.current_region:
self._travel(self.details.residence_country, self.details.residence_region)
def get_country_travel_region(self, country_id: int) -> int: def get_country_travel_region(self, country_id: int) -> int:
r = self.get_travel_regions(country_id=country_id).json() regions = self.get_travel_regions(country_id=country_id)
regions = r.get("regions")
regs = [] regs = []
if regions: if regions:
for region in regions.values(): for region in regions.values():
@ -985,50 +988,98 @@ class Citizen(classes.CitizenAPI):
else: else:
return 0 return 0
def travel(self, holding_id=0, battle_id=0, region_id=0): def _update_citizen_location(self, country_id: int, region_id: int):
r = self.get_travel_regions(holding_id, battle_id, region_id) self.details.current_region = region_id
regions = r.json()["regions"] self.details.current_country = country_id
closest_region = 99999
country_id = int(r.json()["preselectCountryId"]) def travel_to_residence(self):
region_id = int(r.json()["preselectRegionId"]) self.update_citizen_info()
if not any((region_id, country_id)): res_r = self.details.residence_region
for rid, info in regions.items(): if self.details.residence_country and res_r and not res_r == self.details.current_region:
if info.get("distanceInKm", 99999) < closest_region: self._travel(self.details.residence_country, self.details.residence_region)
closest_region = info.get("distanceInKm")
country_id = info.get("countryId") def travel_to_region(self, region_id: int) -> bool:
region_id = rid data = self._post_travel_data(region_id=region_id).json()
self._travel(country_id, region_id) if data.get('alreadyInRegion'):
return True
else:
r = self._travel(data.get('preselectCountryId'), region_id).json()
return r.get('message', '') == 'success'
def travel_to_country(self, country_id: int) -> bool:
data = self._post_travel_data(country_id=country_id, check="getCountryRegions").json()
regs = []
if data.get('regions'):
for region in data.get('regions').values():
if region['countryId'] == country_id: # Is not occupied by other country
regs.append((region['id'], region['distanceInKm']))
if regs:
region_id = min(regs, key=lambda _: int(_[1]))[0]
r = self._travel(country_id, region_id).json()
return r.get('message', '') == 'success'
else:
return False
def travel_to_holding(self, holding_id: int) -> bool:
data = self._post_travel_data(holding_id=holding_id).json()
if data.get('alreadyInRegion'):
return True
else:
r = self._travel(data.get('preselectCountryId'), data.get('preselectRegionId')).json()
return r.get('message', '') == 'success'
def travel_to_battle(self, battle_id: int, *allowed_countries: List[int]) -> bool:
data = self._post_travel_data(battle_id=battle_id).json()
regs = []
if data.get('regions'):
for region in data.get('regions').values():
if region['countryId'] in allowed_countries: # Is not occupied by other country
regs.append((region['distanceInKm'], region['id'], region['countryId']))
if regs:
reg = min(regs, key=lambda _: int(_[0]))
region_id = reg[1]
country_id = reg[2]
r = self._travel(country_id, region_id).json()
return r.get('message', '') == 'success'
else:
return False
def _travel(self, country_id: int, region_id: int = 0) -> Response: def _travel(self, country_id: int, region_id: int = 0) -> Response:
data = { data = {
"toCountryId": country_id, "toCountryId": country_id,
"inRegionId": region_id, "inRegionId": region_id,
"battleId": 0,
} }
return self._post_travel("moveAction", **data) return self._post_travel("moveAction", **data)
def get_travel_regions(self, holding_id: int = 0, battle_id: int = 0, region_id: int = 0, def get_travel_regions(self, holding_id: int = 1, battle_id: int = 0,
country_id: int = 0) -> Response: country_id: int = 0) -> Union[List[Any], Dict[str, Dict[str, Any]]]:
data = { d = self._post_travel_data(holdingId=holding_id, battleId=battle_id, regionId=0, countryId=country_id).json()
"holdingId": holding_id, return d.get('regions', [])
"battleId": battle_id,
"regionId": region_id, def get_travel_countries(self, holding_id: int = 1, battle_id: int = 0,
} region_id: int = 0) -> Union[List[Any], Dict[str, Dict[str, Any]]]:
data.update(countryId=country_id) d = self._post_travel_data(holdingId=holding_id, battleId=battle_id,
return self._post_travel_data(**data) regionId=region_id).json()
return [cid['id'] for cid in d['countries'].values() if cid['currentRegions']]
def parse_notifications(self, page: int = 1) -> list: def parse_notifications(self, page: int = 1) -> list:
response = self.get_message_alerts(page) community = self._get_notifications_ajax_community(page).json()
notifications = re.findall(r"<p class=\"smallpadded\">(.*?)</p>", response.text, re.M | re.I | re.S) system = self._get_notifications_ajax_system(page).json()
return notifications return community['alertsList'] + system['alertsList']
def delete_notifications(self): def delete_notifications(self):
def notification_ids(html): response = self._get_notifications_ajax_community().json()
return re.findall(r"id=\"delete_alert_(\d+)\"", html) while response['totalAlerts']:
self._post_messages_alert([_['id'] for _ in response['alertList']])
response = self._get_notifications_ajax_community().json()
response = self.get_message_alerts() response = self._get_notifications_ajax_system().json()
while notification_ids(response.text): while response['totalAlerts']:
response = self._post_messages_alert(notification_ids(response.text)) self._post_messages_alert([_['id'] for _ in response['alertList']])
response = self._get_notifications_ajax_system().json()
def collect_weekly_reward(self): def collect_weekly_reward(self):
self.update_weekly_challenge() self.update_weekly_challenge()
@ -1065,15 +1116,17 @@ class Citizen(classes.CitizenAPI):
"foodRaw": dict(q1=item_data.copy()), "weaponRaw": dict(q1=item_data.copy()), "foodRaw": dict(q1=item_data.copy()), "weaponRaw": dict(q1=item_data.copy()),
"houseRaw": dict(q1=item_data.copy()), "airplaneRaw": dict(q1=item_data.copy())} "houseRaw": dict(q1=item_data.copy()), "airplaneRaw": dict(q1=item_data.copy())}
countries = [country_id] if country_id else self.countries if country_id:
countries = [country_id]
else:
good_countries = self.get_travel_countries()
countries = [cid for cid in self.countries.keys() if cid in good_countries]
if product not in self.available_industries: if product not in self.available_industries:
self.write_log("Industry '{}' not implemented".format(product)) self.write_log("Industry '{}' not implemented".format(product))
return ret raise IndexError("Industry '{}' not implemented".format(product))
start_dt = self.now start_dt = self.now
for country in countries: for country in countries:
if not country_id and not self.get_country_travel_region(country):
continue
for industry in [product] or items: for industry in [product] or items:
for q in [quality] if quality else range(1, 8): for q in [quality] if quality else range(1, 8):
if (q > 1 and industry in q1_industries) or (q > 5 and industry == "house"): if (q > 1 and industry in q1_industries) or (q > 5 and industry == "house"):