# Class SocketServer

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

All Implemented Interfaces:
  `VirtualServer`, `Remote`

public class SocketServer extends Object implements VirtualServer
The SocketServer class represents a server that listens for client connections, manages them, and delegates their requests to the ControllerManager for processing. It implements the VirtualServer interface.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SocketServer(ServerSocket listenSocket, ControllerManager controllerManager)`
  Initializes a SocketServer instance with the specified listen socket and controller manager.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `abandonRace(UUID clientId)`
  Handles a request to abandon the race.
  `void`
  `attachComponent(Component component, int x, int y, UUID clientId)`
  Handles a request to attach specified component on player's ship at specified coordinates
  `void`
  `connect(VirtualView client)`
  Handles a connection request
  `void`
  `createGame(UUID clientId)`
  Handles a request to create a new game.
  `void`
  `drawCard(UUID clientId)`
  Handles a request to draw a card for the specified client.
  `void`
  `drawFaceDownComponent(UUID clientId)`
  Handles a request to draw a component from face down components,
  `void`
  `drawFaceUpComponent(int id, UUID clientId)`
  Handles a request to draw a face up component.
  `void`
  `drawFromReserved(int index, UUID clientId)`
  Handles a request to draw a component from reserved components
  `void`
  `endAssemblePhase(UUID clientId)`
  Handles a request to end assembling phase
  `void`
  `endCrewPlacingPhase(UUID clientId)`
  Handles a request to end crew placing phase
  `void`
  `getFaceUpComponentList(UUID clientId)`
  Handles a request to see face up components list
  `void`
  `getSubDeck(UUID clientId, int index)`
  Handles a request to see sub deck in specified index.
  `void`
  `increment(UUID clientId)`
  Handles a request to increment the connection counter for a specific client.
  `void`
  `joinGame(UUID clientId, int game)`
  Handles a request for a client to join an existing game.
  `void`
  `leaveGame(UUID clientId)`
  Handles request to leave game.
  `void`
  `ping(UUID clientId)`
  Sends a ping request to the controller associated with the specified client.
  `void`
  `placeCrewMember(UUID clientId, int x, int y, AlienColor type)`
  Handles a request to place crew members/alien of specified type in cabin in specified coordinates of the ship of the player
  `void`
  `placeRocket(UUID clientId, int position)`
  Handles a request to place player's rocket in specified position.
  `void`
  `putDownComponent(Component component, UUID clientId)`
  Handles a request to put down a component
  `void`
  `removeComponent(UUID clientId, int x, int y)`
  Handles a request to remove a component from the player's ship at the specified coordinates.
  `void`
  `rollDice(UUID clientId)`
  Handles a request to roll dice.
  `void`
  `setCardContext(UUID clientId, Map<String,Object> context)`
  Handles request to set card context (performing an operation that the player may/must do right before card effect is applied)
  `void`
  `setEffectContext(UUID clientId, Map<String,Object> context)`
  Handles a request to apply card effect
  `void`
  `setGameLevel(UUID clientId, int level)`
  Handles request to set game level.
  `void`
  `setMaxPlayers(UUID clientId, int userNumber)`
  Handles request to set max players.
  `void`
  `setNickname(String nickname, UUID clientId)`
  Handles request to set nickname.
  `void`
  `showShip(UUID clientId, String nickname)`
  Handles a request to view a specific player's ship.
  `static void`
  `start(ControllerManager controllerManager)`
  Starts the server by creating a listening server socket and initializing the socket server with the provided controller manager.
  `void`
  `startFinalHourGlass(UUID clientId)`
  Handles a request to start the final hourglass for the specified client.
  `void`
  `startHourGlass(UUID clientId)`
  Handles a request to start the hourglass timer for the specified client.
  `void`
  `viewGame(UUID clientId)`
  Handles a request to view the list of currently available games.

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

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

- ## Constructor Details

  - ### SocketServer

    public SocketServer(ServerSocket listenSocket, ControllerManager controllerManager)
    Initializes a SocketServer instance with the specified listen socket and controller manager.
    Parameters:
      `listenSocket` - the server socket that will listen for incoming connections
      `controllerManager` - the controller manager responsible for handling proper games management logic.

- ## Method Details

  - ### start

    public static void start(ControllerManager controllerManager) throws Exception
    Starts the server by creating a listening server socket and initializing the socket server with the provided controller manager.
    Parameters:
      `controllerManager` - the controller manager responsible for handling proper games management logic.
    Throws:
      `Exception` - if an error occurs while starting the server

  - ### connect

    public void connect(VirtualView client) throws Exception
    Handles a connection request
    Specified by:
      `connect` in interface `VirtualServer`
    Parameters:
      `client` - client to call the remote methods on
    Throws:
      `Exception`

  - ### createGame

    public void createGame(UUID clientId) throws Exception
    Handles a request to create a new game.
    Specified by:
      `createGame` in interface `VirtualServer`
    Parameters:
      `clientId` - the unique identifier of the client requesting the game creation
    Throws:
      `Exception` - if an error occurs during the game creation process

  - ### viewGame

    public void viewGame(UUID clientId) throws Exception
    Handles a request to view the list of currently available games.
    Specified by:
      `viewGame` in interface `VirtualServer`
    Parameters:
      `clientId` - the UUID of the client making the request
    Throws:
      `Exception` - if an error occurs while processing the request

  - ### joinGame

    public void joinGame(UUID clientId, int game) throws Exception
    Handles a request for a client to join an existing game.
    Specified by:
      `joinGame` in interface `VirtualServer`
    Parameters:
      `clientId` - the UUID of the client requesting to join the game
      `game` - the identifier of the game that the client wishes to join
    Throws:
      `Exception` - if an error occurs during the join operation

  - ### ping

    public void ping(UUID clientId) throws Exception
    Sends a ping request to the controller associated with the specified client.
    Specified by:
      `ping` in interface `VirtualServer`
    Parameters:
      `clientId` - the UUID of the client to be pinged.
    Throws:
      `Exception` - if an error occurs during the ping operation.

  - ### increment

    public void increment(UUID clientId) throws Exception
    Handles a request to increment the connection counter for a specific client.
    Specified by:
      `increment` in interface `VirtualServer`
    Parameters:
      `clientId` - the UUID of the client whose connection counter should be incremented
    Throws:
      `Exception` - if an error occurs during the operation

  - ### setMaxPlayers

    public void setMaxPlayers(UUID clientId, int userNumber) throws Exception
    Handles request to set max players.
    Specified by:
      `setMaxPlayers` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
      `userNumber` - number of players required to start the game
    Throws:
      `Exception`

  - ### setGameLevel

    public void setGameLevel(UUID clientId, int level) throws Exception
    Handles request to set game level.
    Specified by:
      `setGameLevel` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
      `level` - level of the game
    Throws:
      `Exception`

  - ### leaveGame

    public void leaveGame(UUID clientId) throws Exception
    Handles request to leave game.
    Specified by:
      `leaveGame` in interface `VirtualServer`
    Parameters:
      `clientId` - id of the client leaving the game
    Throws:
      `Exception`

  - ### setNickname

    public void setNickname(String nickname, UUID clientId) throws Exception
    Handles request to set nickname.
    Specified by:
      `setNickname` in interface `VirtualServer`
    Parameters:
      `nickname` - nickname the player wants to set
      `clientId` - id of the player's client
    Throws:
      `Exception`

  - ### drawCard

    public void drawCard(UUID clientId) throws Exception
    Handles a request to draw a card for the specified client.
    Specified by:
      `drawCard` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client making the request
    Throws:
      `Exception` - if an error occurs during the card draw operation

  - ### drawFaceDownComponent

    public void drawFaceDownComponent(UUID clientId) throws Exception
    Handles a request to draw a component from face down components,
    Specified by:
      `drawFaceDownComponent` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`

  - ### putDownComponent

    public void putDownComponent(Component component, UUID clientId) throws Exception
    Handles a request to put down a component
    Specified by:
      `putDownComponent` in interface `VirtualServer`
    Parameters:
      `component` - component to be put back
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`

  - ### getFaceUpComponentList

    public void getFaceUpComponentList(UUID clientId) throws Exception
    Handles a request to see face up components list
    Specified by:
      `getFaceUpComponentList` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`

  - ### drawFaceUpComponent

    public void drawFaceUpComponent(int id, UUID clientId) throws Exception
    Handles a request to draw a face up component.
    Specified by:
      `drawFaceUpComponent` in interface `VirtualServer`
    Parameters:
      `id` - component's id
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception` - if either specified component is not in face up components list or someone else already took it

  - ### startHourGlass

    public void startHourGlass(UUID clientId) throws Exception
    Handles a request to start the hourglass timer for the specified client.
    Specified by:
      `startHourGlass` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client requesting to start the hourglass
    Throws:
      `Exception` - if an error occurs during the hourglass start process

  - ### startFinalHourGlass

    public void startFinalHourGlass(UUID clientId) throws Exception
    Handles a request to start the final hourglass for the specified client.
    Specified by:
      `startFinalHourGlass` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception` - if an error occurs during the execution of the final hourglass process

  - ### drawFromReserved

    public void drawFromReserved(int index, UUID clientId) throws Exception
    Handles a request to draw a component from reserved components
    Specified by:
      `drawFromReserved` in interface `VirtualServer`
    Parameters:
      `index` - index of the component that the player wants to draw
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`

  - ### removeComponent

    public void removeComponent(UUID clientId, int x, int y) throws Exception
    Handles a request to remove a component from the player's ship at the specified coordinates.
    Specified by:
      `removeComponent` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client making the request.
      `x` - The row of the component to be removed.
      `y` - The column of the component to be removed.
    Throws:
      `Exception` - if an error occurs during the removal process.

  - ### attachComponent

    public void attachComponent(Component component, int x, int y, UUID clientId) throws Exception
    Handles a request to attach specified component on player's ship at specified coordinates
    Specified by:
      `attachComponent` in interface `VirtualServer`
    Parameters:
      `component` - component to be attached to the ship
      `x` - row to place the component on
      `y` - column to place the component on
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`

  - ### setCardContext

    public void setCardContext(UUID clientId, Map<String,Object> context) throws Exception
    Handles request to set card context (performing an operation that the player may/must do right before card effect is applied)
    Specified by:
      `setCardContext` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
      `context` - parameters used to set card context (actions done immediately before card effect resolution)
    Throws:
      `Exception`

  - ### setEffectContext

    public void setEffectContext(UUID clientId, Map<String,Object> context) throws Exception
    Handles a request to apply card effect
    Specified by:
      `setEffectContext` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
      `context` - parameters used for card effect resolution
    Throws:
      `Exception`

  - ### endAssemblePhase

    public void endAssemblePhase(UUID clientId) throws Exception
    Handles a request to end assembling phase
    Specified by:
      `endAssemblePhase` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`

  - ### placeRocket

    public void placeRocket(UUID clientId, int position) throws Exception
    Handles a request to place player's rocket in specified position.
    Specified by:
      `placeRocket` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
      `position` - integer representing the position from which the player wants to start
    Throws:
      `Exception`

  - ### endCrewPlacingPhase

    public void endCrewPlacingPhase(UUID clientId) throws Exception
    Handles a request to end crew placing phase
    Specified by:
      `endCrewPlacingPhase` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`

  - ### placeCrewMember

    public void placeCrewMember(UUID clientId, int x, int y, AlienColor type) throws Exception
    Handles a request to place crew members/alien of specified type in cabin in specified coordinates of the ship of the player
    Specified by:
      `placeCrewMember` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
      `x` - row of the cabin
      `y` - column of the cabin
      `type` - color of the alien to place
    Throws:
      `Exception`

  - ### showShip

    public void showShip(UUID clientId, String nickname) throws Exception
    Handles a request to view a specific player's ship.
    Specified by:
      `showShip` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client making the request
      `nickname` - Nickname of the player whose ship is being requested to view
    Throws:
      `Exception` - if an error occurs during the operation

  - ### rollDice

    public void rollDice(UUID clientId) throws Exception
    Handles a request to roll dice.
    Specified by:
      `rollDice` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception` - if player can't roll dice

  - ### getSubDeck

    public void getSubDeck(UUID clientId, int index) throws Exception
    Handles a request to see sub deck in specified index.
    Specified by:
      `getSubDeck` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
      `index` - index of sub deck requested
    Throws:
      `Exception`

  - ### abandonRace

    public void abandonRace(UUID clientId) throws Exception
    Handles a request to abandon the race.
    Specified by:
      `abandonRace` in interface `VirtualServer`
    Parameters:
      `clientId` - UUID of the client calling the method
    Throws:
      `Exception`