java.lang.Object
it.polimi.ingsw.am13.controller.Lobby
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
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
FieldsModifier and TypeFieldDescriptionprivate 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)All rooms created, with both games started or not (mapped via their gameId) -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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 gamesstatic Lobby
Fetches the unique instance existing of this class, or first instantiates it (Singleton Pattern)getRooms()
private boolean
isNickInvalid
(GameListener player) Checks if specified nickname is valid, that is if it's not already chosen by someone else in the lobbyprivate 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 roomvoid
joinRoom
(int gameId, GameListener player) Adds a players to an existing room, specified by the given gameId.void
leaveRoom
(GameListener player) Removes a players from the existing room (specified by the given gameId) they joined.reconnectPlayer
(GameListener player) Reconnects a disconnected player for the already started game they took part in.static void
Test purposes onlyprivate void
startGame
(int gameId) Starts the game for the room specified by gameId.
-
Field Details
-
instance
Unique instance of this class (implementing Singleton pattern) -
controllers
Controllers of the games already started (mapped via their gameId) -
rooms
All rooms created, with both games started or not (mapped via their gameId)
-
-
Constructor Details
-
Lobby
private Lobby()
-
-
Method Details
-
getInstance
Fetches the unique instance existing of this class, or first instantiates it (Singleton Pattern)- Returns:
- Unique instance of Lobby
-
getRooms
- Returns:
- List of all rooms in the lobby, both the ones with game starter and not already started
-
isNickInvalid
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
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 validroom
- Room the player wants to join- Returns:
- False if player is valid (has not been already chosen), true otherwise
-
createRoom
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 futurenPlayers
- 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
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 joinplayer
- 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
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
Starts the game for the room specified by gameId. It creates the associatedGameController
, 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 yetInvalidPlayersNumberException
- If the game contains only 1 player
-
endGame
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
public GameController reconnectPlayer(GameListener player) throws LobbyException, ConnectionException, GameStatusException 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 gameConnectionException
- If the player was already connectedGameStatusException
- 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
-