Feat/96 add getlobbystatus command on server side #260
@@ -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)
|
||||
|
||||
<!-- Please see the comments for copy ‚ n' paste ready examples -->
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
+21
-2
@@ -8,7 +8,14 @@ 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.
|
||||
*
|
||||
* <p>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<GetLobbyStatusRequest> {
|
||||
private final LobbyManager lobbyManager;
|
||||
private final UserRegistry userRegistry;
|
||||
@@ -46,10 +53,22 @@ public class GetLobbyStatusHandler extends CommandHandler<GetLobbyStatusRequest>
|
||||
|
||||
@Override
|
||||
public void execute(GetLobbyStatusRequest request) {
|
||||
// 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()))
|
||||
: lobbyManager.getLobbyByUsername(request.getUsername());
|
||||
: (targetUsername != null
|
||||
? lobbyManager.getLobbyByUsername(targetUsername)
|
||||
: null);
|
||||
|
||||
if (lobby == null) {
|
||||
responseDispatcher.dispatch(
|
||||
|
||||
+15
@@ -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<GetLobbyStatusRequest> {
|
||||
/**
|
||||
* Parse the incoming primitive request into a {@link GetLobbyStatusRequest}.
|
||||
*
|
||||
* <p>Supported optional parameters:
|
||||
*
|
||||
* <ul>
|
||||
* <li>`ID` (int) — numeric lobby id to query
|
||||
* <li>`USERNAME` (String) — username to lookup the lobby for
|
||||
* </ul>
|
||||
*
|
||||
* 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 =
|
||||
|
||||
+9
@@ -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;
|
||||
}
|
||||
|
||||
+12
-1
@@ -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`.
|
||||
*
|
||||
* <p>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,
|
||||
|
||||
Reference in New Issue
Block a user