Feat: Add addPlayer command on sever side #258

Merged
jona.walpert merged 13 commits from feat/97-add-joinlobby-command-on-server-side into main 2026-04-11 19:49:57 +02:00
4 changed files with 120 additions and 99 deletions
Showing only changes of commit 7788bbc5d3 - Show all commits
@@ -24,6 +24,7 @@ import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.ping.PingRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.send_message.SendMessageHandler;
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.send_message.SendMessageParser;
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.send_message.SendMessageRequest;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyManager;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserCleanupJob;
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
import ch.unibas.dmi.dbis.cs108.casono.server.network.NetworkManager;
@@ -85,7 +86,8 @@ public class ServerApp {
SESSION_DISCONNECT_JOB_PERIOD,
TimeUnit.SECONDS);
registerCommands(dispatcher, router, responseDispatcher, userRegistry);
LobbyManager lobbyManager = new LobbyManager();
registerCommands(dispatcher, router, responseDispatcher, userRegistry, lobbyManager);
NetworkManager networkManager = new NetworkManager(port, sessionManager, router);
networkManager.start();
@@ -94,15 +96,19 @@ public class ServerApp {
/**
* Registers command parsers and handlers.
*
* @param parserDispatcher the dispatcher responsible for parsing incoming commands
* @param commandRouter the router that dispatches parsed commands to appropriate handlers
* @param responseDispatcher the dispatcher responsible for sending responses back to clients
* @param parserDispatcher the dispatcher responsible for parsing incoming
* commands
* @param commandRouter the router that dispatches parsed commands to
* appropriate handlers
* @param responseDispatcher the dispatcher responsible for sending responses
* back to clients
*/
private static void registerCommands(
CommandParserDispatcher parserDispatcher,
CommandRouter commandRouter,
ResponseDispatcher responseDispatcher,
UserRegistry userRegistry) {
UserRegistry userRegistry,
LobbyManager lobbyManager) {
parserDispatcher.register("PING", new PingParser());
commandRouter.register(PingRequest.class, new PingHandler(responseDispatcher));
@@ -136,5 +142,24 @@ public class ServerApp {
parserDispatcher.register("LIST_USERS", new ListUsersParser());
commandRouter.register(
ListUsersRequest.class, new ListUsersHandler(responseDispatcher, userRegistry));
// Lobby commands
parserDispatcher.register(
"JOIN_LOBBY",
new ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.join_lobby.JoinLobbyParser());
commandRouter.register(
ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.join_lobby.JoinLobbyRequest.class,
new ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.join_lobby.JoinLobbyHandler(
responseDispatcher, lobbyManager, userRegistry));
// Game state: allow client to request game snapshot for their lobby
parserDispatcher.register(
"GET_GAME_STATE",
new ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.get_game_state.GetGameStateParser());
commandRouter.register(
ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.get_game_state.GetGameStateRequest.class,
new ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.get_game_state.GetGameStateHandler(
responseDispatcher, lobbyManager, userRegistry));
}
}
@@ -13,12 +13,10 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatch
/**
* Handler for the `JOIN_LOBBY` command.
*
* <p>
* Resolves the username from the session (requires {@link UserLoggedInCheck}),
* looks up the target lobby and attempts to add the user. On success an
* `+OK` response is dispatched; on failure an appropriate {@link ErrorResponse}
* with one of the error codes `LOBBY_NOT_FOUND`, `USER_NOT_LOGGED_IN` or
* `LOBBY_FULL_OR_ALREADY_IN` is returned.
* <p>Resolves the username from the session (requires {@link UserLoggedInCheck}), looks up the
* target lobby and attempts to add the user. On success an `+OK` response is dispatched; on failure
* an appropriate {@link ErrorResponse} with one of the error codes `LOBBY_NOT_FOUND`,
* `USER_NOT_LOGGED_IN` or `LOBBY_FULL_OR_ALREADY_IN` is returned.
*/
public class JoinLobbyHandler extends CommandHandler<JoinLobbyRequest> {
private final LobbyManager lobbyManager;
@@ -27,8 +25,7 @@ public class JoinLobbyHandler extends CommandHandler<JoinLobbyRequest> {
/**
* Create a new {@link JoinLobbyHandler}.
*
* @param responseDispatcher dispatcher used to send responses back to the
* client
* @param responseDispatcher dispatcher used to send responses back to the client
* @param lobbyManager manager providing lobby state and operations
* @param userRegistry registry to resolve session -> user mappings
*/
@@ -7,15 +7,14 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.
/**
* Parser for the `JOIN_LOBBY` command.
*
* <p>
* Expected request parameters:
* <p>Expected request parameters:
*
* <ul>
* <li>`ID` (int) — numeric lobby identifier (required)
* </ul>
*
* <p>
* The parser builds a {@link JoinLobbyRequest} containing the parsed lobby id
* and the original request context.
* <p>The parser builds a {@link JoinLobbyRequest} containing the parsed lobby id and the original
* request context.
*/
public class JoinLobbyParser implements CommandParser<JoinLobbyRequest> {
/**
@@ -26,7 +25,8 @@ public class JoinLobbyParser implements CommandParser<JoinLobbyRequest> {
*/
@Override
public JoinLobbyRequest parse(PrimitiveRequest primitiveRequest) {
RequestParameterAccessor accessor = new RequestParameterAccessor(primitiveRequest.parameters());
RequestParameterAccessor accessor =
new RequestParameterAccessor(primitiveRequest.parameters());
int id = accessor.require("ID", Integer::parseInt);
return new JoinLobbyRequest(primitiveRequest.context(), id);
}
@@ -6,9 +6,8 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestCo
/**
* Request data for the `JOIN_LOBBY` command.
*
* <p>
* Contains the lobby id the client wants to join and inherits the
* {@link Request} contextual information.
* <p>Contains the lobby id the client wants to join and inherits the {@link Request} contextual
* information.
*/
public class JoinLobbyRequest extends Request {
private final int id;