Package it.polimi.ingsw.Server
Class Lobby
java.lang.Object
it.polimi.ingsw.Server.Lobby
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final Stringprivate Controllerprivate final UUIDprivate booleanprivate final booleanprivate final intprivate final Map<String,BlockingQueue<ClientEvent>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddPlayer(String nick, BlockingQueue<ClientEvent> playerChannel) Connect a player to the lobbyprotected voidclose()Closes the lobby and notifies players throughLobbyClosedEventprotected voiddisconnectPlayer(String nick) Removes a player from the lobby.voidAttempts to forward an action to the game's controllergetAdmin()Get the name of the admingetId()Get the id of the lobbyintGet the maximum amount of players for the lobbyGet a list of connected playersbooleanCheck to see if the game has started or if clients are still waiting for the game to startbooleanCheck to see if the lobby is fullbooleanisPublic()Check if the lobby is public or notvoidnotifyPlayers(ClientEvent event) Propagates aClientEventto all playersprotected voidStarts the game.
-
Field Details
-
id
-
admin
-
isPublic
private final boolean isPublic -
maxPlayers
private final int maxPlayers -
players
-
playerEventQueues
-
isClosed
private boolean isClosed -
controller
-
-
Constructor Details
-
Lobby
Create a new lobby- Parameters:
id- a unique ID used to refer to the lobbyisPublic- if the lobby is supposed to be publicly available to new clientsmaxPlayers- the amount of clients the game connected to the lobby will hostadmin- 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
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 positivelyOperationException- if no controller is online or if a validated action failed to run properly
-
getId
Get the id of the lobby- Returns:
- the
UUIDof the lobby
-
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
Get a list of connected players- Returns:
- a
Listof the nicknames of the connected players
-
addPlayer
Connect a player to the lobby- Parameters:
nick- the nickname of the player to connectplayerChannel- the queue to forward newClientEvents to- Returns:
- true if the player was successfully connected, false otherwise
-
notifyPlayers
Propagates aClientEventto all players- Parameters:
event- the event to propagate to all players
-
disconnectPlayer
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 throughLobbyClosedEvent- 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 throughLobbyClosedEvent -
startGame
Starts the game.LobbyServers will receive aGameStartEvent- Parameters:
gameMode- theGameModeto start the game in- Throws:
InputValidationException- if the players in lobby are more than 4 or less than 2
-