From e7a64c44d720778a9be0d28ee770ccc93e40d988 Mon Sep 17 00:00:00 2001 From: Roman Vlasenko Date: Sun, 15 Sep 2024 00:47:05 +0300 Subject: [PATCH] Add ship count to the header of the Fleet widget --- battleship/tui/screens/game.py | 4 ++-- battleship/tui/styles.tcss | 1 + battleship/tui/widgets/fleet.py | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/battleship/tui/screens/game.py b/battleship/tui/screens/game.py index b69cd56..1acbf4c 100644 --- a/battleship/tui/screens/game.py +++ b/battleship/tui/screens/game.py @@ -83,19 +83,19 @@ def __init__( self.player_fleet = Fleet( id="player_fleet", + title="You", roster=strategy.roster, cell_factory=player_cell_factory, classes="player", ) - self.player_fleet.border_title = "Your fleet" self.enemy_fleet = Fleet( id="enemy_fleet", + title="Enemy", roster=strategy.roster, cell_factory=enemy_cell_factory, allow_placing=False, classes="enemy", ) - self.enemy_fleet.border_title = "Enemy fleet" self.board_map: dict[str, Board] = { self._player_name: self.player_board, diff --git a/battleship/tui/styles.tcss b/battleship/tui/styles.tcss index 28da5aa..e2a2adc 100644 --- a/battleship/tui/styles.tcss +++ b/battleship/tui/styles.tcss @@ -106,6 +106,7 @@ Fleet { scrollbar-size-vertical: 1; padding: 1 0; border: wide $accent; + border-title-align: center; } Fleet.player { diff --git a/battleship/tui/widgets/fleet.py b/battleship/tui/widgets/fleet.py index 22a2efa..aed8915 100644 --- a/battleship/tui/widgets/fleet.py +++ b/battleship/tui/widgets/fleet.py @@ -5,7 +5,7 @@ from textual.app import ComposeResult from textual.events import Mount from textual.message import Message -from textual.reactive import var +from textual.reactive import reactive, var from textual.widget import Widget from textual.widgets import Static @@ -87,10 +87,12 @@ def action_preview(self) -> None: class Fleet(Widget): previewing_id: var[str] = var("") + ships_alive: reactive[int] = reactive(0) def __init__( self, *args: Any, + title: str, roster: Roster, cell_factory: CellFactory, allow_placing: bool = True, @@ -107,7 +109,6 @@ def ship_factory(hp: int, damage: int, destroyed: bool) -> Text: damage_parts = [cell_factory.damaged().render() for _ in range(damage)] return Text.assemble(*(hp_parts + damage_parts), "\n") - self._roster = roster self._allow_placing = allow_placing self._previewing_id = "" self._ships: dict[str, Ship] = { @@ -121,6 +122,10 @@ def ship_factory(hp: int, damage: int, destroyed: bool) -> Text: ) for ship in roster } + self._title = title + + self.ships_total = len(self._ships) + self.ships_alive = self.ships_total def watch_previewing_id(self, old: str, new: str) -> None: if old: @@ -129,6 +134,9 @@ def watch_previewing_id(self, old: str, new: str) -> None: if new: self._ships[new].previewing = True + def watch_ships_alive(self) -> None: + self.update_title() + def compose(self) -> ComposeResult: yield from self._ships.values() @@ -144,3 +152,9 @@ def place(self, ship_id: str) -> None: def damage(self, ship_id: str) -> None: ship = self._ships[ship_id] ship.damage() + + if ship.destroyed: + self.ships_alive -= 1 + + def update_title(self) -> None: + self.border_title = f"[{self.ships_alive}/{self.ships_total}] {self._title}"