All battle div div containing dicts where reference to the same object

This commit is contained in:
Eriks Karls 2019-08-01 17:23:24 +03:00
parent e5b7cde044
commit 048ce798dd
2 changed files with 23 additions and 19 deletions

View File

@ -5,7 +5,7 @@ import sys
import threading import threading
import time import time
from json import loads, dumps from json import loads, dumps
from typing import Dict, List, Tuple, Any, Union, Mapping from typing import Dict, List, Tuple, Any, Union
import requests import requests
from requests import Response, RequestException from requests import Response, RequestException
@ -18,9 +18,9 @@ class Citizen(classes.CitizenAPI):
division = 0 division = 0
all_battles: Dict[int, classes.Battle] = dict() all_battles: Dict[int, classes.Battle] = None
countries: Dict[int, Dict[str, Union[str, List[int]]]] = dict() countries: Dict[int, Dict[str, Union[str, List[int]]]] = None
__last_war_update_data = {} __last_war_update_data = None
__last_full_update: datetime.datetime = utils.now().min __last_full_update: datetime.datetime = utils.now().min
active_fs: bool = False active_fs: bool = False
@ -44,8 +44,8 @@ class Citizen(classes.CitizenAPI):
work_units = 0 work_units = 0
ot_points = 0 ot_points = 0
tg_contract = {} tg_contract = None
promos = {} promos = None
eday = 0 eday = 0
@ -293,7 +293,7 @@ class Citizen(classes.CitizenAPI):
html = self.r.text html = self.r.text
ugly_js = re.search(r"promotions: (\[{?.*}?]),\s+", html).group(1) ugly_js = re.search(r"promotions: (\[{?.*}?]),\s+", html).group(1)
promos = loads(utils.normalize_html_json(ugly_js)) promos = loads(utils.normalize_html_json(ugly_js))
self.promos = {k: v for k, v in self.promos.items() if v > self.now} self.promos = {k: v for k, v in (self.promos.items() if self.promos else {}) if v > self.now}
send_mail = False send_mail = False
for promo in promos: for promo in promos:
promo_name = promo.get("id") promo_name = promo.get("id")
@ -443,8 +443,9 @@ class Citizen(classes.CitizenAPI):
self.update_citizen_info() self.update_citizen_info()
resp_json = self._get_military_campaigns().json() resp_json = self._get_military_campaigns().json()
self.all_battles = {}
if resp_json.get("countries"): if resp_json.get("countries"):
self.all_battles = {}
self.countries = {}
for c_id, c_data in resp_json.get("countries").items(): for c_id, c_data in resp_json.get("countries").items():
if int(c_id) not in self.countries: if int(c_id) not in self.countries:
self.countries.update({ self.countries.update({
@ -807,7 +808,7 @@ class Citizen(classes.CitizenAPI):
r = self._get_training_grounds_json() r = self._get_training_grounds_json()
tg_json = r.json() tg_json = r.json()
self.details.gold = tg_json["page_details"]["gold"] self.details.gold = tg_json["page_details"]["gold"]
self.tg_contract.update({"free_train": tg_json["hasFreeTrain"]}) self.tg_contract = {"free_train": tg_json["hasFreeTrain"]}
if tg_json["contracts"]: if tg_json["contracts"]:
self.tg_contract.update(**tg_json["contracts"][0]) self.tg_contract.update(**tg_json["contracts"][0])

View File

@ -26,11 +26,13 @@ class ErepublikNetworkException(Exception):
class MyCompanies: class MyCompanies:
work_units: int = 0 work_units: int = 0
next_ot_time: datetime.datetime next_ot_time: datetime.datetime
holdings: Dict[int, Dict] = dict() holdings: Dict[int, Dict] = None
companies: Dict[int, Dict] = dict() companies: Dict[int, Dict] = None
ff_lockdown: int = 0 ff_lockdown: int = 0
def __init__(self): def __init__(self):
self.holdings = dict()
self.companies = dict()
self.next_ot_time = utils.now() self.next_ot_time = utils.now()
def prepare_holdings(self, holdings: dict): def prepare_holdings(self, holdings: dict):
@ -970,8 +972,8 @@ class MyJSONEncoder(JSONEncoder):
class BattleSide: class BattleSide:
id: int id: int
points: int points: int
deployed: List[int] = list() deployed: List[int] = None
allies: List[int] = list() allies: List[int] = None
def __init__(self, country_id: int, points: int, allies: List[int], deployed: List[int]): def __init__(self, country_id: int, points: int, allies: List[int], deployed: List[int]):
self.id = country_id self.id = country_id
@ -983,8 +985,8 @@ class BattleSide:
class BattleDivision: class BattleDivision:
end: datetime.datetime end: datetime.datetime
epic: bool epic: bool
dom_pts: Dict[str, int] = dict() dom_pts: Dict[str, int] = None
wall: Dict[str, Union[int, float]] = dict() wall: Dict[str, Union[int, float]] = None
@property @property
def div_end(self) -> bool: def div_end(self) -> bool:
@ -993,8 +995,8 @@ class BattleDivision:
def __init__(self, end: datetime.datetime, epic: bool, inv_pts: int, def_pts: int, wall_for: int, wall_dom: float): def __init__(self, end: datetime.datetime, epic: bool, inv_pts: int, def_pts: int, wall_for: int, wall_dom: float):
self.end = end self.end = end
self.epic = epic self.epic = epic
self.dom_pts.update({"inv": inv_pts, "def": def_pts}) self.dom_pts = dict({"inv": inv_pts, "def": def_pts})
self.wall.update({"for": wall_for, "dom": wall_dom}) self.wall = dict({"for": wall_for, "dom": wall_dom})
class Battle(object): class Battle(object):
@ -1006,13 +1008,13 @@ class Battle(object):
start: datetime.datetime = None start: datetime.datetime = None
invader: BattleSide = None invader: BattleSide = None
defender: BattleSide = None defender: BattleSide = None
div: Dict[int, BattleDivision] = dict() div: Dict[int, BattleDivision] = None
@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): def __init__(self, battle: Dict[str, Any]):
self.id = int(battle.get('id', 0)) self.id = int(battle.get('id', 0))
self.war_id = int(battle.get('war_id', 0)) self.war_id = int(battle.get('war_id', 0))
self.zone_id = int(battle.get('zone_id', 0)) self.zone_id = int(battle.get('zone_id', 0))
@ -1029,6 +1031,7 @@ class Battle(object):
[row.get('id') for row in battle.get('def', {}).get('ally_list')], [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']]) [row.get('id') for row in battle.get('def', {}).get('ally_list') if row['deployed']])
self.div = {}
for div, data in battle.get('div', {}).items(): for div, data in battle.get('div', {}).items():
div = int(div) div = int(div)
if data.get('end'): if data.get('end'):