serialize messages

This commit is contained in:
Johannes Schmelz 2024-11-25 02:05:56 +01:00
parent 1a75d6d6a8
commit 72bef7143a
13 changed files with 119 additions and 16 deletions

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,10 +1,18 @@
package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.server.PlayerHandler;
@Serializable
public class GameStart extends ServerMessage{
private final PlayerHandler ph;
private PlayerHandler ph;
/**
* Default constructor for serialization purposes.
*/
private GameStart() { /* empty */ }
public GameStart(PlayerHandler ph) {
this.ph = ph;

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

@ -122,6 +122,13 @@ public class MonopolyServer implements MessageListener<HostedConnection>, Connec
}
private void registerListeners() {
myServer.addMessageListener(this, BuyPropertyRequest.class);
myServer.addMessageListener(this, EndTurn.class);
myServer.addMessageListener(this, PlayerReady.class);
myServer.addMessageListener(this, RollDice.class);
myServer.addMessageListener(this, TradeOffer.class);
myServer.addMessageListener(this, TradeResponse.class);
myServer.addMessageListener(this, ViewAssetsRequest.class);
myServer.addConnectionListener(this);
}