Feat: Add addPlayer command on sever side #258
+24
-1
@@ -1,19 +1,37 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.join_lobby;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.UserLoggedInCheck;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyId;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyManager;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.UserLoggedInCheck;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorResponse;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.OkResponse;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public class JoinLobbyHandler extends CommandHandler<JoinLobbyRequest> {
|
||||
private final LobbyManager lobbyManager;
|
||||
private final UserRegistry userRegistry;
|
||||
|
||||
/**
|
||||
* Create a new {@link JoinLobbyHandler}.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public JoinLobbyHandler(
|
||||
ResponseDispatcher responseDispatcher,
|
||||
LobbyManager lobbyManager,
|
||||
@@ -24,6 +42,11 @@ public class JoinLobbyHandler extends CommandHandler<JoinLobbyRequest> {
|
||||
addCheck(new UserLoggedInCheck(userRegistry));
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the join-lobby request.
|
||||
*
|
||||
* @param request the parsed {@link JoinLobbyRequest}
|
||||
*/
|
||||
@Override
|
||||
public void execute(JoinLobbyRequest request) {
|
||||
LobbyId lid = LobbyId.of(request.getId());
|
||||
|
||||
+20
-2
@@ -4,11 +4,29 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandPar
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.RequestParameterAccessor;
|
||||
|
||||
/**
|
||||
* Parser for the `JOIN_LOBBY` command.
|
||||
*
|
||||
* <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.
|
||||
*/
|
||||
public class JoinLobbyParser implements CommandParser<JoinLobbyRequest> {
|
||||
/**
|
||||
* Parse the incoming primitive request into a {@link JoinLobbyRequest}.
|
||||
*
|
||||
* @param primitiveRequest the raw primitive request
|
||||
* @return a {@link JoinLobbyRequest} with the parsed lobby id and context
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
|
||||
+18
@@ -3,14 +3,32 @@ package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.lobby.join_lobby;
|
||||
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 data for the `JOIN_LOBBY` command.
|
||||
*
|
||||
* <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;
|
||||
|
||||
/**
|
||||
* Create a new {@link JoinLobbyRequest}.
|
||||
*
|
||||
* @param context the request context
|
||||
* @param id numeric lobby id to join
|
||||
*/
|
||||
public JoinLobbyRequest(RequestContext context, int id) {
|
||||
super(context);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the lobby id requested by the client.
|
||||
*
|
||||
* @return numeric lobby id
|
||||
*/
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user