Clients: some improvements (#602)

* Clients: some improvements
SNIClient is the only client that uses slow_mode, so its definition should be moved there.
type info for CommandProcessor was int for some reason.
Moved a lot of type info from init to class body, making it easier for type checkers to find.
getLogger("") and getLogger(None) is technically different, just happens that our root logger is "", fixed it in case of future confusion though.

* Logging: log that init_logging was run and what the current AP version is.
This commit is contained in:
Fabian Dill 2022-06-08 00:34:45 +02:00 committed by GitHub
parent fbf993566d
commit 517a2db9d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 20 deletions

View File

@ -114,30 +114,52 @@ class ClientCommandProcessor(CommandProcessor):
asyncio.create_task(self.ctx.send_msgs([{"cmd": "Say", "text": raw}]), name="send Say") asyncio.create_task(self.ctx.send_msgs([{"cmd": "Say", "text": raw}]), name="send Say")
class CommonContext(): class CommonContext:
# Should be adjusted as needed in subclasses
tags: typing.Set[str] = {"AP"} tags: typing.Set[str] = {"AP"}
game: typing.Optional[str] = None
items_handling: typing.Optional[int] = None
# defaults
starting_reconnect_delay: int = 5 starting_reconnect_delay: int = 5
current_reconnect_delay: int = starting_reconnect_delay current_reconnect_delay: int = starting_reconnect_delay
command_processor: int = ClientCommandProcessor command_processor: type(CommandProcessor) = ClientCommandProcessor
game: typing.Optional[str] = None
ui = None ui = None
ui_task: typing.Optional[asyncio.Task] = None ui_task: typing.Optional[asyncio.Task] = None
input_task: typing.Optional[asyncio.Task] = None input_task: typing.Optional[asyncio.Task] = None
keep_alive_task: typing.Optional[asyncio.Task] = None keep_alive_task: typing.Optional[asyncio.Task] = None
items_handling: typing.Optional[int] = None server_task: typing.Optional[asyncio.Task] = None
slot_info: typing.Dict[int, NetworkSlot] server: typing.Optional[Endpoint] = None
server_version: Version = Version(0, 0, 0)
current_energy_link_value: int = 0 # to display in UI, gets set by server current_energy_link_value: int = 0 # to display in UI, gets set by server
last_death_link: float = time.time() # last send/received death link on AP layer
# remaining type info
slot_info: typing.Dict[int, NetworkSlot]
server_address: str
password: typing.Optional[str]
hint_cost: typing.Optional[int]
games: typing.Dict[int, str]
player_names: typing.Dict[int, str]
# locations
locations_checked: typing.Set[int] # local state
locations_scouted: typing.Set[int]
missing_locations: typing.Set[int]
checked_locations: typing.Set[int] # server state
locations_info: typing.Dict[int, NetworkItem]
# current message box through kvui
_messagebox = None _messagebox = None
def __init__(self, server_address, password): def __init__(self, server_address, password):
# server state # server state
self.server_address = server_address self.server_address = server_address
self.password = password self.password = password
self.server_task = None self.hint_cost = None
self.server: typing.Optional[Endpoint] = None self.games = {}
self.server_version = Version(0, 0, 0)
self.hint_cost: typing.Optional[int] = None
self.games: typing.Dict[int, str] = {}
self.slot_info = {} self.slot_info = {}
self.permissions = { self.permissions = {
"forfeit": "disabled", "forfeit": "disabled",
@ -153,24 +175,21 @@ class CommonContext():
self.auth = None self.auth = None
self.seed_name = None self.seed_name = None
self.locations_checked: typing.Set[int] = set() # local state self.locations_checked = set() # local state
self.locations_scouted: typing.Set[int] = set() self.locations_scouted = set()
self.items_received = [] self.items_received = []
self.missing_locations: typing.Set[int] = set() self.missing_locations = set()
self.checked_locations: typing.Set[int] = set() # server state self.checked_locations = set() # server state
self.locations_info: typing.Dict[int, NetworkItem] = {} self.locations_info = {}
self.input_queue = asyncio.Queue() self.input_queue = asyncio.Queue()
self.input_requests = 0 self.input_requests = 0
self.last_death_link: float = time.time() # last send/received death link on AP layer
# game state # game state
self.player_names: typing.Dict[int: str] = {0: "Archipelago"} self.player_names = {0: "Archipelago"}
self.exit_event = asyncio.Event() self.exit_event = asyncio.Event()
self.watcher_event = asyncio.Event() self.watcher_event = asyncio.Event()
self.slow_mode = False
self.jsontotextparser = JSONtoTextParser(self) self.jsontotextparser = JSONtoTextParser(self)
self.set_getters(network_data_package) self.set_getters(network_data_package)

View File

@ -128,6 +128,7 @@ class Context(CommonContext):
self.death_state = DeathState.alive # for death link flop behaviour self.death_state = DeathState.alive # for death link flop behaviour
self.killing_player_task = None self.killing_player_task = None
self.allow_collect = False self.allow_collect = False
self.slow_mode = False
self.awaiting_rom = False self.awaiting_rom = False
self.rom = None self.rom = None

View File

@ -430,7 +430,8 @@ loglevel_mapping = {'error': logging.ERROR, 'info': logging.INFO, 'warning': log
def init_logging(name: str, loglevel: typing.Union[str, int] = logging.INFO, write_mode: str = "w", def init_logging(name: str, loglevel: typing.Union[str, int] = logging.INFO, write_mode: str = "w",
log_format: str = "[%(name)s at %(asctime)s]: %(message)s", exception_logger: str = ""): log_format: str = "[%(name)s at %(asctime)s]: %(message)s",
exception_logger: typing.Optional[str] = None):
loglevel: int = loglevel_mapping.get(loglevel, loglevel) loglevel: int = loglevel_mapping.get(loglevel, loglevel)
log_folder = user_path("logs") log_folder = user_path("logs")
os.makedirs(log_folder, exist_ok=True) os.makedirs(log_folder, exist_ok=True)
@ -466,6 +467,8 @@ def init_logging(name: str, loglevel: typing.Union[str, int] = logging.INFO, wri
sys.excepthook = handle_exception sys.excepthook = handle_exception
logging.info(f"Archipelago ({__version__}) logging initialized.")
def stream_input(stream, queue): def stream_input(stream, queue):
def queuer(): def queuer():