refactor: formated lobbymanager properly

This commit is contained in:
Yannik Bretschneider 2021-06-07 02:01:31 +02:00
parent 16c425c507
commit 752f43b86f
5 changed files with 34 additions and 47 deletions

View File

@ -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);

View File

@ -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<Participant, LobbyConnection> lobbies;
private final HashMap<String, LobbyConnection> resourceDescriptorToLobby;
private String localResourceDescriptor;
private final BiConsumer<WebSocket, BasicMessage> sendMessageCallback;
private final BiConsumer<WebSocket, String> sendErrorCallback;
private String localResourceDescriptor;
public LobbyManager(BiConsumer<WebSocket, BasicMessage> sendMessageCallback,
BiConsumer<WebSocket, String> 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<String, LobbyConnection> getResourceDescriptorToLobby() {

View File

@ -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<LobbyConnection, Thread> 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();
}

View File

@ -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);

View File

@ -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",