Client connect to server

This commit is contained in:
Johannes Schmelz
2024-11-25 02:46:03 +00:00
parent 687d1621fc
commit 5268f75e17
51 changed files with 1183 additions and 893 deletions

View File

@@ -28,7 +28,7 @@ public class MonopolyConfig extends Config {
* The default port number for the Monopoly server.
*/
@Property("port")
private int port = 4321;
private int port = 42069;
/**
* The width of the game map in terms of grid units.

View File

@@ -5,7 +5,7 @@ import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.List;
import pp.monopoly.game.server.PlayerHandler;
import pp.monopoly.game.server.Player;
import pp.monopoly.message.client.ClientMessage;
import pp.monopoly.message.server.BuyPropertyResponse;
import pp.monopoly.message.server.DiceResult;
@@ -22,6 +22,7 @@ import pp.monopoly.message.server.TradeRequest;
import pp.monopoly.message.server.ViewAssetsResponse;
import pp.monopoly.model.Board;
import pp.monopoly.model.IntPoint;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.notification.ClientStateEvent;
import pp.monopoly.notification.GameEvent;
import pp.monopoly.notification.GameEventBroker;
@@ -51,7 +52,9 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
/** The current state of the client game logic. */
private ClientState state = new LobbyState(this);
private static PlayerHandler playerHandler;
private List<Player> players;
private BoardManager boardManager = new BoardManager();
/**
* Constructs a ClientGameLogic with the specified sender object.
@@ -62,6 +65,14 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
this.clientSender = clientSender;
}
/**
* Reutns the BoardManager
* @return the boardManager
*/
public BoardManager getBoardManager() {
return boardManager;
}
/**
* Returns the current state of the game logic.
*
@@ -83,8 +94,8 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
state.entry();
}
public static PlayerHandler getPlayerHandler() {
return playerHandler;
public List<Player> getPlayers() {
return players;
}
/**
@@ -128,11 +139,12 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*
* @param msg the message to be sent
*/
void send(ClientMessage msg) {
public void send(ClientMessage msg) {
if (clientSender == null) {
LOGGER.log(Level.ERROR, "trying to send {0} with sender==null", msg); //NON-NLS
} else {
clientSender.send(msg);
System.out.println("Message gesendet");
}
}
@@ -243,7 +255,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(GameStart msg) {
playerHandler = msg.getPlayerHandler();
players = msg.getPlayers();
setInfoText("The game has started! Good luck!");
setState(new WaitForTurnState(this));
}

View File

@@ -10,6 +10,8 @@ package pp.monopoly.game.server;
import java.util.List;
import java.util.Random;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.model.FieldVisitor;
import pp.monopoly.model.Figure;
@@ -28,6 +30,7 @@ import pp.monopoly.model.fields.WacheField;
/**
* Class representing a player
*/
@Serializable
public class Player implements FieldVisitor<Void>{
private final int id;
private String name;
@@ -40,6 +43,14 @@ public class Player implements FieldVisitor<Void>{
private final PlayerHandler handler;
private PlayerState state = new LobbyState();
/**
* Default constructor for serialization purposes.
*/
private Player(){
id = 0;
handler = null;
}
/**
* Constructs a player with the speciefied params
* @param id the id of the player

View File

@@ -8,10 +8,13 @@ import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.model.LimitedLinkedList;
/**
* A class for helping with player actions and managing thier turns
*/
@Serializable
public class PlayerHandler {
private List<Player> players = new LimitedLinkedList<>(6);
private Set<Player> readyPlayers = new HashSet<>();
@@ -19,6 +22,11 @@ public class PlayerHandler {
private Player hostPlayer;
private Player extra = null;
/**
* Default constructor for serialization purposes.
*/
private PlayerHandler() {}
/**
* Contructs a PlayerHandler
* @param logic the {@link ServerGameLogic} this PlayerHandler is a part of

View File

@@ -204,7 +204,7 @@ public class ServerGameLogic implements ClientInterpreter {
if(playerHandler.allPlayersReady()) {
playerHandler.setStartBalance(startMoney);
for (Player p : playerHandler.getPlayers()) {
send(p, new GameStart(playerHandler));
send(p, new GameStart(playerHandler.getPlayers()));
}
playerHandler.randomOrder();
send(playerHandler.getPlayerAtIndex(0), new NextPlayerTurn(playerHandler.getPlayerAtIndex(0)));

View File

@@ -1,11 +1,19 @@
package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
/**
* Represents a request from a player to buy a property.
*/
@Serializable
public class BuyPropertyRequest extends ClientMessage{
private int propertyId;
/**
* Default constructor for serialization purposes.
*/
private BuyPropertyRequest() { /* empty */ }
/**
* Constructs a BuyPropertyRequest with the specified property ID.
*

View File

@@ -1,9 +1,18 @@
package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
/**
* Represents a message indicating the player wants to end their turn.
*/
@Serializable
public class EndTurn extends ClientMessage{
/**
* Default constructor for serialization purposes.
*/
public EndTurn() { /* empty */ }
@Override
public void accept(ClientInterpreter interpreter, int from) {
interpreter.received(this, from);

View File

@@ -1,13 +1,21 @@
package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
/**
* Represents a message indicating the player is ready to play.
*/
@Serializable
public class PlayerReady extends ClientMessage {
private final boolean isReady;
private final String name;
private final String figure;
private final int startMoney;
private boolean isReady;
private String name;
private String figure;
private int startMoney;
/**
* Default constructor for serialization purposes.
*/
private PlayerReady() { /* empty */ }
/**
* Constructs a PlayerReady message.

View File

@@ -1,9 +1,18 @@
package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
/**
* Represents a message requesting to roll the dice.
*/
@Serializable
public class RollDice extends ClientMessage{
/**
* Default constructor for serialization purposes.
*/
private RollDice() { /* empty */ }
@Override
public void accept(ClientInterpreter interpreter, int from) {
interpreter.received(this, from);

View File

@@ -1,14 +1,21 @@
package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.model.TradeHandler;
/**
* Represents a trade Request message from one player to another.
*/
@Serializable
public class TradeOffer extends ClientMessage{
private int receiverId;
private TradeHandler tradehandler;
/**
* Default constructor for serialization purposes.
*/
private TradeOffer() { /* empty */ }
/**
* Constructs a TradeOffer with the specified details.

View File

@@ -1,14 +1,22 @@
package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.model.TradeHandler;
/**
* Represents a response to a trade offer.
*/
@Serializable
public class TradeResponse extends ClientMessage{
private int initiatorId;
private TradeHandler tradeHandler;
/**
* Default constructor for serialization purposes.
*/
private TradeResponse() { /* empty */ }
/**
* Constructs a TradeResponse with the specified response details.
*

View File

@@ -1,13 +1,21 @@
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 final Player player;
private Player player;
/**
* Default constructor for serialization purposes.
*/
private ViewAssetsRequest() { /* empty */ }
public ViewAssetsRequest(Player player) {
this.player = player;

View File

@@ -1,12 +1,20 @@
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 final boolean successful;
private final String propertyName;
private final String reason; // Reason for failure, if any
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;

View File

@@ -2,10 +2,18 @@ package pp.monopoly.message.server;
import java.util.List;
import com.jme3.network.serializing.Serializable;
@Serializable
public class DiceResult extends ServerMessage{
private List<Integer> rollResult;
/**
* Default constructor for serialization purposes.
*/
private DiceResult() { /* empty */ }
public DiceResult(List<Integer> rollResult) {
this.rollResult = rollResult;
}

View File

@@ -1,7 +1,15 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
@Serializable
public class EventDrawCard extends ServerMessage{
private final String cardDescription;
private String cardDescription;
/**
* Default constructor for serialization purposes.
*/
private EventDrawCard() { /* empty */ }
public EventDrawCard(String cardDescription) {
this.cardDescription = cardDescription;

View File

@@ -1,7 +1,15 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
@Serializable
public class GameOver extends ServerMessage{
private final boolean isWinner;
private boolean isWinner;
/**
* Default constructor for serialization purposes.
*/
private GameOver() { /* empty */ }
public GameOver(boolean isWinner) {
this.isWinner = isWinner;

View File

@@ -1,17 +1,27 @@
package pp.monopoly.message.server;
import pp.monopoly.game.server.PlayerHandler;
import java.util.List;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.server.Player;
@Serializable
public class GameStart extends ServerMessage{
private final PlayerHandler ph;
private List<Player> players;
public GameStart(PlayerHandler ph) {
this.ph = ph;
/**
* Default constructor for serialization purposes.
*/
private GameStart() { /* empty */ }
public GameStart(List<Player> players) {
this.players = players;
}
public PlayerHandler getPlayerHandler() {
return ph;
public List<Player> getPlayers() {
return players;
}
@Override

View File

@@ -1,8 +1,16 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
@Serializable
public class JailEvent extends ServerMessage{
private final boolean goingToJail;
private boolean goingToJail;
/**
* Default constructor for serialization purposes.
*/
private JailEvent() { /* empty */ }
public JailEvent(boolean goingToJail) {
this.goingToJail = goingToJail;

View File

@@ -1,10 +1,18 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.server.Player;
@Serializable
public class NextPlayerTurn extends ServerMessage{
private final Player player;
private Player player;
/**
* Default constructor for serialization purposes.
*/
private NextPlayerTurn() { /* empty */ }
public NextPlayerTurn(Player player) {
this.player = player;

View File

@@ -1,12 +1,20 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.server.PlayerColor;
@Serializable
public class PlayerStatusUpdate extends ServerMessage{
private final String playerName;
private final String status;
private final PlayerColor color;
private String playerName;
private String status;
private PlayerColor color;
/**
* Default constructor for serialization purposes.
*/
private PlayerStatusUpdate() { /* empty */ }
public PlayerStatusUpdate(String playerName, String status, PlayerColor color) {
this.playerName = playerName;

View File

@@ -1,8 +1,16 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
@Serializable
public class TimeOutWarning extends ServerMessage{
private final int remainingTime;
private int remainingTime;
/**
* Default constructor for serialization purposes.
*/
private TimeOutWarning() { /* empty */ }
public TimeOutWarning(int remainingTime) {
this.remainingTime = remainingTime;

View File

@@ -1,14 +1,22 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.model.TradeHandler;
/**
* Represents a response to a trade offer.
*/
@Serializable
public class TradeReply extends ServerMessage{
private int initiatorId;
private TradeHandler tradeHandler;
/**
* Default constructor for serialization purposes.
*/
private TradeReply() { /* empty */ }
/**
* Constructs a TradeResponse with the specified response details.
*

View File

@@ -1,15 +1,23 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.model.TradeHandler;
/**
* Represents a trade Request message from one player to another.
*/
@Serializable
public class TradeRequest extends ServerMessage{
private int receiverId;
private TradeHandler tradehandler;
/**
* Default constructor for serialization purposes.
*/
private TradeRequest() { /* empty */ }
/**
* Constructs a TradeRequest with the specified details.
*

View File

@@ -2,18 +2,26 @@ 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.
*/
@Serializable
public class ViewAssetsResponse extends ServerMessage{
private final List<PropertyField> properties;
private final BoardManager board;
private final int accountBalance;
private final int jailCards;
private List<PropertyField> properties;
private BoardManager board;
private int accountBalance;
private int jailCards;
/**
* Default constructor for serialization purposes.
*/
private ViewAssetsResponse() { /* empty */ }
/**
* Constructs a ViewAssetsResponse with the specified properties and account balance.

View File

@@ -4,9 +4,12 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import com.jme3.network.serializing.Serializable;
import static java.lang.Math.max;
import static java.lang.Math.min;
@Serializable
public class Figure implements Item{
private final String type;
private final int length; // The length of the Figure

View File

@@ -2,14 +2,22 @@ package pp.monopoly.model;
import java.util.LinkedList;
import com.jme3.network.serializing.Serializable;
/**
* A LinkedList with a maximum size limit.
*
* @param <E> the type of elements held in this collection
*/
@Serializable
public class LimitedLinkedList<E> extends LinkedList<E> {
private final int maxSize;
private int maxSize;
/**
* Default constructor for serialization purposes.
*/
private LimitedLinkedList() {}
/**
* Constructs a LimitedLinkedList with the specified maximum size.