java.lang.Object
it.polimi.ingsw.am13.controller.ListenerHandler
it.polimi.ingsw.am13.controller.Room
- All Implemented Interfaces:
RoomIF
,Serializable
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final int
GameId for the room, denoting a game yet not started or already startedprivate boolean
Flag indicating if the game for this is started or not.private final int
Target number of players to reach in order to start the gameprivate List
<PlayerLobby> List of players that are in the game, null if the game has not started yet -
Constructor Summary
ConstructorsConstructorDescriptionRoom
(int gameId, GameListener player, int nPlayersTarget) Creates a nuw room with only the specified player, and sets the target number of players. -
Method Summary
Modifier and TypeMethodDescriptionint
int
boolean
void
joinRoom
(GameListener player) 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.boolean
leaveRoom
(PlayerLobby player) It removes the given player from the room.private void
notifyPlayerDisconnected
(PlayerLobby player) Notifies the view that a player has disconnected from the game.private void
notifyPlayerJoinedRoom
(PlayerLobby player) Notify the view that a Player has entered a Room.private void
notifyPlayerLeftRoom
(PlayerLobby player) Notify the view that a Player has left a Room.private void
notifyPlayerReconnected
(PlayerLobby player, GameController controller, GameModelIF model) Notifies the view that a player has reconnected to the game.void
reconnectToRoom
(GameListener player, GameModelIF model, GameController controller) If the game has started, it reconnects the given player to the room.toString()
Methods inherited from class it.polimi.ingsw.am13.controller.ListenerHandler
addListener, getListeners, notifyChosenPersonalObjective, notifyEndGame, notifyFinalPhase, notifyInGame, notifyNextTurn, notifyPickedCard, notifyPlayedCard, notifyPlayedStarter, notifyPointsCalculated, notifyStartGame, notifyWinner, removeListener, transmitChatMessage
-
Field Details
-
gameId
private final int gameIdGameId for the room, denoting a game yet not started or already started -
nPlayersTarget
private final int nPlayersTargetTarget number of players to reach in order to start the game -
gameStarted
private boolean gameStartedFlag 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
List of players that are in the game, null if the game has not started yet
-
-
Constructor Details
-
Room
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 roomplayer
- Player who created the room. It is automatically added to the roomnPlayersTarget
- 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
- Specified by:
getPlayers
in interfaceRoomIF
- Returns:
- List of players in this room
-
getPlayersInGame
- Specified by:
getPlayersInGame
in interfaceRoomIF
- 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() -
getnPlayersTarget
public int getnPlayersTarget()- Specified by:
getnPlayersTarget
in interfaceRoomIF
- Returns:
- Target number of players to reach in order to start the game
-
isGameStarted
public boolean isGameStarted()- Specified by:
isGameStarted
in interfaceRoomIF
- 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
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 addmodel
- 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
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
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
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
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
- TheGameModelIF
containing the updated version of the game.
-
toString
-