# Class ClientModel

java.lang.Object
it.polimi.ingsw.is25am31.view.ClientModel

public class ClientModel extends Object
Manages the client-side model, handling game state, player data, and interactions with the UI and server.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ClientModel(UiRenderer renderer, VirtualServer server, VirtualView client)`
  Constructs a new ClientModel.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addComponentToMyShip(int x, int y)`
  Adds the current component to the client's ship at the specified coordinates.
  `static void`
  `closeClient()`
  Closes the client application after a short delay.
  `void`
  `connect(VirtualView client)`
  Connects the client to the server and starts the UI.
  `void`
  `connectionResponse(ConnectionResponse response)`
  Handles a connection response from the server.
  `void`
  `crewPlacingPhase(Ship ship, Map<Integer,Integer> positions)`
  Initiates the crew placing phase.
  `void`
  `diceResult(int diceResult)`
  Displays the result of a dice roll and updates the client state.
  `void`
  `effectResolution()`
  Starts the effect resolution phase.
  `void`
  `finishFinalHourglass()`
  Finishes the final hourglass phase.
  `UUID`
  `getClientId()`
  `ClientState`
  `getClientState()`
  `Card`
  `getCurrentCard()`
  `Component`
  `getCurrentComponent()`
  `List<Component>`
  `getFaceUpComponents()`
  `int`
  `getLevel()`
  `Ship`
  `getMyShip()`
  Retrieves the client's ship.
  `String`
  `getNickname()`
  `List<Player>`
  `getPlayers()`
  `boolean`
  `isViewingOpponentShip()`
  Checks if the client is currently viewing an opponent's ship.
  `void`
  `lockClient()`
  Locks the client's state, preventing further interaction.
  `void`
  `newLeader(boolean isNewLeader)`
  Notifies the client if they are the new leader and updates the client state.
  `void`
  `notifyDefeat(int loseType)`
  Notifies the client of the defeat and sets the client state to SPECTATOR.
  `void`
  `playerJoined(String newPlayer, List<String> players)`
  Notifies the client that a new player has joined and displays the updated player list.
  `void`
  `removePlayer(Player player)`
  `void`
  `reportException(Exception e)`
  Reports an exception to the renderer.
  `void`
  `roundResult(boolean roundWon)`
  Displays the result of a round (won or lost).
  `void`
  `setClientState(ClientState clientState)`
  `void`
  `setCurrentCard(Card currentCard)`
  `void`
  `setCurrentComponent(Component currentComponent)`
  `void`
  `setFaceUpComponents(List<Component> faceUpComponents)`
  `void`
  `setGameLevel(int level)`
  Sets the game level and initializes the client's ship accordingly.
  `void`
  `setLevel(int level)`
  `void`
  `setMyShip(int level)`
  `void`
  `setMyShip(Ship myShip)`
  Sets the client's ship.
  `void`
  `setNickname()`
  Initiates the nickname setting phase for the client.
  `void`
  `setNickname(String nickname)`
  `void`
  `setPlayers(List<Player> players)`
  `void`
  `setUUID(UUID clientId)`
  Sets the client's ID and starts the pinging process to the server.
  `void`
  `setViewingOpponentShip(boolean flag)`
  Sets whether the client is viewing an opponent's ship.
  `void`
  `showCard(Card card)`
  Shows a drawn card to the client.
  `void`
  `showComponent(Component component)`
  Shows a drawn component to the client and prints a prompt symbol.
  `void`
  `showFaceUpComponentList(List<Component> components)`
  Displays the list of face-up components to the client.
  `void`
  `showFinalRanking(Map<Player,Integer> ranking)`
  Displays the final ranking of players.
  `void`
  `showGameList(List<Integer> gameList)`
  Displays the list of available games.
  `void`
  `showSecondsLeft(int count, int secondsLeft)`
  Displays the seconds left (for TUI).
  `void`
  `showSecondsLeftForGui(int counter, int secondsLeft)`
  Displays the seconds left for GUI updates.
  `void`
  `showShip(Ship ship)`
  Displays a ship to the client.
  `void`
  `showSubDeck(List<Card> subDeck)`
  Displays the sub-deck of cards.
  `void`
  `showUpdate(UpdateMessage msg)`
  Displays an update message received from the server.
  `void`
  `startAssemblingPhase(List<Player> players, int level)`
  Starts the assembling phase of the game.
  `void`
  `startFlightPhase(Ship ship, Map<Integer,Integer> positions)`
  Starts the flight phase of the game.
  `void`
  `startGame()`
  Starts the game by setting the client state to initial.
  `void`
  `turnChange(boolean isMyTurn, int turnType, int currentPlayer)`
  Handles a turn change, indicating whether it's the client's turn and the turn type.
  `void`
  `updatePositions(Map<Integer,Integer> positions)`
  Updates the positions of players' rockets and displays them.

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

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

- ## Constructor Details

  - ### ClientModel

    public ClientModel(UiRenderer renderer, VirtualServer server, VirtualView client)
    Constructs a new ClientModel.
    Parameters:
      `renderer` - The UI renderer responsible for displaying game information.
      `server` - The virtual server for network communication.
      `client` - The virtual view representing the client.

- ## Method Details

  - ### setNickname

    public void setNickname(String nickname)
    Parameters:
      `nickname` - The nickname to set.

  - ### getNickname

    public String getNickname()
    Returns:
      The client's nickname.

  - ### removePlayer

    public void removePlayer(Player player)
    Parameters:
      `player` - The player to remove.

  - ### setPlayers

    public void setPlayers(List<Player> players)
    Parameters:
      `players` - The list of players.

  - ### getPlayers

    public List<Player> getPlayers()
    Returns:
      The players in the game.

  - ### getFaceUpComponents

    public List<Component> getFaceUpComponents()
    Returns:
      The list of face-up components.

  - ### setFaceUpComponents

    public void setFaceUpComponents(List<Component> faceUpComponents)
    Parameters:
      `faceUpComponents` - The list of face-up components.

  - ### getLevel

    public int getLevel()
    Returns:
      The game level.

  - ### getClientId

    public UUID getClientId()
    Returns:
      The `UUID` of the client.

  - ### setLevel

    public void setLevel(int level)
    Parameters:
      `level` - The level to set.

  - ### setClientState

    public void setClientState(ClientState clientState)
    Parameters:
      `clientState` - The `ClientState` to set.

  - ### getClientState

    public ClientState getClientState()
    Returns:
      The current `ClientState`.

  - ### setCurrentCard

    public void setCurrentCard(Card currentCard)
    Parameters:
      `currentCard` - The `Card` to set.

  - ### getCurrentCard

    public Card getCurrentCard()
    Returns:
      The current `Card`.

  - ### setCurrentComponent

    public void setCurrentComponent(Component currentComponent)
    Parameters:
      `currentComponent` - The component to set.

  - ### getCurrentComponent

    public Component getCurrentComponent()
    Returns:
      The current component.

  - ### setMyShip

    public void setMyShip(int level)
    Parameters:
      `level` - The level of the gameboard for the ship.

  - ### setMyShip

    public void setMyShip(Ship myShip)
    Sets the client's ship.
    Parameters:
      `myShip` - The `Ship` to set.

  - ### getMyShip

    public Ship getMyShip()
    Retrieves the client's ship.
    Returns:
      The client's `Ship`.

  - ### addComponentToMyShip

    public void addComponentToMyShip(int x, int y)
    Adds the current component to the client's ship at the specified coordinates. Throws a RuntimeException if `currentComponent` is null.
    Parameters:
      `x` - The x-coordinate on the ship's gameboard.
      `y` - The y-coordinate on the ship's gameboard.
    Throws:
      `RuntimeException` - If `currentComponent` is null.

  - ### isViewingOpponentShip

    public boolean isViewingOpponentShip()
    Checks if the client is currently viewing an opponent's ship.
    Returns:
      True if viewing an opponent's ship, false otherwise.

  - ### setViewingOpponentShip

    public void setViewingOpponentShip(boolean flag)
    Sets whether the client is viewing an opponent's ship.
    Parameters:
      `flag` - True to indicate viewing an opponent's ship, false otherwise.

  - ### closeClient

    public static void closeClient()
    Closes the client application after a short delay.

  - ### connect

    public void connect(VirtualView client) throws Exception
    Connects the client to the server and starts the UI.
    Parameters:
      `client` - The `VirtualView` representing the client.
    Throws:
      `Exception` - If an error occurs during connection or UI rendering.

  - ### showUpdate

    public void showUpdate(UpdateMessage msg) throws Exception
    Displays an update message received from the server.
    Parameters:
      `msg` - The `UpdateMessage` to display.
    Throws:
      `Exception` - If an error occurs during message handling by the renderer.

  - ### showCard

    public void showCard(Card card) throws Exception
    Shows a drawn card to the client.
    Parameters:
      `card` - The `Card` to show.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### showComponent

    public void showComponent(Component component) throws Exception
    Shows a drawn component to the client and prints a prompt symbol.
    Parameters:
      `component` - The `Component` to show.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### lockClient

    public void lockClient() throws Exception
    Locks the client's state, preventing further interaction.
    Throws:
      `Exception` - If an error occurs during state transition.

  - ### setNickname

    public void setNickname() throws InterruptedException
    Initiates the nickname setting phase for the client.
    Throws:
      `InterruptedException` - If the thread is interrupted during sleep.

  - ### startAssemblingPhase

    public void startAssemblingPhase(List<Player> players, int level) throws Exception
    Starts the assembling phase of the game.
    Parameters:
      `players` - The list of players in the game.
      `level` - The current game level.
    Throws:
      `Exception` - If an error occurs during setup or rendering.

  - ### startGame

    public void startGame()
    Starts the game by setting the client state to initial.

  - ### showShip

    public void showShip(Ship ship) throws InterruptedException
    Displays a ship to the client.
    Parameters:
      `ship` - The `Ship` to display.
    Throws:
      `InterruptedException` - If the thread is interrupted during rendering.

  - ### showFaceUpComponentList

    public void showFaceUpComponentList(List<Component> components)
    Displays the list of face-up components to the client.
    Parameters:
      `components` - The list of components to display.

  - ### showSecondsLeftForGui

    public void showSecondsLeftForGui(int counter, int secondsLeft) throws Exception
    Displays the seconds left for GUI updates.
    Parameters:
      `counter` - The current hourglass number.
      `secondsLeft` - The number of seconds remaining.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### showSecondsLeft

    public void showSecondsLeft(int count, int secondsLeft) throws Exception
    Displays the seconds left (for TUI).
    Parameters:
      `count` - The current hourglass number.
      `secondsLeft` - The number of seconds remaining.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### finishFinalHourglass

    public void finishFinalHourglass() throws Exception
    Finishes the final hourglass phase. If in ASSEMBLE state, it ends the assembling phase and updates the client state.
    Throws:
      `Exception` - If an error occurs during phase transition or server interaction.

  - ### updatePositions

    public void updatePositions(Map<Integer,Integer> positions)
    Updates the positions of players' rockets and displays them.
    Parameters:
      `positions` - A map where keys are player IDs and values are their new positions.

  - ### effectResolution

    public void effectResolution()
    Starts the effect resolution phase.

  - ### startFlightPhase

    public void startFlightPhase(Ship ship, Map<Integer,Integer> positions) throws Exception
    Starts the flight phase of the game.
    Parameters:
      `ship` - The client's `Ship`.
      `positions` - A map of player IDs to their rocket positions.
    Throws:
      `Exception` - If an error occurs during rendering or setup.

  - ### crewPlacingPhase

    public void crewPlacingPhase(Ship ship, Map<Integer,Integer> positions) throws Exception
    Initiates the crew placing phase.
    Parameters:
      `ship` - The client's `Ship`.
      `positions` - A map of player IDs to their rocket positions.
    Throws:
      `Exception` - If an error occurs during rendering or setup.

  - ### showSubDeck

    public void showSubDeck(List<Card> subDeck) throws Exception
    Displays the sub-deck of cards.
    Parameters:
      `subDeck` - The list of cards in the sub-deck.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### showFinalRanking

    public void showFinalRanking(Map<Player,Integer> ranking) throws Exception
    Displays the final ranking of players.
    Parameters:
      `ranking` - A map where keys are `Player`s and values are their final scores.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### notifyDefeat

    public void notifyDefeat(int loseType)
    Notifies the client of the defeat and sets the client state to SPECTATOR.
    Parameters:
      `loseType` - An integer representing the type of defeat: 1 - lost due to engines 2 - lost due to crew 3 - lost because lapped 4 - abandonment

  - ### setGameLevel

    public void setGameLevel(int level)
    Sets the game level and initializes the client's ship accordingly.
    Parameters:
      `level` - The game level to set.

  - ### setUUID

    public void setUUID(UUID clientId) throws Exception
    Sets the client's ID and starts the pinging process to the server.
    Parameters:
      `clientId` - The `UUID` of the client.
    Throws:
      `Exception` - If an error occurs during UUID setting or pinging.

  - ### connectionResponse

    public void connectionResponse(ConnectionResponse response)
    Handles a connection response from the server. If the response is SET_NICKNAME, the client state is updated.
    Parameters:
      `response` - The `ConnectionResponse` from the server.

  - ### playerJoined

    public void playerJoined(String newPlayer, List<String> players)
    Notifies the client that a new player has joined and displays the updated player list.
    Parameters:
      `newPlayer` - The nickname of the new player.
      `players` - The updated list of player nicknames.

  - ### newLeader

    public void newLeader(boolean isNewLeader) throws Exception
    Notifies the client if they are the new leader and updates the client state.
    Parameters:
      `isNewLeader` - True if the client is the new leader, false otherwise.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### diceResult

    public void diceResult(int diceResult) throws Exception
    Displays the result of a dice roll and updates the client state.
    Parameters:
      `diceResult` - The result of the dice roll.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### roundResult

    public void roundResult(boolean roundWon) throws Exception
    Displays the result of a round (won or lost).
    Parameters:
      `roundWon` - True if the round was won, false otherwise.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### turnChange

    public void turnChange(boolean isMyTurn, int turnType, int currentPlayer) throws Exception
    Handles a turn change, indicating whether it's the client's turn and the turn type.
    Parameters:
      `isMyTurn` - True if it's the client's turn, false otherwise.
      `turnType` - The type of turn.
      `currentPlayer` - The index of the current player in the players list.
    Throws:
      `Exception` - If an error occurs during rendering.

  - ### reportException

    public void reportException(Exception e)
    Reports an exception to the renderer. If the exception is a `PingErrorException`, it indicates a disconnection and closes the client.
    Parameters:
      `e` - The exception to report.

  - ### showGameList

    public void showGameList(List<Integer> gameList) throws Exception
    Displays the list of available games.
    Parameters:
      `gameList` - The list of game IDs.
    Throws:
      `Exception` - If an error occurs during rendering.