added rent payment logic

added player on field logic
This commit is contained in:
Johannes Schmelz 2024-11-14 23:50:06 +01:00
parent 232e3a117c
commit c5ad476eaf
9 changed files with 205 additions and 59 deletions

View File

@ -13,6 +13,7 @@ import com.jme3.math.ColorRGBA;
import pp.monopoly.model.FieldVisitor; import pp.monopoly.model.FieldVisitor;
import pp.monopoly.model.Figure; import pp.monopoly.model.Figure;
import pp.monopoly.model.card.DeckHelper;
import pp.monopoly.model.fields.BuildingProperty; import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.model.fields.EventField; import pp.monopoly.model.fields.EventField;
import pp.monopoly.model.fields.FineField; import pp.monopoly.model.fields.FineField;
@ -28,6 +29,7 @@ import pp.monopoly.model.fields.WacheField;
* Class representing a player * Class representing a player
*/ */
public class Player implements FieldVisitor<Void>{ public class Player implements FieldVisitor<Void>{
private final int id;
private String name; private String name;
private ColorRGBA color; private ColorRGBA color;
private int accountBalance = 0; private int accountBalance = 0;
@ -35,15 +37,25 @@ public class Player implements FieldVisitor<Void>{
private List<PropertyField> properties; private List<PropertyField> properties;
private int getOutOfJailCard; private int getOutOfJailCard;
private int fieldID; 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.name = name;
this.color = color; this.id = id;
figure = new Figure(); this.handler = handler;
} }
public int move(int steps){ 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) { public void buyProperty(PropertyField property) {
@ -79,20 +91,31 @@ public class Player implements FieldVisitor<Void>{
@Override @Override
public Void visit(BuildingProperty field) { public Void visit(BuildingProperty field) {
// TODO Auto-generated method stub int rent = field.calcRent();
throw new UnsupportedOperationException("Unimplemented method 'visit'");
field.getOwner().earnMoney(rent);
payRent(rent);
return null;
} }
@Override @Override
public Void visit(FoodField field) { public Void visit(FoodField field) {
// TODO Auto-generated method stub int factor = 4;
throw new UnsupportedOperationException("Unimplemented method 'visit'"); if (field.getOwner().getNumProp(field) == 2) {
factor = 10;
}
field.getOwner().earnMoney(rollResult*factor);
payRent(rollResult*factor);
return null;
} }
@Override @Override
public Void visit(GateField field) { public Void visit(GateField field) {
// TODO Auto-generated method stub int rent = field.calcRent() * field.getOwner().getNumProp(field);
throw new UnsupportedOperationException("Unimplemented method 'visit'");
field.getOwner().earnMoney(rent);
payRent(rent);
return null;
} }
@Override @Override
@ -103,25 +126,27 @@ public class Player implements FieldVisitor<Void>{
@Override @Override
public Void visit(TestStreckeField field) { public Void visit(TestStreckeField field) {
// TODO Auto-generated method stub earnMoney(field.collectMoney());
throw new UnsupportedOperationException("Unimplemented method 'visit'"); return null;
} }
@Override @Override
public Void visit(EventField field) { public Void visit(EventField field) {
// TODO Auto-generated method stub DeckHelper.drawCard();
throw new UnsupportedOperationException("Unimplemented method 'visit'"); return null;
} }
@Override @Override
public Void visit(WacheField field) { public Void visit(WacheField field) {
// TODO Auto-generated method stub movePos(10);
throw new UnsupportedOperationException("Unimplemented method 'visit'");
return null;
} }
@Override @Override
public Void visit(GoField field) { public Void visit(GoField field) {
accountBalance += 4000; accountBalance += 4000;
visit((GulagField)handler.getLogic().getBoardManager().getFieldAtIndex(10));
return null; return null;
} }
@ -130,4 +155,15 @@ public class Player implements FieldVisitor<Void>{
// TODO Auto-generated method stub // TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visit'"); 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;
}
} }

View File

@ -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<Player> players = new LinkedList<>();
private Set<Player> 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<Player> 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;
}
}

View File

@ -17,15 +17,13 @@ import pp.monopoly.message.client.TradeOffer;
import pp.monopoly.message.client.TradeResponse; import pp.monopoly.message.client.TradeResponse;
import pp.monopoly.message.client.ViewAssetsRequest; import pp.monopoly.message.client.ViewAssetsRequest;
import pp.monopoly.message.server.ServerMessage; import pp.monopoly.message.server.ServerMessage;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.IntPoint;
import java.lang.System.Logger; import java.lang.System.Logger;
import java.lang.System.Logger.Level; import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Random;
/** /**
* Controls the server-side game logic for Monopoly. * 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 static final Logger LOGGER = System.getLogger(ServerGameLogic.class.getName());
private final MonopolyConfig config; private final MonopolyConfig config;
private final List<Player> players = new ArrayList<>(2); private final PlayerHandler playerHandler = new PlayerHandler(this);
private final Set<Player> readyPlayers = new HashSet<>();
private final ServerSender serverSender; private final ServerSender serverSender;
private Player activePlayer; private ServerState state = ServerState.CREATEGAME;
private ServerState state = ServerState.WAIT; private Player hostPlayer;
private BoardManager boardManager = new BoardManager();
/** /**
* Constructs a ServerGameLogic with the specified sender and configuration. * Constructs a ServerGameLogic with the specified sender and configuration.
@ -52,6 +50,27 @@ public class ServerGameLogic implements ClientInterpreter {
this.config = config; 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<Integer> rollDice() {
return List.of(Dice.rollDice(), Dice.rollDice());
}
/** /**
* Returns the state of the game. * Returns the state of the game.
*/ */
@ -59,6 +78,8 @@ public class ServerGameLogic implements ClientInterpreter {
return state; return state;
} }
/** /**
* Sets the new state of the game and logs the state transition. * Sets the new state of the game and logs the state transition.
* *
@ -69,20 +90,6 @@ public class ServerGameLogic implements ClientInterpreter {
state = newState; 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. * 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 * @return the player associated with the client ID, or null if not found
*/ */
public Player getPlayerById(int id) { public Player getPlayerById(int id) {
// TODO Auto-generated method stub return playerHandler.getPlayerAtIndex(id);
throw new UnsupportedOperationException("Unimplemented method");
} }
/** /**
@ -107,15 +113,15 @@ public class ServerGameLogic implements ClientInterpreter {
} }
/** /**
* Adds a new player to the game if there are less than two players. * Adds a new player to the game if there are less than six players.
* Transitions the state to SET_UP if two players are present. *
* *
* @param id the connection ID of the new player * @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 * @return the player added to the game, or null if the game is not in the right state
*/ */
public Player addPlayer(int id) { public Player addPlayer(Player player) {
// TODO Auto-generated method stub playerHandler.addPlayer(player);
throw new UnsupportedOperationException("Unimplemented method"); return player;
} }
/** /**
@ -124,9 +130,8 @@ public class ServerGameLogic implements ClientInterpreter {
* *
* @param player the player who is ready * @param player the player who is ready
*/ */
void playerReady(Player player) { void playerReady(Player player, boolean status) {
// TODO Auto-generated method stub playerHandler.setPlayerReady(player, status);
throw new UnsupportedOperationException("Unimplemented method");
} }
@Override @Override
@ -171,4 +176,7 @@ public class ServerGameLogic implements ClientInterpreter {
throw new UnsupportedOperationException("Unimplemented method 'received'"); throw new UnsupportedOperationException("Unimplemented method 'received'");
} }
public BoardManager getBoardManager() {
return boardManager;
}
} }

View File

@ -2,10 +2,27 @@ package pp.monopoly.model.fields;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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<Field> createBoard() { private List<Field> board;
/**
* Constructs a BoardManager
*/
public BoardManager() {
board = createBoard();
}
/**
* Creates a Monopoly GameBoard
* @return the List of Fields in correct Order
*/
private static List<Field> createBoard() {
ArrayList<Field> fields = new ArrayList<>(); ArrayList<Field> fields = new ArrayList<>();
fields.addLast(new GoField()); fields.addLast(new GoField());
@ -51,4 +68,24 @@ public class FieldFactory {
return fields; 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();
}
} }

View File

@ -12,7 +12,7 @@ public class BuildingProperty extends PropertyField {
} }
@Override @Override
protected int calcRent() { public int calcRent() {
if (hotel) { if (hotel) {
return (int) Math.round(rent*70/10)*10; return (int) Math.round(rent*70/10)*10;
} }

View File

@ -10,8 +10,7 @@ public class FineField extends Field{
@Override @Override
public void accept(Player player) { public void accept(Player player) {
// TODO Auto-generated method stub player.visit(this);
throw new UnsupportedOperationException("Unimplemented method 'accept'");
} }
} }

View File

@ -9,9 +9,8 @@ public class FoodField extends PropertyField {
} }
@Override @Override
protected int calcRent() { public int calcRent() {
// TODO Auto-generated method stub return 0;
throw new UnsupportedOperationException("Unimplemented method 'calcRent'");
} }
@Override @Override

View File

@ -9,7 +9,7 @@ public class GateField extends PropertyField{
} }
@Override @Override
protected int calcRent() { public int calcRent() {
return rent; return rent;
} }

View File

@ -16,7 +16,7 @@ public abstract class PropertyField extends Field {
} }
protected abstract int calcRent(); public abstract int calcRent();
public int getPrice() { public int getPrice() {
return price; return price;
@ -25,4 +25,8 @@ public abstract class PropertyField extends Field {
public int getHypo() { public int getHypo() {
return price/2; return price/2;
} }
public Player getOwner() {
return owner;
}
} }