Class Lobby

java.lang.Object
it.polimi.ingsw.Server.Lobby

public class Lobby extends Object
Multiple LobbyServer instances will need to communicate to run the game. This class groups the the BlockingQueue<ClientEvent> used by each server to dispatch gameLobby-wide events.
  • Field Details

    • id

      private final UUID id
    • admin

      private final String admin
    • isPublic

      private final boolean isPublic
    • maxPlayers

      private final int maxPlayers
    • players

      private final List<String> players
    • playerEventQueues

      private final Map<String,BlockingQueue<ClientEvent>> playerEventQueues
    • isClosed

      private boolean isClosed
    • controller

      private Controller controller
  • Constructor Details

    • Lobby

      public Lobby(UUID id, boolean isPublic, int maxPlayers, String admin)
      Create a new lobby
      Parameters:
      id - a unique ID used to refer to the lobby
      isPublic - if the lobby is supposed to be publicly available to new clients
      maxPlayers - the amount of clients the game connected to the lobby will host
      admin - the name of the admin client. The admin owns the game and even though everyone can start a session, if the admin disconnects while in the waiting lobby, the lobby is closed.
  • Method Details

    • executeAction

      public void executeAction(PlayerAction pa) throws InputValidationException, OperationException
      Attempts to forward an action to the game's controller
      Parameters:
      pa - the action to forward
      Throws:
      InputValidationException - if the controller does not validate the action positively
      OperationException - if no controller is online or if a validated action failed to run properly
    • getId

      public UUID getId()
      Get the id of the lobby
      Returns:
      the UUID of the lobby
    • getAdmin

      public String getAdmin()
      Get the name of the admin
      Returns:
      the nickname of the admin client
    • isPublic

      public boolean isPublic()
      Check if the lobby is public or not
      Returns:
      true if the lobby is public, false otherwise
    • isLobbyFull

      public boolean isLobbyFull()
      Check to see if the lobby is full
      Returns:
      true if the lobby is full, false otherwise
    • getMaxPlayers

      public int getMaxPlayers()
      Get the maximum amount of players for the lobby
      Returns:
      the max amount of clients that can connect to the lobby
    • getPlayers

      public List<String> getPlayers()
      Get a list of connected players
      Returns:
      a List of the nicknames of the connected players
    • addPlayer

      public boolean addPlayer(String nick, BlockingQueue<ClientEvent> playerChannel)
      Connect a player to the lobby
      Parameters:
      nick - the nickname of the player to connect
      playerChannel - the queue to forward new ClientEvents to
      Returns:
      true if the player was successfully connected, false otherwise
    • notifyPlayers

      public void notifyPlayers(ClientEvent event)
      Propagates a ClientEvent to all players
      Parameters:
      event - the event to propagate to all players
    • disconnectPlayer

      protected void disconnectPlayer(String nick)
      Removes a player from the lobby. If the game has started for the lobby or the admin decided to leave, the lobby is closed and players are notified through LobbyClosedEvent
      Parameters:
      nick - the nickname of the player to remove from the lobby
    • isGameInProgress

      public boolean isGameInProgress()
      Check to see if the game has started or if clients are still waiting for the game to start
      Returns:
      true if the game is on, otherwise false
    • close

      protected void close()
      Closes the lobby and notifies players through LobbyClosedEvent
    • startGame

      protected void startGame(GameMode gameMode) throws InputValidationException
      Starts the game. LobbyServers will receive a GameStartEvent
      Parameters:
      gameMode - the GameMode to start the game in
      Throws:
      InputValidationException - if the players in lobby are more than 4 or less than 2