From 2e76c41d3a77247f6f727bd9b874d3aa499f851b Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Tue, 26 Nov 2024 18:02:19 +0100 Subject: [PATCH] added new 'Disconnect' client message and updated 'Player' and 'Game' classes --- Projekte/.run/MdgaApp.run.xml | 17 - .../src/main/java/pp/mdga/game/Game.java | 473 ++++++++---------- .../src/main/java/pp/mdga/game/Player.java | 46 +- .../pp/mdga/message/client/Disconnected.java | 34 ++ .../message/server/{Dice.java => Die.java} | 14 +- .../src/main/java/pp/mdga/server/Game.java | 7 - 6 files changed, 263 insertions(+), 328 deletions(-) delete mode 100644 Projekte/.run/MdgaApp.run.xml create mode 100644 Projekte/mdga/model/src/main/java/pp/mdga/message/client/Disconnected.java rename Projekte/mdga/model/src/main/java/pp/mdga/message/server/{Dice.java => Die.java} (86%) delete mode 100644 Projekte/mdga/model/src/main/java/pp/mdga/server/Game.java diff --git a/Projekte/.run/MdgaApp.run.xml b/Projekte/.run/MdgaApp.run.xml deleted file mode 100644 index 82e2f2b6..00000000 --- a/Projekte/.run/MdgaApp.run.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - \ No newline at end of file diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java index 87669c71..39b5984d 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java @@ -8,29 +8,29 @@ * The game state is updated by the game logic. */ public class Game { + /** + * Constants. + */ + public static final int MAX_PLAYERS = 4; + public static final int AMOUNT_OF_TURBO_CARDS = 16; + public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12; + + /** + * Attributes. + */ private int diceModifier = 1; private int diceEyes; - private Map players = new EnumMap<>(Color.class); + private Map players = new HashMap<>(); private Statistic gameStatistics; private List drawPile; private List discardPile = new ArrayList<>(); private Board board; private Color activeColor; - private List order; - private final ArrayList playerList = new ArrayList<>(4); - private final ArrayList observers = new ArrayList<>(); - private Player startPlayer; - private Boolean gameHasStarted = false; - private Boolean playerHasDisconnected = false; - private Boolean gameIsInterrupted = false; - private Boolean allRanked = false; - private Boolean movablePieces = false; - - private Boolean allReady = false; - - private static final int AMOUNT_OF_TURBO_CARDS = 16; - private static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12; + private Player currentPlayer; + private boolean allRanked = false; + private boolean movablePieces = false; + private boolean allReady = false; /** * This constructor creates a new Game object. @@ -48,185 +48,23 @@ public Game() { board = new Board(); } - /** - * This method returns the dice modifier. - * - * @return the dice modifier - */ - public int getDiceModifier() { - return diceModifier; - } - - /** - * This method sets the dice modifier. - * - * @param diceModifier the new dice modifier - */ - public void setDiceModifier(int diceModifier) { - this.diceModifier = diceModifier; - } - - /** - * This method returns the dice eyes. - * - * @return the dice eyes - */ - public int getDiceEyes() { - return diceEyes; - } - - /** - * This method sets the dice eyes. - * - * @param diceEyes the new dice eyes - */ - public void setDiceEyes(int diceEyes) { - this.diceEyes = diceEyes; - } - - /** - * This method returns the players. - * - * @return the players - */ - public Map getPlayers() { - return players; - } - - /** - * This method sets the players. - * - * @param players the new players - */ - public void setPlayers(Map players) { - this.players = players; - } - - /** - * This method returns the game statistics. - * - * @return the game statistics - */ - public Statistic getGameStatistics() { - return gameStatistics; - } - - /** - * This method sets the game statistics. - * - * @param gameStatistics the new game statistics - */ - public void setGameStatistics(Statistic gameStatistics) { - this.gameStatistics = gameStatistics; - } - - /** - * This method returns the draw pile. - * - * @return the draw pile - */ - public List getDrawPile() { - return drawPile; - } - - /** - * This method sets the draw pile. - * - * @param drawPile the new draw pile - */ - public void setDrawPile(List drawPile) { - this.drawPile = drawPile; - } - - /** - * This method returns the discard pile. - * - * @return the discard pile - */ - public List getDiscardPile() { - return discardPile; - } - - /** - * This method sets the discard pile. - * - * @param discardPile the new discard pile - */ - public void setDiscardPile(List discardPile) { - this.discardPile = discardPile; - } - - /** - * This method returns the board. - * - * @return the board - */ - public Board getBoard() { - return board; - } - - /** - * This method sets the board. - * - * @param board the new board - */ - public void setBoard(Board board) { - this.board = board; - } - - /** - * This method returns the active color. - * - * @return the active color - */ - public Color getActiveColor() { - return activeColor; - } - - /** - * This method sets the active color. - * - * @param activeColor the new active color - */ - public void setActiveColor(Color activeColor) { - this.activeColor = activeColor; - } - - /** - * This method returns the order of the players. - * - * @return the order of the players - */ - public List getOrder() { - return order; - } - - /** - * This method sets the order of the players. - * - * @param order the new order of the players - */ - public void setOrder(List order) { - this.order = order; - } - /** * This method adds a player to the game. * - * @param color the color of the player + * @param id the id of the player * @param player the player to be added */ - public void addPlayer(Color color, Player player) { - players.put(color, player); + public void addPlayer(int id, Player player) { + players.put(id, player); } /** * This method removes a player from the game. * - * @param color the color of the player + * @param id the color of the player */ - public void removePlayer(Color color) { - players.remove(color); + public void removePlayer(int id) { + players.remove(id); } /** @@ -248,49 +86,129 @@ public void removeObserver(Observer observer) { } /** - * This method returns the game has started. * - * @return the game has started + * @param id + * @param active */ - public Boolean getGameHasStarted() { - return gameHasStarted; + public void updatePlayerActiveState(int id, boolean active) { + this.players.get(id).setActive(active); } /** - * This method sets the game has started. + * This method will be used to return the player which has the given id parameter. * - * @param gameHasStarted the new game has started + * @param id as the unique id of a player as an Integer. + * @return the player with the given id as a Player object. */ - public void setGameHasStarted(Boolean gameHasStarted) { - this.gameHasStarted = gameHasStarted; + public Player getPlayerById(int id) { + return this.players.get(id); + } + + public Player getCurrentPlayer(Color color) { + for (Map.Entry entry : this.players.entrySet()) { + if (entry.getValue().getColor() == color) { + return entry.getValue(); + } + } + + return null; } /** - * This method returns the player has disconnected. + * This method will be used to return the number of active players of this game. * - * @return the player has disconnected + * @return activePlayers as an Integer. */ - public Boolean playerHasDisconnected() { - return playerHasDisconnected; + public int getNumberOfActivePlayers() { + int activePlayers = 0; + for (Map.Entry entry : this.players.entrySet()) { + if (entry.getValue().isActive()) { + activePlayers++; + } + } + + return activePlayers; } /** - * This method sets the game interruption state. - * - * @param gameIsInterrupted the new game interruption state + * This method notifies the observers. */ - public void setGameIsInterrupted(Boolean gameIsInterrupted) { - this.gameIsInterrupted = gameIsInterrupted; - if (Boolean.FALSE.equals(gameIsInterrupted)) notifyObservers(); + public void notifyObservers() { + for (Observer observer : new ArrayList<>(observers)) { + observer.update(); + } } /** - * This method returns whether the game is interrupted. + * This method returns the dice modifier. * - * @return true if the game is interrupted, false otherwise + * @return the dice modifier */ - public Boolean gameIsInterrupted() { - return gameIsInterrupted; + public int getDiceModifier() { + return diceModifier; + } + + /** + * This method returns the dice eyes. + * + * @return the dice eyes + */ + public int getDiceEyes() { + return diceEyes; + } + + /** + * This method returns the players. + * + * @return the players + */ + public Map getPlayers() { + return players; + } + + /** + * This method returns the game statistics. + * + * @return the game statistics + */ + public Statistic getGameStatistics() { + return gameStatistics; + } + + /** + * This method returns the draw pile. + * + * @return the draw pile + */ + public List getDrawPile() { + return drawPile; + } + + /** + * This method returns the discard pile. + * + * @return the discard pile + */ + public List getDiscardPile() { + return discardPile; + } + + /** + * This method returns the board. + * + * @return the board + */ + public Board getBoard() { + return board; + } + + /** + * This method returns the active color. + * + * @return the active color + */ + public Color getActiveColor() { + return activeColor; } /** @@ -302,6 +220,78 @@ public Boolean getMovablePieces() { return movablePieces; } + /** + * This method sets the dice modifier. + * + * @param diceModifier the new dice modifier + */ + public void setDiceModifier(int diceModifier) { + this.diceModifier = diceModifier; + } + + /** + * This method sets the dice eyes. + * + * @param diceEyes the new dice eyes + */ + public void setDiceEyes(int diceEyes) { + this.diceEyes = diceEyes; + } + + /** + * This method sets the players. + * + * @param players the new players + */ + public void setPlayers(Map players) { + this.players = players; + } + + /** + * This method sets the game statistics. + * + * @param gameStatistics the new game statistics + */ + public void setGameStatistics(Statistic gameStatistics) { + this.gameStatistics = gameStatistics; + } + + /** + * This method sets the draw pile. + * + * @param drawPile the new draw pile + */ + public void setDrawPile(List drawPile) { + this.drawPile = drawPile; + } + + /** + * This method sets the discard pile. + * + * @param discardPile the new discard pile + */ + public void setDiscardPile(List discardPile) { + this.discardPile = discardPile; + } + + /** + * This method sets the board. + * + * @param board the new board + */ + public void setBoard(Board board) { + this.board = board; + } + + /** + * This method sets the active color. + * + * @param activeColor the new active color + */ + public void setActiveColor(Color activeColor) { + this.activeColor = activeColor; + } + /** * This method sets the game interruption state. * @@ -312,16 +302,6 @@ public void setMovablePieces(Boolean movablePieces) { if (Boolean.FALSE.equals(movablePieces)) notifyObservers(); } - /** - * This method sets the player has disconnected. - * - * @param playerHasDisconnected the new player has disconnected - */ - public void setPlayerHasDisconnected(Boolean playerHasDisconnected) { - this.playerHasDisconnected = playerHasDisconnected; - if (Boolean.TRUE.equals(playerHasDisconnected)) notifyObservers(); - } - /** * This method returns whether all players have ranked. * @@ -341,24 +321,6 @@ public void setAllRanked(Boolean allRanked) { if (Boolean.TRUE.equals(allRanked)) notifyObservers(); } - /** - * This method returns the start player. - * - * @return the start player - */ - public Player getStartPlayer() { - return startPlayer; - } - - /** - * This method sets the start player. - * - * @param startPlayer the new start player - */ - public void setStartPlayer(Player startPlayer) { - this.startPlayer = startPlayer; - } - /** * This method returns the all ready state. * @@ -371,53 +333,10 @@ public Boolean allReady() { /** * This method sets the all ready state. * - * @param allReady the new all ready state + * @param allReady the new all-ready state */ public void setAllReady(Boolean allReady) { this.allReady = allReady; if (Boolean.TRUE.equals(allReady)) notifyObservers(); } - - /** - * This method notifies the observers. - */ - public void notifyObservers() { - for (Observer observer : new ArrayList<>(observers)) { - observer.update(); - } - } - - /** - * This method returns the piece through its identifier. - * - * @param identifier the identifier of the piece - * @return the piece - */ - public Piece getPieceThroughIdentifier(String identifier) { - String[] parts = identifier.split("-"); - Color color = Color.valueOf(parts[0]); - int index = Integer.parseInt(parts[1]); - return board.getPlayerData().get(color).getPieces()[index]; - } - - public ArrayList getPlayerList() { - return playerList; - } - - public void addPlayerToList(Player player) { - playerList.add(player); - } - - public void removePlayerFromList(Player player) { - playerList.remove(player); - } - - public Player getPlayerFromList(String name) { - for (Player player : playerList) { - if (player.getName().equals(name)) { - return player; - } - } - return null; - } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Player.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Player.java index 7371cf7b..1038bb6b 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Player.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Player.java @@ -10,18 +10,9 @@ public class Player { private String name; private Statistic playerStatistic; private ArrayList handCards; - private final int id; private Color color; private boolean isReady; - - /** - * This constructor constructs a new Player object - */ - public Player(int id) { - this.id = id; - playerStatistic = new Statistic(); - handCards = new ArrayList<>(); - } + private boolean active = true; /** * This constructor constructs a new Player object @@ -32,7 +23,13 @@ public Player(String name) { this.name = name; playerStatistic = new Statistic(); handCards = new ArrayList<>(); - id = 0; + } + + /** + * Constructor. + */ + public Player() { + super(""); } /** @@ -94,15 +91,6 @@ public BonusCard removeHandCard(BonusCard card) { return cardToRemove; } - /** - * Returns the id of the connection to the client represented by this player. - * - * @return the id - */ - public int getId() { - return id; - } - /** * This method returns the color of the player * @@ -130,6 +118,15 @@ public boolean isReady() { return isReady; } + /** + * This method will be used to return active attribute of Player class. + * + * @return active as a Boolean. + */ + public boolean isActive() { + return this.active; + } + /** * This method sets the player to ready * @@ -138,4 +135,13 @@ public boolean isReady() { public void setReady(boolean ready) { isReady = ready; } + + /** + * This method will be used to set active attribute of Player class to the given active parameter. + * + * @param active as the new active value as a Boolean. + */ + public void setActive(boolean active) { + this.active = active; + } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/message/client/Disconnected.java b/Projekte/mdga/model/src/main/java/pp/mdga/message/client/Disconnected.java new file mode 100644 index 00000000..0b901ff7 --- /dev/null +++ b/Projekte/mdga/model/src/main/java/pp/mdga/message/client/Disconnected.java @@ -0,0 +1,34 @@ +package pp.mdga.message.client; + +import com.jme3.network.serializing.Serializable; + +/** + * + */ +@Serializable +public class Disconnected extends ClientMessage { + public Disconnected() { + super(); + } + + /** + * Returns a string representation of this message. + * + * @return a string representation of this message + */ + @Override + public String toString() { + return "ClientStartGame{}"; + } + + /** + * Accepts a visitor to process this message. + * + * @param interpreter the visitor to process this message + * @param from the connection ID from which the message was received + */ + @Override + public void accept(ClientInterpreter interpreter, int from) { + interpreter.received(this, from); + } +} diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/Dice.java b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/Die.java similarity index 86% rename from Projekte/mdga/model/src/main/java/pp/mdga/message/server/Dice.java rename to Projekte/mdga/model/src/main/java/pp/mdga/message/server/Die.java index 6b4bc1ee..607dea38 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/Dice.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/Die.java @@ -8,7 +8,7 @@ * A message sent by the server to the client to inform about the dice roll. */ @Serializable -public class Dice extends ServerMessage { +public class Die extends ServerMessage { /** * The eye of the dice */ @@ -25,7 +25,7 @@ public class Dice extends ServerMessage { * @param diceEye the eye of the dice * @param moveablePieces the pieces that can be moved */ - public Dice(int diceEye, List moveablePieces) { + public Die(int diceEye, List moveablePieces) { super(); this.diceEye = diceEye; this.moveablePieces = moveablePieces; @@ -34,7 +34,7 @@ public Dice(int diceEye, List moveablePieces) { /** * Default constructor for serialization purposes. */ - private Dice() { + private Die() { diceEye = 0; moveablePieces = null; } @@ -45,8 +45,8 @@ private Dice() { * @param diceEye the eye of the dice * @return a new Dice object */ - public static Dice inactivePlayer(int diceEye) { - return new Dice(diceEye, null); + public static Die inactivePlayer(int diceEye) { + return new Die(diceEye, null); } /** @@ -56,8 +56,8 @@ public static Dice inactivePlayer(int diceEye) { * @param moveablePieces the pieces that can be moved * @return a new Dice object */ - public static Dice activePlayer(int diceEye, List moveablePieces) { - return new Dice(diceEye, moveablePieces); + public static Die activePlayer(int diceEye, List moveablePieces) { + return new Die(diceEye, moveablePieces); } /** diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/server/Game.java b/Projekte/mdga/model/src/main/java/pp/mdga/server/Game.java deleted file mode 100644 index ad3f5897..00000000 --- a/Projekte/mdga/model/src/main/java/pp/mdga/server/Game.java +++ /dev/null @@ -1,7 +0,0 @@ -package pp.mdga.server; - -public class Game extends ServerState { - public Game(ServerGameLogic logic) { - super(logic); - } -}