From c46abd7e653e0a63da9bfbebdaee53bccffcd9b5 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Mon, 29 Nov 2021 21:35:06 +0100 Subject: [PATCH] Client UI: allow auto filling !getitem --- CommonClient.py | 35 ++++++++++++++++++++++------------- FactorioClient.py | 8 +++++--- kvui.py | 21 +++++++++++++++++++-- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/CommonClient.py b/CommonClient.py index cd21aa2903a8..ea8170b11abb 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -102,7 +102,9 @@ def _cmd_ready(self): asyncio.create_task(self.ctx.send_msgs([{"cmd": "StatusUpdate", "status": state}]), name="send StatusUpdate") def default(self, raw: str): - asyncio.create_task(self.ctx.send_msgs([{"cmd": "Say", "text": raw}]), name="send Say") + raw = self.ctx.on_user_say(raw) + if raw: + asyncio.create_task(self.ctx.send_msgs([{"cmd": "Say", "text": raw}]), name="send Say") class CommonContext(): @@ -273,6 +275,11 @@ def on_package(self, cmd: str, args: dict): """For custom package handling in subclasses.""" pass + def on_user_say(self, text: str) -> typing.Optional[str]: + """Gets called before sending a Say to the server from the user. + Returned text is sent, or sending is aborted if None is returned.""" + return text + def update_permissions(self, permissions: typing.Dict[str, int]): for permission_name, permission_flag in permissions.items(): try: @@ -282,6 +289,20 @@ def update_permissions(self, permissions: typing.Dict[str, int]): except Exception as e: # safeguard against permissions that may be implemented in the future logger.exception(e) + async def shutdown(self): + self.server_address = None + if self.server and not self.server.socket.closed: + await self.server.socket.close() + if self.server_task: + await self.server_task + + while self.input_requests > 0: + self.input_queue.put_nowait(None) + self.input_requests -= 1 + self.keep_alive_task.cancel() + + # DeathLink hooks + def on_deathlink(self, data: dict): """Gets dispatched when a new DeathLink is triggered by another linked player.""" self.last_death_link = max(data["time"], self.last_death_link) @@ -303,18 +324,6 @@ async def send_death(self, death_text: str = ""): } }]) - async def shutdown(self): - self.server_address = None - if self.server and not self.server.socket.closed: - await self.server.socket.close() - if self.server_task: - await self.server_task - - while self.input_requests > 0: - self.input_queue.put_nowait(None) - self.input_requests -= 1 - self.keep_alive_task.cancel() - async def update_death_link(self, death_link): old_tags = self.tags.copy() if death_link: diff --git a/FactorioClient.py b/FactorioClient.py index 4e88ac9ac2ff..bff2b81a637a 100644 --- a/FactorioClient.py +++ b/FactorioClient.py @@ -134,7 +134,7 @@ async def game_watcher(ctx: FactorioContext): ctx.finished_game = True if ctx.locations_checked != research_data: - bridge_logger.info( + bridge_logger.debug( f"New researches done: " f"{[lookup_id_to_name[rid] for rid in research_data - ctx.locations_checked]}") ctx.locations_checked = research_data @@ -192,7 +192,7 @@ async def factorio_server_watcher(ctx: FactorioContext): while not factorio_queue.empty(): msg = factorio_queue.get() factorio_queue.task_done() - factorio_server_logger.info(msg) + if not ctx.rcon_client and "Starting RCON interface at IP ADDR:" in msg: ctx.rcon_client = factorio_rcon.RCONClient("localhost", rcon_port, rcon_password) if not ctx.server: @@ -201,7 +201,9 @@ async def factorio_server_watcher(ctx: FactorioContext): if not ctx.awaiting_bridge and "Archipelago Bridge Data available for game tick " in msg: ctx.awaiting_bridge = True - + factorio_server_logger.debug(msg) + else: + factorio_server_logger.info(msg) if ctx.rcon_client: commands = {} while ctx.send_index < len(ctx.items_received): diff --git a/kvui.py b/kvui.py index 1b2ca8d7760e..73ef26de94d3 100644 --- a/kvui.py +++ b/kvui.py @@ -179,7 +179,7 @@ def on_touch_down(self, touch): if text.startswith(question): name = Utils.get_text_between(text, question, "? (") - cmdinput.text = f"!hint {name}" + cmdinput.text = f"!{App.get_running_app().last_autofillable_command} {name}" break Clipboard.copy(text) @@ -194,7 +194,8 @@ class GameManager(App): logging_pairs = [ ("Client", "Archipelago"), ] - base_title = "Archipelago Client" + base_title: str = "Archipelago Client" + last_autofillable_command: str def __init__(self, ctx: context_type): self.title = self.base_title @@ -203,6 +204,22 @@ def __init__(self, ctx: context_type): self.icon = r"data/icon.png" self.json_to_kivy_parser = KivyJSONtoTextParser(ctx) self.log_panels = {} + + # keep track of last used command to autofill on click + self.last_autofillable_command = "hint" + autofillable_commands = ("hint", "getitem") + original_say = ctx.on_user_say + + def intercept_say(text): + text = original_say(text) + if text: + for command in autofillable_commands: + if text.startswith("!"+command): + self.last_autofillable_command = command + break + return text + ctx.on_user_say = intercept_say + super(GameManager, self).__init__() def build(self):