diff --git a/.vscode/launch.json b/.vscode/launch.json index 1e3dfdb..4b149a0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,14 +6,16 @@ "type": "python", "request": "launch", "program": "${workspaceFolder}/src/server.py", - "console": "integratedTerminal" + "console": "integratedTerminal", + "envFile": "" }, { "name": "Emulator", "type": "python", "request": "launch", "program": "${workspaceFolder}/src/emulator.py", - "console": "integratedTerminal" + "console": "integratedTerminal", + "envFile": "" } ], "compounds": [ diff --git a/src/emulator.py b/src/emulator.py index c269eeb..804ba75 100644 --- a/src/emulator.py +++ b/src/emulator.py @@ -43,7 +43,7 @@ def next_action(): Returns: int: key used by mgba """ - votes = list(map(int, r.mget(KEYS_ID))) + votes: list[int] = list(map(int, r.mget(KEYS_ID))) if any(votes): r.mset(KEYS_RESET) return votes.index(max(votes)) @@ -79,12 +79,24 @@ stream = Popen( "low_delay", "-strict", "experimental", + # "-loglevel", + # "quiet", RTMP_STREAM_URI, ], stdin=PIPE, ) +async def state_manager(): + ps = r.pubsub() + ps.subscribe("admin") + while True: + message = ps.get_message(ignore_subscribe_messages=True) + if message is not None: + logging.debug(message) + await asyncio.sleep(5.0) + + async def emulator(): while True: last_frame_t = time.time() @@ -108,7 +120,9 @@ async def emulator(): async def main(): task_emulator = asyncio.create_task(emulator()) + task_state_manager = asyncio.create_task(state_manager()) await task_emulator + await task_state_manager if __name__ == "__main__": diff --git a/src/server.py b/src/server.py index 9f08ad1..ea943c5 100644 --- a/src/server.py +++ b/src/server.py @@ -40,6 +40,18 @@ async def parse_message(user: User, message: dict[str, str]) -> None: logging.debug(f"admin authenticated: {user}") await user.send('{"auth":"success"}') + if "admin" in message: + if user == USERS.admin: + data = message["admin"] + if data == "save": + r.publish("admin", "save") + elif data == data.startswith("load:"): + r.publish("admin", data) + else: + logging.error(f"unsupported admin action: {data}") + else: + logging.error(f"user is not admin: {user}") + if "action" in message: data = message["action"]