From 752f43b86ff1f900d69af0fd485812c6ffb8cea3 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Mon, 7 Jun 2021 02:01:31 +0200 Subject: [PATCH] refactor: formated lobbymanager properly --- .../server/lobbymanager/LobbyConnection.java | 5 ++- .../server/lobbymanager/LobbyManager.java | 33 ++++++++++--------- .../server/lobbymanager/LobbyRunner.java | 25 +++++--------- .../server/lobbymanager/Participant.java | 14 ++++---- .../lobbymanager/RandomWordGenerator.java | 4 +-- 5 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java index 700f9bf..e3ce86c 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java @@ -201,8 +201,7 @@ public class LobbyConnection implements Runnable { } /** - * Tries to read the next message in the messageQueue. waits for timeoutMillis - * for a queue item + * Tries to read the next message in the messageQueue. waits for timeoutMillis for a queue item * * @param timeoutMillis is the amount of time the method waits until continuing even though not being notified. */ @@ -504,7 +503,7 @@ public class LobbyConnection implements Runnable { public void broadcastEvents(Event... events) { Logger.trace("Broadcasting {} events to all participants", events.length); - var message = new EventMessage(); + var message = new EventMessage(); message.messages = events; broadcast(message); diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java index 80b5047..cd1554f 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java @@ -8,7 +8,6 @@ import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum; import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage; import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage; import uulm.teamname.marvelous.gamelibrary.messages.client.RequestMessage; -import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage; import uulm.teamname.marvelous.server.lobby.Lobby; import java.util.HashMap; @@ -18,9 +17,9 @@ import java.util.function.BiConsumer; public class LobbyManager { private final HashMap lobbies; private final HashMap resourceDescriptorToLobby; - private String localResourceDescriptor; private final BiConsumer sendMessageCallback; private final BiConsumer sendErrorCallback; + private String localResourceDescriptor; public LobbyManager(BiConsumer sendMessageCallback, BiConsumer sendErrorCallback) { @@ -31,12 +30,13 @@ public class LobbyManager { } /** - * Assigns a lobby to the given participant. If there are no lobbies available, a new lobby will be created. - * The {@link WebSocket#getResourceDescriptor() ResourceDescriptor} is hereby preferred as the LobbyID, whereby - * spectators are always assigned to the lobby specified in said resourceDescriptor while players are connected - * to a lobby with a similar resourceDescriptor if the lobby they requested is already full + * Assigns a lobby to the given participant. If there are no lobbies available, a new lobby will be created. The + * {@link WebSocket#getResourceDescriptor() ResourceDescriptor} is hereby preferred as the LobbyID, whereby + * spectators are always assigned to the lobby specified in said resourceDescriptor while players are connected to a + * lobby with a similar resourceDescriptor if the lobby they requested is already full + * * @param playerName is the name of the player be assigned to a lobby - * @param message is the {@link PlayerReadyMessage} sent by the participant that triggered the LobbyAssignment + * @param message is the {@link PlayerReadyMessage} sent by the participant that triggered the LobbyAssignment * @return the {@link Participant} that was actually assigned to the lobby */ public Participant assignLobbyToConnection(WebSocket connection, String playerName, PlayerReadyMessage message) { @@ -140,14 +140,15 @@ public class LobbyManager { /** * Initializes a new {@link LobbyConnection} with a not yet initialized {@link Lobby}. The {@link LobbyConnection} - * gets some GameID, and also the sendMessageCallback and sendErrorCallback from the - * {@link uulm.teamname.marvelous.server.netconnector.UserManager}. - * @param gameID is the ID that the {@link LobbyConnection} is initialized with. This is normally - * the resourceDescriptor. + * gets some GameID, and also the sendMessageCallback and sendErrorCallback from the {@link + * uulm.teamname.marvelous.server.netconnector.UserManager}. + * + * @param gameID is the ID that the {@link LobbyConnection} is initialized with. This is normally the + * resourceDescriptor. * @return the newly initialized LobbyConnection */ private LobbyConnection initializeNewLobby(String gameID) { - var lobby = new LobbyConnection(gameID, sendMessageCallback, sendErrorCallback); + var lobby = new LobbyConnection(gameID, sendMessageCallback, sendErrorCallback); Logger.debug("Adding mapping from gameID (resourceDescriptor) '{}' to new lobby...", gameID); synchronized (resourceDescriptorToLobby) { resourceDescriptorToLobby.put(gameID, lobby); @@ -156,8 +157,8 @@ public class LobbyManager { } /** - * Returns the local resource descriptor if it is pointing to a not yet filled lobby (joinable lobby), or - * generates a new one if the lobby described by the resourceDescriptor is full already + * Returns the local resource descriptor if it is pointing to a not yet filled lobby (joinable lobby), or generates + * a new one if the lobby described by the resourceDescriptor is full already */ private String getLocalResourceDescriptor() { Logger.trace("Getting local resourceDescriptor. Currently this is '{}'", localResourceDescriptor); @@ -195,8 +196,8 @@ public class LobbyManager { } /** - * A method to obtain the resourceDescriptorToLobby HashMap. - * Meant for testing, and shouldn't be used anywhere else. + * A method to obtain the resourceDescriptorToLobby HashMap. Meant for testing, and shouldn't be used anywhere + * else. */ @Deprecated Map getResourceDescriptorToLobby() { diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java index b1563bc..2601127 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java @@ -2,16 +2,8 @@ package uulm.teamname.marvelous.server.lobbymanager; import org.tinylog.Logger; import uulm.teamname.marvelous.server.Server; -import uulm.teamname.marvelous.server.lobby.Lobby; -import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; /** * Class meant for running lobbies. It manages said lobbys, creates threads for it, and moves it into an executor @@ -19,17 +11,9 @@ import java.util.concurrent.TimeUnit; public class LobbyRunner { private static LobbyRunner instance; - - static LobbyRunner getInstance() { - if (instance == null) { - instance = new LobbyRunner(); - } - return instance; - } - - private final HashMap activeLobbies; + /** * Constructs a new LobbyRunner */ @@ -37,6 +21,13 @@ public class LobbyRunner { this.activeLobbies = new HashMap<>(); } + static LobbyRunner getInstance() { + if (instance == null) { + instance = new LobbyRunner(); + } + return instance; + } + boolean canAddLobby() { return activeLobbies.size() < Server.getMaxLobbies(); } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java index 3d8794e..a6f1ba3 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java @@ -8,20 +8,21 @@ import java.util.Objects; public class Participant { - /** The Websocket to contact the participant with */ - private WebSocket connection; - /** The ID of the device that the client provided at connect */ public final String deviceID; - public final String name; - /** The type (as in role) of participant */ public final ParticipantType type; + /** Persistent HashCode over the lifetime of the Participant */ + Integer hashCode; + /* Whether the participant is an AI */ // public final boolean AI; + /** The Websocket to contact the participant with */ + private WebSocket connection; + /** Creates a new {@link Participant} */ public Participant(WebSocket connection, ParticipantType type, String deviceID, String name) { this.connection = connection; @@ -58,7 +59,6 @@ public class Participant { this.connection = null; } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -67,8 +67,6 @@ public class Participant { return Objects.equals(connection, that.connection) && Objects.equals(deviceID, that.deviceID) && Objects.equals(name, that.name) && type == that.type; } - Integer hashCode; - @Override public int hashCode() { if (hashCode == null) hashCode = Objects.hash(connection, deviceID, name, type); diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/RandomWordGenerator.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/RandomWordGenerator.java index 83aec6a..71eee4c 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/RandomWordGenerator.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/RandomWordGenerator.java @@ -1,6 +1,5 @@ package uulm.teamname.marvelous.server.lobbymanager; -import java.util.Locale; import java.util.Random; public class RandomWordGenerator { @@ -18,11 +17,10 @@ public class RandomWordGenerator { firstWord = firstWord.substring(0, 1).toUpperCase() + firstWord.substring(1).toLowerCase(); secondWord = secondWord.substring(0, 1).toUpperCase() + secondWord.substring(1).toLowerCase(); return firstWord + secondWord; - } - private static final String[] randomWords = new String[] { + private static final String[] randomWords = new String[]{ "wait", "release", "river",