From 6219c2d66080c8e2bbed5a7f2048e954f6cd2dcc Mon Sep 17 00:00:00 2001 From: Jona Walpert Date: Sat, 11 Apr 2026 20:48:58 +0200 Subject: [PATCH 1/4] Feat: Add get_Lobby_Status command on server side --- .../GetLobbyStatusHandler.java | 32 ++++++++++++++++--- .../GetLobbyStatusParser.java | 15 +++++++++ .../GetLobbyStatusRequest.java | 9 ++++++ .../GetLobbyStatusResponse.java | 13 +++++++- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java index 5e04fcd..3f191ad 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java @@ -8,7 +8,18 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorRes import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher; import java.util.Optional; -/** Handler for GET_LOBBY_STATUS: returns lobby details and player list. */ +/** + * Handler for the `GET_LOBBY_STATUS` command. + * + *

+ * Resolves the target lobby either by the optional `ID` parameter or by + * `USERNAME`. If both are + * omitted the handler requires the session to be associated with a logged-in + * user (see the inline + * pre-execution check). On success a {@link GetLobbyStatusResponse} is + * dispatched, otherwise an + * {@link ErrorResponse} with code `LOBBY_NOT_FOUND` is sent. + */ public class GetLobbyStatusHandler extends CommandHandler { private final LobbyManager lobbyManager; private final UserRegistry userRegistry; @@ -46,10 +57,21 @@ public class GetLobbyStatusHandler extends CommandHandler @Override public void execute(GetLobbyStatusRequest request) { - var lobby = - (request.getId() != null) - ? lobbyManager.getLobby(LobbyId.of(request.getId())) - : lobbyManager.getLobbyByUsername(request.getUsername()); + // Resolve username from request or session when no explicit ID/USERNAME + // provided + String targetUsername = request.getUsername(); + if (targetUsername == null) { + var maybeUser = userRegistry.getBySessionId(request.getSessionId()); + if (maybeUser.isPresent()) { + targetUsername = maybeUser.get().getName(); + } + } + + var lobby = (request.getId() != null) + ? lobbyManager.getLobby(LobbyId.of(request.getId())) + : (targetUsername != null + ? lobbyManager.getLobbyByUsername(targetUsername) + : null); if (lobby == null) { responseDispatcher.dispatch( diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusParser.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusParser.java index e6aec4c..1f38f2a 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusParser.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusParser.java @@ -5,6 +5,21 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Primitive import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.RequestParameterAccessor; public class GetLobbyStatusParser implements CommandParser { + /** + * Parse the incoming primitive request into a {@link GetLobbyStatusRequest}. + * + *

Supported optional parameters: + * + *

+ * + * If both are omitted the handler may require a logged-in session. + * + * @param primitiveRequest raw request containing parameters and context + * @return parsed {@link GetLobbyStatusRequest} + */ @Override public GetLobbyStatusRequest parse(PrimitiveRequest primitiveRequest) { RequestParameterAccessor accessor = diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusRequest.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusRequest.java index 5d73882..6baa338 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusRequest.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusRequest.java @@ -7,16 +7,25 @@ public class GetLobbyStatusRequest extends Request { private final Integer id; private final String username; + /** + * Create a new GetLobbyStatusRequest. + * + * @param context request context (session id, source, etc.) + * @param id optional numeric lobby id to query, or null + * @param username optional username to query the lobby for, or null + */ public GetLobbyStatusRequest(RequestContext context, Integer id, String username) { super(context); this.id = id; this.username = username; } + /** Returns the optional lobby id. */ public Integer getId() { return id; } + /** Returns the optional username. */ public String getUsername() { return username; } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusResponse.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusResponse.java index 94bc690..043eb3d 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusResponse.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusResponse.java @@ -5,8 +5,19 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestCo import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.SuccessResponse; import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody; -/** Response with lobby player listing and simple READY placeholders. */ +/** + * Success response for `GET_LOBBY_STATUS`. + * + *

Builds a response with a single `LOBBY` block containing `ID`, `NAME` and a nested `PLAYERS` + * collection with repeated `PLAYER` blocks (`USERNAME`, `READY`). + */ public class GetLobbyStatusResponse extends SuccessResponse { + /** + * Create a new response for the provided {@link Lobby}. + * + * @param context request context to use for the response + * @param lobby lobby domain object containing id, name and players + */ public GetLobbyStatusResponse(RequestContext context, Lobby lobby) { super( context, From 517df2369d778738881964800029974bf7ca1d90 Mon Sep 17 00:00:00 2001 From: Jona Walpert Date: Sat, 11 Apr 2026 20:49:23 +0200 Subject: [PATCH 2/4] Add: register get_lobby_statsu command --- .../dmi/dbis/cs108/casono/server/ServerApp.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/ServerApp.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/ServerApp.java index 881705a..694b81a 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/ServerApp.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/ServerApp.java @@ -140,6 +140,21 @@ public class ServerApp { commandRouter.register( ListUsersRequest.class, new ListUsersHandler(responseDispatcher, userRegistry)); + // GET_LOBBY_STATUS registration + parserDispatcher.register( + "GET_LOBBY_STATUS", + new ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.get_lobby_status + .GetLobbyStatusParser()); + commandRouter.register( + ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.get_lobby_status + .GetLobbyStatusRequest.class, + (ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler< + ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby + .get_lobby_status.GetLobbyStatusRequest>) + new ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby + .get_lobby_status.GetLobbyStatusHandler( + responseDispatcher, lobbyManager, userRegistry)); + // JOIN_LOBBY registration parserDispatcher.register( "JOIN_LOBBY", From 050beb9d9844e2fd4ceb4c9257770bc4dac3bdcc Mon Sep 17 00:00:00 2001 From: Jona Walpert Date: Sat, 11 Apr 2026 20:50:19 +0200 Subject: [PATCH 3/4] Docs: update command documentation --- .../networking/commands/protocol-document.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/documents/docs/networking/commands/protocol-document.md b/documents/docs/networking/commands/protocol-document.md index 266bb29..33b0220 100644 --- a/documents/docs/networking/commands/protocol-document.md +++ b/documents/docs/networking/commands/protocol-document.md @@ -50,6 +50,26 @@ This document describes the protocol for client-server communication in our appl - [Error Response](#error-response-6) - [Example Request](#example-request-4) - [Example Response](#example-response-4) + - [SEND_MESSAGE command](#send_message-command) + - [Required pre-execution checks](#required-pre-execution-checks) + - [Request Parameters](#request-parameters) + - [Success Response](#success-response) + - [GET_MESSAGE_COUNT command](#get_message_count-command) + - [Required pre-execution checks](#required-pre-execution-checks) + - [Request Parameters](#request-parameters) + - [Success Response](#success-response) + - [GET_NEXT_MESSAGE command](#get_next_message-command) + - [Required pre-execution checks](#required-pre-execution-checks) + - [Request Parameters](#request-parameters) + - [Success Response](#success-response) + - [JOIN_LOBBY command](#join_lobby-command) + - [Required pre-execution checks](#required-pre-execution-checks) + - [Request Parameters](#request-parameters) + - [Success Response](#success-response) + - [GET_LOBBY_STATUS command](#get_lobby_status-command) + - [Required pre-execution checks](#required-pre-execution-checks) + - [Request Parameters](#request-parameters) + - [Success Response](#success-response) From 46ccae40ad2333c624aa5e0b62b167ab5f9559be Mon Sep 17 00:00:00 2001 From: Jona Walpert Date: Sat, 11 Apr 2026 20:51:49 +0200 Subject: [PATCH 4/4] Style: apply spotless --- .../GetLobbyStatusHandler.java | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java index 3f191ad..6ff4379 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/app/commands/lobby/get_lobby_status/GetLobbyStatusHandler.java @@ -11,13 +11,9 @@ import java.util.Optional; /** * Handler for the `GET_LOBBY_STATUS` command. * - *

- * Resolves the target lobby either by the optional `ID` parameter or by - * `USERNAME`. If both are - * omitted the handler requires the session to be associated with a logged-in - * user (see the inline - * pre-execution check). On success a {@link GetLobbyStatusResponse} is - * dispatched, otherwise an + *

Resolves the target lobby either by the optional `ID` parameter or by `USERNAME`. If both are + * omitted the handler requires the session to be associated with a logged-in user (see the inline + * pre-execution check). On success a {@link GetLobbyStatusResponse} is dispatched, otherwise an * {@link ErrorResponse} with code `LOBBY_NOT_FOUND` is sent. */ public class GetLobbyStatusHandler extends CommandHandler { @@ -67,11 +63,12 @@ public class GetLobbyStatusHandler extends CommandHandler } } - var lobby = (request.getId() != null) - ? lobbyManager.getLobby(LobbyId.of(request.getId())) - : (targetUsername != null - ? lobbyManager.getLobbyByUsername(targetUsername) - : null); + var lobby = + (request.getId() != null) + ? lobbyManager.getLobby(LobbyId.of(request.getId())) + : (targetUsername != null + ? lobbyManager.getLobbyByUsername(targetUsername) + : null); if (lobby == null) { responseDispatcher.dispatch(