From b37a5095f02c18bcb1155b0664e82160ddd3cc49 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Mon, 18 Nov 2024 03:20:39 +0100 Subject: [PATCH] client recieve message logic --- .../monopoly/game/client/ClientGameLogic.java | 88 +++++++++---------- .../message/server/BuyPropertyResponse.java | 28 +++++- .../message/server/EventDrawCard.java | 9 ++ .../pp/monopoly/message/server/GameOver.java | 9 ++ .../pp/monopoly/message/server/JailEvent.java | 10 +++ .../message/server/PlayerStatusUpdate.java | 24 +++++ .../message/server/ServerInterpreter.java | 7 -- .../message/server/TimeOutWarning.java | 10 +++ .../message/server/UpdatePlayerAssets.java | 16 ---- .../message/server/ViewAssetsResponse.java | 6 +- .../java/pp/monopoly/notification/Sound.java | 55 +++++++++++- 11 files changed, 186 insertions(+), 76 deletions(-) delete mode 100644 Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/UpdatePlayerAssets.java diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java index 67e291c..1f440c0 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java @@ -19,7 +19,6 @@ import pp.monopoly.message.server.ServerInterpreter; import pp.monopoly.message.server.TimeOutWarning; import pp.monopoly.message.server.TradeReply; import pp.monopoly.message.server.TradeRequest; -import pp.monopoly.message.server.UpdatePlayerAssets; import pp.monopoly.message.server.ViewAssetsResponse; import pp.monopoly.model.IntPoint; import pp.monopoly.model.Board; @@ -38,8 +37,6 @@ import java.lang.System.Logger.Level; import java.util.ArrayList; import java.util.List; -import static java.lang.Math.max; - /** * Controls the client-side game logic for Monopoly. * Manages the player's placement, interactions with the map, and response to server messages. @@ -48,9 +45,8 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { static final Logger LOGGER = System.getLogger(ClientGameLogic.class.getName()); private final ClientSender clientSender; private final List listeners = new ArrayList<>(); - private Board ownMap; - private Board harbor; - private Board opponentMap; + private Board board; + private ClientState state = new ClientState(this) { }; @@ -88,8 +84,8 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { * * @return the player's own map */ - public Board getMap() { - return ownMap; + public Board getBoard() { + return board; } /** @@ -185,73 +181,73 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { @Override public void received(BuyPropertyResponse msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + if (msg.isSuccessful()) { + setInfoText("You successfully bought " + msg.getPropertyName() + "!"); + playSound(Sound.MONEY_LOST); + } else { + setInfoText("Unable to buy " + msg.getPropertyName() + ". Reason: " + msg.getReason()); + } } - + @Override public void received(DiceResult msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + setInfoText("You rolled a " + msg.calcTotal() + "!"); + playSound(Sound.DICE_ROLL); } - + @Override public void received(EventDrawCard msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + setInfoText("Event card drawn: " + msg.getCardDescription()); + playSound(Sound.EVENT_CARD); } - + @Override public void received(GameOver msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + if (msg.isWinner()) { + setInfoText("Congratulations! You have won the game!"); + playSound(Sound.WINNER); + } else { + setInfoText("Game over. Better luck next time!"); + playSound(Sound.LOSER); + } } - + @Override public void received(GameStart msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + setInfoText("The game has started! Good luck!"); } - + @Override public void received(JailEvent msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + if (msg.isGoingToJail()) { + setInfoText("You are sent to jail!"); + playSound(Sound.GULAG); + } else { + setInfoText("You are out of jail!"); + } } - + @Override public void received(PlayerStatusUpdate msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + setInfoText("Player " + msg.getPlayerName() + " status updated: " + msg.getStatus()); } - + @Override public void received(TimeOutWarning msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + setInfoText("Warning! Time is running out. You have " + msg.getRemainingTime() + " seconds left."); } - - @Override - public void received(UpdatePlayerAssets msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); - } - + @Override public void received(ViewAssetsResponse msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); + setInfoText("Your current assets are being displayed."); } - + @Override public void received(TradeReply msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); } - + @Override public void received(TradeRequest msg) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'received'"); } + } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/BuyPropertyResponse.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/BuyPropertyResponse.java index cd2b906..e926317 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/BuyPropertyResponse.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/BuyPropertyResponse.java @@ -1,11 +1,34 @@ package pp.monopoly.message.server; +/** + * Represents the server's response to a player's request to buy a property. + */ public class BuyPropertyResponse extends ServerMessage{ + private final boolean successful; + private final String propertyName; + private final String reason; // Reason for failure, if any + + public BuyPropertyResponse(boolean successful, String propertyName, String reason) { + this.successful = successful; + this.propertyName = propertyName; + this.reason = reason; + } + + public boolean isSuccessful() { + return successful; + } + + public String getPropertyName() { + return propertyName; + } + + public String getReason() { + return reason; + } @Override public void accept(ServerInterpreter interpreter) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); + interpreter.received(this); } @Override @@ -13,5 +36,4 @@ public class BuyPropertyResponse extends ServerMessage{ // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'getInfoTextKey'"); } - } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/EventDrawCard.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/EventDrawCard.java index 7ed4938..968f2bb 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/EventDrawCard.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/EventDrawCard.java @@ -1,6 +1,11 @@ package pp.monopoly.message.server; public class EventDrawCard extends ServerMessage{ + private final String cardDescription; + + public EventDrawCard(String cardDescription) { + this.cardDescription = cardDescription; + } @Override public void accept(ServerInterpreter interpreter) { @@ -13,4 +18,8 @@ public class EventDrawCard extends ServerMessage{ throw new UnsupportedOperationException("Unimplemented method 'getInfoTextKey'"); } + public String getCardDescription() { + return cardDescription; + } + } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameOver.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameOver.java index 9b2cff3..e91041e 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameOver.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameOver.java @@ -1,6 +1,15 @@ package pp.monopoly.message.server; public class GameOver extends ServerMessage{ + private final boolean isWinner; + + public GameOver(boolean isWinner) { + this.isWinner = isWinner; + } + + public boolean isWinner() { + return isWinner; + } @Override public void accept(ServerInterpreter interpreter) { diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/JailEvent.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/JailEvent.java index a802398..485a7fd 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/JailEvent.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/JailEvent.java @@ -2,6 +2,16 @@ package pp.monopoly.message.server; public class JailEvent extends ServerMessage{ + private final boolean goingToJail; + + public JailEvent(boolean goingToJail) { + this.goingToJail = goingToJail; + } + + public boolean isGoingToJail() { + return goingToJail; + } + @Override public void accept(ServerInterpreter interpreter) { interpreter.received(this); diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/PlayerStatusUpdate.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/PlayerStatusUpdate.java index 8b51066..c876108 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/PlayerStatusUpdate.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/PlayerStatusUpdate.java @@ -1,7 +1,31 @@ package pp.monopoly.message.server; +import pp.monopoly.game.server.PlayerColor; + public class PlayerStatusUpdate extends ServerMessage{ + private final String playerName; + private final String status; + private final PlayerColor color; + + public PlayerStatusUpdate(String playerName, String status, PlayerColor color) { + this.playerName = playerName; + this.status = status; + this.color = color; + } + + public String getPlayerName() { + return playerName; + } + + public String getStatus() { + return status; + } + + public PlayerColor getColor() { + return color; + } + @Override public void accept(ServerInterpreter interpreter) { interpreter.received(this); diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ServerInterpreter.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ServerInterpreter.java index 3480d20..1f78ba7 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ServerInterpreter.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ServerInterpreter.java @@ -69,13 +69,6 @@ public interface ServerInterpreter { */ void received(TimeOutWarning msg); - /** - * Handles a UpdatePlayerAssets message received from the server. - * - * @param msg the UpdatePlayerAssets message received - */ - void received(UpdatePlayerAssets msg); - /** * Handles a ViewAssetsResponse message received from the server. * diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/TimeOutWarning.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/TimeOutWarning.java index f9510a7..b862170 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/TimeOutWarning.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/TimeOutWarning.java @@ -2,6 +2,16 @@ package pp.monopoly.message.server; public class TimeOutWarning extends ServerMessage{ + private final int remainingTime; + + public TimeOutWarning(int remainingTime) { + this.remainingTime = remainingTime; + } + + public int getRemainingTime() { + return remainingTime; + } + @Override public void accept(ServerInterpreter interpreter) { interpreter.received(this); diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/UpdatePlayerAssets.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/UpdatePlayerAssets.java deleted file mode 100644 index e37c78c..0000000 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/UpdatePlayerAssets.java +++ /dev/null @@ -1,16 +0,0 @@ -package pp.monopoly.message.server; - -public class UpdatePlayerAssets extends ServerMessage{ - - @Override - public void accept(ServerInterpreter interpreter) { - interpreter.received(this); - } - - @Override - public String getInfoTextKey() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getInfoTextKey'"); - } - -} diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ViewAssetsResponse.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ViewAssetsResponse.java index 116366e..9a00833 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ViewAssetsResponse.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ViewAssetsResponse.java @@ -8,9 +8,9 @@ import pp.monopoly.model.fields.PropertyField; */ public class ViewAssetsResponse extends ServerMessage{ - private List properties; - private int accountBalance; - private int jailCards; + private final List properties; + private final int accountBalance; + private final int jailCards; /** * Constructs a ViewAssetsResponse with the specified properties and account balance. diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/Sound.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/Sound.java index 280ad4f..36c12d6 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/Sound.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/Sound.java @@ -1,8 +1,61 @@ package pp.monopoly.notification; /** - * Enumeration representing different types of sounds used in the game. + * Enum representing various sound effects in the game. */ public enum Sound { + /** + * UC-sound-01: Sound effect for passing the start/Los field. + */ + PASS_START, + /** + * UC-sound-02: Sound effect for drawing an event card. + */ + EVENT_CARD, + + /** + * UC-sound-03: Sound effect for entering the Gulag. + */ + GULAG, + + /** + * UC-sound-04: Sound effect for rolling the dice. + */ + DICE_ROLL, + + /** + * UC-sound-05: Sound effect for collecting money. + */ + MONEY_COLLECTED, + + /** + * UC-sound-06: Sound effect for losing money. + */ + MONEY_LOST, + + /** + * UC-sound-07: Sound effect for accepting a trade offer. + */ + TRADE_ACCEPTED, + + /** + * UC-sound-08: Sound effect for rejecting a trade offer. + */ + TRADE_REJECTED, + + /** + * UC-sound-09: Sound effect for winning the game. + */ + WINNER, + + /** + * UC-sound-10: Sound effect for losing the game. + */ + LOSER, + + /** + * UC-sound-11: Sound effect for button click. + */ + BUTTON; }