From b5aef9b65f433522f513f79fa277d349d041cb89 Mon Sep 17 00:00:00 2001 From: Trevor Bayless <3620552+trevorbayless@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:25:02 -0600 Subject: [PATCH] Add additional logging #33 --- .../core/game/offline_game/offline_game_model.py | 3 +++ src/cli_chess/modules/board/board_model.py | 14 ++++++++++++-- .../tests/modules/board/test_board_model.py | 2 ++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/cli_chess/core/game/offline_game/offline_game_model.py b/src/cli_chess/core/game/offline_game/offline_game_model.py index a4360e1..17a089e 100644 --- a/src/cli_chess/core/game/offline_game/offline_game_model.py +++ b/src/cli_chess/core/game/offline_game/offline_game_model.py @@ -104,4 +104,7 @@ def _report_game_over(self) -> None: self.game_metadata.game_status.winner = COLOR_NAMES[outcome.winner] log.info(f"Game over (status={outcome.termination} winner={COLOR_NAMES[outcome.winner]})") + log.debug(f"Ending fen: {self.board_model.board.fen()}") + log.debug(f"Final move stack: {self.board_model.get_move_stack(as_string=True)}") + self._notify_game_model_updated(EventTopics.GAME_END) diff --git a/src/cli_chess/modules/board/board_model.py b/src/cli_chess/modules/board/board_model.py index d4ac70e..8de3cac 100644 --- a/src/cli_chess/modules/board/board_model.py +++ b/src/cli_chess/modules/board/board_model.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from cli_chess.utils.event import EventManager, EventTopics from cli_chess.utils.logging import log import chess @@ -155,6 +157,8 @@ def takeback(self, caller_color: chess.Color): self.board.pop() self.highlight_move = self.board.peek() if len(self.board.move_stack) > 0 else chess.Move.null() + + log.debug(f"Takeback issued. New fen: {self.board.fen()}") self._notify_board_model_updated(EventTopics.MOVE_MADE) except Exception as e: @@ -164,9 +168,15 @@ def takeback(self, caller_color: chess.Color): log.error(e) raise - def get_move_stack(self) -> List[chess.Move]: + def get_move_stack(self, as_string: bool = False) -> List[chess.Move] | str: """Returns the boards move stack""" - return self.board.move_stack + if as_string: + moves = "" + for move in self.board.move_stack: + moves += f"{move.uci()} " + return moves.strip() + else: + return self.board.move_stack def get_variant_name(self) -> str: """Returns a string holding the board variant name""" diff --git a/src/cli_chess/tests/modules/board/test_board_model.py b/src/cli_chess/tests/modules/board/test_board_model.py index eb217dd..21bff91 100644 --- a/src/cli_chess/tests/modules/board/test_board_model.py +++ b/src/cli_chess/tests/modules/board/test_board_model.py @@ -170,6 +170,8 @@ def test_get_move_stack(model: BoardModel): model.takeback(chess.WHITE) assert model.get_move_stack() == model.board.move_stack + assert model.get_move_stack(as_string=True) == "e2e4 d7d6 d2d4 g8f6" + def test_get_variant_name(): # Test chess960