Feat: Add get_lobby_listcommand to server

This commit is contained in:
Jona Walpert
2026-04-11 21:11:40 +02:00
parent 57b8bca1a8
commit 50052b72ff
4 changed files with 97 additions and 0 deletions
@@ -0,0 +1,28 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.get_lobby_list;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyManager;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
import java.util.Collection;
/**
* Handler for the `GET_LOBBY_LIST` command.
*
* <p>Queries the {@link LobbyManager} for all available lobbies and dispatches a {@link
* GetLobbyListResponse} containing basic metadata for each lobby (`ID`, `NAME`, `PLAYER_COUNT`).
*/
public class GetLobbyListHandler extends CommandHandler<GetLobbyListRequest> {
private final LobbyManager lobbyManager;
public GetLobbyListHandler(ResponseDispatcher responseDispatcher, LobbyManager lobbyManager) {
super(responseDispatcher);
this.lobbyManager = lobbyManager;
}
@Override
public void execute(GetLobbyListRequest request) {
Collection<ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.Lobby> l =
lobbyManager.getAllLobbies();
responseDispatcher.dispatch(new GetLobbyListResponse(request.getContext(), l));
}
}
@@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.get_lobby_list;
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParser;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
/**
* Parser for the `GET_LOBBY_LIST` command.
*
* <p>Parses the incoming primitive request and produces a {@link GetLobbyListRequest}. This command
* takes no parameters.
*/
public class GetLobbyListParser implements CommandParser<GetLobbyListRequest> {
@Override
public GetLobbyListRequest parse(PrimitiveRequest primitiveRequest) {
return new GetLobbyListRequest(primitiveRequest.context());
}
}
@@ -0,0 +1,15 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.get_lobby_list;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
/**
* Request object for the `GET_LOBBY_LIST` command.
*
* <p>Contains only the request context; this command does not require parameters.
*/
public class GetLobbyListRequest extends Request {
public GetLobbyListRequest(RequestContext context) {
super(context);
}
}
@@ -0,0 +1,37 @@
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.get_lobby_list;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.Lobby;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
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 java.util.Collection;
/**
* Success response for `GET_LOBBY_LIST`.
*
* <p>Builds a `LOBBIES` collection with repeated `LOBBY` blocks containing `ID`, `NAME` and
* `PLAYER_COUNT`.
*/
public class GetLobbyListResponse extends SuccessResponse {
public GetLobbyListResponse(RequestContext context, Collection<Lobby> lobbies) {
super(
context,
ResponseBody.builder()
.block(
"LOBBIES",
lobbies_block -> {
for (Lobby lobby : lobbies) {
lobbies_block.block(
"LOBBY",
lb -> {
lb.param("ID", lobby.getId().value());
lb.param("NAME", lobby.getName());
lb.param(
"PLAYER_COUNT",
lobby.getPlayerNames().size());
});
}
})
.build());
}
}