# Class TuiRenderer

java.lang.Object
it.polimi.ingsw.is25am31.view.tui.TuiRenderer

All Implemented Interfaces:
  `UiRenderer`

public class TuiRenderer extends Object implements UiRenderer
`TuiRenderer` is an implementation of the `UiRenderer` interface that provides a Text-based User Interface (TUI) for the game. It handles displaying game elements, processing user input, and updating the console based on game state changes. This class follows the Singleton design pattern.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `customCardPrintMyTurn(int turnType)`
  Provides custom messages for the current player's turn.
  `void`
  `customCardPrintNotMyTurn(String currentPlayer)`
  Provides custom messages when it's not the current player's turn, specifically for cards where another player needs to perform an action.
  `void`
  `drawShip(Ship ship)`
  Draws the entire ship grid in a formatted ASCII art representation.
  `void`
  `effectResolution()`
  This method is a placeholder for effect resolution in GUI and does nothing in the TUI.
  `void`
  `endAssemblingPhase()`
  Notifies the user that the assembling phase has ended and provides relevant commands or waiting messages.
  `static TuiRenderer`
  `getInstance()`
  Returns the singleton instance of `TuiRenderer`.
  `void`
  `handleConnectionResponse(ConnectionResponse response)`
  Handles and displays the response received after a connection attempt, informing the user about the game's current state.
  `void`
  `handleUpdateMessage(UpdateMessage msg)`
  Handles and processes various update messages received from the server, displaying appropriate messages to the user.
  `void`
  `newLeader(boolean isNewLeader)`
  Notifies the player about the new leader for the next round.
  `void`
  `newPlayerJoined(String nickname)`
  Notifies the user that a new player has joined the game.
  `void`
  `notifyDefeat(int loseType)`
  Notifies the user about the reason for their defeat.
  `void`
  `onError(String message)`
  Displays an error message to the user, prints it to System.err.
  `void`
  `onInfo(String message)`
  Displays a general informational message to the user.
  `void`
  `printPromptSymbol()`
  Prints the prompt symbol ("> ") to the console, indicating that the UI is ready for user input.
  `void`
  `reportException(Exception e)`
  Reports an exception to the console, printing an error message.
  `void`
  `roundResult(boolean roundWon)`
  Displays the result of a game round, informing the player if they won or lost the phase and what action to take next.
  `void`
  `runUi(VirtualView client, VirtualServer server, ClientModel clientModel)`
  Starts and runs the TUI.
  `void`
  `setNickname()`
  Prompts the user to set their nickname after successfully joining a game.
  `void`
  `setUUID(UUID clientId)`
  Sets the unique client ID (UUID) for the `CommandParser`.
  `void`
  `showCard(Card card)`
  Displays a game card in a formatted ASCII art frame.
  `void`
  `showDiceResult(int diceResult)`
  Displays the result of a dice roll.
  `void`
  `showFaceUpComponentList(List<Component> components)`
  Displays a list of face-up components, showing each component's ID and its ASCII art.
  `void`
  `showFinalRanking(Map<Player,Integer> ranking)`
  Displays the final ranking of players at the end of the game, including their scores and announcing the winner.
  `void`
  `showGameList(List<Integer> gameList)`
  Displays a list of available games that the user can join, along with the command to join one.
  `void`
  `showPlayersList(List<String> players)`
  Displays the list of players currently in the lobby or game.
  `void`
  `showRocketsPosition(List<Player> players, int level)`
  Displays the current positions of all players' rockets on the TUI.
  `void`
  `showsDrawnComponent(Component component)`
  Displays a drawn game component in a formatted ASCII art representation.
  `void`
  `showSecondsLeft(int count, int secondsLeft)`
  Displays the remaining seconds for an hourglass timer.
  `void`
  `showSecondsLeftForGui(int count, int secondsLeft)`
  This method is a placeholder for GUI-specific timer updates and does nothing in the TUI.
  `void`
  `showSubDeck(List<Card> subDeck)`
  Displays a sub-deck of cards by showing each card's ASCII art representation.
  `void`
  `startAssemblingPhase()`
  Informs the user that the assembling phase has started, displays their ship, and lists all commands available during this phase.
  `void`
  `startCrewPlacingPhase(Ship ship, Map<Integer,Integer> positions)`
  Informs the user that they are in the crew placing phase and lists the available commands.
  `void`
  `startFlightPhase(Ship ship, Map<Integer,Integer> positions)`
  Handles the start of the flight phase.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getInstance

    public static TuiRenderer getInstance()
    Returns the singleton instance of `TuiRenderer`. If no instance exists, it creates one.
    Returns:
      The singleton instance of `TuiRenderer`.

  - ### runUi

    public void runUi(VirtualView client, VirtualServer server, ClientModel clientModel)
    Starts and runs the TUI. It initializes the `CommandParser` in a new thread to continuously read and handle user input from the console.
    Specified by:
      `runUi` in interface `UiRenderer`
    Parameters:
      `client` - The virtual view representing the client's connection to the server.
      `server` - The virtual server instance.
      `clientModel` - The client-side model containing the current state of the game for this client.

  - ### printPromptSymbol

    public void printPromptSymbol()
    Prints the prompt symbol ("> ") to the console, indicating that the UI is ready for user input. This is typically called after an action or message.
    Specified by:
      `printPromptSymbol` in interface `UiRenderer`

  - ### setUUID

    public void setUUID(UUID clientId) throws InterruptedException
    Sets the unique client ID (UUID) for the `CommandParser`. It waits until the parser is initialized before setting the ID to avoid NullPointerExceptions.
    Specified by:
      `setUUID` in interface `UiRenderer`
    Parameters:
      `clientId` - The `UUID` to be set for this client.
    Throws:
      `InterruptedException` - if the current thread is interrupted while waiting for the parser to be initialized.

  - ### showsDrawnComponent

    public void showsDrawnComponent(Component component)
    Displays a drawn game component in a formatted ASCII art representation.
    Specified by:
      `showsDrawnComponent` in interface `UiRenderer`
    Parameters:
      `component` - The `Component` to be displayed.

  - ### showFaceUpComponentList

    public void showFaceUpComponentList(List<Component> components)
    Displays a list of face-up components, showing each component's ID and its ASCII art.
    Specified by:
      `showFaceUpComponentList` in interface `UiRenderer`
    Parameters:
      `components` - A list of components to be displayed.

  - ### drawShip

    public void drawShip(Ship ship)
    Draws the entire ship grid in a formatted ASCII art representation.
    Specified by:
      `drawShip` in interface `UiRenderer`
    Parameters:
      `ship` - The `Ship` object to be drawn.

  - ### showRocketsPosition

    public void showRocketsPosition(List<Player> players, int level)
    Displays the current positions of all players' rockets on the TUI.
    Specified by:
      `showRocketsPosition` in interface `UiRenderer`
    Parameters:
      `players` - The list of players in the game.
      `level` - The current level of the game, which determines the track length.

  - ### showCard

    public void showCard(Card card)
    Displays a game card in a formatted ASCII art frame.
    Specified by:
      `showCard` in interface `UiRenderer`
    Parameters:
      `card` - The `Card` to be displayed.

  - ### startCrewPlacingPhase

    public void startCrewPlacingPhase(Ship ship, Map<Integer,Integer> positions)
    Informs the user that they are in the crew placing phase and lists the available commands.
    Specified by:
      `startCrewPlacingPhase` in interface `UiRenderer`
    Parameters:
      `ship` - The ship for which crew is being placed.
      `positions` - A map indicating possible positions for placing crew members.

  - ### endAssemblingPhase

    public void endAssemblingPhase()
    Notifies the user that the assembling phase has ended and provides relevant commands or waiting messages.
    Specified by:
      `endAssemblingPhase` in interface `UiRenderer`

  - ### startAssemblingPhase

    public void startAssemblingPhase()
    Informs the user that the assembling phase has started, displays their ship, and lists all commands available during this phase.
    Specified by:
      `startAssemblingPhase` in interface `UiRenderer`

  - ### showFinalRanking

    public void showFinalRanking(Map<Player,Integer> ranking)
    Displays the final ranking of players at the end of the game, including their scores and announcing the winner.
    Specified by:
      `showFinalRanking` in interface `UiRenderer`
    Parameters:
      `ranking` - A map where keys are `Player`s and values are their final scores.

  - ### startFlightPhase

    public void startFlightPhase(Ship ship, Map<Integer,Integer> positions)
    Handles the start of the flight phase. For level 1, it updates rocket positions and displays them. It also redraws the player's ship.
    Specified by:
      `startFlightPhase` in interface `UiRenderer`
    Parameters:
      `ship` - The current player's ship.
      `positions` - Updated rocket's positions on the game board, received by the server.

  - ### notifyDefeat

    public void notifyDefeat(int loseType)
    Notifies the user about the reason for their defeat.
    Specified by:
      `notifyDefeat` in interface `UiRenderer`
    Parameters:
      `loseType` - An integer representing the type of defeat: 1 - lost due to engine power. 2 - lost due to no crew members left. 3 - lost because someone lapped them. 4 - voluntarily abandoned the race.

  - ### customCardPrintMyTurn

    public void customCardPrintMyTurn(int turnType)
    Provides custom messages for the current player's turn.
    Specified by:
      `customCardPrintMyTurn` in interface `UiRenderer`
    Parameters:
      `turnType` - An integer indicating the specific phase or context of the turn for a given card effect.

  - ### customCardPrintNotMyTurn

    public void customCardPrintNotMyTurn(String currentPlayer)
    Provides custom messages when it's not the current player's turn, specifically for cards where another player needs to perform an action.
    Specified by:
      `customCardPrintNotMyTurn` in interface `UiRenderer`
    Parameters:
      `currentPlayer` - The nickname of the player whose turn it currently is.

  - ### reportException

    public void reportException(Exception e)
    Reports an exception to the console, printing an error message.
    Specified by:
      `reportException` in interface `UiRenderer`
    Parameters:
      `e` - The `Exception` to be reported.

  - ### handleConnectionResponse

    public void handleConnectionResponse(ConnectionResponse response)
    Handles and displays the response received after a connection attempt, informing the user about the game's current state.
    Specified by:
      `handleConnectionResponse` in interface `UiRenderer`
    Parameters:
      `response` - The `ConnectionResponse` to be handled.

  - ### showPlayersList

    public void showPlayersList(List<String> players)
    Displays the list of players currently in the lobby or game.
    Specified by:
      `showPlayersList` in interface `UiRenderer`
    Parameters:
      `players` - A `List` of player nicknames.

  - ### newPlayerJoined

    public void newPlayerJoined(String nickname)
    Notifies the user that a new player has joined the game.
    Specified by:
      `newPlayerJoined` in interface `UiRenderer`
    Parameters:
      `nickname` - The nickname of the player who joined.

  - ### showDiceResult

    public void showDiceResult(int diceResult)
    Displays the result of a dice roll.
    Specified by:
      `showDiceResult` in interface `UiRenderer`
    Parameters:
      `diceResult` - The integer result of the dice roll.

  - ### roundResult

    public void roundResult(boolean roundWon)
    Displays the result of a game round, informing the player if they won or lost the phase and what action to take next.
    Specified by:
      `roundResult` in interface `UiRenderer`
    Parameters:
      `roundWon` - A boolean indicating if the round was won (`true`) or lost (`false`) by the player.

  - ### newLeader

    public void newLeader(boolean isNewLeader)
    Notifies the player about the new leader for the next round. If the current player is the leader, they are prompted to draw a card. Otherwise, they wait for the leader.
    Specified by:
      `newLeader` in interface `UiRenderer`
    Parameters:
      `isNewLeader` - A boolean indicating if the current player is the new leader.

  - ### onInfo

    public void onInfo(String message)
    Displays a general informational message to the user.
    Specified by:
      `onInfo` in interface `UiRenderer`
    Parameters:
      `message` - The informational message to display.

  - ### onError

    public void onError(String message)
    Displays an error message to the user, prints it to System.err.
    Specified by:
      `onError` in interface `UiRenderer`
    Parameters:
      `message` - The error message to display.

  - ### setNickname

    public void setNickname()
    Prompts the user to set their nickname after successfully joining a game.
    Specified by:
      `setNickname` in interface `UiRenderer`

  - ### showSecondsLeftForGui

    public void showSecondsLeftForGui(int count, int secondsLeft)
    This method is a placeholder for GUI-specific timer updates and does nothing in the TUI.
    Specified by:
      `showSecondsLeftForGui` in interface `UiRenderer`
    Parameters:
      `count` - The current hourglass number.
      `secondsLeft` - The total seconds left.

  - ### showSecondsLeft

    public void showSecondsLeft(int count, int secondsLeft)
    Displays the remaining seconds for an hourglass timer.
    Specified by:
      `showSecondsLeft` in interface `UiRenderer`
    Parameters:
      `count` - The current hourglass number.
      `secondsLeft` - The total seconds left for the hourglass.

  - ### showSubDeck

    public void showSubDeck(List<Card> subDeck)
    Displays a sub-deck of cards by showing each card's ASCII art representation.
    Specified by:
      `showSubDeck` in interface `UiRenderer`
    Parameters:
      `subDeck` - A `List` of `Card` objects representing the sub-deck.

  - ### handleUpdateMessage

    public void handleUpdateMessage(UpdateMessage msg)
    Handles and processes various update messages received from the server, displaying appropriate messages to the user.
    Specified by:
      `handleUpdateMessage` in interface `UiRenderer`
    Parameters:
      `msg` - The `UpdateMessage` to be handled.

  - ### effectResolution

    public void effectResolution()
    This method is a placeholder for effect resolution in GUI and does nothing in the TUI.
    Specified by:
      `effectResolution` in interface `UiRenderer`

  - ### showGameList

    public void showGameList(List<Integer> gameList)
    Displays a list of available games that the user can join, along with the command to join one.
    Specified by:
      `showGameList` in interface `UiRenderer`
    Parameters:
      `gameList` - A list of game IDs to be displayed.