This commit is contained in:
Luca Puderbach
2024-11-29 04:41:14 +01:00
28 changed files with 1148 additions and 702 deletions

View File

@@ -5,10 +5,9 @@ import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.List;
import pp.monopoly.game.server.Player;
import pp.monopoly.game.server.PlayerHandler;
import pp.monopoly.message.client.ClientMessage;
import pp.monopoly.message.server.BuyPropertyResponse;
import pp.monopoly.message.server.BuyPropertyRequest;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.message.server.EventDrawCard;
import pp.monopoly.message.server.GameOver;
@@ -26,11 +25,13 @@ import pp.monopoly.model.IntPoint;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.notification.ClientStateEvent;
import pp.monopoly.notification.DiceRollEvent;
import pp.monopoly.notification.ButtonStatusEvent;
import pp.monopoly.notification.EventCardEvent;
import pp.monopoly.notification.GameEvent;
import pp.monopoly.notification.GameEventBroker;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.InfoTextEvent;
import pp.monopoly.notification.PopUpEvent;
import pp.monopoly.notification.Sound;
import pp.monopoly.notification.SoundEvent;
import pp.monopoly.notification.UpdatePlayerView;
@@ -60,6 +61,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
private BoardManager boardManager = new BoardManager();
/**
* Constructs a ClientGameLogic with the specified sender object.
*
@@ -194,21 +196,6 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
state.update(delta);
}
/**
* Handles the response for buying a property.
*
* @param msg the message containing the buy property response
*/
@Override
public void received(BuyPropertyResponse msg) {
if (msg.isSuccessful()) {
playSound(Sound.MONEY_LOST);
} else {
}
}
/**
* Handles the result of a dice roll.
*
@@ -258,7 +245,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
public void received(GameStart msg) {
playerHandler = msg.getPlayerHandler();
setState(new WaitForTurnState(this));
notifyListeners(new ButtonStatusEvent(false));
}
/**
@@ -304,7 +291,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(ViewAssetsResponse msg) {
boardManager = msg.getboard();
}
/**
@@ -330,9 +317,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(TradeRequest msg) {
// playSound(Sound.TRADE_REQUEST); no sound effect
// notifyListeners();
}
/**
@@ -342,7 +327,12 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(NextPlayerTurn msg) {
notifyListeners(new ButtonStatusEvent(true));
setState(new ActiveState(this));
}
@Override
public void received(BuyPropertyRequest msg) {
notifyListeners(new PopUpEvent());
}
}

View File

@@ -13,8 +13,10 @@ import java.util.Random;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.message.server.BuyPropertyRequest;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.message.server.EventDrawCard;
import pp.monopoly.message.server.NextPlayerTurn;
import pp.monopoly.message.server.PlayerStatusUpdate;
import pp.monopoly.model.FieldVisitor;
import pp.monopoly.model.Figure;
@@ -39,12 +41,14 @@ public class Player implements FieldVisitor<Void>{
private String name;
private int accountBalance = 15000;
private Figure figure;
private List<PropertyField> properties = new ArrayList<>();
private List<Integer> properties = new ArrayList<>();
private int getOutOfJailCard;
private int fieldID;
private DiceResult rollResult;
private transient final PlayerHandler handler;
private transient PlayerState state = new LobbyState();
private transient PlayerState state = new WaitForTurnState();
private int doubletscounter = 0;
private boolean mayRollDice = false;
/**
* Default constructor for serialization purposes.
@@ -131,6 +135,8 @@ public class Player implements FieldVisitor<Void>{
}
void setActive() {
state = new ActiveState();
doubletscounter = 0;
mayRollDice = true;
}
boolean finishTurn() {
@@ -161,20 +167,37 @@ public class Player implements FieldVisitor<Void>{
*/
public int movePos(int position){
fieldID = fieldID+position;
System.out.println("Aktuelle Position" +fieldID);
System.err.println("Würfelergebniss"+ rollResult.calcTotal());
if(fieldID >= 40) {
fieldID = fieldID%40;
earnMoney(2000);
}
figure.moveTo(fieldID);
getHandler().getLogic().send(this, new PlayerStatusUpdate(handler));
handler.getLogic().getBoardManager().getFieldAtIndex(fieldID).accept(this);
return fieldID;
}
/**
* Sets the player to the specified Position on the board
* @param position the position to move to
* @return the new position
*/
public int setPosition(int position){
if(position < 40 && position > 0) {
fieldID = position;
figure.moveTo(fieldID);
handler.getLogic().getBoardManager().getFieldAtIndex(fieldID).accept(this);
}
return fieldID;
}
/**
* Gets all the properties owned by this player
* @return List of all properties owned by this player
*/
public List<PropertyField> getProperties() {
public List<Integer> getProperties() {
return properties;
}
@@ -185,7 +208,7 @@ public class Player implements FieldVisitor<Void>{
*/
public void buyProperty(PropertyField property) {
if (property.getOwner() == null && accountBalance >= property.getPrice()) {
properties.add(property);
properties.add(property.getId());
property.setOwner(this);
pay(property.getPrice());
}
@@ -284,30 +307,41 @@ public class Player implements FieldVisitor<Void>{
@Override
public Void visit(BuildingProperty field) {
int rent = field.calcRent();
field.getOwner().earnMoney(rent);
pay(rent);
if(field.getOwner() == null) {
getHandler().getLogic().send(this, new BuyPropertyRequest());
} else if (field.getOwner() != this){
int rent = field.calcRent();
field.getOwner().earnMoney(rent);
pay(rent);
}
return null;
}
@Override
public Void visit(FoodField field) {
int factor = 4;
if (field.getOwner().getNumProp(field) == 2) {
factor = 10;
if(field.getOwner() == null) {
getHandler().getLogic().send(this, new BuyPropertyRequest());
} else {
int factor = 4;
if (field.getOwner().getNumProp(field) == 2) {
factor = 10;
}
field.getOwner().earnMoney(rollResult.calcTotal()*factor);
pay(rollResult.calcTotal()*factor);
}
field.getOwner().earnMoney(rollResult.calcTotal()*factor);
pay(rollResult.calcTotal()*factor);
return null;
}
@Override
public Void visit(GateField field) {
int rent = field.calcRent() * field.getOwner().getNumProp(field);
if(field.getOwner() == null) {
getHandler().getLogic().send(this, new BuyPropertyRequest());
} else {
int rent = field.calcRent() * field.getOwner().getNumProp(field);
field.getOwner().earnMoney(rent);
pay(rent);
field.getOwner().earnMoney(rent);
pay(rent);
}
return null;
}
@@ -334,15 +368,13 @@ public class Player implements FieldVisitor<Void>{
@Override
public Void visit(WacheField field) {
movePos(10);
setPosition(10);
return null;
}
@Override
public Void visit(GoField field) {
earnMoney(2000);
GulagField res = (GulagField) handler.getLogic().getBoardManager().getFieldAtIndex(10);
res.accept(this);
return null;
}
@@ -355,6 +387,16 @@ public class Player implements FieldVisitor<Void>{
return null;
}
public List<PropertyField> getPropertyFields() {
List<PropertyField> properties = new ArrayList<>();
for (Integer i : this.properties) {
properties.add((PropertyField)getHandler().getLogic().getBoardManager().getFieldAtIndex(i));
}
return properties;
}
/**
* Return the number of Properties of the speciefied fild type
* @param field the type of field to search for
@@ -362,7 +404,9 @@ public class Player implements FieldVisitor<Void>{
*/
public int getNumProp(PropertyField field) {
int count = 0;
for (PropertyField propertyField : properties) {
if (properties.isEmpty()) return 0;
for (PropertyField propertyField : getPropertyFields()) {
if (propertyField.getClass() == field.getClass()) {
count++;
}
@@ -373,7 +417,7 @@ public class Player implements FieldVisitor<Void>{
public int getNumHouses() {
int total = 0;
for (PropertyField field : properties) {
for (PropertyField field : getPropertyFields()) {
if (field.getClass() == BuildingProperty.class) {
total += ((BuildingProperty) field).getHouses();
}
@@ -383,7 +427,7 @@ public class Player implements FieldVisitor<Void>{
public int getNumHotels() {
int total = 0;
for (PropertyField field : properties) {
for (PropertyField field : getPropertyFields()) {
if (field.getClass() == BuildingProperty.class) {
total += ((BuildingProperty) field).getHotel();
}
@@ -414,11 +458,21 @@ public class Player implements FieldVisitor<Void>{
* @return a List of two integers representing the dice roll results
*/
DiceResult rollDice() {
return state.rollDice();
}
private void visitEvent() {
getHandler().getLogic().getBoardManager().getFieldAtIndex(36).accept(this);
if (mayRollDice) {
state.rollDice();
}
if (rollResult.isDoublets()) {
doubletscounter++;
mayRollDice = true;
getHandler().getLogic().send(this, new NextPlayerTurn());
setName(name);
if (doubletscounter >= 3) {
setPosition(10);
}
} else {
mayRollDice = false;
}
return rollResult;
}
/**
@@ -453,6 +507,8 @@ public class Player implements FieldVisitor<Void>{
public DiceResult rollDice() {
List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice());
rollResult = new DiceResult(roll.get(0), roll.get(1));
System.out.println(rollResult.calcTotal());
move(rollResult.calcTotal());
return rollResult;
}
@@ -468,31 +524,6 @@ public class Player implements FieldVisitor<Void>{
}
/**
* A class to represent the Lobby PlayerState
* Set when in Lobby
*/
private class LobbyState implements PlayerState{
@Override
public DiceResult rollDice() {
//do nothing
return null;
}
@Override
public void payBail() {
//do nothing
}
@Override
public void useJailCard() {
// do nothing
}
}
/**
* A class to represent the Jailed PlayerState
* Set when in Gulag

View File

@@ -4,7 +4,7 @@ import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import pp.monopoly.MonopolyConfig;
import pp.monopoly.message.client.BuyPropertyRequest;
import pp.monopoly.message.client.BuyPropertyResponse;
import pp.monopoly.message.client.ClientInterpreter;
import pp.monopoly.message.client.EndTurn;
import pp.monopoly.message.client.PlayerReady;
@@ -19,7 +19,6 @@ import pp.monopoly.message.server.ServerMessage;
import pp.monopoly.message.server.TradeReply;
import pp.monopoly.message.server.TradeRequest;
import pp.monopoly.message.server.ViewAssetsResponse;
import pp.monopoly.model.Board;
import pp.monopoly.model.Figure;
import pp.monopoly.model.Rotation;
import pp.monopoly.model.card.DeckHelper;
@@ -144,19 +143,14 @@ public class ServerGameLogic implements ClientInterpreter {
* @param from the connection ID of the player who sent the request
*/
@Override
public void received(BuyPropertyRequest msg, int from) {
public void received(BuyPropertyResponse msg, int from) {
Player player = playerHandler.getPlayerById(from);
if (player != null && state == ServerState.INGAME) {
PropertyField property = (PropertyField) boardManager.getFieldAtIndex(player.move(0)); // Assuming player position for property
if (player != null) {
PropertyField property = (PropertyField) boardManager.getFieldAtIndex(player.getFieldID()); // Assuming player position for property
if (property.getOwner() == null && player.getAccountBalance() >= property.getPrice()) {
player.buyProperty(property);
property.setOwner(player);
player.earnMoney(-property.getPrice());
LOGGER.log(Level.INFO, "Player {0} bought property {1}", player.getName(), property.getName());
} else {
LOGGER.log(Level.WARNING, "Player {0} cannot buy property {1}", player.getName(), property.getName());
}
player.buyProperty(property);
System.out.println("Properties:" +player.getProperties().toString());
LOGGER.log(Level.INFO, "Player {0} bought property {1}", player.getName(), property.getName());
}
}
@@ -169,12 +163,14 @@ public class ServerGameLogic implements ClientInterpreter {
@Override
public void received(EndTurn msg, int from) {
Player player = playerHandler.getPlayerById(from);
if (player != null && state == ServerState.INGAME) {
if (player != null && player == playerHandler.getPlayerAtIndex(0)) {
if (player.finishTurn()) {
LOGGER.log(Level.DEBUG, "Ending turn for player {0}", player.getName());
Player next = playerHandler.nextPlayer();
next.setActive();
send(next, new NextPlayerTurn());
send(next, new PlayerStatusUpdate(playerHandler));
send(player, new PlayerStatusUpdate(playerHandler));
}
}
}
@@ -267,11 +263,12 @@ public class ServerGameLogic implements ClientInterpreter {
@Override
public void received(ViewAssetsRequest msg, int from) {
Player sender = playerHandler.getPlayerById(from);
Player player = msg.getPlayer();
if (sender != null && player != null) {
LOGGER.log(Level.DEBUG, "Processing ViewAssetsRequest for player {0}", sender.getName());
send(sender, new ViewAssetsResponse(boardManager, player.getProperties(), player.getAccountBalance(), player.getNumJailCard()));
if (sender != null) {
LOGGER.log(Level.DEBUG, "Processing ViewAssetsRequest for player {0}", sender.getName());
send(sender, new ViewAssetsResponse(boardManager));
for (Player player : playerHandler.getPlayers()) {
send(player, new PlayerStatusUpdate(playerHandler));
}
}
}

View File

@@ -6,14 +6,14 @@ import com.jme3.network.serializing.Serializable;
* Represents a request from a player to buy a property.
*/
@Serializable
public class BuyPropertyRequest extends ClientMessage{
public class BuyPropertyResponse extends ClientMessage{
/**
* Constructs a BuyPropertyRequest with the specified property ID.
*
* @param propertyId the ID of the property to buy
*/
public BuyPropertyRequest() {}
public BuyPropertyResponse() {}
@Override

View File

@@ -17,7 +17,7 @@ public interface ClientInterpreter {
* @param msg the BuyPropertyRequest to be processed
* @param from the connection ID from which the message was received
*/
void received(BuyPropertyRequest msg, int from);
void received(BuyPropertyResponse msg, int from);
/**
* Processes a received EndTurn.

View File

@@ -2,23 +2,13 @@ package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.server.Player;
/**
* Represents a request from a player to view their assets.
*/
@Serializable
public class ViewAssetsRequest extends ClientMessage{
private Player player;
/**
* Default constructor for serialization purposes.
*/
private ViewAssetsRequest() { /* empty */ }
public ViewAssetsRequest(Player player) {
this.player = player;
public ViewAssetsRequest() {
}
@Override
@@ -26,8 +16,4 @@ public class ViewAssetsRequest extends ClientMessage{
interpreter.received(this, from);
}
public Player getPlayer() {
return player;
}
}

View File

@@ -0,0 +1,21 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
@Serializable
public class BuyPropertyRequest extends ServerMessage{
public BuyPropertyRequest(){}
@Override
public void accept(ServerInterpreter interpreter) {
interpreter.received(this);
}
@Override
public String getInfoTextKey() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getInfoTextKey'");
}
}

View File

@@ -1,47 +0,0 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* Represents the server's response to a player's request to buy a property.
*/
@Serializable
public class BuyPropertyResponse extends ServerMessage{
private boolean successful;
private String propertyName;
private String reason; // Reason for failure, if any
/**
* Default constructor for serialization purposes.
*/
private BuyPropertyResponse() { /* empty */ }
public BuyPropertyResponse(boolean successful, String propertyName, String reason) {
this.successful = successful;
this.propertyName = propertyName;
this.reason = reason;
}
public boolean isSuccessful() {
return successful;
}
public String getPropertyName() {
return propertyName;
}
public String getReason() {
return reason;
}
@Override
public void accept(ServerInterpreter interpreter) {
interpreter.received(this);
}
@Override
public String getInfoTextKey() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getInfoTextKey'");
}
}

View File

@@ -13,13 +13,6 @@ package pp.monopoly.message.server;
*/
public interface ServerInterpreter {
/**
* Handles a BuyPropertyResponse message received from the server.
*
* @param msg the BuyPropertyResponse message received
*/
void received(BuyPropertyResponse msg);
/**
* Handles a DiceResult message received from the server.
*
@@ -96,4 +89,11 @@ public interface ServerInterpreter {
* @param msg the NextPlayerTurn message received
*/
void received(NextPlayerTurn msg);
/**
* Handles a NextPlayerTurn message received from the server.
*
* @param msg the NextPlayerTurn message received
*/
void received(BuyPropertyRequest msg);
}

View File

@@ -1,11 +1,9 @@
package pp.monopoly.message.server;
import java.util.List;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.PropertyField;
/**
* Represents a response containing the player's assets.
@@ -13,10 +11,7 @@ import pp.monopoly.model.fields.PropertyField;
@Serializable
public class ViewAssetsResponse extends ServerMessage{
private List<PropertyField> properties;
private BoardManager board;
private int accountBalance;
private int jailCards;
/**
* Default constructor for serialization purposes.
@@ -29,11 +24,8 @@ public class ViewAssetsResponse extends ServerMessage{
* @param properties a List of PropertyField objects representing the player's properties
* @param accountBalance the player's current account balance
*/
public ViewAssetsResponse(BoardManager board, List<PropertyField> properties, int accountBalance, int jailCards) {
public ViewAssetsResponse(BoardManager board) {
this.board = board;
this.properties = properties;
this.accountBalance = accountBalance;
this.jailCards = jailCards;
}
@Override
@@ -47,18 +39,6 @@ public class ViewAssetsResponse extends ServerMessage{
throw new UnsupportedOperationException("Unimplemented method 'getInfoTextKey'");
}
public List<PropertyField> getProperties() {
return properties;
}
public int getAccountBalance() {
return accountBalance;
}
public int getJailCards() {
return jailCards;
}
public BoardManager getboard() {
return board;
}

View File

@@ -4,9 +4,12 @@ import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import com.jme3.network.serializing.Serializable;
/**
* Simple Manager class responsible for managing the GameBoard of Monopoly
*/
@Serializable
public class BoardManager {
private List<Field> board;
@@ -92,4 +95,12 @@ public class BoardManager {
public List<Field> getBoard() {
return board;
}
public List<PropertyField> getPropertyFields(List<Integer> source) {
List<PropertyField> properties = new ArrayList<>();
for (Integer i : source) {
properties.add((PropertyField)getFieldAtIndex(i));
}
return properties;
}
}

View File

@@ -0,0 +1,13 @@
package pp.monopoly.notification;
public record ButtonStatusEvent(boolean buttonsEnabled) implements GameEvent{
/**
* Notifies the game event listener of this event.
*
* @param listener the game event listener
*/
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);
}
}

View File

@@ -66,4 +66,18 @@ public interface GameEventListener {
* @param event the received event
*/
default void receivedEvent(EventCardEvent event) { /*Do nothing */}
/**
* Indicates that all buttons in the toolbar should be disabled
*
* @param event the received event
*/
default void receivedEvent(ButtonStatusEvent event) { /*Do nothing */}
/**
* Indicates that all buttons in the toolbar should be disabled
*
* @param event the received event
*/
default void receivedEvent(PopUpEvent event) { /*Do nothing */}
}

View File

@@ -0,0 +1,10 @@
package pp.monopoly.notification;
public record PopUpEvent() implements GameEvent{
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);
}
}