Code cleanup and serialization improvements

This commit is contained in:
Eriks Karls 2019-10-30 16:55:33 +02:00
parent e060f67666
commit 1abfdb71ac

View File

@ -4,7 +4,6 @@ import hashlib
import random import random
import threading import threading
import time import time
import traceback
from collections import deque from collections import deque
from json import JSONDecodeError, loads, JSONEncoder from json import JSONDecodeError, loads, JSONEncoder
from typing import Any, Dict, List, Union, Mapping, Iterable, Tuple from typing import Any, Dict, List, Union, Mapping, Iterable, Tuple
@ -202,7 +201,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()
@ -328,14 +327,6 @@ class Config:
self.telegram_chat_id = 0 self.telegram_chat_id = 0
self.telegram_token = "" self.telegram_token = ""
# @property
# def __dict__(self) -> Dict[str, Union[bool, str, List[str]]]:
# ret = {}
# for key in dir(self):
# if not key.startswith('_') and key not in ['email', 'password']:
# ret[key] = getattr(self, key)
# return ret
class Energy: class Energy:
limit = 500 # energyToRecover limit = 500 # energyToRecover
@ -381,14 +372,6 @@ 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(object): class Details(object):
xp = 0 xp = 0
@ -433,14 +416,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
@ -451,14 +426,6 @@ 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(object): class House(object):
quality = None quality = None
@ -940,6 +907,11 @@ class Reporter:
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"
@ -1041,14 +1013,6 @@ 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
@ -1066,14 +1030,6 @@ class BattleDivision:
self.dom_pts = dict({"inv": inv_pts, "def": def_pts}) self.dom_pts = dict({"inv": inv_pts, "def": def_pts})
self.wall = dict({"for": wall_for, "dom": wall_dom}) self.wall = dict({"for": wall_for, "dom": wall_dom})
# @property
# def __dict__(self):
# ret = {}
# for key in dir(self):
# if not key.startswith('_'):
# ret[key] = getattr(self, key)
# return ret
class Battle(object): class Battle(object):
id: int = 0 id: int = 0
@ -1134,14 +1090,6 @@ class Battle(object):
self.id, utils.COUNTRIES[self.invader.id], utils.COUNTRIES[self.defender.id], self.zone_id, time_part self.id, utils.COUNTRIES[self.invader.id], utils.COUNTRIES[self.defender.id], self.zone_id, time_part
) )
# @property
# def __dict__(self):
# ret = {}
# for key in dir(self):
# if not key.startswith('_'):
# ret[key] = getattr(self, key)
# return ret
class EnergyToFight: class EnergyToFight:
energy: int = 0 energy: int = 0
@ -1186,9 +1134,9 @@ class TelegramBot:
_threads: List[threading.Thread] = [] _threads: List[threading.Thread] = []
def __dict__(self): def __dict__(self):
ret = super().__dict__.copy() return dict(chat_id=self.chat_id, api_url=self.api_url, player=self.player_name, last_time=self._last_time,
ret.update(_threads=len(self._threads)) next_time=self._next_time, queue=self.__queue, initialized=self.__initialized,
return ret 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
@ -1207,7 +1155,7 @@ class TelegramBot:
self._threads = [t for t in self._threads if t.is_alive()] self._threads = [t for t in self._threads if t.is_alive()]
self._next_time = utils.good_timedelta(utils.now(), datetime.timedelta(minutes=1)) self._next_time = utils.good_timedelta(utils.now(), datetime.timedelta(minutes=1))
if not self._threads: if not self._threads:
name = "send_telegram".format(threading.active_count() - 1) name = "telegram_send"
send_thread = threading.Thread(target=self.__send_messages, name=name) send_thread = threading.Thread(target=self.__send_messages, name=name)
send_thread.start() send_thread.start()
self._threads.append(send_thread) self._threads.append(send_thread)