Skip to content

Commit

Permalink
Client UI: allow auto filling !getitem
Browse files Browse the repository at this point in the history
  • Loading branch information
Berserker66 committed Nov 29, 2021
1 parent f478b65 commit c46abd7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
35 changes: 22 additions & 13 deletions CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions FactorioClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
21 changes: 19 additions & 2 deletions kvui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit c46abd7

Please sign in to comment.