Feat/94 add get lobby list command on server side #261
+28
@@ -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));
|
||||
}
|
||||
}
|
||||
+17
@@ -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());
|
||||
}
|
||||
}
|
||||
+15
@@ -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);
|
||||
}
|
||||
}
|
||||
+37
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user