From 0297193be1b5678f8d647920b5924ca6c173cd25 Mon Sep 17 00:00:00 2001 From: Fleischer Hanno Date: Sat, 30 Nov 2024 16:23:09 +0100 Subject: [PATCH] added more functionality to the client state machine and implemeted the first notifications --- .../mdga/client/NotificationSynchronizer.java | 8 ++--- .../java/pp/mdga/client/ClientGameLogic.java | 29 +++++++++++++--- .../main/java/pp/mdga/client/ClientState.java | 2 +- .../java/pp/mdga/client/DialogsState.java | 33 +++++++++++++++++-- .../main/java/pp/mdga/client/GameState.java | 12 +++++++ .../java/pp/mdga/client/InterruptState.java | 20 ++++++++++- .../mdga/client/dialogState/LobbyState.java | 21 ++++++++++-- .../client/dialogState/StartDialogState.java | 9 +++-- .../gameState/turnState/ChoosePieceState.java | 6 +++- .../gameState/turnState/PowerCardState.java | 4 +++ .../mdga/message/server/PauseGameMessage.java | 23 +++++++++++++ .../mdga/message/server/UpdateTSKMessage.java | 2 +- .../notification/TskSelectNotification.java | 17 ++++++++-- 13 files changed, 162 insertions(+), 24 deletions(-) diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java index 76767434..1472f920 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java @@ -89,13 +89,13 @@ private void handleGame(Notification notification) { } else if (notification instanceof MovePieceNotification) { MovePieceNotification n = (MovePieceNotification)notification; //gameView.getBoardHandler().movePiece(n.get); //TODO - } else if (notification instanceof MoveThrowPieceNotification) { - MoveThrowPieceNotification n = (MoveThrowPieceNotification)notification; + //TODO: } else if (notification instanceof MoveThrowPieceNotification) { + //TODO: MoveThrowPieceNotification n = (MoveThrowPieceNotification)notification; //gameView.getBoardHandler().throwPiece(n.); //TODO } else if (notification instanceof NoShieldNotification) { NoShieldNotification n = (NoShieldNotification)notification; gameView.getBoardHandler().unshieldPiece(n.getPieceId()); - } else if (notification instanceof PieceInGameNotification) { + //TODO:} else if (notification instanceof PieceInGameNotification) { // Handle PieceInGameNotification } else if (notification instanceof PlayCardNotification) { // Handle PlayCardNotification @@ -107,7 +107,7 @@ private void handleGame(Notification notification) { // Handle RollDiceNotification } else if (notification instanceof SelectableCardsNotification) { // Handle SelectableCardsNotification - } else if (notification instanceof SelectablePiecesNotification) { + //TODO: } else if (notification instanceof SelectablePiecesNotification) { // Handle SelectablePiecesNotification } else if (notification instanceof ShieldActiveNotification) { ShieldActiveNotification n = (ShieldActiveNotification)notification; diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientGameLogic.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientGameLogic.java index c40f7e02..32cf9754 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientGameLogic.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientGameLogic.java @@ -1,5 +1,6 @@ package pp.mdga.client; +import pp.mdga.game.BonusCard; import pp.mdga.game.Color; import pp.mdga.game.Game; import pp.mdga.game.Piece; @@ -19,6 +20,7 @@ public class ClientGameLogic implements ServerInterpreter { private final ClientSender clientSender; private ClientState state; private final ArrayList notifications = new ArrayList<>(); + private boolean isHost; private final DialogsState dialogsState = new DialogsState(null, this); private final GameState gameState = new GameState(null, this); @@ -59,6 +61,14 @@ public ClientState getState(){ return state; } + public boolean isHost(){ + return isHost; + } + + public void setHost(boolean isHost){ + this.isHost = isHost; + } + @Override public void received(ActivePlayerMessage msg) { state.received(msg); @@ -201,16 +211,15 @@ public void received(SpectatorMessage msg) { @Override public void received(SelectPieceMessage msg) { - + state.received(msg); } public void selectPiece(UUID pieceId){ state.selectPiece(getPiece(pieceId)); } - //TODO: implement - public void selectCard(UUID cardId){ - state.selectCard(null); + public void selectCard(BonusCard card){ + state.selectCard(card); } public void selectTsk(Color color){ @@ -226,7 +235,7 @@ public void selectName(String name){ } public void selectReady(boolean ready){ - state.selectReady(); + state.selectReady(ready); } public void selectHost(String name){ @@ -255,6 +264,12 @@ public void setState(ClientState state){ this.state = state; } + public void enterInterrupt(){ + interruptState.enter(); + interruptState.setPreviousState(state); + this.state = interruptState; + } + public GameState getGameState(){ return gameState; } @@ -275,4 +290,8 @@ public Notification getNotification(){ return notifications.remove(0); } + public void addNotification(Notification notification){ + notifications.add(notification); + } + } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientState.java index a9accf58..e16b9c91 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/ClientState.java @@ -199,7 +199,7 @@ public void setName(String name) { LOGGER.log(Level.DEBUG, "Setting name not allowed."); } - public void selectReady() { + public void selectReady(boolean ready) { LOGGER.log(Level.DEBUG, "Selecting ready not allowed."); } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/DialogsState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/DialogsState.java index 7f30a8c4..ca8cb4ef 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/DialogsState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/DialogsState.java @@ -7,12 +7,15 @@ import pp.mdga.game.Player; import pp.mdga.message.server.LobbyPlayerJoinMessage; import pp.mdga.message.server.LobbyPlayerLeaveMessage; +import pp.mdga.message.server.UpdateReadyMessage; +import pp.mdga.message.server.UpdateTSKMessage; public class DialogsState extends ClientState { private DialogStates currentState; - private Player ownPlayer; + private int ownPlayerID; + private String ownPlayerName; private final LobbyState lobbyState = new LobbyState(this, logic); private final NetworkDialogState networkDialogState = new NetworkDialogState(this, logic); @@ -31,6 +34,8 @@ public void exit(){ @Override public void enter(){ currentState = startDialogState; + ownPlayerID = 0; + ownPlayerName = null; } public void setState(DialogStates newState){ @@ -39,8 +44,16 @@ public void setState(DialogStates newState){ currentState = newState; } - public Player getOwnPlayer() { - return ownPlayer; + public int getOwnPlayerId() { + return ownPlayerID; + } + + public String getOwnPlayerName() { + return ownPlayerName; + } + + public void setOwnPlayerName(String ownPlayerName) { + this.ownPlayerName = ownPlayerName; } public LobbyState getLobby() { @@ -69,4 +82,18 @@ public void received(LobbyPlayerJoinMessage msg){ public void received(LobbyPlayerLeaveMessage msg){ currentState.received(msg); } + + @Override + public void received(UpdateTSKMessage msg){ + currentState.received(msg); + } + + @Override + public void received(UpdateReadyMessage msg){ + currentState.received(msg); + } + + public DialogStates getState() { + return currentState; + } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/GameState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/GameState.java index 56901970..fa8827ed 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/GameState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/GameState.java @@ -1,6 +1,8 @@ package pp.mdga.client; import pp.mdga.client.gameState.*; +import pp.mdga.message.server.PauseGameMessage; +import pp.mdga.notification.InterruptNotification; public class GameState extends ClientState { @@ -38,6 +40,16 @@ public void selectAnimationEnd(){ state.selectAnimationEnd(); } + @Override + public void received(PauseGameMessage msg){ + logic.enterInterrupt(); + logic.addNotification(new InterruptNotification(logic.getGame().getPlayers().get(msg.getPlayerId()).getColor())); + } + + public GameStates getState(){ + return state; + } + public AnimationState getAnimation() { return animationState; } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/InterruptState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/InterruptState.java index f58e2fe7..7ea0e21d 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/InterruptState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/InterruptState.java @@ -1,18 +1,36 @@ package pp.mdga.client; +import pp.mdga.message.server.ResumeGameMessage; +import pp.mdga.notification.ResumeNotification; + public class InterruptState extends ClientState { + private ClientState previousState; + public InterruptState(ClientState parent, ClientGameLogic logic) { super(parent, logic); } @Override public void enter() { - + previousState = null; } @Override public void exit() { + previousState = null; + } + public void setPreviousState(ClientState previousState) { + this.previousState = previousState; + } + + public ClientState getPreviousState() { + return previousState; + } + + public void received(ResumeGameMessage msg) { + //TODO: logic.addNotification(new ResumeNotification()); + logic.setState(previousState); } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/LobbyState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/LobbyState.java index 5f09da11..81c5fa6c 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/LobbyState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/LobbyState.java @@ -9,6 +9,10 @@ import pp.mdga.message.server.LobbyPlayerJoinMessage; import pp.mdga.message.server.LobbyPlayerLeaveMessage; import pp.mdga.message.server.ServerStartGameMessage; +import pp.mdga.message.server.UpdateReadyMessage; +import pp.mdga.message.server.UpdateTSKMessage; +import pp.mdga.notification.TskSelectNotification; +import pp.mdga.notification.TskUnselectNotification; public class LobbyState extends DialogStates { @@ -21,12 +25,10 @@ public LobbyState(ClientState parent, ClientGameLogic logic) { @Override public void enter() { - } @Override public void exit() { - } @Override @@ -45,7 +47,7 @@ public void deselectTSK(Color color) { } @Override - public void selectReady() { + public void selectReady(boolean ready) { logic.send(new LobbyReadyMessage()); } @@ -69,8 +71,21 @@ public void received(LobbyPlayerJoinMessage msg){ logic.getGame().getPlayers().put(msg.getId(), new Player(msg.getName())); } + @Override + public void received(UpdateTSKMessage msg){ + logic.addNotification(new TskUnselectNotification(logic.getGame().getPlayers().get(msg.getId()).getColor())); + logic.getGame().getPlayers().get(msg.getId()).setColor(msg.getColor()); + logic.addNotification(new TskSelectNotification(msg.getColor(), logic.getGame().getPlayers().get(msg.getId()).getName(), parent.getOwnPlayerId()== msg.getId())); + } + @Override public void received(LobbyPlayerLeaveMessage msg){ + logic.addNotification(new TskUnselectNotification(logic.getGame().getPlayers().get(msg.getId()).getColor())); logic.getGame().getPlayers().remove(msg.getId()); } + + @Override + public void received(UpdateReadyMessage msg){ + logic.getGame().getPlayers().get(msg.getPlayerId()).setReady(msg.isReady()); + } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/StartDialogState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/StartDialogState.java index 7e2bcc86..a9e5d148 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/StartDialogState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/dialogState/StartDialogState.java @@ -23,16 +23,21 @@ public void exit() { } + @Override public void selectJoin(String name) { - parent.getOwnPlayer().setName(name); + parent.setOwnPlayerName(name); parent.setState(parent.getNetworkDialog()); + logic.setHost(false); } + @Override public void selectHost(String name) { - parent.getOwnPlayer().setName(name); + parent.setOwnPlayerName(name); parent.setState(parent.getLobby()); + logic.setHost(true); } + @Override public void selectLeave() { parent.exit(); } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/ChoosePieceState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/ChoosePieceState.java index d5d86ffd..1c501853 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/ChoosePieceState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/ChoosePieceState.java @@ -27,7 +27,7 @@ public void enter() { @Override public void exit() { - + currentState= null; } public void setState(ChoosePieceStates state){ @@ -52,6 +52,10 @@ public WaitingPieceState getWaitingPiece(){ return waitingPieceState; } + public ChoosePieceStates getState(){ + return currentState; + } + public TurnState getParent(){ return parent; } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/PowerCardState.java b/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/PowerCardState.java index 1a880df4..b2cc1c7f 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/PowerCardState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/client/gameState/turnState/PowerCardState.java @@ -54,4 +54,8 @@ public SwapState getSwap() { public TurnState getParent() { return parent; } + + public PowerCardStates getState() { + return state; + } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/PauseGameMessage.java b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/PauseGameMessage.java index de9c6a8f..536a918f 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/PauseGameMessage.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/PauseGameMessage.java @@ -7,6 +7,12 @@ */ @Serializable public class PauseGameMessage extends ServerMessage { + + /** + * the id of the player who has disconnected + */ + private int playerId; + /** * Constructs a new PauseGame instance. */ @@ -14,6 +20,23 @@ public PauseGameMessage() { super(); } + /** + * Constructs a new PauseGame instance. + */ + public PauseGameMessage(int playerId) { + super(); + this.playerId = playerId; + } + + /** + * Returns the player id of the disconnected player + * + * @return the id of the disconnected player as an int + */ + public int getPlayerId() { + return playerId; + } + /** * Accepts a visitor to process this message. * diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/UpdateTSKMessage.java b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/UpdateTSKMessage.java index 8597a1e2..bbbdf14b 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/UpdateTSKMessage.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/UpdateTSKMessage.java @@ -19,7 +19,7 @@ public class UpdateTSKMessage extends ServerMessage { private final Color color; /** - * Constructs a new UpdateTSK instance with the specified name and color. + * Constructs a new UpdateTSK instance with the specified id and color. * * @param id the name associated with the update * @param color the color associated with the update diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/notification/TskSelectNotification.java b/Projekte/mdga/model/src/main/java/pp/mdga/notification/TskSelectNotification.java index 2d0f1c49..bf339e0c 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/notification/TskSelectNotification.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/notification/TskSelectNotification.java @@ -7,17 +7,19 @@ */ public class TskSelectNotification extends Notification{ - private Color color; - private String name; + private final Color color; + private final String name; + private final boolean isSelf; /** * Constructor. * @param color the color of the player that is in the game. * @param name the name of the player that is in the game. */ - public TskSelectNotification(Color color, String name) { + public TskSelectNotification(Color color, String name, boolean isSelf) { this.color = color; this.name = name; + this.isSelf = isSelf; } /** @@ -35,4 +37,13 @@ public Color getColor() { public String getName() { return name; } + + /** + * returns a boolean based of if the select notification affects the own user + * + * @return boolean isSelf + */ + public boolean isSelf() { + return isSelf; + } }