From 362c0e56795a85b07d3110e6924cc136a405bb0a Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Mon, 25 Nov 2024 03:07:57 +0100 Subject: [PATCH] added serialisables --- .../pp/monopoly/game/client/ClientGameLogic.java | 10 +++++----- .../main/java/pp/monopoly/game/server/Player.java | 11 +++++++++++ .../pp/monopoly/game/server/PlayerHandler.java | 8 ++++++++ .../pp/monopoly/game/server/ServerGameLogic.java | 2 +- .../java/pp/monopoly/message/server/GameStart.java | 14 ++++++++------ .../src/main/java/pp/monopoly/model/Figure.java | 3 +++ .../java/pp/monopoly/model/LimitedLinkedList.java | 10 +++++++++- .../java/pp/monopoly/server/MonopolyServer.java | 11 +++++++++++ 8 files changed, 56 insertions(+), 13 deletions(-) diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java index de58eda..3260474 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java @@ -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; @@ -52,7 +52,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { /** The current state of the client game logic. */ private ClientState state = new LobbyState(this); - private PlayerHandler playerHandler; + private List players; private BoardManager boardManager = new BoardManager(); @@ -94,8 +94,8 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { state.entry(); } - public PlayerHandler getPlayerHandler() { - return playerHandler; + public List getPlayers() { + return players; } /** @@ -255,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)); } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java index 7dc6fab..6040fb4 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java @@ -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{ private final int id; private String name; @@ -40,6 +43,14 @@ public class Player implements FieldVisitor{ 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 diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java index 2005fc9..fc97a65 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java @@ -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 players = new LimitedLinkedList<>(6); private Set 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 diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java index 53555a5..a901fac 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java @@ -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))); diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameStart.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameStart.java index f01e553..9dfab8d 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameStart.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/GameStart.java @@ -1,25 +1,27 @@ package pp.monopoly.message.server; +import java.util.List; + import com.jme3.network.serializing.Serializable; -import pp.monopoly.game.server.PlayerHandler; +import pp.monopoly.game.server.Player; @Serializable public class GameStart extends ServerMessage{ - private PlayerHandler ph; + private List players; /** * Default constructor for serialization purposes. */ private GameStart() { /* empty */ } - public GameStart(PlayerHandler ph) { - this.ph = ph; + public GameStart(List players) { + this.players = players; } - public PlayerHandler getPlayerHandler() { - return ph; + public List getPlayers() { + return players; } @Override diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/Figure.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/Figure.java index 99e6ab9..9ac874b 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/Figure.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/Figure.java @@ -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 diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java index 5a0285e..34990f6 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/LimitedLinkedList.java @@ -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 the type of elements held in this collection */ +@Serializable public class LimitedLinkedList extends LinkedList { - private final int maxSize; + private int maxSize; + + /** + * Default constructor for serialization purposes. + */ + private LimitedLinkedList() {} /** * Constructs a LimitedLinkedList with the specified maximum size. diff --git a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java index 6033ec5..4c1b56d 100644 --- a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java +++ b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java @@ -26,6 +26,7 @@ import com.jme3.network.serializing.Serializer; import pp.monopoly.MonopolyConfig; import pp.monopoly.game.server.Player; +import pp.monopoly.game.server.PlayerHandler; import pp.monopoly.game.server.ServerGameLogic; import pp.monopoly.game.server.ServerSender; import pp.monopoly.message.client.BuyPropertyRequest; @@ -36,8 +37,12 @@ import pp.monopoly.message.client.RollDice; import pp.monopoly.message.client.TradeOffer; import pp.monopoly.message.client.TradeResponse; import pp.monopoly.message.client.ViewAssetsRequest; +import pp.monopoly.message.server.GameStart; +import pp.monopoly.message.server.NextPlayerTurn; import pp.monopoly.message.server.ServerMessage; +import pp.monopoly.model.Figure; import pp.monopoly.model.IntPoint; +import pp.monopoly.model.LimitedLinkedList; /** * Server implementing the visitor pattern as MessageReceiver for ClientMessages @@ -119,6 +124,12 @@ public class MonopolyServer implements MessageListener, Connec Serializer.registerClass(TradeOffer.class); Serializer.registerClass(TradeResponse.class); Serializer.registerClass(ViewAssetsRequest.class); + Serializer.registerClass(GameStart.class); + Serializer.registerClass(LimitedLinkedList.class); + Serializer.registerClass(NextPlayerTurn.class); + Serializer.registerClass(Player.class); + Serializer.registerClass(Figure.class); + Serializer.registerClass(PlayerHandler.class); } private void registerListeners() {