From c900b6384d9c9eb09cc37a86492ed69a7993ab5f Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Thu, 28 Nov 2024 19:40:30 +0100 Subject: [PATCH] lock button when not active --- .../java/pp/monopoly/client/gui/Toolbar.java | 23 ++++-- .../monopoly/game/client/ClientGameLogic.java | 22 +----- .../java/pp/monopoly/game/server/Player.java | 70 ++++++++++--------- .../monopoly/game/server/ServerGameLogic.java | 4 +- .../message/server/BuyPropertyResponse.java | 47 ------------- .../message/server/ServerInterpreter.java | 7 -- .../notification/ButtonStatusEvent.java | 13 ++++ .../notification/GameEventListener.java | 7 ++ 8 files changed, 81 insertions(+), 112 deletions(-) delete mode 100644 Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/BuyPropertyResponse.java create mode 100644 Projekte/monopoly/model/src/main/java/pp/monopoly/notification/ButtonStatusEvent.java diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java index 72e7a84..24379db 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java @@ -16,6 +16,7 @@ import pp.monopoly.game.server.PlayerHandler; import pp.monopoly.message.client.EndTurn; import pp.monopoly.message.client.RollDice; import pp.monopoly.notification.DiceRollEvent; +import pp.monopoly.notification.ButtonStatusEvent; import pp.monopoly.notification.GameEventListener; import pp.monopoly.notification.Sound; import pp.monopoly.notification.UpdatePlayerView; @@ -29,6 +30,10 @@ public class Toolbar extends Dialog implements GameEventListener { private Container overviewContainer; private Container accountContainer; private PlayerHandler playerHandler; + private Button diceButton; + private Button tradeButton; + private Button propertyMenuButton; + private Button endTurnButton; private volatile boolean animatingDice = false; private volatile DiceRollEvent latestDiceRollEvent = null; @@ -83,9 +88,10 @@ public class Toolbar extends Dialog implements GameEventListener { diceContainer.addChild(horizontalContainer); // Würfeln-Button - Button diceButton = new Button("Würfeln"); + diceButton = new Button("Würfeln"); diceButton.setPreferredSize(new Vector3f(200, 50, 0)); diceButton.addClickCommands(s -> ifTopDialog(() -> { + diceButton.setEnabled(false); startDiceAnimation(); app.getGameLogic().send(new RollDice()); app.getGameLogic().playSound(Sound.BUTTON); @@ -103,7 +109,7 @@ public class Toolbar extends Dialog implements GameEventListener { } private Button addTradeMenuButton() { - Button tradeButton = new Button("Handeln"); + tradeButton = new Button("Handeln"); tradeButton.setPreferredSize(new Vector3f(150, 50, 0)); tradeButton.addClickCommands(s -> ifTopDialog(() -> { app.getGameLogic().playSound(Sound.BUTTON); @@ -113,7 +119,7 @@ public class Toolbar extends Dialog implements GameEventListener { } private Button addPropertyMenuButton() { - Button propertyMenuButton = new Button("Grundstücke"); + propertyMenuButton = new Button("Grundstücke"); propertyMenuButton.setPreferredSize(new Vector3f(150, 50, 0)); propertyMenuButton.addClickCommands(s -> ifTopDialog(() -> { app.getGameLogic().playSound(Sound.BUTTON); @@ -123,11 +129,12 @@ public class Toolbar extends Dialog implements GameEventListener { } private Button addEndTurnButton() { - Button endTurnButton = new Button("Zug beenden"); + endTurnButton = new Button("Zug beenden"); endTurnButton.setPreferredSize(new Vector3f(150, 50, 0)); endTurnButton.addClickCommands(s -> ifTopDialog(() -> { app.getGameLogic().playSound(Sound.BUTTON); app.getGameLogic().send(new EndTurn()); + receivedEvent(new ButtonStatusEvent(false)); })); return endTurnButton; } @@ -259,4 +266,12 @@ public class Toolbar extends Dialog implements GameEventListener { public void escape() { new SettingsMenu(app).open(); } + + @Override + public void receivedEvent(ButtonStatusEvent event) { + propertyMenuButton.setEnabled(event.buttonsEnabled()); + diceButton.setEnabled(event.buttonsEnabled()); + endTurnButton.setEnabled(event.buttonsEnabled()); + tradeButton.setEnabled(event.buttonsEnabled()); + } } 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 05f1329..dc9736b 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 @@ -5,10 +5,8 @@ import java.lang.System.Logger.Level; import java.util.ArrayList; import java.util.List; -import pp.monopoly.game.server.Player; import pp.monopoly.game.server.PlayerHandler; import pp.monopoly.message.client.ClientMessage; -import pp.monopoly.message.server.BuyPropertyResponse; import pp.monopoly.message.server.DiceResult; import pp.monopoly.message.server.EventDrawCard; import pp.monopoly.message.server.GameOver; @@ -26,6 +24,7 @@ import pp.monopoly.model.IntPoint; import pp.monopoly.model.fields.BoardManager; import pp.monopoly.notification.ClientStateEvent; import pp.monopoly.notification.DiceRollEvent; +import pp.monopoly.notification.ButtonStatusEvent; import pp.monopoly.notification.EventCardEvent; import pp.monopoly.notification.GameEvent; import pp.monopoly.notification.GameEventBroker; @@ -194,21 +193,6 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { state.update(delta); } - /** - * Handles the response for buying a property. - * - * @param msg the message containing the buy property response - */ - @Override - public void received(BuyPropertyResponse msg) { - if (msg.isSuccessful()) { - - playSound(Sound.MONEY_LOST); - } else { - - } - } - /** * Handles the result of a dice roll. * @@ -258,7 +242,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { public void received(GameStart msg) { playerHandler = msg.getPlayerHandler(); setState(new WaitForTurnState(this)); - + notifyListeners(new ButtonStatusEvent(false)); } /** @@ -342,7 +326,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { */ @Override public void received(NextPlayerTurn msg) { - + notifyListeners(new ButtonStatusEvent(true)); setState(new ActiveState(this)); } } 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 182d0a8..7de65c9 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 @@ -15,6 +15,7 @@ import com.jme3.network.serializing.Serializable; import pp.monopoly.message.server.DiceResult; import pp.monopoly.message.server.EventDrawCard; +import pp.monopoly.message.server.NextPlayerTurn; import pp.monopoly.message.server.PlayerStatusUpdate; import pp.monopoly.model.FieldVisitor; import pp.monopoly.model.Figure; @@ -44,7 +45,9 @@ public class Player implements FieldVisitor{ private int fieldID; private DiceResult rollResult; private transient final PlayerHandler handler; - private transient PlayerState state = new LobbyState(); + private transient PlayerState state = new WaitForTurnState(); + private int doubletscounter = 0; + private boolean mayRollDice = false; /** * Default constructor for serialization purposes. @@ -127,6 +130,8 @@ public class Player implements FieldVisitor{ } void setActive() { state = new ActiveState(); + doubletscounter = 0; + mayRollDice = true; } boolean finishTurn() { @@ -162,9 +167,23 @@ public class Player implements FieldVisitor{ earnMoney(2000); } figure.moveTo(fieldID); + handler.getLogic().getBoardManager().getFieldAtIndex(fieldID).accept(this); return fieldID; } + /** + * Sets the player to the specified Position on the board + * @param position the position to move to + * @return the new position + */ + public int setPosition(int position){ + if(position < 40 && position > 0) { + fieldID = position; + figure.moveTo(fieldID); + handler.getLogic().getBoardManager().getFieldAtIndex(fieldID).accept(this); + } + return fieldID; + } /** * Gets all the properties owned by this player @@ -330,15 +349,13 @@ public class Player implements FieldVisitor{ @Override public Void visit(WacheField field) { - movePos(10); + setPosition(10); return null; } @Override public Void visit(GoField field) { earnMoney(2000); - GulagField res = (GulagField) handler.getLogic().getBoardManager().getFieldAtIndex(10); - res.accept(this); return null; } @@ -410,11 +427,21 @@ public class Player implements FieldVisitor{ * @return a List of two integers representing the dice roll results */ DiceResult rollDice() { - return state.rollDice(); - } - - private void visitEvent() { - getHandler().getLogic().getBoardManager().getFieldAtIndex(36).accept(this); + if (mayRollDice) { + state.rollDice(); + } + if (rollResult.isDoublets()) { + doubletscounter++; + mayRollDice = true; + getHandler().getLogic().send(this, new NextPlayerTurn()); + setName(name); + if (doubletscounter >= 3) { + setPosition(10); + } + } else { + mayRollDice = false; + } + return rollResult; } /** @@ -464,31 +491,6 @@ public class Player implements FieldVisitor{ } - /** - * A class to represent the Lobby PlayerState - * Set when in Lobby - */ - private class LobbyState implements PlayerState{ - - @Override - public DiceResult rollDice() { - //do nothing - return null; - } - - @Override - public void payBail() { - //do nothing - } - - @Override - public void useJailCard() { - // do nothing - } - - } - - /** * A class to represent the Jailed PlayerState * Set when in Gulag diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java index 1d1c7ce..8b211ba 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java @@ -169,12 +169,14 @@ public class ServerGameLogic implements ClientInterpreter { @Override public void received(EndTurn msg, int from) { Player player = playerHandler.getPlayerById(from); - if (player != null && state == ServerState.INGAME) { + if (player != null && player == playerHandler.getPlayerAtIndex(0)) { if (player.finishTurn()) { LOGGER.log(Level.DEBUG, "Ending turn for player {0}", player.getName()); Player next = playerHandler.nextPlayer(); next.setActive(); send(next, new NextPlayerTurn()); + send(next, new PlayerStatusUpdate(playerHandler)); + send(player, new PlayerStatusUpdate(playerHandler)); } } } 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 deleted file mode 100644 index 39e1e24..0000000 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/BuyPropertyResponse.java +++ /dev/null @@ -1,47 +0,0 @@ -package pp.monopoly.message.server; - -import com.jme3.network.serializing.Serializable; - -/** - * Represents the server's response to a player's request to buy a property. - */ -@Serializable -public class BuyPropertyResponse extends ServerMessage{ - private boolean successful; - private String propertyName; - private String reason; // Reason for failure, if any - - /** - * Default constructor for serialization purposes. - */ - private BuyPropertyResponse() { /* empty */ } - - 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) { - 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 4817e2a..51529fe 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 @@ -13,13 +13,6 @@ package pp.monopoly.message.server; */ public interface ServerInterpreter { - /** - * Handles a BuyPropertyResponse message received from the server. - * - * @param msg the BuyPropertyResponse message received - */ - void received(BuyPropertyResponse msg); - /** * Handles a DiceResult message received from the server. * diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/ButtonStatusEvent.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/ButtonStatusEvent.java new file mode 100644 index 0000000..96a649f --- /dev/null +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/ButtonStatusEvent.java @@ -0,0 +1,13 @@ +package pp.monopoly.notification; + +public record ButtonStatusEvent(boolean buttonsEnabled) implements GameEvent{ + /** + * Notifies the game event listener of this event. + * + * @param listener the game event listener + */ + @Override + public void notifyListener(GameEventListener listener) { + listener.receivedEvent(this); + } +} diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java index 1d50dcb..1ea06ff 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java @@ -66,4 +66,11 @@ public interface GameEventListener { * @param event the received event */ default void receivedEvent(EventCardEvent event) { /*Do nothing */} + + /** + * Indicates that all buttons in the toolbar should be disabled + * + * @param event the received event + */ + default void receivedEvent(ButtonStatusEvent event) { /*Do nothing */} }