added new 'Disconnect' client message and updated 'Player' and 'Game' classes

This commit is contained in:
Daniel Grigencha
2024-11-26 18:02:19 +01:00
parent 7eafa3da39
commit 2e76c41d3a
6 changed files with 263 additions and 328 deletions

View File

@@ -1,17 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="MdgaApp" type="Application" factoryName="Application" singleton="false" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="pp.mdga.client.MdgaApp" />
<module name="Projekte.mdga.client.main" />
<option name="VM_PARAMETERS" value="-Djava.util.logging.config.file=logging.properties -ea" />
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="pp.mdga.client.board.Outline.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

@@ -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<Color, Player> players = new EnumMap<>(Color.class);
private Map<Integer, Player> players = new HashMap<>();
private Statistic gameStatistics;
private List<BonusCard> drawPile;
private List<BonusCard> discardPile = new ArrayList<>();
private Board board;
private Color activeColor;
private List<Color> order;
private final ArrayList<Player> playerList = new ArrayList<>(4);
private final ArrayList<Observer> 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<Color, Player> getPlayers() {
return players;
}
/**
* This method sets the players.
*
* @param players the new players
*/
public void setPlayers(Map<Color, Player> 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<BonusCard> getDrawPile() {
return drawPile;
}
/**
* This method sets the draw pile.
*
* @param drawPile the new draw pile
*/
public void setDrawPile(List<BonusCard> drawPile) {
this.drawPile = drawPile;
}
/**
* This method returns the discard pile.
*
* @return the discard pile
*/
public List<BonusCard> getDiscardPile() {
return discardPile;
}
/**
* This method sets the discard pile.
*
* @param discardPile the new discard pile
*/
public void setDiscardPile(List<BonusCard> 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<Color> getOrder() {
return order;
}
/**
* This method sets the order of the players.
*
* @param order the new order of the players
*/
public void setOrder(List<Color> 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<Integer, Player> 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<Integer, Player> 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<Integer, Player> 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<BonusCard> getDrawPile() {
return drawPile;
}
/**
* This method returns the discard pile.
*
* @return the discard pile
*/
public List<BonusCard> 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<Integer, Player> 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<BonusCard> drawPile) {
this.drawPile = drawPile;
}
/**
* This method sets the discard pile.
*
* @param discardPile the new discard pile
*/
public void setDiscardPile(List<BonusCard> 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<Player> 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;
}
}

View File

@@ -10,18 +10,9 @@ public class Player {
private String name;
private Statistic playerStatistic;
private ArrayList<BonusCard> 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;
}
}

View File

@@ -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);
}
}

View File

@@ -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<String> moveablePieces) {
public Die(int diceEye, List<String> moveablePieces) {
super();
this.diceEye = diceEye;
this.moveablePieces = moveablePieces;
@@ -34,7 +34,7 @@ public Dice(int diceEye, List<String> 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<String> moveablePieces) {
return new Dice(diceEye, moveablePieces);
public static Die activePlayer(int diceEye, List<String> moveablePieces) {
return new Die(diceEye, moveablePieces);
}
/**

View File

@@ -1,7 +0,0 @@
package pp.mdga.server;
public class Game extends ServerState {
public Game(ServerGameLogic logic) {
super(logic);
}
}