From 4d941814d9c6f124c63fff406a9fa2964ffe1d14 Mon Sep 17 00:00:00 2001 From: Zoltan Szabatin Date: Sun, 2 Mar 2025 19:59:57 -0800 Subject: refactor: Generic game handling --- src/splitscreen_duo/__init__.py | 133 ++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 88 deletions(-) diff --git a/src/splitscreen_duo/__init__.py b/src/splitscreen_duo/__init__.py index 9689db4..309135c 100644 --- a/src/splitscreen_duo/__init__.py +++ b/src/splitscreen_duo/__init__.py @@ -28,6 +28,45 @@ def main() -> int: logger.info(f"running as {os.getenv('INSTANCE', 'unknown')}") menu.init_display() + game_handlers = { + Game.BENCHMARK.value: ( + "benchmark", + lambda: benchmark.main_loop(menu.screen, serial), + ), + Game.SNAKE.value: ( + "snake", + lambda: Snake(menu.screen, serial, INSTANCE).main_loop(), + ), + Game.BREAKOUT.value: ( + "breakout", + lambda: Breakout(menu.screen, serial, INSTANCE).main_loop(), + ), + Game.PONG.value: ( + "pong", + lambda: Pong(menu.screen, serial, INSTANCE).main_loop(), + ), + } + + def handle_game_selection(message_or_command, is_secondary=False): + game_value = message_or_command.get("value") + + if game_value in game_handlers: + game_name, game_func = game_handlers[game_value] + + logger.info( + f"{'received' if is_secondary else 'starting'} {game_name} game selection from {'primary' if is_secondary else 'menu'}" + ) + + game_result = game_func() + + if game_result and game_result.get("command") == Command.QUIT.value: + logger.info(f"{game_name} game returned QUIT, shutting down") + pygame.quit() + + return 0 + + return None + is_running = True while is_running: @@ -45,57 +84,10 @@ def main() -> int: return 0 elif message.get("command") == Command.SELECT_GAME.value: - if message.get("value") == Game.BENCHMARK.value: - logger.info("received benchmark game selection from primary") - - game_result = benchmark.main_loop(menu.screen, serial) - - if ( - game_result - and game_result.get("command") == Command.QUIT.value - ): - pygame.quit() - - return 0 - elif message.get("value") == Game.SNAKE.value: - logger.info("received snake game selection from primary") - - snake_game = Snake(menu.screen, serial, INSTANCE) - game_result = snake_game.main_loop() + result = handle_game_selection(message, is_secondary=True) - if ( - game_result - and game_result.get("command") == Command.QUIT.value - ): - pygame.quit() - - return 0 - elif message.get("value") == Game.BREAKOUT.value: - logger.info("received breakout game selection from primary") - - breakout_game = Breakout(menu.screen, serial, INSTANCE) - game_result = breakout_game.main_loop() - - if ( - game_result - and game_result.get("command") == Command.QUIT.value - ): - pygame.quit() - - return 0 - elif message.get("value") == Game.PONG.value: - logger.info("received pong game selection from primary") - - pong_game = Pong(menu.screen, serial, INSTANCE) - game_result = pong_game.main_loop() - - if ( - game_result - and game_result.get("command") == Command.QUIT.value - ): - pygame.quit() - - return 0 + if result == 0: + return 0 except json.JSONDecodeError: logger.error("failed to decode serial message") @@ -114,45 +106,10 @@ def main() -> int: return 0 elif serial_command.get("command") == Command.SELECT_GAME.value: - if serial_command.get("value") == Game.BENCHMARK.value: - logger.info("starting benchmark game") + result = handle_game_selection(serial_command) - game_result = benchmark.main_loop(menu.screen, serial) - - if game_result and game_result.get("command") == Command.QUIT.value: - pygame.quit() - - return 0 - elif serial_command.get("value") == Game.SNAKE.value: - logger.info("starting snake game") - - snake_game = Snake(menu.screen, serial, INSTANCE) - game_result = snake_game.main_loop() - - if game_result and game_result.get("command") == Command.QUIT.value: - pygame.quit() - - return 0 - elif serial_command.get("value") == Game.BREAKOUT.value: - logger.info("starting breakout game") - - breakout_game = Breakout(menu.screen, serial, INSTANCE) - game_result = breakout_game.main_loop() - - if game_result and game_result.get("command") == Command.QUIT.value: - pygame.quit() - - return 0 - elif serial_command.get("value") == Game.PONG.value: - logger.info("starting pong game") - - pong_game = Pong(menu.screen, serial, INSTANCE) - game_result = pong_game.main_loop() - - if game_result and game_result.get("command") == Command.QUIT.value: - pygame.quit() - - return 0 + if result == 0: + return 0 menu.draw_menu() -- cgit v1.2.3