refactor: changed method order in LobbyManager

This commit is contained in:
Yannik Bretschneider 2021-06-07 17:03:06 +02:00
parent c2ad2e118e
commit 1be8a503bf

View File

@ -74,6 +74,56 @@ public class LobbyManager {
return true; return true;
} }
/**
* Adds a participant to a lobby. If the maximum amount of lobbies is already filled, or if the lobby requested
* isn't free, the participant is disconnected.
*/
private void addParticipant(Client client, String lobbyID, RoleEnum role) {
Logger.trace("Adding participant '{}' to the lobby '{}'", client.id.getName(), lobbyID);
if (!lobbies.containsKey(lobbyID)) {
if (!LobbyRunner.getInstance().canAddLobby()) {
Logger.info("Rejecting participant '{}' as server is already full", client.id.getName());
UserManager.getInstance().removeClient(client, "The server has currently its maximum" +
"lobby number. Please connect as a spectator instead.");
return;
}
Logger.info("Lobby '{}' didn't exist yet, initializing", lobbyID);
lobbies.put(lobbyID, new LobbyConnection(lobbyID));
}
LobbyConnection lobby = lobbies.get(lobbyID);
if (!lobby.hasFreePlayerSlot() && role != RoleEnum.SPECTATOR) {
Logger.debug("No free player slots available, disconnecting client '{}'", client.id.getName());
UserManager.getInstance()
.removeClient(client, "The lobby your requested is already full. " +
"Please connect as a spectator instead.");
return;
}
ParticipantType type = lobby.freeSlot();
Logger.trace("New participant '{}' has the role '{}'", client.id.getName(), type);
Participant participant = new Participant(client, lobbyID, type);
participants.put(client.id, participant);
lobby.addParticipant(participant);
if (type != ParticipantType.Spectator) {
Logger.debug("Sending GameAssignment message to user '{}'", client.id.getName());
GameAssignmentMessage response = new GameAssignmentMessage();
response.gameID = lobby.gameID;
response.characterSelection = lobby.options.get(type);
participant.sendMessage(response);
} else {
Logger.debug("Sending GeneralAssignment message to user '{}'", client.id.getName());
GeneralAssignmentMessage response = new GeneralAssignmentMessage();
response.gameID = lobby.gameID;
participant.sendMessage(response);
}
}
/** /**
* Handles a {@link CharacterSelectionMessage}, computes the characters that have been selected and relays that * Handles a {@link CharacterSelectionMessage}, computes the characters that have been selected and relays that
* information to the {@link LobbyConnection} concerned by this information. * information to the {@link LobbyConnection} concerned by this information.
@ -187,54 +237,4 @@ public class LobbyManager {
public void removeParticipant(Participant participant) { public void removeParticipant(Participant participant) {
participants.remove(participant.id); participants.remove(participant.id);
} }
/**
* Adds a participant to a lobby. If the maximum amount of lobbies is already filled, or if the lobby requested
* isn't free, the participant is disconnected.
*/
private void addParticipant(Client client, String lobbyID, RoleEnum role) {
Logger.trace("Adding participant '{}' to the lobby '{}'", client.id.getName(), lobbyID);
if (!lobbies.containsKey(lobbyID)) {
if (!LobbyRunner.getInstance().canAddLobby()) {
Logger.info("Rejecting participant '{}' as server is already full", client.id.getName());
UserManager.getInstance().removeClient(client, "The server has currently its maximum" +
"lobby number. Please connect as a spectator instead.");
return;
}
Logger.info("Lobby '{}' didn't exist yet, initializing", lobbyID);
lobbies.put(lobbyID, new LobbyConnection(lobbyID));
}
LobbyConnection lobby = lobbies.get(lobbyID);
if (!lobby.hasFreePlayerSlot() && role != RoleEnum.SPECTATOR) {
Logger.debug("No free player slots available, disconnecting client '{}'", client.id.getName());
UserManager.getInstance()
.removeClient(client, "The lobby your requested is already full. " +
"Please connect as a spectator instead.");
return;
}
ParticipantType type = lobby.freeSlot();
Logger.trace("New participant '{}' has the role '{}'", client.id.getName(), type);
Participant participant = new Participant(client, lobbyID, type);
participants.put(client.id, participant);
lobby.addParticipant(participant);
if (type != ParticipantType.Spectator) {
Logger.debug("Sending GameAssignment message to user '{}'", client.id.getName());
GameAssignmentMessage response = new GameAssignmentMessage();
response.gameID = lobby.gameID;
response.characterSelection = lobby.options.get(type);
participant.sendMessage(response);
} else {
Logger.debug("Sending GeneralAssignment message to user '{}'", client.id.getName());
GeneralAssignmentMessage response = new GeneralAssignmentMessage();
response.gameID = lobby.gameID;
participant.sendMessage(response);
}
}
} }