Feat: accept optional client username and forward to ClientApp #271
@@ -20,8 +20,8 @@ public class User {
|
||||
/**
|
||||
* Creates a new User with the given ID, name and session.
|
||||
*
|
||||
* @param id the unique identifier for this user
|
||||
* @param name the display name of this user
|
||||
* @param id the unique identifier for this user
|
||||
* @param name the display name of this user
|
||||
* @param sessionId the session currently associated with this user
|
||||
*/
|
||||
public User(UserId id, String name, SessionId sessionId) {
|
||||
@@ -51,8 +51,7 @@ public class User {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new display name for this user. Thread-safe; callers must ensure the
|
||||
* registry is
|
||||
* Sets a new display name for this user. Thread-safe; callers must ensure the registry is
|
||||
* updated to maintain uniqueness when needed.
|
||||
*
|
||||
* @param newName the new display name
|
||||
@@ -73,8 +72,7 @@ public class User {
|
||||
/**
|
||||
* Returns the time at which this user disconnected, if applicable.
|
||||
*
|
||||
* @return an Optional containing the disconnect timestamp, or empty if
|
||||
* connected
|
||||
* @return an Optional containing the disconnect timestamp, or empty if connected
|
||||
*/
|
||||
public Optional<Instant> getDisconnectedAt() {
|
||||
return Optional.ofNullable(disconnectedAt);
|
||||
@@ -90,10 +88,7 @@ public class User {
|
||||
this.disconnectedAt = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks this user as disconnected by clearing the session and recording the
|
||||
* timestamp.
|
||||
*/
|
||||
/** Marks this user as disconnected by clearing the session and recording the timestamp. */
|
||||
public void markDisconnected() {
|
||||
this.sessionId = null;
|
||||
this.disconnectedAt = Instant.now();
|
||||
|
||||
@@ -18,16 +18,13 @@ public class UserFactory {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and registers a new user with the given name and session. If the name
|
||||
* is already
|
||||
* taken, a numeric suffix is appended and incremented until a free name is
|
||||
* found (e.g.
|
||||
* Lars_001, Lars_002, ...). If the desiredName is null or empty, an automatic
|
||||
* name of the
|
||||
* form "playerN" is assigned, incrementing N until a free name is found.
|
||||
* Creates and registers a new user with the given name and session. If the name is already
|
||||
* taken, a numeric suffix is appended and incremented until a free name is found (e.g.
|
||||
* Lars_001, Lars_002, ...). If the desiredName is null or empty, an automatic name of the form
|
||||
* "playerN" is assigned, incrementing N until a free name is found.
|
||||
*
|
||||
* @param desiredName the preferred display name
|
||||
* @param sessionId the session to associate with the new user
|
||||
* @param sessionId the session to associate with the new userwas
|
||||
* @return the newly created and registered user
|
||||
*/
|
||||
public User create(String desiredName, SessionId sessionId) {
|
||||
|
||||
+16
-26
@@ -12,11 +12,10 @@ public class UserRegistry {
|
||||
private final ConcurrentHashMap<SessionId, User> bySessionId = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Attempts to register a user under the given name atomically. Returns the
|
||||
* registered user, or
|
||||
* Attempts to register a user under the given name atomically. Returns the registered user, or
|
||||
* empty if the name is already taken.
|
||||
*
|
||||
* @param name the desired display name
|
||||
* @param name the desired display name
|
||||
* @param sessionId the session to associate with the new user
|
||||
* @return an Optional containing the new user, or empty if the name was taken
|
||||
*/
|
||||
@@ -93,10 +92,8 @@ public class UserRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the user with the given ID, but only if they are still disconnected.
|
||||
* This prevents
|
||||
* removing a user who has reconnected between the cleanup job's check and its
|
||||
* removal call.
|
||||
* Removes the user with the given ID, but only if they are still disconnected. This prevents
|
||||
* removing a user who has reconnected between the cleanup job's check and its removal call.
|
||||
*
|
||||
* @param userId the ID of the user to remove
|
||||
*/
|
||||
@@ -115,8 +112,7 @@ public class UserRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the user associated with the given session as disconnected, clearing
|
||||
* the session
|
||||
* Marks the user associated with the given session as disconnected, clearing the session
|
||||
* association and recording the disconnect timestamp.
|
||||
*
|
||||
* @param sessionId the session ID of the disconnected client
|
||||
@@ -131,10 +127,9 @@ public class UserRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reassociates a user with a new session, effectively restoring them after a
|
||||
* reconnect.
|
||||
* Reassociates a user with a new session, effectively restoring them after a reconnect.
|
||||
*
|
||||
* @param userId the ID of the user to reconnect
|
||||
* @param userId the ID of the user to reconnect
|
||||
* @param sessionId the new session ID
|
||||
* @return an Optional containing the user, or empty if the user was not found
|
||||
*/
|
||||
@@ -153,9 +148,8 @@ public class UserRegistry {
|
||||
* Looks up a user by their {@link SessionId}.
|
||||
*
|
||||
* @param sessionId the SessionId to look up
|
||||
* @return an Optional containing the {@link User}, or empty if no user is
|
||||
* associated with this
|
||||
* SessionId
|
||||
* @return an Optional containing the {@link User}, or empty if no user is associated with this
|
||||
* SessionId
|
||||
*/
|
||||
public Optional<User> getBySessionId(SessionId sessionId) {
|
||||
return Optional.ofNullable(bySessionId.get(sessionId));
|
||||
@@ -165,9 +159,8 @@ public class UserRegistry {
|
||||
* Looks up a user by their {@link UserId}.
|
||||
*
|
||||
* @param userId the UserId to look up
|
||||
* @return an Optional containing the {@link User}, or empty if no user is
|
||||
* associated with this
|
||||
* UserId
|
||||
* @return an Optional containing the {@link User}, or empty if no user is associated with this
|
||||
* UserId
|
||||
*/
|
||||
public Optional<User> getByUserId(UserId userId) {
|
||||
return Optional.ofNullable(byId.get(userId));
|
||||
@@ -177,9 +170,8 @@ public class UserRegistry {
|
||||
* Looks up a user by their username.
|
||||
*
|
||||
* @param username the username to look up
|
||||
* @return an Optional containing the {@link User}, or empty if no user is
|
||||
* associated with this
|
||||
* username
|
||||
* @return an Optional containing the {@link User}, or empty if no user is associated with this
|
||||
* username
|
||||
*/
|
||||
public Optional<User> getByUsername(String username) {
|
||||
return Optional.ofNullable(byName.get(username));
|
||||
@@ -195,14 +187,12 @@ public class UserRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to change the username of an existing user. Ensures the new name is
|
||||
* not already
|
||||
* Attempts to change the username of an existing user. Ensures the new name is not already
|
||||
* taken and updates internal indices atomically.
|
||||
*
|
||||
* @param userId the id of the user to rename
|
||||
* @param userId the id of the user to rename
|
||||
* @param newName the desired new name
|
||||
* @return true if the rename succeeded, false if the name was already taken or
|
||||
* user not found
|
||||
* @return true if the rename succeeded, false if the name was already taken or user not found
|
||||
*/
|
||||
public synchronized boolean changeUsername(UserId userId, String newName) {
|
||||
User user = byId.get(userId);
|
||||
|
||||
Reference in New Issue
Block a user