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
Modifier and TypeFieldDescriptionprivate final String
private Controller
private final UUID
private boolean
private final boolean
private final int
private final Map<String,
BlockingQueue<ClientEvent>> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
addPlayer
(String nick, BlockingQueue<ClientEvent> playerChannel) Connect a player to the lobbyprotected void
close()
Closes the lobby and notifies players throughLobbyClosedEvent
protected void
disconnectPlayer
(String nick) Removes a player from the lobby.void
Attempts to forward an action to the game's controllergetAdmin()
Get the name of the admingetId()
Get the id of the lobbyint
Get the maximum amount of players for the lobbyGet a list of connected playersboolean
Check to see if the game has started or if clients are still waiting for the game to startboolean
Check to see if the lobby is fullboolean
isPublic()
Check if the lobby is public or notvoid
notifyPlayers
(ClientEvent event) Propagates aClientEvent
to all playersprotected void
Starts 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
UUID
of 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
List
of 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 newClientEvent
s to- Returns:
- true if the player was successfully connected, false otherwise
-
notifyPlayers
Propagates aClientEvent
to 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.LobbyServer
s will receive aGameStartEvent
- Parameters:
gameMode
- theGameMode
to start the game in- Throws:
InputValidationException
- if the players in lobby are more than 4 or less than 2
-