java.lang.Object
it.polimi.ingsw.am13.controller.Lobby

public class Lobby extends Object
Controller used to manage multiple games, that is to handle the lobby of players who are playing or are in a room willing to start a game.
It stores the game listeners of players in a room which has not starter the game yet. A player joining the lobby can create a new room, setting the number of player for the future game to start, or can join an existing room. (In the moment they join/create a room, they decide their nickname and their token) Until game is not actually started, the players can be added and removed. If the room remains empty, it is automatically deleted.
When the right number of players is reached, the game automatically starts, adn the associated gameController is created. The players who were in the room in that moment are so the definite ones for that game and they cannot change. All the rooms and the already started games are associated to a unique gameId.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Map<Integer,GameController>
    Controllers of the games already started (mapped via their gameId)
    private static Lobby
    Unique instance of this class (implementing Singleton pattern)
    private final Map<Integer,Room>
    All rooms created, with both games started or not (mapped via their gameId)
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    createRoom(GameListener player, int nPlayers)
    Creates a new Room, for now populated only with the specified player.
    void
    endGame(int gameId)
    Ends the given started game, by removing it from the stored games
    static Lobby
    Fetches the unique instance existing of this class, or first instantiates it (Singleton Pattern)
     
    private boolean
    Checks if specified nickname is valid, that is if it's not already chosen by someone else in the lobby
    private boolean
    Checks if the color of spcified player's token is valid, that is if it's not already chosen by someone else in the room
    void
    joinRoom(int gameId, GameListener player)
    Adds a players to an existing room, specified by the given gameId.
    void
    Removes a players from the existing room (specified by the given gameId) they joined.
    Reconnects a disconnected player for the already started game they took part in.
    static void
    Test purposes only
    private void
    startGame(int gameId)
    Starts the game for the room specified by gameId.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • instance

      private static Lobby instance
      Unique instance of this class (implementing Singleton pattern)
    • controllers

      private final Map<Integer,GameController> controllers
      Controllers of the games already started (mapped via their gameId)
    • rooms

      private final Map<Integer,Room> rooms
      All rooms created, with both games started or not (mapped via their gameId)
  • Constructor Details

    • Lobby

      private Lobby()
  • Method Details

    • getInstance

      public static Lobby getInstance()
      Fetches the unique instance existing of this class, or first instantiates it (Singleton Pattern)
      Returns:
      Unique instance of Lobby
    • getRooms

      public List<RoomIF> getRooms()
      Returns:
      List of all rooms in the lobby, both the ones with game starter and not already started
    • isNickInvalid

      private boolean isNickInvalid(GameListener player)
      Checks if specified nickname is valid, that is if it's not already chosen by someone else in the lobby
      Parameters:
      player - Listener of the player whose nickname is to be checked if it is valid
      Returns:
      False if player is valid (has not been already chosen), true otherwise
    • isTokenInvalid

      private boolean isTokenInvalid(GameListener player, Room room)
      Checks if the color of spcified player's token is valid, that is if it's not already chosen by someone else in the room
      Parameters:
      player - Listener of the player whose nickname is to be checked if it is valid
      room - Room the player wants to join
      Returns:
      False if player is valid (has not been already chosen), true otherwise
    • createRoom

      public void createRoom(GameListener player, int nPlayers) throws LobbyException
      Creates a new Room, for now populated only with the specified player. The gameId is automatically found as the next suitable gameId, hence it is different from the other ones created before by the lobby In case of success, the player who created the room is notified.
      Parameters:
      player - First player who creates the game which will start in the future
      nPlayers - The number of players to start the game, chosen by the player who creates the room
      Throws:
      LobbyException - If the player has a nickName already chosen by another player in the lobby, or if the given target number of players to reach is <2 or >4
    • joinRoom

      public void joinRoom(int gameId, GameListener player) throws LobbyException
      Adds a players to an existing room, specified by the given gameId. In case of success, the players in that room are notified. If the room with the newly joined players if full, it makes the game start (and notifies the players of this).
      Parameters:
      gameId - Id of the room the player wants to join
      player - Listener of the player to add to that room
      Throws:
      LobbyException - If
      • the player has a nickName already chosen by another player in the lobby
      • or if the color chosen is already taken
      • or if the room with the given gameId does not exist
      • or if it exists but the room is already full
    • leaveRoom

      public void leaveRoom(GameListener player) throws LobbyException
      Removes a players from the existing room (specified by the given gameId) they joined. In case of success, it notifies the players in the room If the room becomes empty, it is automatically removed, as if it has never been created. (it does not notify the players of this)
      Parameters:
      player - Player to remove from that room
      Throws:
      LobbyException - If the specified player is not in any existing room
    • startGame

      private void startGame(int gameId) throws LobbyException, InvalidPlayersNumberException
      Starts the game for the room specified by gameId. It creates the associated GameController, actually starting that game
      Parameters:
      gameId - Game to start
      Throws:
      LobbyException - If the room corresponding to the given gameId does not exist or has not started the game yet
      InvalidPlayersNumberException - If the game contains only 1 player
    • endGame

      public void endGame(int gameId) throws LobbyException
      Ends the given started game, by removing it from the stored games
      Parameters:
      gameId - Id of the started game to end
      Throws:
      LobbyException - If the specified game does not exist
    • reconnectPlayer

      Reconnects a disconnected player for the already started game they took part in. Triggers the notification for other players
      Parameters:
      player - Listener of the player to reconnect
      Returns:
      GameController for the game the player reconnected to
      Throws:
      LobbyException - If the given player (considering nickname and color of token) is not among players of any started game
      ConnectionException - If the player was already connected
      GameStatusException - if any of the methods called directly or indirectly by this method are called in wrong game phase (generic error, should not happen)
    • resetLobby

      public static void resetLobby()
      Test purposes only