From 1582e8a73494723a719b047135526cc7ed69f3bc Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Tue, 7 Mar 2023 23:08:49 -0500 Subject: [PATCH] [+] Listen for inputs --- tngame/telnet.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tngame/telnet.py b/tngame/telnet.py index c0c561a..8fa50a0 100644 --- a/tngame/telnet.py +++ b/tngame/telnet.py @@ -74,6 +74,42 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode): draw_cat() await asyncio.sleep(0.1) + # Listen input function + async def listen(): + while True: + inp: str = await reader.read(3) + if inp and await on_input(inp): + return + + # Handle input function + async def on_input(inp: str): + nonlocal x, y + log.info(repr(inp)) + # Switch case + match inp: + case '\x1b[C': # Right + x += 1 + case '\x1b[D': # Left + x -= 1 + # case '\x1b[A': # Up + # y -= 1 + # case '\x1b[B': # Down + # y += 1 + case '\x1b': # Escape + writer.write('\r\nBye!\r\n') + writer.close() + return True + case _: + log.info(f'Unknown input: {repr(inp)}') + + # Draw the cat + draw_cat() + # Flush the output + await writer.drain() + + # Run listen and update in parallel + await asyncio.gather(listen(), update()) + def run(): # Create a new event loop, start the server and wait for it to close