Refactor/121 duplicate check lobby code #302
+75
@@ -0,0 +1,75 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.checks;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.bet.PlayerBetRequest;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.call.PlayerCallRequest;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.fold.PlayerFoldRequest;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.raise.PlayerRaiseRequest;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.Lobby;
|
||||||
|
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.network.command.execution.checks.HandlerCheck;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorResponse;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.Response;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether a target lobby exists for game action commands.
|
||||||
|
*
|
||||||
|
* <p>Supported commands are BET, CALL, FOLD and RAISE. If a request specifies a GAME_ID, the check
|
||||||
|
* validates that this lobby exists. Otherwise, it validates that the logged-in user is in a lobby.
|
||||||
|
*/
|
||||||
|
public class GameLobbyExistsCheck implements HandlerCheck {
|
||||||
|
private final UserRegistry userRegistry;
|
||||||
|
private final LobbyManager lobbyManager;
|
||||||
|
|
||||||
|
public GameLobbyExistsCheck(UserRegistry userRegistry, LobbyManager lobbyManager) {
|
||||||
|
this.userRegistry = userRegistry;
|
||||||
|
this.lobbyManager = lobbyManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Response> check(Request request) {
|
||||||
|
Integer gameId = extractGameId(request);
|
||||||
|
User user = userRegistry.getBySessionId(request.getSessionId()).get();
|
||||||
|
|
||||||
|
Lobby lobby;
|
||||||
|
if (gameId != null) {
|
||||||
|
lobby = lobbyManager.getLobby(LobbyId.of(gameId));
|
||||||
|
} else {
|
||||||
|
lobby = lobbyManager.getLobbyByUsername(user.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lobby != null) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gameId != null) {
|
||||||
|
return Optional.of(
|
||||||
|
new ErrorResponse(request.getContext(), "LOBBY_NOT_FOUND", "Lobby not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.of(
|
||||||
|
new ErrorResponse(request.getContext(), "NOT_IN_LOBBY", "User not in a lobby"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Integer extractGameId(Request request) {
|
||||||
|
if (request instanceof PlayerBetRequest) {
|
||||||
|
return ((PlayerBetRequest) request).getGameId();
|
||||||
|
}
|
||||||
|
if (request instanceof PlayerCallRequest) {
|
||||||
|
return ((PlayerCallRequest) request).getGameId();
|
||||||
|
}
|
||||||
|
if (request instanceof PlayerFoldRequest) {
|
||||||
|
return ((PlayerFoldRequest) request).getGameId();
|
||||||
|
}
|
||||||
|
if (request instanceof PlayerRaiseRequest) {
|
||||||
|
return ((PlayerRaiseRequest) request).getGameId();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"GameLobbyExistsCheck is only supported for BET, CALL, FOLD and RAISE requests");
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
-16
@@ -1,7 +1,10 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.bet;
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.bet;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.GameLobbyExistsCheck;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.UserLoggedInCheck;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.Lobby;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyId;
|
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.lobby.LobbyManager;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||||
@@ -41,6 +44,8 @@ public class PlayerBetHandler extends CommandHandler<PlayerBetRequest> {
|
|||||||
super(responseDispatcher);
|
super(responseDispatcher);
|
||||||
this.userRegistry = userRegistry;
|
this.userRegistry = userRegistry;
|
||||||
this.lobbyManager = lobbyManager;
|
this.lobbyManager = lobbyManager;
|
||||||
|
addCheck(new UserLoggedInCheck(userRegistry));
|
||||||
|
addCheck(new GameLobbyExistsCheck(userRegistry, lobbyManager));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,22 +74,11 @@ public class PlayerBetHandler extends CommandHandler<PlayerBetRequest> {
|
|||||||
String username = opt.get().getName();
|
String username = opt.get().getName();
|
||||||
|
|
||||||
Integer gameId = request.getGameId();
|
Integer gameId = request.getGameId();
|
||||||
var lobby =
|
Lobby lobby;
|
||||||
(gameId != null)
|
if (gameId != null) {
|
||||||
? lobbyManager.getLobby(LobbyId.of(gameId))
|
lobby = lobbyManager.getLobby(LobbyId.of(gameId));
|
||||||
: lobbyManager.getLobbyByUsername(username);
|
} else {
|
||||||
|
lobby = lobbyManager.getLobbyByUsername(username);
|
||||||
if (lobby == null) {
|
|
||||||
if (gameId != null) {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "LOBBY_NOT_FOUND", "Lobby not found"));
|
|
||||||
} else {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "NOT_IN_LOBBY", "User not in a lobby"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController game = lobby.getGameController();
|
GameController game = lobby.getGameController();
|
||||||
|
|||||||
+10
-16
@@ -1,7 +1,10 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.call;
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.call;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.GameLobbyExistsCheck;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.UserLoggedInCheck;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.Lobby;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyId;
|
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.lobby.LobbyManager;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||||
@@ -37,6 +40,8 @@ public class PlayerCallHandler extends CommandHandler<PlayerCallRequest> {
|
|||||||
super(responseDispatcher);
|
super(responseDispatcher);
|
||||||
this.userRegistry = userRegistry;
|
this.userRegistry = userRegistry;
|
||||||
this.lobbyManager = lobbyManager;
|
this.lobbyManager = lobbyManager;
|
||||||
|
addCheck(new UserLoggedInCheck(userRegistry));
|
||||||
|
addCheck(new GameLobbyExistsCheck(userRegistry, lobbyManager));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,22 +62,11 @@ public class PlayerCallHandler extends CommandHandler<PlayerCallRequest> {
|
|||||||
String username = opt.get().getName();
|
String username = opt.get().getName();
|
||||||
|
|
||||||
Integer gameId = request.getGameId();
|
Integer gameId = request.getGameId();
|
||||||
var lobby =
|
Lobby lobby;
|
||||||
(gameId != null)
|
if (gameId != null) {
|
||||||
? lobbyManager.getLobby(LobbyId.of(gameId))
|
lobby = lobbyManager.getLobby(LobbyId.of(gameId));
|
||||||
: lobbyManager.getLobbyByUsername(username);
|
} else {
|
||||||
|
lobby = lobbyManager.getLobbyByUsername(username);
|
||||||
if (lobby == null) {
|
|
||||||
if (gameId != null) {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "LOBBY_NOT_FOUND", "Lobby not found"));
|
|
||||||
} else {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "NOT_IN_LOBBY", "User not in a lobby"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController game = lobby.getGameController();
|
GameController game = lobby.getGameController();
|
||||||
|
|||||||
+10
-16
@@ -1,7 +1,10 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.fold;
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.fold;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.GameLobbyExistsCheck;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.UserLoggedInCheck;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.Lobby;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyId;
|
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.lobby.LobbyManager;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||||
@@ -37,6 +40,8 @@ public class PlayerFoldHandler extends CommandHandler<PlayerFoldRequest> {
|
|||||||
super(responseDispatcher);
|
super(responseDispatcher);
|
||||||
this.userRegistry = userRegistry;
|
this.userRegistry = userRegistry;
|
||||||
this.lobbyManager = lobbyManager;
|
this.lobbyManager = lobbyManager;
|
||||||
|
addCheck(new UserLoggedInCheck(userRegistry));
|
||||||
|
addCheck(new GameLobbyExistsCheck(userRegistry, lobbyManager));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,22 +62,11 @@ public class PlayerFoldHandler extends CommandHandler<PlayerFoldRequest> {
|
|||||||
String username = opt.get().getName();
|
String username = opt.get().getName();
|
||||||
|
|
||||||
Integer gameId = request.getGameId();
|
Integer gameId = request.getGameId();
|
||||||
var lobby =
|
Lobby lobby;
|
||||||
(gameId != null)
|
if (gameId != null) {
|
||||||
? lobbyManager.getLobby(LobbyId.of(gameId))
|
lobby = lobbyManager.getLobby(LobbyId.of(gameId));
|
||||||
: lobbyManager.getLobbyByUsername(username);
|
} else {
|
||||||
|
lobby = lobbyManager.getLobbyByUsername(username);
|
||||||
if (lobby == null) {
|
|
||||||
if (gameId != null) {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "LOBBY_NOT_FOUND", "Lobby not found"));
|
|
||||||
} else {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "NOT_IN_LOBBY", "User not in a lobby"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController game = lobby.getGameController();
|
GameController game = lobby.getGameController();
|
||||||
|
|||||||
+10
-16
@@ -1,7 +1,10 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.raise;
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.game.raise;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.GameLobbyExistsCheck;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.app.checks.UserLoggedInCheck;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.GameController;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.Lobby;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.lobby.LobbyId;
|
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.lobby.LobbyManager;
|
||||||
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||||
@@ -41,6 +44,8 @@ public class PlayerRaiseHandler extends CommandHandler<PlayerRaiseRequest> {
|
|||||||
super(responseDispatcher);
|
super(responseDispatcher);
|
||||||
this.userRegistry = userRegistry;
|
this.userRegistry = userRegistry;
|
||||||
this.lobbyManager = lobbyManager;
|
this.lobbyManager = lobbyManager;
|
||||||
|
addCheck(new UserLoggedInCheck(userRegistry));
|
||||||
|
addCheck(new GameLobbyExistsCheck(userRegistry, lobbyManager));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,22 +76,11 @@ public class PlayerRaiseHandler extends CommandHandler<PlayerRaiseRequest> {
|
|||||||
String username = opt.get().getName();
|
String username = opt.get().getName();
|
||||||
|
|
||||||
Integer gameId = request.getGameId();
|
Integer gameId = request.getGameId();
|
||||||
var lobby =
|
Lobby lobby;
|
||||||
(gameId != null)
|
if (gameId != null) {
|
||||||
? lobbyManager.getLobby(LobbyId.of(gameId))
|
lobby = lobbyManager.getLobby(LobbyId.of(gameId));
|
||||||
: lobbyManager.getLobbyByUsername(username);
|
} else {
|
||||||
|
lobby = lobbyManager.getLobbyByUsername(username);
|
||||||
if (lobby == null) {
|
|
||||||
if (gameId != null) {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "LOBBY_NOT_FOUND", "Lobby not found"));
|
|
||||||
} else {
|
|
||||||
responseDispatcher.dispatch(
|
|
||||||
new ErrorResponse(
|
|
||||||
request.getContext(), "NOT_IN_LOBBY", "User not in a lobby"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController game = lobby.getGameController();
|
GameController game = lobby.getGameController();
|
||||||
|
|||||||
Reference in New Issue
Block a user