# Class SocketClientHandler

java.lang.Object
it.polimi.ingsw.is25am31.network.socket.server.SocketClientHandler

All Implemented Interfaces:
  `VirtualView`, `Remote`

public class SocketClientHandler extends Object implements VirtualView
The `SocketClientHandler` class is a server-side component responsible for managing the communication with a single connected client in a socket-based network environment. It implements the `VirtualView` interface, which means it acts as a listener for the server, allowing the server's core logic to send updates and commands to a specific client as if it were directly interacting with the client's view.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SocketClientHandler(SocketServer server, ObjectInputStream input, ObjectOutputStream output)`
  Constructs a new `SocketClientHandler`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `connectionResponse(ConnectionResponse connectionResponse)`
  Sends a `ConnectionResponse` to the client after a connection attempt.
  `void`
  `connectionTest()`
  Sends a ping command to the client to test connectivity.
  `void`
  `crewPlacingPhase(Ship ship, Map<Integer,Integer> positions)`
  Notifies the client to start the crew placing phase, providing the ship and its positions.
  `void`
  `diceResult(int diceResult)`
  Sends the result of a dice roll to the client.
  `void`
  `effectResolution()`
  Notifies the client to initiate an effect resolution phase.
  `void`
  `finishFinalHourglass()`
  Notifies the client that the final hourglass has finished.
  `void`
  `lockClient()`
  Commands the client to lock its input.
  `void`
  `newLeader(boolean isNewLeader)`
  Notifies the client of the new game leader.
  `void`
  `notifyDefeat(int loseType)`
  Notifies the client about a defeat condition, specifying the type of loss.
  `void`
  `playerJoined(String newPlayer, List<String> players)`
  Notifies the client that a new player has joined the game, along with an updated list of all players.
  `void`
  `roundResult(boolean roundWon)`
  Notifies the client of the result of a game round in specific card effects.
  `void`
  `run()`
  The main execution loop for this client handler.
  `void`
  `setGameLevel(int level)`
  Commands the client to set the game level.
  `void`
  `setNickname()`
  Commands the client to prompt the user for a nickname.
  `void`
  `setUUID(UUID clientId)`
  Sends the unique identifier (UUID) for this client.
  `void`
  `showCard(Card card)`
  Sends a `Card` object to the client for display.
  `void`
  `showComponent(Component component)`
  Sends a `Component` object to the client for display.
  `void`
  `showFaceUpComponentList(List<Component> component)`
  Sends the current list of face-up `Component`s to the client for display.
  `void`
  `showFinalRanking(Map<Player,Integer> ranking)`
  Sends the final ranking of players to the client.
  `void`
  `showGameList(List<Integer> gameList)`
  Sends a list of available game IDs to the client.
  `void`
  `showSecondsLeft(int count, int secondsLeft)`
  Sends the remaining seconds for an hourglass timer to the client.
  `void`
  `showSecondsLeftForGui(int counter, int secondsLeft)`
  Sends the remaining seconds for a GUI-specific timer to the client.
  `void`
  `showShip(Ship ship)`
  Sends a `Ship` to the client for display.
  `void`
  `showSubDeck(List<Card> subDeck)`
  Sends a sub-deck of `Card`s to the client for display.
  `void`
  `showUpdate(UpdateMessage msg)`
  Sends an `UpdateMessage` to the client.
  `void`
  `startAssemblingPhase(List<Player> players, int level)`
  Initiates the assembling phase on the client, providing player list and game level.
  `void`
  `startFlightPhase(Ship ship, Map<Integer,Integer> positions)`
  Notifies the client to start the flight phase, providing the ship and its positions.
  `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, including whether it's their turn, the turn type, and the current player.
  `void`
  `updatePositions(Map<Integer,Integer> positions)`
  Sends updated positions for game elements to the client.

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

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

- ## Constructor Details

  - ### SocketClientHandler

    public SocketClientHandler(SocketServer server, ObjectInputStream input, ObjectOutputStream output)
    Constructs a new `SocketClientHandler`.
    Parameters:
      `server` - The `SocketServer` instance that created this handler.
      `input` - The `ObjectInputStream` connected to the client, used for receiving commands.
      `output` - The `ObjectOutputStream` connected to the client, used for sending updates and responses.

- ## Method Details

  - ### run

    public void run() throws IOException, ClassNotFoundException
    The main execution loop for this client handler. It continuously reads `ClientServerCommand` objects from the client's input stream. Each received command is then executed on the `SocketServer`. If an exception occurs during command execution, an `ExceptionCommand` is sent back to the client.
    Throws:
      `IOException` - If an I/O error occurs while reading objects, or if other unexpected errors occur.
      `ClassNotFoundException` - If the class of the object to deserialize can't be found.

  - ### showUpdate

    public void showUpdate(UpdateMessage msg) throws IOException
    Sends an `UpdateMessage` to the client.
    Specified by:
      `showUpdate` in interface `VirtualView`
    Parameters:
      `msg` - The `UpdateMessage` to be sent.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showCard

    public void showCard(Card card) throws IOException
    Sends a `Card` object to the client for display.
    Specified by:
      `showCard` in interface `VirtualView`
    Parameters:
      `card` - The `Card` to be shown.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showComponent

    public void showComponent(Component component) throws IOException
    Sends a `Component` object to the client for display.
    Specified by:
      `showComponent` in interface `VirtualView`
    Parameters:
      `component` - The `Component` to be shown.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### startGame

    public void startGame() throws IOException
    Notifies the client that the game has started.
    Specified by:
      `startGame` in interface `VirtualView`
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### lockClient

    public void lockClient() throws IOException
    Commands the client to lock its input.
    Specified by:
      `lockClient` in interface `VirtualView`
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### setNickname

    public void setNickname() throws IOException
    Commands the client to prompt the user for a nickname.
    Specified by:
      `setNickname` in interface `VirtualView`
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### startAssemblingPhase

    public void startAssemblingPhase(List<Player> players, int level) throws IOException
    Initiates the assembling phase on the client, providing player list and game level.
    Specified by:
      `startAssemblingPhase` in interface `VirtualView`
    Parameters:
      `players` - The list of `Player`s in the game.
      `level` - The current game level.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showShip

    public void showShip(Ship ship) throws IOException
    Sends a `Ship` to the client for display.
    Specified by:
      `showShip` in interface `VirtualView`
    Parameters:
      `ship` - The `Ship` to be shown.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showFaceUpComponentList

    public void showFaceUpComponentList(List<Component> component) throws IOException
    Sends the current list of face-up `Component`s to the client for display.
    Specified by:
      `showFaceUpComponentList` in interface `VirtualView`
    Parameters:
      `component` - The list of face-up `Component`s.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### updatePositions

    public void updatePositions(Map<Integer,Integer> positions) throws IOException
    Sends updated positions for game elements to the client.
    Specified by:
      `updatePositions` in interface `VirtualView`
    Parameters:
      `positions` - A map of integer keys to integer values representing updated positions.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### effectResolution

    public void effectResolution() throws IOException
    Notifies the client to initiate an effect resolution phase.
    Specified by:
      `effectResolution` in interface `VirtualView`
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### startFlightPhase

    public void startFlightPhase(Ship ship, Map<Integer,Integer> positions) throws IOException
    Notifies the client to start the flight phase, providing the ship and its positions.
    Specified by:
      `startFlightPhase` in interface `VirtualView`
    Parameters:
      `ship` - The `Ship` that is flying.
      `positions` - A map of integer keys to integer values representing updated rockets positions on the game board.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showSubDeck

    public void showSubDeck(List<Card> subDeck) throws IOException
    Sends a sub-deck of `Card`s to the client for display.
    Specified by:
      `showSubDeck` in interface `VirtualView`
    Parameters:
      `subDeck` - The list of `Card`s in the sub-deck.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### connectionTest

    public void connectionTest() throws IOException
    Sends a ping command to the client to test connectivity.
    Specified by:
      `connectionTest` in interface `VirtualView`
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### crewPlacingPhase

    public void crewPlacingPhase(Ship ship, Map<Integer,Integer> positions) throws IOException
    Notifies the client to start the crew placing phase, providing the ship and its positions.
    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:
      `IOException` - If an I/O error occurs during communication.

  - ### showSecondsLeft

    public void showSecondsLeft(int count, int secondsLeft) throws IOException
    Sends the remaining seconds for an hourglass timer to the client.
    Specified by:
      `showSecondsLeft` in interface `VirtualView`
    Parameters:
      `count` - The current hourglass number.
      `secondsLeft` - The number of seconds remaining.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### finishFinalHourglass

    public void finishFinalHourglass() throws IOException
    Notifies the client that the final hourglass has finished.
    Specified by:
      `finishFinalHourglass` in interface `VirtualView`
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showSecondsLeftForGui

    public void showSecondsLeftForGui(int counter, int secondsLeft) throws IOException
    Sends the remaining seconds for a GUI-specific timer to the client.
    Specified by:
      `showSecondsLeftForGui` in interface `VirtualView`
    Parameters:
      `counter` - The current hourglass number.
      `secondsLeft` - The number of seconds remaining.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showFinalRanking

    public void showFinalRanking(Map<Player,Integer> ranking) throws IOException
    Sends the final ranking of players to the client.
    Specified by:
      `showFinalRanking` in interface `VirtualView`
    Parameters:
      `ranking` - A map where keys are `Player` objects and values are their final scores.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### notifyDefeat

    public void notifyDefeat(int loseType) throws IOException
    Notifies the client about a defeat condition, specifying the type of loss.
    Specified by:
      `notifyDefeat` in interface `VirtualView`
    Parameters:
      `loseType` - An integer representing the type of defeat.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### setGameLevel

    public void setGameLevel(int level) throws IOException
    Commands the client to set the game level.
    Specified by:
      `setGameLevel` in interface `VirtualView`
    Parameters:
      `level` - The current game level.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### setUUID

    public void setUUID(UUID clientId) throws IOException
    Sends the unique identifier (UUID) for this client.
    Specified by:
      `setUUID` in interface `VirtualView`
    Parameters:
      `clientId` - The `UUID` to be set for the client.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### connectionResponse

    public void connectionResponse(ConnectionResponse connectionResponse) throws IOException
    Sends a `ConnectionResponse` to the client after a connection attempt.
    Specified by:
      `connectionResponse` in interface `VirtualView`
    Parameters:
      `connectionResponse` - The `ConnectionResponse`, indicating current game state.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### playerJoined

    public void playerJoined(String newPlayer, List<String> players) throws IOException
    Notifies the client that a new player has joined the game, along with an updated list of all players.
    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:
      `IOException` - If an I/O error occurs during communication.

  - ### newLeader

    public void newLeader(boolean isNewLeader) throws IOException
    Notifies the client of the new game leader.
    Specified by:
      `newLeader` in interface `VirtualView`
    Parameters:
      `isNewLeader` - A boolean indicating if the current client is the new leader.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### diceResult

    public void diceResult(int diceResult) throws IOException
    Sends 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:
      `IOException` - If an I/O error occurs during communication.

  - ### roundResult

    public void roundResult(boolean roundWon) throws IOException
    Notifies the client of the result of a game round in specific card effects.
    Specified by:
      `roundResult` in interface `VirtualView`
    Parameters:
      `roundWon` - A boolean indicating whether the round was won.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### turnChange

    public void turnChange(boolean isMyTurn, int turnType, int currentPlayer) throws IOException
    Notifies the client about a change in turn, including whether it's their turn, the turn type, and the current player.
    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.
      `currentPlayer` - The ID of the current player whose turn it is.
    Throws:
      `IOException` - If an I/O error occurs during communication.

  - ### showGameList

    public void showGameList(List<Integer> gameList) throws IOException
    Sends a list of available game IDs to the client.
    Specified by:
      `showGameList` in interface `VirtualView`
    Parameters:
      `gameList` - A list of integers, where each integer represents a game ID.
    Throws:
      `IOException` - If an I/O error occurs during communication.