From 373b9e6d531df871c4d678804fb74462d4b7c19a Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Sat, 23 Nov 2024 13:20:05 +0100 Subject: [PATCH] allow the host player to set start money --- .../java/pp/monopoly/game/server/Player.java | 10 +++++++++- .../pp/monopoly/game/server/PlayerHandler.java | 18 ++++++++++++++++++ .../monopoly/game/server/ServerGameLogic.java | 6 ++++++ .../monopoly/message/client/PlayerReady.java | 14 ++++++++++---- 4 files changed, 43 insertions(+), 5 deletions(-) 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 7208209..28f1f16 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 @@ -31,7 +31,7 @@ import pp.monopoly.model.fields.WacheField; public class Player implements FieldVisitor{ private final int id; private String name; - private int accountBalance = 0; + private int accountBalance = 15000; private Figure figure; private List properties; private int getOutOfJailCard; @@ -154,6 +154,14 @@ public class Player implements FieldVisitor{ } } + /** + * Set the account Balance + * @param accountBalance the amount to be set to + */ + public void setAccountBalance(int accountBalance) { + this.accountBalance = accountBalance; + } + /** * Gets this players current accountBalanece * @return the amount of money currently owned by this player diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java index b92d4e7..1bef778 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java @@ -14,6 +14,7 @@ public class PlayerHandler { private List players = new LinkedList<>(); private Set readyPlayers = new HashSet<>(); private ServerGameLogic logic; + private Player hostPlayer; /** * Contructs a PlayerHandler @@ -43,6 +44,14 @@ public class PlayerHandler { players.addAll(players); } + /** + * Return the host player + * @return the host player + */ + public Player getHostPlayer() { + return hostPlayer; + } + /** * Return the number of players * @return number of players in the game @@ -94,6 +103,9 @@ public class PlayerHandler { throw new IllegalArgumentException("Player already registered"); } players.add(player); + if(hostPlayer == null) { + hostPlayer = player; + } } /** @@ -150,4 +162,10 @@ public class PlayerHandler { void randomOrder() { Collections.shuffle(players); } + + void setStartBalance(int amount) { + for (Player player : players) { + player.setAccountBalance(amount); + } + } } 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 2f13f59..cd6d051 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 @@ -35,6 +35,7 @@ public class ServerGameLogic implements ClientInterpreter { private ServerState state = ServerState.LOBBY; private static final int MAX_PLAYERS = 6; private BoardManager boardManager = new BoardManager(); + private int startMoney; /** * Constructs a ServerGameLogic instance with the specified sender and configuration. @@ -180,6 +181,10 @@ public class ServerGameLogic implements ClientInterpreter { @Override public void received(PlayerReady msg, int from) { Player player = playerHandler.getPlayerById(from); + if(player == playerHandler.getHostPlayer()) { + startMoney = msg.getStartMoney(); + } + if (player != null) { player.setName(msg.getName()); playerHandler.setPlayerReady(player, true); @@ -187,6 +192,7 @@ public class ServerGameLogic implements ClientInterpreter { } if(playerHandler.allPlayersReady()) { + playerHandler.setStartBalance(startMoney); for (Player p : playerHandler.getPlayers()) { send(p, new GameStart(playerHandler.getPlayers())); } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/PlayerReady.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/PlayerReady.java index 3dcec70..42aa0d6 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/PlayerReady.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/PlayerReady.java @@ -4,9 +4,10 @@ package pp.monopoly.message.client; * Represents a message indicating the player is ready to play. */ public class PlayerReady extends ClientMessage { - private boolean isReady; - private String name; - private String figure; + private final boolean isReady; + private final String name; + private final String figure; + private final int startMoney; /** * Constructs a PlayerReady message. @@ -15,10 +16,11 @@ public class PlayerReady extends ClientMessage { * @param name the name of the player * @param color the color of the player (can be null) */ - public PlayerReady(boolean isReady, String name, String figure) { + public PlayerReady(boolean isReady, String name, String figure, int startMoney) { this.isReady = isReady; this.name = name; this.figure = figure; + this.startMoney = startMoney; } public String getName() { @@ -33,6 +35,10 @@ public class PlayerReady extends ClientMessage { return isReady; } + public int getStartMoney() { + return startMoney; + } + @Override public void accept(ClientInterpreter interpreter, int from) { interpreter.received(this, from);