allow the host player to set start money

This commit is contained in:
Johannes Schmelz 2024-11-23 13:20:05 +01:00
parent 3de31de9f7
commit 373b9e6d53
4 changed files with 43 additions and 5 deletions

View File

@ -31,7 +31,7 @@ import pp.monopoly.model.fields.WacheField;
public class Player implements FieldVisitor<Void>{ public class Player implements FieldVisitor<Void>{
private final int id; private final int id;
private String name; private String name;
private int accountBalance = 0; private int accountBalance = 15000;
private Figure figure; private Figure figure;
private List<PropertyField> properties; private List<PropertyField> properties;
private int getOutOfJailCard; private int getOutOfJailCard;
@ -154,6 +154,14 @@ public class Player implements FieldVisitor<Void>{
} }
} }
/**
* 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 * Gets this players current accountBalanece
* @return the amount of money currently owned by this player * @return the amount of money currently owned by this player

View File

@ -14,6 +14,7 @@ public class PlayerHandler {
private List<Player> players = new LinkedList<>(); private List<Player> players = new LinkedList<>();
private Set<Player> readyPlayers = new HashSet<>(); private Set<Player> readyPlayers = new HashSet<>();
private ServerGameLogic logic; private ServerGameLogic logic;
private Player hostPlayer;
/** /**
* Contructs a PlayerHandler * Contructs a PlayerHandler
@ -43,6 +44,14 @@ public class PlayerHandler {
players.addAll(players); players.addAll(players);
} }
/**
* Return the host player
* @return the host player
*/
public Player getHostPlayer() {
return hostPlayer;
}
/** /**
* Return the number of players * Return the number of players
* @return number of players in the game * @return number of players in the game
@ -94,6 +103,9 @@ public class PlayerHandler {
throw new IllegalArgumentException("Player already registered"); throw new IllegalArgumentException("Player already registered");
} }
players.add(player); players.add(player);
if(hostPlayer == null) {
hostPlayer = player;
}
} }
/** /**
@ -150,4 +162,10 @@ public class PlayerHandler {
void randomOrder() { void randomOrder() {
Collections.shuffle(players); Collections.shuffle(players);
} }
void setStartBalance(int amount) {
for (Player player : players) {
player.setAccountBalance(amount);
}
}
} }

View File

@ -35,6 +35,7 @@ public class ServerGameLogic implements ClientInterpreter {
private ServerState state = ServerState.LOBBY; private ServerState state = ServerState.LOBBY;
private static final int MAX_PLAYERS = 6; private static final int MAX_PLAYERS = 6;
private BoardManager boardManager = new BoardManager(); private BoardManager boardManager = new BoardManager();
private int startMoney;
/** /**
* Constructs a ServerGameLogic instance with the specified sender and configuration. * Constructs a ServerGameLogic instance with the specified sender and configuration.
@ -180,6 +181,10 @@ public class ServerGameLogic implements ClientInterpreter {
@Override @Override
public void received(PlayerReady msg, int from) { public void received(PlayerReady msg, int from) {
Player player = playerHandler.getPlayerById(from); Player player = playerHandler.getPlayerById(from);
if(player == playerHandler.getHostPlayer()) {
startMoney = msg.getStartMoney();
}
if (player != null) { if (player != null) {
player.setName(msg.getName()); player.setName(msg.getName());
playerHandler.setPlayerReady(player, true); playerHandler.setPlayerReady(player, true);
@ -187,6 +192,7 @@ public class ServerGameLogic implements ClientInterpreter {
} }
if(playerHandler.allPlayersReady()) { if(playerHandler.allPlayersReady()) {
playerHandler.setStartBalance(startMoney);
for (Player p : playerHandler.getPlayers()) { for (Player p : playerHandler.getPlayers()) {
send(p, new GameStart(playerHandler.getPlayers())); send(p, new GameStart(playerHandler.getPlayers()));
} }

View File

@ -4,9 +4,10 @@ package pp.monopoly.message.client;
* Represents a message indicating the player is ready to play. * Represents a message indicating the player is ready to play.
*/ */
public class PlayerReady extends ClientMessage { public class PlayerReady extends ClientMessage {
private boolean isReady; private final boolean isReady;
private String name; private final String name;
private String figure; private final String figure;
private final int startMoney;
/** /**
* Constructs a PlayerReady message. * Constructs a PlayerReady message.
@ -15,10 +16,11 @@ public class PlayerReady extends ClientMessage {
* @param name the name of the player * @param name the name of the player
* @param color the color of the player (can be null) * @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.isReady = isReady;
this.name = name; this.name = name;
this.figure = figure; this.figure = figure;
this.startMoney = startMoney;
} }
public String getName() { public String getName() {
@ -33,6 +35,10 @@ public class PlayerReady extends ClientMessage {
return isReady; return isReady;
} }
public int getStartMoney() {
return startMoney;
}
@Override @Override
public void accept(ClientInterpreter interpreter, int from) { public void accept(ClientInterpreter interpreter, int from) {
interpreter.received(this, from); interpreter.received(this, from);