# Class ControllerManager

java.lang.Object
it.polimi.ingsw.is25am31.controller.ControllerManager

public class ControllerManager extends Object
The ControllerManager class handles the management of game controllers, client connections, managing the starting and closing of games. It is used to create new games, connect clients to games, monitor client connections, and close games.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ControllerManager()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `checkPlayersConnection(Controller controller)`
  Continuously monitors player connections for a specific game controller.
  `void`
  `connectToGame(int gameNumber, UUID id)`
  Connects a client to an existing game by game number.
  `void`
  `connectUUID(VirtualView client)`
  Registers a new client connection by generating a unique UUID and initializing the client Sets up the client's virtual view and sends initial connection messages
  `void`
  `createNewGame(UUID id)`
  Creates a new game and assigns the specified client as the first player.
  `Controller`
  `getController(UUID id)`
  Retrieves the controller of the specified client ID
  `void`
  `increment(UUID clientId)`
  Increments the connection counter for a specific client.
  `void`
  `removeGame(int gameNumber)`
  Removes a game from the active games map and logs the closure.
  `void`
  `viewListGame(UUID id)`
  Sends a list of available games to the specified client Only includes games that have available slots

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

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

- ## Constructor Details

  - ### ControllerManager

    public ControllerManager()

- ## Method Details

  - ### getController

    public Controller getController(UUID id)
    Retrieves the controller of the specified client ID
    Parameters:
      `id` - UUID of the client
    Returns:
      Controller associated with the client, or null if not found

  - ### createNewGame

    public void createNewGame(UUID id)
    Creates a new game and assigns the specified client as the first player. Initializes a new controller, starts connection monitoring, and increments the game counter.
    Parameters:
      `id` - UUID of the client creating the game

  - ### viewListGame

    public void viewListGame(UUID id)
    Sends a list of available games to the specified client Only includes games that have available slots
    Parameters:
      `id` - UUID of the client requesting the game list

  - ### connectToGame

    public void connectToGame(int gameNumber, UUID id)
    Connects a client to an existing game by game number. Validates that the game exists and has available slots before connecting.
    Parameters:
      `gameNumber` - number identifier of the game to join
      `id` - UUID of the client attempting to connect
    Throws:
      `RuntimeException` - if the game is full or doesn't exist

  - ### connectUUID

    public void connectUUID(VirtualView client)
    Registers a new client connection by generating a unique UUID and initializing the client Sets up the client's virtual view and sends initial connection messages
    Parameters:
      `client` - VirtualView representing the client connection

  - ### removeGame

    public void removeGame(int gameNumber)
    Removes a game from the active games map and logs the closure.
    Parameters:
      `gameNumber` - the number identifier of the game to remove

  - ### increment

    public void increment(UUID clientId)
    Increments the connection counter for a specific client. Used as part of the connection monitoring mechanism.
    Parameters:
      `clientId` - the UUID of the client whose counter should be incremented

  - ### checkPlayersConnection

    public void checkPlayersConnection(Controller controller) throws Exception
    Continuously monitors player connections for a specific game controller. Tests client connections periodically and removes disconnected clients. Runs in a separate thread and handles connection timeouts.
    Parameters:
      `controller` - the Controller whose players' connections should be monitored
    Throws:
      `Exception` - if there's an error during connection monitoring