doc: added comments and logs to LobbyManager

This commit is contained in:
Yannik Bretschneider 2021-06-07 15:33:27 +02:00
parent 8c1e55d9b0
commit 9f366c7776

View File

@ -1,5 +1,6 @@
package uulm.teamname.marvelous.server.lobbymanager; package uulm.teamname.marvelous.server.lobbymanager;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties; import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum; import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum;
@ -20,7 +21,7 @@ public class LobbyManager {
private static LobbyManager instance; private static LobbyManager instance;
/** /**
* @return the current instance of the UserManager * @return the current instance of the LobbyManager
*/ */
public static LobbyManager getInstance() { public static LobbyManager getInstance() {
if (instance == null) { if (instance == null) {
@ -157,7 +158,12 @@ public class LobbyManager {
return true; return true;
} }
/**
* Handles the disconnect of a WebSocket. Note that this is not a leave from the game, but instead just that:
* a reconnectable disconnect.
*/
public void handleDisconnect(Client client, boolean byRemote) { public void handleDisconnect(Client client, boolean byRemote) {
Logger.trace("Handling disconnect of Client");
if (!participants.containsKey(client.id)) { if (!participants.containsKey(client.id)) {
return; return;
} }
@ -174,39 +180,58 @@ public class LobbyManager {
} }
/**
* Removes a participant from the game entirely. This is done when for example a player gets removed from the
* Lobby because of a timeout.
*/
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) { 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 (!lobbies.containsKey(lobbyID)) {
if (!LobbyRunner.getInstance().canAddLobby()) { if (!LobbyRunner.getInstance().canAddLobby()) {
UserManager.getInstance().removeClient(client, "The server is currently full."); 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; return;
} }
Logger.info("Lobby '{}' didn't exist yet, initializing", lobbyID);
lobbies.put(lobbyID, new LobbyConnection(lobbyID)); lobbies.put(lobbyID, new LobbyConnection(lobbyID));
} }
LobbyConnection lobby = lobbies.get(lobbyID); LobbyConnection lobby = lobbies.get(lobbyID);
ParticipantType type = lobby.freeSlot(); if (!lobby.hasFreePlayerSlot() && role != RoleEnum.SPECTATOR) {
Logger.debug("No free player slots available, disconnecting client '{}'", client.id.getName());
if (type == ParticipantType.Spectator && role != RoleEnum.SPECTATOR) { UserManager.getInstance()
UserManager.getInstance().removeClient(client, "The game is already full. Please connect as a spectator instead."); .removeClient(client, "The lobby your requested is already full. " +
"Please connect as a spectator instead.");
return; return;
} }
ParticipantType type = lobby.freeSlot();
Logger.trace("New participant '{}' has the role '{}'", client.id.getName(), type);
Participant participant = new Participant(client, lobbyID, type); Participant participant = new Participant(client, lobbyID, type);
participants.put(client.id, participant); participants.put(client.id, participant);
lobby.addParticipant(participant); lobby.addParticipant(participant);
if (type != ParticipantType.Spectator) { if (type != ParticipantType.Spectator) {
Logger.debug("Sending GameAssignment message to user '{}'", client.id.getName());
GameAssignmentMessage response = new GameAssignmentMessage(); GameAssignmentMessage response = new GameAssignmentMessage();
response.gameID = lobby.gameID; response.gameID = lobby.gameID;
response.characterSelection = lobby.options.get(type); response.characterSelection = lobby.options.get(type);
participant.sendMessage(response); participant.sendMessage(response);
} else { } else {
Logger.debug("Sending GeneralAssignment message to user '{}'", client.id.getName());
GeneralAssignmentMessage response = new GeneralAssignmentMessage(); GeneralAssignmentMessage response = new GeneralAssignmentMessage();
response.gameID = lobby.gameID; response.gameID = lobby.gameID;
participant.sendMessage(response); participant.sendMessage(response);