From b87b3f054b2ee2bc829d45e6914d24e526f35607 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Sat, 30 Nov 2024 21:49:16 +0100 Subject: [PATCH] trigger rent popup --- .../pp/monopoly/client/gui/TestWorld.java | 4 ++ .../pp/monopoly/client/gui/popups/Rent.java | 30 +++++------ .../monopoly/game/client/ClientGameLogic.java | 18 +++++-- .../java/pp/monopoly/game/server/Player.java | 18 ++++++- .../message/server/NotificationMessage.java | 50 +++++++++++++++++++ .../message/server/ServerInterpreter.java | 7 +++ .../pp/monopoly/notification/PopUpEvent.java | 4 +- .../pp/monopoly/server/MonopolyServer.java | 2 + 8 files changed, 108 insertions(+), 25 deletions(-) create mode 100644 Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/NotificationMessage.java diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java index 2be8734..580c95d 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java @@ -11,10 +11,12 @@ import pp.monopoly.client.gui.popups.EventCardPopup; import pp.monopoly.client.gui.popups.FoodFieldCard; import pp.monopoly.client.gui.popups.GateFieldCard; import pp.monopoly.client.gui.popups.LooserPopUp; +import pp.monopoly.client.gui.popups.Rent; import pp.monopoly.client.gui.popups.TimeOut; import pp.monopoly.client.gui.popups.WinnerPopUp; import pp.monopoly.game.server.Player; import pp.monopoly.game.server.PlayerHandler; +import pp.monopoly.message.server.NotificationMessage; import pp.monopoly.model.fields.BuildingProperty; import pp.monopoly.model.fields.FoodField; import pp.monopoly.model.fields.GateField; @@ -153,6 +155,8 @@ public class TestWorld implements GameEventListener{ new TimeOut(app).open(); } else if (event.msg().equals("tradeRequest")) { new ConfirmTrade(app).open(); + } else if(event.msg().equals("rent")) { + new Rent(app, ( (NotificationMessage) event.message()).getRentOwner(), ( (NotificationMessage) event.message()).getRentAmount() ).open(); } } diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/Rent.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/Rent.java index d15df2f..f11c947 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/Rent.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/Rent.java @@ -13,7 +13,7 @@ import com.simsilica.lemur.component.QuadBackgroundComponent; import com.simsilica.lemur.style.ElementId; import pp.dialog.Dialog; import pp.monopoly.client.MonopolyApp; -import pp.monopoly.model.fields.BuildingProperty; +import pp.monopoly.notification.Sound; /** * Rent is a popup that is triggered when a player enters a field that does not belong himself and is owned by another player @@ -24,17 +24,10 @@ public class Rent extends Dialog { private final Container rentContainer; private final Container backgroundContainer; - - private int index = 21; // TODO fixed for testing - - - - public Rent(MonopolyApp app) { + public Rent(MonopolyApp app, String playerName, int amount) { super(app.getDialogManager()); this.app = app; - BuildingProperty field = (BuildingProperty) app.getGameLogic().getBoardManager().getFieldAtIndex(index); - // Halbtransparentes Overlay hinzufügen overlayBackground = createOverlayBackground(); app.getGuiNode().attachChild(overlayBackground); @@ -48,26 +41,29 @@ public class Rent extends Dialog { // Hauptcontainer für die Warnung rentContainer = new Container(); - rentContainer.setBackground(new QuadBackgroundComponent(field.getColor().getColor())); + rentContainer.setBackground(new QuadBackgroundComponent(ColorRGBA.Gray)); rentContainer.setPreferredSize(new Vector3f(550,250,10)); float padding = 10; // Passt den backgroundContainer an die Größe des bankruptContainers an backgroundContainer.setPreferredSize(rentContainer.getPreferredSize().addLocal(padding, padding, 0)); // Titel - Label gateFieldTitle = rentContainer.addChild(new Label( "Miete fürs "+field.getName()+" !", new ElementId("label-Bold"))); + Label gateFieldTitle = rentContainer.addChild(new Label( "Miete !", new ElementId("label-Bold"))); gateFieldTitle.setFontSize(48); gateFieldTitle.setColor(ColorRGBA.Black); // Text, der auf der Karte steht Container textContainer = rentContainer.addChild(new Container()); - textContainer.addChild(new Label("Du bist auf das " + field.getName()+ " gekommen!", new ElementId("label-Text"))); - textContainer.addChild(new Label("Du must Spieler XXX XXX EUR Miete zahlen", new ElementId("label-Text"))); + textContainer.addChild(new Label("Du must Spieler "+ playerName + " "+ amount+" EUR Miete zahlen", new ElementId("label-Text"))); textContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f))); textContainer.setPreferredSize(rentContainer.getPreferredSize().addLocal(-250,-200,0)); // Beenden-Button Button quitButton = rentContainer.addChild(new Button("Überweisen", new ElementId("button"))); + quitButton.addClickCommands(s -> ifTopDialog(() -> { + app.getGameLogic().playSound(Sound.BUTTON); + close(); + })); quitButton.setFontSize(32); quitButton.addClickCommands(source -> close()); @@ -76,14 +72,14 @@ public class Rent extends Dialog { rentContainer.setLocalTranslation( (app.getCamera().getWidth() - rentContainer.getPreferredSize().x) / 2, (app.getCamera().getHeight() + rentContainer.getPreferredSize().y) / 2, - 8 + 10 ); // Zentriere das Popup backgroundContainer.setLocalTranslation( (app.getCamera().getWidth() - rentContainer.getPreferredSize().x - padding) / 2, (app.getCamera().getHeight() + rentContainer.getPreferredSize().y+ padding) / 2, - 7 + 10 ); app.getGuiNode().attachChild(rentContainer); @@ -101,7 +97,7 @@ public class Rent extends Dialog { material.setColor("Color", new ColorRGBA(0, 0, 0, 0.5f)); // Halbtransparent material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); overlay.setMaterial(material); - overlay.setLocalTranslation(0, 0, 0); + overlay.setLocalTranslation(0, 0, 10); return overlay; } @@ -110,10 +106,10 @@ public class Rent extends Dialog { */ @Override public void close() { + super.close(); app.getGuiNode().detachChild(rentContainer); // Entferne das Menü app.getGuiNode().detachChild(backgroundContainer); //Entfernt Rand app.getGuiNode().detachChild(overlayBackground); // Entferne das Overlay - super.close(); } @Override 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 9115192..8c4b925 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 @@ -14,6 +14,7 @@ import pp.monopoly.message.server.GameOver; import pp.monopoly.message.server.GameStart; import pp.monopoly.message.server.JailEvent; import pp.monopoly.message.server.NextPlayerTurn; +import pp.monopoly.message.server.NotificationMessage; import pp.monopoly.message.server.PlayerStatusUpdate; import pp.monopoly.message.server.ServerInterpreter; import pp.monopoly.message.server.TimeOutWarning; @@ -233,10 +234,10 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { @Override public void received(GameOver msg) { if (msg.isWinner()) { - notifyListeners(new PopUpEvent("Winner")); + notifyListeners(new PopUpEvent("Winner", msg)); playSound(Sound.WINNER); } else { - notifyListeners(new PopUpEvent("Looser")); + notifyListeners(new PopUpEvent("Looser", msg)); playSound(Sound.LOSER); } } @@ -287,7 +288,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { */ @Override public void received(TimeOutWarning msg) { - notifyListeners(new PopUpEvent("timeout")); + notifyListeners(new PopUpEvent("timeout", msg)); } /** @@ -322,7 +323,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { @Override public void received(TradeRequest msg) { tradeHandler = msg.getTradeHandler(); - notifyListeners(new PopUpEvent("tradeRequest")); + notifyListeners(new PopUpEvent("tradeRequest", msg)); } /** @@ -338,6 +339,13 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { @Override public void received(BuyPropertyRequest msg) { - notifyListeners(new PopUpEvent("Buy")); + notifyListeners(new PopUpEvent("Buy", msg)); + } + + @Override + public void received(NotificationMessage msg) { + if (msg.getKeyWord().equals("rent")) { + notifyListeners(new PopUpEvent("rent", msg)); + } } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java index 1d0d141..c69e655 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java @@ -19,6 +19,7 @@ import pp.monopoly.message.server.BuyPropertyRequest; import pp.monopoly.message.server.DiceResult; import pp.monopoly.message.server.EventDrawCard; import pp.monopoly.message.server.NextPlayerTurn; +import pp.monopoly.message.server.NotificationMessage; import pp.monopoly.message.server.PlayerStatusUpdate; import pp.monopoly.model.FieldVisitor; import pp.monopoly.model.Figure; @@ -304,6 +305,10 @@ public class Player implements FieldVisitor{ int rent = field.calcRent(); field.getOwner().earnMoney(rent); pay(rent); + NotificationMessage msg = new NotificationMessage("rent"); + msg.setRentAmount(rent); + msg.setRentOwnerId(field.getOwner().getName()); + getHandler().getLogic().send(this, msg); } return null; } @@ -317,8 +322,13 @@ public class Player implements FieldVisitor{ if (field.getOwner().getNumProp(field) == 2) { factor = 10; } - field.getOwner().earnMoney(rollResult.calcTotal()*factor); - pay(rollResult.calcTotal()*factor); + int rent = rollResult.calcTotal()*factor; + field.getOwner().earnMoney(rent); + pay(rent); + NotificationMessage msg = new NotificationMessage("rent"); + msg.setRentAmount(rent); + msg.setRentOwnerId(field.getOwner().getName()); + getHandler().getLogic().send(this, msg); } return null; } @@ -332,6 +342,10 @@ public class Player implements FieldVisitor{ field.getOwner().earnMoney(rent); pay(rent); + NotificationMessage msg = new NotificationMessage("rent"); + msg.setRentAmount(rent); + msg.setRentOwnerId(field.getOwner().getName()); + getHandler().getLogic().send(this, msg); } return null; } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/NotificationMessage.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/NotificationMessage.java new file mode 100644 index 0000000..5713875 --- /dev/null +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/NotificationMessage.java @@ -0,0 +1,50 @@ +package pp.monopoly.message.server; + +import com.jme3.network.serializing.Serializable; + +@Serializable +public class NotificationMessage extends ServerMessage{ + + private final String keyWord; + + private int rentAmount; + private String rentOwner; + + private NotificationMessage(){ keyWord = null;} + + public NotificationMessage(String keyWord) { + this.keyWord = keyWord; + } + + public int getRentAmount() { + return rentAmount; + } + + public void setRentAmount(int rentAmount) { + this.rentAmount = rentAmount; + } + + public void setRentOwnerId(String rentOwnerId) { + this.rentOwner = rentOwnerId; + } + + public String getRentOwner() { + return rentOwner; + } + + public String getKeyWord() { + return keyWord; + } + + @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/ServerInterpreter.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/ServerInterpreter.java index 308cfca..72aa99d 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 @@ -96,4 +96,11 @@ public interface ServerInterpreter { * @param msg the NextPlayerTurn message received */ void received(BuyPropertyRequest msg); + + /** + * Handles a NextPlayerTurn message received from the server. + * + * @param msg the NextPlayerTurn message received + */ + void received(NotificationMessage msg); } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/PopUpEvent.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/PopUpEvent.java index a808eed..f53474e 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/PopUpEvent.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/PopUpEvent.java @@ -1,6 +1,8 @@ package pp.monopoly.notification; -public record PopUpEvent(String msg) implements GameEvent{ +import pp.monopoly.message.server.ServerMessage; + +public record PopUpEvent(String msg, ServerMessage message) implements GameEvent{ @Override public void notifyListener(GameEventListener listener) { diff --git a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java index 3b44378..ba1888b 100644 --- a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java +++ b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java @@ -42,6 +42,7 @@ import pp.monopoly.message.server.DiceResult; import pp.monopoly.message.server.EventDrawCard; import pp.monopoly.message.server.GameStart; import pp.monopoly.message.server.NextPlayerTurn; +import pp.monopoly.message.server.NotificationMessage; import pp.monopoly.message.server.PlayerStatusUpdate; import pp.monopoly.message.server.ServerMessage; import pp.monopoly.message.server.TradeReply; @@ -170,6 +171,7 @@ public class MonopolyServer implements MessageListener, Connec Serializer.registerClass(TradeRequest.class); Serializer.registerClass(TradeReply.class); Serializer.registerClass(TradeHandler.class); + Serializer.registerClass(NotificationMessage.class); } private void registerListeners() {