Feat/96 add getlobbystatus command on server side #260
+27
-5
@@ -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 ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
|
||||||
import java.util.Optional;
|
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> {
|
public class GetLobbyStatusHandler extends CommandHandler<GetLobbyStatusRequest> {
|
||||||
private final LobbyManager lobbyManager;
|
private final LobbyManager lobbyManager;
|
||||||
private final UserRegistry userRegistry;
|
private final UserRegistry userRegistry;
|
||||||
@@ -46,10 +57,21 @@ public class GetLobbyStatusHandler extends CommandHandler<GetLobbyStatusRequest>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(GetLobbyStatusRequest request) {
|
public void execute(GetLobbyStatusRequest request) {
|
||||||
var lobby =
|
// Resolve username from request or session when no explicit ID/USERNAME
|
||||||
(request.getId() != null)
|
// provided
|
||||||
? lobbyManager.getLobby(LobbyId.of(request.getId()))
|
String targetUsername = request.getUsername();
|
||||||
: lobbyManager.getLobbyByUsername(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) {
|
if (lobby == null) {
|
||||||
responseDispatcher.dispatch(
|
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;
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.RequestParameterAccessor;
|
||||||
|
|
||||||
public class GetLobbyStatusParser implements CommandParser<GetLobbyStatusRequest> {
|
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
|
@Override
|
||||||
public GetLobbyStatusRequest parse(PrimitiveRequest primitiveRequest) {
|
public GetLobbyStatusRequest parse(PrimitiveRequest primitiveRequest) {
|
||||||
RequestParameterAccessor accessor =
|
RequestParameterAccessor accessor =
|
||||||
|
|||||||
+9
@@ -7,16 +7,25 @@ public class GetLobbyStatusRequest extends Request {
|
|||||||
private final Integer id;
|
private final Integer id;
|
||||||
private final String username;
|
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) {
|
public GetLobbyStatusRequest(RequestContext context, Integer id, String username) {
|
||||||
super(context);
|
super(context);
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the optional lobby id. */
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the optional username. */
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
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.SuccessResponse;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.builder.ResponseBody;
|
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 {
|
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) {
|
public GetLobbyStatusResponse(RequestContext context, Lobby lobby) {
|
||||||
super(
|
super(
|
||||||
context,
|
context,
|
||||||
|
|||||||
Reference in New Issue
Block a user