# Class SocketClient

java.lang.Object
it.polimi.ingsw.is25am31.network.socket.client.SocketClient

All Implemented Interfaces:
  `VirtualView`, `Remote`

public class SocketClient extends Object implements VirtualView
The `SocketClient` class represents the client-side implementation for Socket communication in the application. This class acts as a bridge between the Socket server and the client's local model (`ClientModel`), forwarding server-side calls to the appropriate client-side logic.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SocketClient(ObjectInputStream input, ObjectOutputStream output, UiRenderer renderer)`
  Constructs a new instance of the SocketClient.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `connectionResponse(ConnectionResponse response)`
  Handles the response received after a connection attempt.
  `void`
  `connectionTest()`
  A method to test the connection.
  `void`
  `crewPlacingPhase(Ship ship, Map<Integer,Integer> positions)`
  Initiates the crew placing phase for a ship.
  `void`
  `diceResult(int diceResult)`
  Displays the result of a dice roll to the client.
  `void`
  `effectResolution()`
  Starts the effect resolution phase.
  `void`
  `finishFinalHourglass()`
  Notifies the client that the final hourglass has finished.
  `void`
  `lockClient()`
  Locks the client's interaction.
  `void`
  `newLeader(boolean isNewLeader)`
  Notifies the client if they have become the new leader of the game.
  `void`
  `notifyDefeat(int loseType)`
  Notifies the client of his defeat and the reason.
  `void`
  `playerJoined(String newPlayer, List<String> players)`
  Notifies the client when a new player has joined the game.
  `void`
  `roundResult(boolean roundWon)`
  Notifies the client of the result of a game round during effect application of certain cards.
  `void`
  `run()`
  Executes the main process of the `SocketClient`.
  `void`
  `setGameLevel(int level)`
  Sets the game level on the client side.
  `void`
  `setNickname()`
  Prompts the client to set their nickname.
  `void`
  `setUUID(UUID clientId)`
  Sets the unique identifier (UUID) for this client.
  `void`
  `showCard(Card card)`
  Displays a given `Card` to the client.
  `void`
  `showComponent(Component component)`
  Displays a given `Component` to the client.
  `void`
  `showFaceUpComponentList(List<Component> components)`
  Displays a list of face-up `Component`s to the client.
  `void`
  `showFinalRanking(Map<Player,Integer> ranking)`
  Displays the final ranking of players at the end of the game.
  `void`
  `showGameList(List<Integer> gameList)`
  Displays a list of available games to the client.
  `void`
  `showSecondsLeft(int count, int secondsLeft)`
  Displays the countdown for the hourglass timer (TUI).
  `void`
  `showSecondsLeftForGui(int counter, int secondsLeft)`
  Displays the countdown for the hourglass timer (GUI).
  `void`
  `showShip(Ship ship)`
  Displays his own or another player's `Ship` to the client.
  `void`
  `showSubDeck(List<Card> subDeck)`
  Displays a sub-deck of `Card`s to the client.
  `void`
  `showUpdate(UpdateMessage msg)`
  Handles update messages received from the server.
  `static void`
  `start(String host, String port, UiRenderer renderer)`
  Establishes a connection to the server and starts the SocketClient.
  `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 client-side.
  `void`
  `startGame()`
  Notifies the client that the game has started.
  `void`
  `turnChange(boolean isMyTurn, int turnType, int currentPlayer)`
  Notifies the client about a change in turn.
  `void`
  `updatePositions(Map<Integer,Integer> positions)`
  Updates the positions of players on the game board.

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

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

- ## Constructor Details

  - ### SocketClient

    public SocketClient(ObjectInputStream input, ObjectOutputStream output, UiRenderer renderer) throws IOException
    Constructs a new instance of the SocketClient.
    Parameters:
      `input` - The `ObjectInputStream` used to receive data from the server.
      `output` - The `ObjectOutputStream` used to send data to the server.
      `renderer` - The `UiRenderer` responsible for rendering the client user interface.
    Throws:
      `IOException` - If an I/O error occurs while initializing the client.

- ## Method Details

  - ### run

    public void run() throws Exception
    Executes the main process of the `SocketClient`. This method runs the client-side loop for receiving and processing commands from the server. It first establishes the connection with the server and listens for incoming `ServerClientCommand` objects. Each command received is executed using the `ServerClientCommand.execute(it.polimi.ingsw.is25am31.network.socket.client.SocketClient)` method. If an exception occurs during the execution of a command, it is reported using the `ClientModel.reportException(Exception)` method.
    Throws:
      `Exception` - If an error occurs during the connection, reading from the input stream, or processing commands.

  - ### start

    public static void start(String host, String port, UiRenderer renderer) throws Exception
    Establishes a connection to the server and starts the SocketClient.
    Parameters:
      `host` - The hostname or IP address of the server to connect to.
      `port` - The port number on which the server is listening.
      `renderer` - The `UiRenderer` responsible for rendering the client user interface.
    Throws:
      `Exception` - If an error occurs during the connection setup or while starting the client.

  - ### connectionTest

    public void connectionTest() throws Exception
    A method to test the connection.
    Specified by:
      `connectionTest` in interface `VirtualView`
    Throws:
      `Exception`

  - ### showUpdate

    public void showUpdate(UpdateMessage msg) throws Exception
    Handles update messages received from the server. These messages represent "simpler" actions or state changes that don't require full method calls. The update is forwarded to the `ClientModel`.
    Specified by:
      `showUpdate` in interface `VirtualView`
    Parameters:
      `msg` - The `UpdateMessage` containing the type of the update.
    Throws:
      `Exception`

  - ### showCard

    public void showCard(Card card) throws Exception
    Displays a given `Card` to the client. The card display logic is delegated to the `ClientModel`.
    Specified by:
      `showCard` in interface `VirtualView`
    Parameters:
      `card` - The `Card` to be displayed.
    Throws:
      `Exception`

  - ### showComponent

    public void showComponent(Component component) throws Exception
    Displays a given `Component` to the client. The component display logic is delegated to the `ClientModel`.
    Specified by:
      `showComponent` in interface `VirtualView`
    Parameters:
      `component` - The `Component` to be displayed.
    Throws:
      `Exception`

  - ### lockClient

    public void lockClient() throws Exception
    Locks the client's interaction.
    Specified by:
      `lockClient` in interface `VirtualView`
    Throws:
      `Exception`

  - ### setNickname

    public void setNickname() throws InterruptedException
    Prompts the client to set their nickname. The nickname setting logic is delegated to the `ClientModel`.
    Specified by:
      `setNickname` in interface `VirtualView`
    Throws:
      `InterruptedException`

  - ### startAssemblingPhase

    public void startAssemblingPhase(List<Player> players, int level) throws Exception
    Starts the assembling phase of the game. This method provides the list of players and the current game level to the client model to begin the assembling phase.
    Specified by:
      `startAssemblingPhase` in interface `VirtualView`
    Parameters:
      `players` - The list of `Player`s in the game.
      `level` - The current game level.
    Throws:
      `Exception`

  - ### startGame

    public void startGame()
    Notifies the client that the game has started.
    Specified by:
      `startGame` in interface `VirtualView`

  - ### showShip

    public void showShip(Ship ship) throws InterruptedException
    Displays his own or another player's `Ship` to the client.
    Specified by:
      `showShip` in interface `VirtualView`
    Parameters:
      `ship` - The `Ship` to be displayed.
    Throws:
      `InterruptedException`

  - ### showFaceUpComponentList

    public void showFaceUpComponentList(List<Component> components)
    Displays a list of face-up `Component`s to the client.
    Specified by:
      `showFaceUpComponentList` in interface `VirtualView`
    Parameters:
      `components` - The list of face-up `Component`s.

  - ### showSecondsLeftForGui

    public void showSecondsLeftForGui(int counter, int secondsLeft) throws Exception
    Displays the countdown for the hourglass timer (GUI).
    Specified by:
      `showSecondsLeftForGui` in interface `VirtualView`
    Parameters:
      `counter` - The current hourglass number.
      `secondsLeft` - The number of seconds remaining.
    Throws:
      `Exception`

  - ### showSecondsLeft

    public void showSecondsLeft(int count, int secondsLeft) throws Exception
    Displays the countdown for the hourglass timer (TUI).
    Specified by:
      `showSecondsLeft` in interface `VirtualView`
    Parameters:
      `count` - The current hourglass number.
      `secondsLeft` - The number of seconds remaining.
    Throws:
      `Exception`

  - ### finishFinalHourglass

    public void finishFinalHourglass() throws Exception
    Notifies the client that the final hourglass has finished.
    Specified by:
      `finishFinalHourglass` in interface `VirtualView`
    Throws:
      `Exception`

  - ### updatePositions

    public void updatePositions(Map<Integer,Integer> positions)
    Updates the positions of players on the game board.
    Specified by:
      `updatePositions` in interface `VirtualView`
    Parameters:
      `positions` - A map of integer keys to integer values representing updated positions.

  - ### effectResolution

    public void effectResolution()
    Starts the effect resolution phase.
    Specified by:
      `effectResolution` in interface `VirtualView`

  - ### startFlightPhase

    public void startFlightPhase(Ship ship, Map<Integer,Integer> positions) throws Exception
    Starts the flight phase client-side.
    Specified by:
      `startFlightPhase` in interface `VirtualView`
    Parameters:
      `ship` - The player's `Ship`.
      `positions` - A map with player ids as keys and their respective positions as values.
    Throws:
      `Exception`

  - ### crewPlacingPhase

    public void crewPlacingPhase(Ship ship, Map<Integer,Integer> positions) throws Exception
    Initiates the crew placing phase for a ship.
    Specified by:
      `crewPlacingPhase` in interface `VirtualView`
    Parameters:
      `ship` - The `Ship` for which crew is being placed.
      `positions` - A map of integer keys to integer values representing updated positions during crew placement.
    Throws:
      `Exception`

  - ### showSubDeck

    public void showSubDeck(List<Card> subDeck) throws Exception
    Displays a sub-deck of `Card`s to the client.
    Specified by:
      `showSubDeck` in interface `VirtualView`
    Parameters:
      `subDeck` - The list of `Card`s in the sub-deck.
    Throws:
      `Exception`

  - ### showFinalRanking

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

  - ### notifyDefeat

    public void notifyDefeat(int loseType)
    Notifies the client of his defeat and the reason.
    Specified by:
      `notifyDefeat` in interface `VirtualView`
    Parameters:
      `loseType` - An integer representing the type of defeat.

  - ### setGameLevel

    public void setGameLevel(int level)
    Sets the game level on the client side.
    Specified by:
      `setGameLevel` in interface `VirtualView`
    Parameters:
      `level` - The current game level.

  - ### setUUID

    public void setUUID(UUID clientId) throws Exception
    Sets the unique identifier (UUID) for this client.
    Specified by:
      `setUUID` in interface `VirtualView`
    Parameters:
      `clientId` - The `UUID` assigned to this client.
    Throws:
      `Exception`

  - ### connectionResponse

    public void connectionResponse(ConnectionResponse response) throws Exception
    Handles the response received after a connection attempt.
    Specified by:
      `connectionResponse` in interface `VirtualView`
    Parameters:
      `response` - The `ConnectionResponse` indicating the current game state.
    Throws:
      `Exception`

  - ### playerJoined

    public void playerJoined(String newPlayer, List<String> players) throws Exception
    Notifies the client when a new player has joined the game.
    Specified by:
      `playerJoined` in interface `VirtualView`
    Parameters:
      `newPlayer` - The nickname of the new player who joined.
      `players` - The updated list of all player nicknames in the game.
    Throws:
      `Exception`

  - ### newLeader

    public void newLeader(boolean isNewLeader) throws Exception
    Notifies the client if they have become the new leader of the game.
    Specified by:
      `newLeader` in interface `VirtualView`
    Parameters:
      `isNewLeader` - A boolean indicating if the current client is the new leader.
    Throws:
      `Exception`

  - ### diceResult

    public void diceResult(int diceResult) throws Exception
    Displays the result of a dice roll to the client.
    Specified by:
      `diceResult` in interface `VirtualView`
    Parameters:
      `diceResult` - The integer result of the dice roll.
    Throws:
      `Exception`

  - ### roundResult

    public void roundResult(boolean roundWon) throws Exception
    Notifies the client of the result of a game round during effect application of certain cards.
    Specified by:
      `roundResult` in interface `VirtualView`
    Parameters:
      `roundWon` - A boolean indicating whether the round was won.
    Throws:
      `Exception`

  - ### turnChange

    public void turnChange(boolean isMyTurn, int turnType, int currentPlayer) throws Exception
    Notifies the client about a change in turn.
    Specified by:
      `turnChange` in interface `VirtualView`
    Parameters:
      `isMyTurn` - A boolean indicating if it is the current client's turn.
      `turnType` - An integer representing the type of turn (e.g., specific phase).
      `currentPlayer` - The ID of the current player whose turn it is.
    Throws:
      `Exception`

  - ### showGameList

    public void showGameList(List<Integer> gameList) throws Exception
    Displays a list of available games to the client.
    Specified by:
      `showGameList` in interface `VirtualView`
    Parameters:
      `gameList` - A list of integers, where each integer represents a game ID.
    Throws:
      `Exception`