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.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<Void>{
private final int id;
private String name;
private ColorRGBA color;
private int accountBalance = 0;
@ -35,15 +37,25 @@ public class Player implements FieldVisitor<Void>{
private List<PropertyField> 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<Void>{
@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<Void>{
@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<Void>{
// 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;
}
}

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.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<Player> players = new ArrayList<>(2);
private final Set<Player> 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<Integer> 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;
}
}

View File

@ -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<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<>();
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();
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -9,7 +9,7 @@ public class GateField extends PropertyField{
}
@Override
protected int calcRent() {
public int calcRent() {
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() {
return price;
@ -25,4 +25,8 @@ public abstract class PropertyField extends Field {
public int getHypo() {
return price/2;
}
public Player getOwner() {
return owner;
}
}