All Implemented Interfaces:
RoomIF, Serializable

public class Room extends ListenerHandler implements RoomIF
Represents a set of players who are willing to start playing or are already playing. When the room is created, the number of players to reach in order to start the game is set and cannot be modified. The players listed by this class can be always added/removed, remaining the size between 0 and the target number chosen. The first time that target number is reached, the room is set as "game started" Note that the room starts managing the listeners for the players, notifying them for other player joinig/leaving the room.
See Also:
  • Field Details

    • gameId

      private final int gameId
      GameId for the room, denoting a game yet not started or already started
    • nPlayersTarget

      private final int nPlayersTarget
      Target number of players to reach in order to start the game
    • gameStarted

      private boolean gameStarted
      Flag indicating if the game for this is started or not. It can change only once, when the room gets full for the first time
    • playersInGame

      private List<PlayerLobby> playersInGame
      List of players that are in the game, null if the game has not started yet
  • Constructor Details

    • Room

      public Room(int gameId, GameListener player, int nPlayersTarget) throws LobbyException
      Creates a nuw room with only the specified player, and sets the target number of players. The handler of listener is created in this moment. It also notifies the player who created the room that he has jointed successfully.
      Parameters:
      gameId - GameId for the new room
      player - Player who created the room. It is automatically added to the room
      nPlayersTarget - Target number of players to reach in order to start the game for this room
      Throws:
      LobbyException - If the target number of players is <2 or >4
  • Method Details

    • getPlayers

      public List<PlayerLobby> getPlayers()
      Specified by:
      getPlayers in interface RoomIF
      Returns:
      List of players in this room
    • getPlayersInGame

      public List<PlayerLobby> getPlayersInGame()
      Specified by:
      getPlayersInGame in interface RoomIF
      Returns:
      List of players that are in the game. If the game has not started yet, it corresponds to the players currently in the room
    • getGameId

      public int getGameId()
      Specified by:
      getGameId in interface RoomIF
      Returns:
      GameId for the room
    • getnPlayersTarget

      public int getnPlayersTarget()
      Specified by:
      getnPlayersTarget in interface RoomIF
      Returns:
      Target number of players to reach in order to start the game
    • isGameStarted

      public boolean isGameStarted()
      Specified by:
      isGameStarted in interface RoomIF
      Returns:
      Flag indicating if the game for this is started or not. It can change only once, when the room gets full for the first time
    • joinRoom

      public void joinRoom(GameListener player) throws LobbyException
      If the game has not started yet, it adds the given player to the room and notifies all players currently in the room of this. If, after this, the room gets full, it sets the game for the room as started (Note: it does not notify the players for this)
      Parameters:
      player - Listener of the player to add
      Throws:
      LobbyException - If the room is already full, or if the game has already started
    • reconnectToRoom

      public void reconnectToRoom(GameListener player, GameModelIF model, GameController controller) throws LobbyException
      If the game has started, it reconnects the given player to the room. If the room is already full, surely no other player can join it. It also notifies all players currently in the room of this.
      Parameters:
      player - Listener of the player to add
      model - Model representing the current situation to give to the reconnected player
      Throws:
      LobbyException - If the room is already full, or if the game has not started yet
    • leaveRoom

      public boolean leaveRoom(PlayerLobby player) throws LobbyException
      It removes the given player from the room. It also notifies all players currently in the room of this.
      Parameters:
      player - Listener of the player to remove
      Returns:
      True if the room gets empty after removing the given player, false otherwise
      Throws:
      LobbyException - If the room is already empty, or the given players is not in the room
    • notifyPlayerJoinedRoom

      private void notifyPlayerJoinedRoom(PlayerLobby player)
      Notify the view that a Player has entered a Room. This method is to be used only when the game hasn't started yet.
      Parameters:
      player - The player that entered the room
    • notifyPlayerLeftRoom

      private void notifyPlayerLeftRoom(PlayerLobby player)
      Notify the view that a Player has left a Room. This method is to be used only when the game hasn't started yet.
      Parameters:
      player - The player that left the room
    • notifyPlayerDisconnected

      private void notifyPlayerDisconnected(PlayerLobby player)
      Notifies the view that a player has disconnected from the game.
      Parameters:
      player - The player that has disconnected from the game.
    • notifyPlayerReconnected

      private void notifyPlayerReconnected(PlayerLobby player, GameController controller, GameModelIF model)
      Notifies the view that a player has reconnected to the game.
      Parameters:
      player - The player that has reconnected to the game.
      model - The GameModelIF containing the updated version of the game.
    • toString

      public String toString()
      Overrides:
      toString in class Object