Feat/103 add raise command on server side #267

Merged
jona.walpert merged 3 commits from feat/103-add-raise-command-on-server-side into main 2026-04-12 00:57:11 +02:00
3 changed files with 24 additions and 37 deletions
Showing only changes of commit ca818d9892 - Show all commits
@@ -14,20 +14,13 @@ import java.util.Optional;
/**
* Handler for the `RAISE` command.
*
* <p>
* This handler validates the request (amount >= 0), resolves the user and
* target lobby (by
* `GAME_ID` when provided or by session username otherwise), checks that a game
* is running and then
* <p>This handler validates the request (amount >= 0), resolves the user and target lobby (by
* `GAME_ID` when provided or by session username otherwise), checks that a game is running and then
* forwards the action to the {@code GameController}.
*
* <p>
* On failure the handler dispatches an {@link ErrorResponse} with an
* appropriate error code
* (e.g. {@code INVALID_AMOUNT}, {@code NOT_IN_LOBBY}, {@code LOBBY_NOT_FOUND},
* {@code
* GAME_NOT_STARTED}, {@code GAME_ACTION_FAILED}). On success it dispatches an
* {@link OkResponse}.
* <p>On failure the handler dispatches an {@link ErrorResponse} with an appropriate error code
* (e.g. {@code INVALID_AMOUNT}, {@code NOT_IN_LOBBY}, {@code LOBBY_NOT_FOUND}, {@code
* GAME_NOT_STARTED}, {@code GAME_ACTION_FAILED}). On success it dispatches an {@link OkResponse}.
*/
public class PlayerRaiseHandler extends CommandHandler<PlayerRaiseRequest> {
private final UserRegistry userRegistry;
@@ -36,11 +29,9 @@ public class PlayerRaiseHandler extends CommandHandler<PlayerRaiseRequest> {
/**
* Creates a new {@code PlayerRaiseHandler}.
*
* @param responseDispatcher dispatcher used to send responses back to the
* client
* @param userRegistry registry to resolve users from session ids
* @param lobbyManager manager used to lookup lobbies and their game
* controllers
* @param responseDispatcher dispatcher used to send responses back to the client
* @param userRegistry registry to resolve users from session ids
* @param lobbyManager manager used to lookup lobbies and their game controllers
*/
public PlayerRaiseHandler(
ResponseDispatcher responseDispatcher,
@@ -52,10 +43,8 @@ public class PlayerRaiseHandler extends CommandHandler<PlayerRaiseRequest> {
}
/**
* Execute the raise request: validate parameters, resolve lobby and game, and
* forward the raise
* action to the game controller. Sends an {@link ErrorResponse} on failure or
* an {@link
* Execute the raise request: validate parameters, resolve lobby and game, and forward the raise
* action to the game controller. Sends an {@link ErrorResponse} on failure or an {@link
* OkResponse} on success.
*
* @param request the parsed {@link PlayerRaiseRequest}
@@ -71,8 +60,8 @@ public class PlayerRaiseHandler extends CommandHandler<PlayerRaiseRequest> {
return;
}
Optional<ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User> opt = userRegistry
.getBySessionId(request.getSessionId());
Optional<ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User> opt =
userRegistry.getBySessionId(request.getSessionId());
if (opt.isEmpty()) {
responseDispatcher.dispatch(
new ErrorResponse(request.getContext(), "NOT_LOGGED_IN", "User not logged in"));
@@ -82,9 +71,10 @@ public class PlayerRaiseHandler extends CommandHandler<PlayerRaiseRequest> {
String username = opt.get().getName();
Integer gameId = request.getGameId();
var lobby = (gameId != null)
? lobbyManager.getLobby(LobbyId.of(gameId))
: lobbyManager.getLobbyByUsername(username);
var lobby =
(gameId != null)
? lobbyManager.getLobby(LobbyId.of(gameId))
: lobbyManager.getLobbyByUsername(username);
if (lobby == null) {
if (gameId != null) {
@@ -7,19 +7,18 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.
/**
* Parser for the `RAISE` command.
*
* <p>
* Parses the request parameters and builds a {@link PlayerRaiseRequest}.
* Expected parameters:
* <p>Parses the request parameters and builds a {@link PlayerRaiseRequest}. Expected parameters:
*
* <ul>
* <li>`GAME_ID` (optional) - numeric id of the lobby/game to target
* <li>`AMOUNT` (required) - amount to raise
* <li>`GAME_ID` (optional) - numeric id of the lobby/game to target
* <li>`AMOUNT` (required) - amount to raise
* </ul>
*/
public class PlayerRaiseParser implements CommandParser<PlayerRaiseRequest> {
@Override
public PlayerRaiseRequest parse(PrimitiveRequest primitiveRequest) {
RequestParameterAccessor accessor = new RequestParameterAccessor(primitiveRequest.parameters());
RequestParameterAccessor accessor =
new RequestParameterAccessor(primitiveRequest.parameters());
Integer gameId = null;
try {
@@ -6,9 +6,7 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestCo
/**
* Request for the `RAISE` command.
*
* <p>
* Contains the optional target `gameId` and the `amount` the player wants to
* raise.
* <p>Contains the optional target `gameId` and the `amount` the player wants to raise.
*/
public class PlayerRaiseRequest extends Request {
private final Integer gameId;
@@ -18,8 +16,8 @@ public class PlayerRaiseRequest extends Request {
* Creates a new {@code PlayerRaiseRequest}.
*
* @param context the request context
* @param gameId optional game id of the targeted lobby (may be {@code null})
* @param amount the raise amount (non-negative)
* @param gameId optional game id of the targeted lobby (may be {@code null})
* @param amount the raise amount (non-negative)
*/
public PlayerRaiseRequest(RequestContext context, Integer gameId, int amount) {
super(context);