Fix: Readd propagation of server assigned username. Login suername changed to... #301

Merged
jona.walpert merged 1 commits from fix/123-server-assigned-username-send-to-client into main 2026-04-24 13:14:23 +02:00
3 changed files with 45 additions and 52 deletions
Showing only changes of commit ea5f6a1083 - Show all commits
@@ -11,7 +11,7 @@ import org.apache.logging.log4j.Logger;
* Entry point and bootstrap helper for the Casono client application. * Entry point and bootstrap helper for the Casono client application.
* *
* <p>Responsibilities: - Parse CLI args (host:port, optional username) - Store username centrally * <p>Responsibilities: - Parse CLI args (host:port, optional username) - Store username centrally
* so UI can reuse it - Create a shared ClientService and do startup LOGIN (optional) * so UI can reuse it - Create a shared ClientService and do startup LOGIN
*/ */
public class ClientApp { public class ClientApp {
@@ -40,7 +40,14 @@ public class ClientApp {
} }
public static void updateSharedUsername(String username) { public static void updateSharedUsername(String username) {
setSharedUsername(username != null && !username.isBlank() ? username.trim() : null); setSharedUsername(normalizeUsername(username));
}
private static String normalizeUsername(String username) {
if (username == null || username.isBlank()) {
return null;
}
return username.trim();
} }
private static void setSharedUsername(String username) { private static void setSharedUsername(String username) {
@@ -52,15 +59,14 @@ public class ClientApp {
String[] hostPort = parseHostAndPort(arg); String[] hostPort = parseHostAndPort(arg);
String host = hostPort[0]; String host = hostPort[0];
int port = Integer.parseInt(hostPort[1]); int port = Integer.parseInt(hostPort[1]);
String normalizedUsername = normalizeUsername(username);
configureServerProperties(host, port); configureServerProperties(host, port);
setSharedUsername(username != null && !username.isBlank() ? username.trim() : null); setSharedUsername(normalizedUsername);
if (getSharedUsername() != null) { performStartupLogin(host, port, normalizedUsername);
performStartupLogin(host, port, username);
}
launchUI(arg, username); launchUI(arg);
} }
private static String[] parseHostAndPort(String arg) { private static String[] parseHostAndPort(String arg) {
@@ -81,52 +87,33 @@ public class ClientApp {
try { try {
ClientService clientService = new ClientService(host, port); ClientService clientService = new ClientService(host, port);
setSharedClientService(clientService); setSharedClientService(clientService);
java.util.concurrent.ExecutorService bg =
java.util.concurrent.Executors.newSingleThreadExecutor(
r -> {
Thread t = new Thread(r);
t.setDaemon(true);
t.setName("casono-startup-login");
return t;
});
bg.submit(
() -> {
try {
LobbyClient lobbyClient = new LobbyClient(clientService); LobbyClient lobbyClient = new LobbyClient(clientService);
LoginResult res = lobbyClient.login(username); LoginResult res = lobbyClient.login(username);
if (res != null if (res != null && res.getUsername() != null && !res.getUsername().isBlank()) {
&& res.getUsername() != null
&& !res.getUsername().isBlank()) {
setSharedUsername(res.getUsername().trim()); setSharedUsername(res.getUsername().trim());
} }
LOGGER.info( String assignedUsername = null;
"Assigned username='{}' id={}", String assignedId = null;
res != null ? res.getUsername() : null, if (res != null) {
res != null ? res.getId() : null); assignedUsername = res.getUsername();
} catch (RuntimeException e) { assignedId = res.getId();
LOGGER.warn("Startup login failed: {}", e.getMessage());
} catch (Exception e) {
LOGGER.warn("Unexpected error during startup login", e);
} finally {
bg.shutdown();
} }
}); LOGGER.info("Assigned username='{}' id={}", assignedUsername, assignedId);
} catch (RuntimeException e) { } catch (RuntimeException e) {
LOGGER.warn( LOGGER.warn(
"Could not establish initial connection for startup login: {}", e.getMessage()); "Could not establish initial connection for startup login: {}", e.getMessage());
} }
} }
private static void launchUI(String arg, String username) { private static void launchUI(String arg) {
if (username != null && !username.isBlank()) { String username = getSharedUsername();
Launcher.main(new String[] {arg, username}); if (username == null || username.isBlank()) {
} else {
Launcher.main(new String[] {arg}); Launcher.main(new String[] {arg});
return;
} }
Launcher.main(new String[] {arg, username});
} }
public static void main(String[] args) { public static void main(String[] args) {
@@ -134,7 +121,10 @@ public class ClientApp {
throw new IllegalArgumentException("Address argument required: <host:port>"); throw new IllegalArgumentException("Address argument required: <host:port>");
} }
String username = args.length > 1 && args[1] != null && !args[1].isBlank() ? args[1] : null; String username = null;
if (args.length > 1) {
username = normalizeUsername(args[1]);
}
start(args[0], username); start(args[0], username);
} }
@@ -81,11 +81,7 @@ public class LobbyClient {
throw new RuntimeException("No LOBBY_ID in response: " + lines); throw new RuntimeException("No LOBBY_ID in response: " + lines);
} }
/** /** Fetches the global highscores from the server. */
* Fetches the global highscores from the server.
*
* @return list of formatted highscore entries, newest entries at the end
*/
public List<String> getHighscores() { public List<String> getHighscores() {
List<String> lines = client.processCommand("GET_HIGHSCORES"); List<String> lines = client.processCommand("GET_HIGHSCORES");
List<RequestParameter> params = ClientService.convertToRequestParameters(lines); List<RequestParameter> params = ClientService.convertToRequestParameters(lines);
@@ -139,7 +135,8 @@ public class LobbyClient {
* server * server
*/ */
public LoginResult login(String user) { public LoginResult login(String user) {
List<String> lines = client.processCommand("LOGIN USERNAME=" + user); String command = buildLoginCommand(user);
List<String> lines = client.processCommand(command);
List<RequestParameter> params = ClientService.convertToRequestParameters(lines); List<RequestParameter> params = ClientService.convertToRequestParameters(lines);
@@ -155,6 +152,13 @@ public class LobbyClient {
return new LoginResult(assigned, id); return new LoginResult(assigned, id);
} }
private String buildLoginCommand(String username) {
if (username == null || username.isBlank()) {
return "LOGIN";
}
return "LOGIN USERNAME=" + username.trim();
}
/** /**
* Changes the username for the currently logged-in session. * Changes the username for the currently logged-in session.
* *
@@ -199,7 +203,6 @@ public class LobbyClient {
String val = p.value(); String val = p.value();
switch (key) { switch (key) {
case "ID": case "ID":
// If we were collecting a lobby, flush it
if (currentId != null) { if (currentId != null) {
result.add( result.add(
new LobbyInfo( new LobbyInfo(
@@ -16,6 +16,6 @@ public class LoginParser implements CommandParser<LoginRequest> {
public LoginRequest parse(PrimitiveRequest primitiveRequest) { public LoginRequest parse(PrimitiveRequest primitiveRequest) {
RequestParameterAccessor accessor = RequestParameterAccessor accessor =
new RequestParameterAccessor(primitiveRequest.parameters()); new RequestParameterAccessor(primitiveRequest.parameters());
return new LoginRequest(primitiveRequest.context(), accessor.require("USERNAME")); return new LoginRequest(primitiveRequest.context(), accessor.optional("USERNAME", null));
} }
} }