feat: converted UserManager to singleton object
This commit is contained in:
parent
7db692f790
commit
0ee264502c
@ -20,6 +20,19 @@ import java.util.*;
|
||||
*/
|
||||
public class UserManager {
|
||||
|
||||
private static UserManager instance;
|
||||
|
||||
/**
|
||||
* @return the current instance of the UserManager
|
||||
*/
|
||||
public static UserManager getInstance() {
|
||||
if (instance == null) {
|
||||
Logger.debug("No instance of UserManager found. Creating new instance...");
|
||||
instance = new UserManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/** A set of users that aren't assigned to lobbies or character selection yet */
|
||||
private final HashSet<WebSocket> newUsers;
|
||||
|
||||
@ -45,7 +58,7 @@ public class UserManager {
|
||||
private final JSON json;
|
||||
|
||||
/** Constructs a new, empty UserManager */
|
||||
public UserManager() {
|
||||
private UserManager() {
|
||||
this.newUsers = new HashSet<>();
|
||||
this.readyToConnect = new HashMap<>();
|
||||
this.readyToReconnect = new HashMap<>();
|
||||
@ -55,7 +68,7 @@ public class UserManager {
|
||||
}
|
||||
|
||||
/** Called on a new WebSocket connection. Places the WebSocket and its ResourceDescriptor in a HashMap. */
|
||||
public void connectUser(WebSocket conn) {
|
||||
void connectUser(WebSocket conn) {
|
||||
newUsers.add(conn);
|
||||
}
|
||||
|
||||
@ -66,7 +79,7 @@ public class UserManager {
|
||||
* @param conn is the {@link WebSocket} that sent the message
|
||||
* @param message is the {@link String} sent by the connection
|
||||
*/
|
||||
public void messageReceived(WebSocket conn, String message) {
|
||||
void messageReceived(WebSocket conn, String message) {
|
||||
Logger.trace("Parsing message...");
|
||||
var parsedMessageOptional = json.parse(message);
|
||||
if (parsedMessageOptional.isEmpty()) {
|
||||
@ -148,8 +161,13 @@ public class UserManager {
|
||||
var participantToRestore = activeParticipants.get(clientID);
|
||||
|
||||
participantToRestore.setConnection(conn);
|
||||
readyToReconnect.remove(conn);
|
||||
inGame.put(conn, participantToRestore);
|
||||
|
||||
synchronized (readyToReconnect) {
|
||||
readyToReconnect.remove(conn);
|
||||
}
|
||||
synchronized (inGame) {
|
||||
inGame.put(conn, participantToRestore);
|
||||
}
|
||||
// activeParticipants remains the same, as no players have been removed from the game
|
||||
} else {
|
||||
Logger.debug(
|
||||
@ -157,8 +175,13 @@ public class UserManager {
|
||||
conn);
|
||||
|
||||
var clientID = readyToReconnect.get(conn);
|
||||
readyToConnect.put(conn, clientID);
|
||||
readyToReconnect.remove(conn);
|
||||
|
||||
synchronized (readyToConnect) {
|
||||
readyToConnect.put(conn, clientID);
|
||||
}
|
||||
synchronized (readyToReconnect) {
|
||||
readyToReconnect.remove(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,6 +195,16 @@ public class UserManager {
|
||||
void relayRequestMessage(Participant conn, RequestMessage message) {
|
||||
}
|
||||
|
||||
void disconnectUser(WebSocket conn, boolean closedByRemote) {
|
||||
// TODO: notify clients and such if the connection was in fact closed by the remote. Also remove participant
|
||||
synchronized (newUsers) {
|
||||
newUsers.remove(conn);
|
||||
}
|
||||
synchronized (inGame) {
|
||||
inGame.remove(conn);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessage(WebSocket conn, BasicMessage message) {
|
||||
var jsonRepresentingMessage = json.stringify(message);
|
||||
if (jsonRepresentingMessage.isEmpty()) {
|
||||
@ -189,16 +222,6 @@ public class UserManager {
|
||||
sendMessage(conn, errorMessage);
|
||||
}
|
||||
|
||||
public void disconnectUser(WebSocket conn, boolean closedByRemote) {
|
||||
// TODO: notify clients and such if the connection was in fact closed by the remote. Also remove participant
|
||||
synchronized (newUsers) {
|
||||
newUsers.remove(conn);
|
||||
}
|
||||
synchronized (inGame) {
|
||||
inGame.remove(conn);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUserConnected(WebSocket user) {
|
||||
return newUsers.contains(user) ||
|
||||
readyToReconnect.containsKey(user) ||
|
||||
|
Loading…
Reference in New Issue
Block a user