From c5ad476eaf8fa730d8a8a32a9fd66b3ab390ee46 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Thu, 14 Nov 2024 23:50:06 +0100 Subject: [PATCH] added rent payment logic added player on field logic --- .../java/pp/monopoly/game/server/Player.java | 68 +++++++++++++---- .../monopoly/game/server/PlayerHandler.java | 63 ++++++++++++++++ .../monopoly/game/server/ServerGameLogic.java | 74 ++++++++++--------- .../{FieldFactory.java => BoardManager.java} | 41 +++++++++- .../model/fields/BuildingProperty.java | 2 +- .../pp/monopoly/model/fields/FineField.java | 3 +- .../pp/monopoly/model/fields/FoodField.java | 5 +- .../pp/monopoly/model/fields/GateField.java | 2 +- .../monopoly/model/fields/PropertyField.java | 6 +- 9 files changed, 205 insertions(+), 59 deletions(-) create mode 100644 Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java rename Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/{FieldFactory.java => BoardManager.java} (72%) 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 1802f4b..e7456f9 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 @@ -13,6 +13,7 @@ import com.jme3.math.ColorRGBA; import pp.monopoly.model.FieldVisitor; import pp.monopoly.model.Figure; +import pp.monopoly.model.card.DeckHelper; import pp.monopoly.model.fields.BuildingProperty; import pp.monopoly.model.fields.EventField; import pp.monopoly.model.fields.FineField; @@ -28,6 +29,7 @@ import pp.monopoly.model.fields.WacheField; * Class representing a player */ public class Player implements FieldVisitor{ + private final int id; private String name; private ColorRGBA color; private int accountBalance = 0; @@ -35,15 +37,25 @@ public class Player implements FieldVisitor{ private List properties; private int getOutOfJailCard; private int fieldID; + private int rollResult; + private PlayerHandler handler; - Player(String name, ColorRGBA color) { + Player(int id, String name, PlayerHandler handler) { this.name = name; - this.color = color; - figure = new Figure(); + this.id = id; + this.handler = handler; } public int move(int steps){ - return fieldID += steps; + return fieldID = (fieldID+steps)%40; + } + + public int movePos(int position){ + return fieldID = (fieldID+position)%40; + } + + public void setRollResult(int rollResult) { + this.rollResult = rollResult; } public void buyProperty(PropertyField property) { @@ -79,20 +91,31 @@ public class Player implements FieldVisitor{ @Override public Void visit(BuildingProperty field) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visit'"); + int rent = field.calcRent(); + + field.getOwner().earnMoney(rent); + payRent(rent); + return null; } @Override public Void visit(FoodField field) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visit'"); + int factor = 4; + if (field.getOwner().getNumProp(field) == 2) { + factor = 10; + } + field.getOwner().earnMoney(rollResult*factor); + payRent(rollResult*factor); + return null; } @Override public Void visit(GateField field) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visit'"); + int rent = field.calcRent() * field.getOwner().getNumProp(field); + + field.getOwner().earnMoney(rent); + payRent(rent); + return null; } @Override @@ -103,25 +126,27 @@ public class Player implements FieldVisitor{ @Override public Void visit(TestStreckeField field) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visit'"); + earnMoney(field.collectMoney()); + return null; } @Override public Void visit(EventField field) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visit'"); + DeckHelper.drawCard(); + return null; } @Override public Void visit(WacheField field) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visit'"); + movePos(10); + + return null; } @Override public Void visit(GoField field) { accountBalance += 4000; + visit((GulagField)handler.getLogic().getBoardManager().getFieldAtIndex(10)); return null; } @@ -130,4 +155,15 @@ public class Player implements FieldVisitor{ // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'visit'"); } + + public int getNumProp(PropertyField field) { + int count = 0; + for (PropertyField propertyField : properties) { + if (propertyField.getClass() == field.getClass()) { + count++; + } + } + + return count; + } } 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 new file mode 100644 index 0000000..817bb53 --- /dev/null +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java @@ -0,0 +1,63 @@ +package pp.monopoly.game.server; + +import java.util.LinkedList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class PlayerHandler { + private List players = new LinkedList<>(); + private Set readyPlayers = new HashSet<>(); + private ServerGameLogic logic; + + PlayerHandler(ServerGameLogic logic) { + this.logic = logic; + } + + PlayerHandler(ServerGameLogic logic, Player p1) { + this(logic); + players.add(p1); + } + + PlayerHandler(ServerGameLogic logic, Collection players) { + this(logic); + players.addAll(players); + } + + void setPlayerReady(Player player, boolean ready) { + if (!players.contains(player)) { + throw new IllegalArgumentException("Player does not belong to this PlayerHandler"); + } else { + if (ready) { + readyPlayers.add(player); + } else { + readyPlayers.remove(player); + } + } + } + + void addPlayer(Player player) { + if (players.contains(player)) { + throw new IllegalArgumentException("Player already registered"); + } + players.add(player); + } + + void removePlayer(Player player) { + players.remove(player); + } + + Player getPlayerAtIndex(int i) { + return players.get(i); + } + + Player nextPlayer() { + players.addLast(players.removeFirst()); + return players.getFirst(); + } + + ServerGameLogic getLogic() { + return logic; + } +} 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 31b241b..364e940 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 @@ -17,15 +17,13 @@ import pp.monopoly.message.client.TradeOffer; import pp.monopoly.message.client.TradeResponse; import pp.monopoly.message.client.ViewAssetsRequest; import pp.monopoly.message.server.ServerMessage; - -import pp.monopoly.model.IntPoint; +import pp.monopoly.model.fields.BoardManager; import java.lang.System.Logger; import java.lang.System.Logger.Level; -import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; +import java.util.Random; + /** * Controls the server-side game logic for Monopoly. @@ -35,11 +33,11 @@ public class ServerGameLogic implements ClientInterpreter { private static final Logger LOGGER = System.getLogger(ServerGameLogic.class.getName()); private final MonopolyConfig config; - private final List players = new ArrayList<>(2); - private final Set readyPlayers = new HashSet<>(); + private final PlayerHandler playerHandler = new PlayerHandler(this); private final ServerSender serverSender; - private Player activePlayer; - private ServerState state = ServerState.WAIT; + private ServerState state = ServerState.CREATEGAME; + private Player hostPlayer; + private BoardManager boardManager = new BoardManager(); /** * Constructs a ServerGameLogic with the specified sender and configuration. @@ -52,6 +50,27 @@ public class ServerGameLogic implements ClientInterpreter { this.config = config; } + /** + * Class responsible for the Dice in Monopoly + */ + private class Dice { + private static Random random = new Random(); + + private static int rollDice() { + return random.nextInt(6)+1; + } + } + + /** + * Throws the Dice. + * Generates a List with two random int in 1..6 + * + * @return a List with the two rollResults of a Dice throw + */ + List rollDice() { + return List.of(Dice.rollDice(), Dice.rollDice()); + } + /** * Returns the state of the game. */ @@ -59,6 +78,8 @@ public class ServerGameLogic implements ClientInterpreter { return state; } + + /** * Sets the new state of the game and logs the state transition. * @@ -69,20 +90,6 @@ public class ServerGameLogic implements ClientInterpreter { state = newState; } - /** - * Returns the opponent of the specified player. - * - * @param p the player - * @return the opponent of the player - */ - Player getOpponent(Player p) { - if (players.size() != 2) - throw new RuntimeException("trying to find opponent without having 2 players"); - final int index = players.indexOf(p); - if (index < 0) - throw new RuntimeException("Nonexistent player " + p); - return players.get(1 - index); - } /** * Returns the player representing the client with the specified connection ID. @@ -91,8 +98,7 @@ public class ServerGameLogic implements ClientInterpreter { * @return the player associated with the client ID, or null if not found */ public Player getPlayerById(int id) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method"); + return playerHandler.getPlayerAtIndex(id); } /** @@ -107,15 +113,15 @@ public class ServerGameLogic implements ClientInterpreter { } /** - * Adds a new player to the game if there are less than two players. - * Transitions the state to SET_UP if two players are present. + * Adds a new player to the game if there are less than six players. + * * * @param id the connection ID of the new player * @return the player added to the game, or null if the game is not in the right state */ - public Player addPlayer(int id) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method"); + public Player addPlayer(Player player) { + playerHandler.addPlayer(player); + return player; } /** @@ -124,9 +130,8 @@ public class ServerGameLogic implements ClientInterpreter { * * @param player the player who is ready */ - void playerReady(Player player) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method"); + void playerReady(Player player, boolean status) { + playerHandler.setPlayerReady(player, status); } @Override @@ -171,4 +176,7 @@ public class ServerGameLogic implements ClientInterpreter { throw new UnsupportedOperationException("Unimplemented method 'received'"); } + public BoardManager getBoardManager() { + return boardManager; + } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FieldFactory.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BoardManager.java similarity index 72% rename from Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FieldFactory.java rename to Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BoardManager.java index 306be20..ab0ee3c 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FieldFactory.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BoardManager.java @@ -2,10 +2,27 @@ package pp.monopoly.model.fields; import java.util.ArrayList; import java.util.List; +import java.util.NoSuchElementException; -public class FieldFactory { +/** + * Simple Manager class responsible for managing the GameBoard of Monopoly + */ +public class BoardManager { - public static List createBoard() { + private List board; + + /** + * Constructs a BoardManager + */ + public BoardManager() { + board = createBoard(); + } + + /** + * Creates a Monopoly GameBoard + * @return the List of Fields in correct Order + */ + private static List createBoard() { ArrayList fields = new ArrayList<>(); fields.addLast(new GoField()); @@ -51,4 +68,24 @@ public class FieldFactory { return fields; } + + + /** + * Method to find the Field at specific index + * @param index the index for which to find the field + * @return the field at the index + */ + public Field getFieldAtIndex(int index) { + return board.get(index); + } + + /** + * Method to find the index of a Monopoly field + * @param field the Field to get the Index of + * @return the Index of the field + */ + public int getIndexOfField(Field field) { + if (board.contains(field)) return field.getId(); + else throw new NoSuchElementException(); + } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BuildingProperty.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BuildingProperty.java index 1e2dcc7..ae73cb8 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BuildingProperty.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/BuildingProperty.java @@ -12,7 +12,7 @@ public class BuildingProperty extends PropertyField { } @Override - protected int calcRent() { + public int calcRent() { if (hotel) { return (int) Math.round(rent*70/10)*10; } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FineField.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FineField.java index 46cd71f..2ec3a8d 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FineField.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FineField.java @@ -10,8 +10,7 @@ public class FineField extends Field{ @Override public void accept(Player player) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); + player.visit(this); } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FoodField.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FoodField.java index efa3eb4..a3f0abf 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FoodField.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/FoodField.java @@ -9,9 +9,8 @@ public class FoodField extends PropertyField { } @Override - protected int calcRent() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'calcRent'"); + public int calcRent() { + return 0; } @Override diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/GateField.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/GateField.java index 6e8440a..ce04a4c 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/GateField.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/GateField.java @@ -9,7 +9,7 @@ public class GateField extends PropertyField{ } @Override - protected int calcRent() { + public int calcRent() { return rent; } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/PropertyField.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/PropertyField.java index 118daa5..889f614 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/PropertyField.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/fields/PropertyField.java @@ -16,7 +16,7 @@ public abstract class PropertyField extends Field { } - protected abstract int calcRent(); + public abstract int calcRent(); public int getPrice() { return price; @@ -25,4 +25,8 @@ public abstract class PropertyField extends Field { public int getHypo() { return price/2; } + + public Player getOwner() { + return owner; + } }