diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java index 2cbf973d..d759c924 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java @@ -59,8 +59,8 @@ public static void main(String[] args) { AppSettings settings = new AppSettings(true); settings.setSamples(128); settings.setCenterWindow(true); - settings.setWidth(1800); - settings.setHeight(900); + settings.setWidth(1920); + settings.setHeight(1080); settings.setVSync(false); MdgaApp app = new MdgaApp(); @@ -91,7 +91,7 @@ public void simpleInitApp() { gameView = new GameView(this); ceremonyView = new CeremonyView(this); - enter(MdgaState.GAME); + enter(MdgaState.MAIN); } /** diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSynchronizer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSynchronizer.java index 6049063f..1f547999 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSynchronizer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSynchronizer.java @@ -127,6 +127,7 @@ public void setReady(boolean ready) { test++; if(test > 2) { + testColor = null; test = 0; enter(MdgaState.GAME); } diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java index 8f87a73b..f1780baf 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.lang.System.Logger; import java.lang.System.Logger.Level; +import java.sql.Connection; import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -25,6 +26,7 @@ public class MdgaServer implements MessageListener, Connection private static final Logger LOGGER = System.getLogger(MdgaServer.class.getName()); private Server myServer; + private static int port; private final ServerGameLogic logic; private final BlockingQueue pendingMessages = new LinkedBlockingQueue<>(); @@ -40,37 +42,38 @@ public class MdgaServer implements MessageListener, Connection } /** - * Starts the Battleships server. + * Constructor. + * + * @param port as the port for this server */ - public static void main(String[] args) { - new MdgaServer().run(); - } - - /** - * Creates a new MdgaServer. - */ - public MdgaServer() { + public MdgaServer(int port) { + MdgaServer.port = port; LOGGER.log(Level.INFO, "Creating MdgaServer"); //NON-NLS logic = new ServerGameLogic(this, new Game()); } + /** + * + */ public void run() { startServer(); - this.connectionAdded(myServer, myServer.getConnection(0)); while (true) processNextMessage(); } + /** + * + */ private void startServer() { try { LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS - myServer = Network.createServer(1234); - + myServer = Network.createServer(port); initializeSerializables(); myServer.start(); registerListeners(); LOGGER.log(Level.INFO, "Server started: {0}", myServer.isRunning()); //NON-NLS - } catch (IOException e) { + } + catch (IOException e) { LOGGER.log(Level.ERROR, "Couldn't start server: {0}", e.getMessage()); //NON-NLS exit(1); } @@ -154,8 +157,30 @@ private void registerListeners() { myServer.addConnectionListener(this); } + /** + * This method will be used to receive network messages from the given source parameter. + * It will check if the given message parameter is a ClientMessage object. If yes it will call the messageReceived + * method with the casted ClientMessage object. + * + * @param source as the connection which sends the message as a HostedConnection object. + * @param message as the received message as a Message object. + */ + @Override + public void messageReceived(HostedConnection source, Message message) { + if (message instanceof ClientMessage) { + this.messageReceived(source, (ClientMessage) message); + } + } - public void messageReceived(HostedConnection source, ClientMessage message) { + /** + * This method will be used to received network messages from the given source parameter. + * It will add the given message parameter to the pendingMessage attribute of MdgaServer after creating + * a ReceivedMessage object with it and its id. + * + * @param source as the connection which sends the message as a HostedConnection object. + * @param message as the received message as a Message object. + */ + private void messageReceived(HostedConnection source, ClientMessage message) { LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS pendingMessages.add(new ReceivedMessage(message, source.getId())); } @@ -163,8 +188,6 @@ public void messageReceived(HostedConnection source, ClientMessage message) { @Override public void connectionAdded(Server server, HostedConnection hostedConnection) { LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS - // ToDo: Synchronize data between server and client. - logic.getGame().addPlayer(hostedConnection.getId(), new Player()); } @Override @@ -203,6 +226,7 @@ public void exit(int exitValue) { //NON-NLS * @param id the connection id * @param message the message */ + @Override public void send(int id, ServerMessage message) { if (myServer == null || !myServer.isRunning()) { LOGGER.log(Level.ERROR, "no server running when trying to send {0}", message); //NON-NLS @@ -221,15 +245,20 @@ public void send(int id, ServerMessage message) { * * @param message as the message which will be sent to all players as a ServerMessage. */ + @Override public void broadcast(ServerMessage message) { for (Map.Entry entry: this.logic.getGame().getPlayers().entrySet()) { this.send(entry.getKey(), message); } } - //TODO: + /** + * This method will be used to diconenect the client depending on the given id parameter. + * + * @param id as the connection id of the client as an Integer. + */ @Override - public void messageReceived(HostedConnection source, Message m) { - + public void disconnectClient(int id) { + this.myServer.getConnection(id).close(""); } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Board.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Board.java index 26af00c7..730c9f45 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Board.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Board.java @@ -58,6 +58,12 @@ public void setPieceOnBoard(int index, Piece piece) { infield[index].setOccupant(piece); } + /** + * This method returns the index of a specific piece on the board + * + * @param piece the piece to be searched for + * @return the index of the piece + */ public int getInfieldIndexOfPiece(Piece piece) { for (int i = 0; i < infield.length; i++) { if (infield[i].getOccupant() == piece) { diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusCard.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusCard.java index 4b4f5e47..cce4fd98 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusCard.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusCard.java @@ -1,8 +1,23 @@ package pp.mdga.game; +/** + * Enum representing the different types of bonus cards. + */ public enum BonusCard { + /** + * The hidden bonus card. + */ HIDDEN, + /** + * The shield bonus card. + */ SHIELD, + /** + * The turbo bonus card. + */ TURBO, + /** + * The swap bonus card. + */ SWAP } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusNode.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusNode.java index 8ed4d575..157be2a1 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusNode.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/BonusNode.java @@ -1,20 +1,7 @@ package pp.mdga.game; +/** + * This class represents a BonusNode + */ public class BonusNode extends Node { - - public BonusNode() {} - - /** - * This method is used to set a new Occupant - * - * @param occupant the new occupant of the node - */ - @Override - public void setOccupant(Piece occupant) { - if (occupant.isSuppressed()){ - occupant.setShield(ShieldState.NONE); - } - //Draw Card Event - this.occupant = occupant; - } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java index 218416a6..828450f4 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java @@ -26,16 +26,33 @@ public enum Color { */ NONE; + /** + * This method will be used to return a Color enumeration depending on the given index parameter. + * + * @param index as the index of the color inside the values as an Integer. + * @return a Color enumeration. + */ + public static Color getColorByIndex(int index) { + if (index < 0 || index >= values().length) { + throw new IllegalArgumentException(""); + } + + return values()[index]; + } + /** * This method will be used to calculate the next color inside the sequence. * * @return color as a Color Enumeration. */ public Color next() { - if (this.next() == NONE) { - return AIRFORCE; + Color[] colors = values(); + int nextIndex = (this.ordinal() + 1) % colors.length; + + if (colors[nextIndex] == NONE) { + nextIndex = (nextIndex + 1) % colors.length; } - return values()[(ordinal() + 1) % values().length]; + return colors[nextIndex]; } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Die.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Die.java new file mode 100644 index 00000000..047cf6ce --- /dev/null +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Die.java @@ -0,0 +1,133 @@ +package pp.mdga.game; + +import java.util.random.RandomGenerator; +import java.util.random.RandomGeneratorFactory; + +/** + * This class represents a simple die with the possibilities to shuffle and return the roll result. + * An important fact, the sum of ZERO_EYE, NORMAL_EYE and DOUBLE_EYE have to be 1.0; + */ +public class Die { + /** + * The maximum number of eyes on the die. + */ + public static final int MAXIMUM_EYES = 6; + + /** + * The probability of rolling a zero eye. + */ + public static final double ZERO_EYE = 0.2; + + /** + * The probability of rolling a normal eye. + */ + public static final double NORMAL_EYE = 0.2; + + /** + * The probability of rolling a double eye. + */ + public static final double DOUBLE_EYE = 0.6; + + /** + * The modifier applied to the die roll. + */ + private int dieModifier = 1; + + /** + * The random number generator used for die rolls. + */ + private final RandomGenerator random; + + /** + * The result of the last die roll. + */ + private int lastNumberOfDice; + + /** + * This constructor is used to create a new Die object with a random seed. + */ + public Die() { + this.random = RandomGeneratorFactory.of("Random").create(); + } + + /** + * This constructor is used to create a new Die object with a given seed. + * + * @param seed the seed for the random number generator + */ + public Die(long seed) { + this.random = RandomGeneratorFactory.of("Random").create(seed); + } + + /** + * This method will be used to return a random number generated by the random attribute of Die class. + * + * @return lastNumberOfDice as an Integer. + */ + public int shuffle() { + this.lastNumberOfDice = this.random.nextInt(MAXIMUM_EYES) + 1; + + return this.lastNumberOfDice; + } + + /** + * This method will be used to return the given roll parameter. + * It will be used for test cases. + * + * @param roll as the roll result which will be returned as an Integer. + * @return lastNumberOfDice as an Integer. + */ + public int shuffle(int roll) { + this.lastNumberOfDice = roll; + + return this.lastNumberOfDice; + } + + /** + * This method will be used to modify the value of dieModifier attribute of Die class. + */ + public void modify() { + float randomFloat = this.random.nextFloat(); + if (randomFloat < ZERO_EYE) { + this.dieModifier = 0; + } else if (ZERO_EYE <= randomFloat && randomFloat < ZERO_EYE + NORMAL_EYE) { + this.dieModifier = 1; + } else if (ZERO_EYE + NORMAL_EYE <= randomFloat && randomFloat < ZERO_EYE + NORMAL_EYE + DOUBLE_EYE) { + this.dieModifier = 2; + } + } + + /** + * This method will be used to reset all values of the Die class to its origins. + */ + public void reset() { + this.dieModifier = 1; + } + + /** + * This method will be used to return dieModifier attribute of Die class. + * + * @return dieModifier as an Integer. + */ + public int getDieModifier() { + return this.dieModifier; + } + + /** + * This method will be used return lastNumberOfDice attribute of Die class. + * + * @return lastNumberOfDice as an Integer. + */ + public int getLastNumberOfDice() { + return this.lastNumberOfDice; + } + + /** + * This method will be used to set dieModifier attribute of Die class to the given dieModifier parameter. + * + * @param dieModifier as the new value of dieModifier as an Integer. + */ + public void setDieModifier(int dieModifier) { + this.dieModifier = dieModifier; + } +} 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 883786d3..0ea882ba 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 @@ -9,25 +9,43 @@ */ public class Game { /** - * Constants. + * The number of turbo cards available in the game. */ public static final int AMOUNT_OF_TURBO_CARDS = 16; - public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12; /** - * Attributes. + * The number of shield and swap cards available in the game. */ + public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12; + + // The modifier applied to the dice roll. private int diceModifier = 1; + + // The number of eyes shown on the dice. private int diceEyes; + + // A map of player IDs to Player objects. private Map players = new HashMap<>(); + + // The statistics of the game. private Statistic gameStatistics; + + // The pile of bonus cards available for drawing. private List drawPile; + + // The pile of bonus cards that have been discarded. private List discardPile = new ArrayList<>(); + + // The game board. private Board board; + + // The die used in the game. + private Die die; + + // The color of the active player. private Color activeColor; - private final ArrayList observers = new ArrayList<>(); - private boolean allRanked = false; - private boolean movablePieces = false; + + // A flag indicating whether all players are ready. private boolean allReady = false; /** @@ -66,27 +84,10 @@ public void removePlayer(int id) { } /** - * This method adds an observer to the game. + * This method updates the active state of a player. * - * @param observer the observer to be added - */ - public void addObserver(Observer observer) { - observers.add(observer); - } - - /** - * This method removes an observer from the game. - * - * @param observer the observer to be removed - */ - public void removeObserver(Observer observer) { - observers.remove(observer); - } - - /** - * - * @param id - * @param active + * @param id the id of the player + * @param active the new active state */ public void updatePlayerActiveState(int id, boolean active) { this.players.get(id).setActive(active); @@ -139,7 +140,7 @@ public int getNumberOfActivePlayers() { * * @return the piece specified by the UUID */ - public Piece getPieceThroughUUID(UUID pieceId){ + public Piece getPieceThroughUUID(UUID pieceId) { for (var playerData : board.getPlayerData().values()) { for (var piece : playerData.getPieces()) { if (piece.getUuid().equals(pieceId)) { @@ -150,15 +151,6 @@ public Piece getPieceThroughUUID(UUID pieceId){ return null; } - /** - * This method notifies the observers. - */ - public void notifyObservers() { - for (Observer observer : new ArrayList<>(observers)) { - //TODO: observer.update(); - } - } - /** * This method returns the dice modifier. * @@ -222,6 +214,15 @@ public Board getBoard() { return board; } + /** + * This method will be used to return die attribute of Game class. + * + * @return die as a Die object. + */ + public Die getDie() { + return this.die; + } + /** * This method returns the active color. * @@ -231,15 +232,6 @@ public Color getActiveColor() { return activeColor; } - /** - * This method returns whether the game is interrupted. - * - * @return true if the game is interrupted, false otherwise - */ - public Boolean getMovablePieces() { - return movablePieces; - } - /** * This method sets the dice modifier. * @@ -312,35 +304,6 @@ public void setActiveColor(Color activeColor) { this.activeColor = activeColor; } - /** - * This method sets the game interruption state. - * - * @param movablePieces the new game interruption state - */ - public void setMovablePieces(Boolean movablePieces) { - this.movablePieces = movablePieces; - if (Boolean.FALSE.equals(movablePieces)) notifyObservers(); - } - - /** - * This method returns whether all players have ranked. - * - * @return true if all players have ranked, false otherwise - */ - public Boolean allRanked() { - return allRanked; - } - - /** - * This method sets whether all players have ranked. - * - * @param allRanked the new all ranked state - */ - public void setAllRanked(Boolean allRanked) { - this.allRanked = allRanked; - if (Boolean.TRUE.equals(allRanked)) notifyObservers(); - } - /** * This method returns the all ready state. * @@ -357,6 +320,5 @@ public Boolean allReady() { */ public void setAllReady(Boolean allReady) { this.allReady = allReady; - if (Boolean.TRUE.equals(allReady)) notifyObservers(); } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/HomeNode.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/HomeNode.java index 2150c0a4..8faa06b1 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/HomeNode.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/HomeNode.java @@ -1,6 +1,7 @@ package pp.mdga.game; +/** + * Represents a home node. + */ public class HomeNode extends Node { - - public HomeNode() {} } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Node.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Node.java index f1b0b3d7..6d42092b 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Node.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Node.java @@ -6,8 +6,6 @@ public class Node { protected Piece occupant; - public Node() {} - /** * This method is used to get an occupant of the Node. * @@ -23,7 +21,7 @@ public Piece getOccupant() { * @param occupant the new occupant of the node */ public void setOccupant(Piece occupant) { - if (occupant.isSuppressed()){ + if (occupant.isSuppressed()) { occupant.setShield(ShieldState.NONE); } this.occupant = occupant; diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java index 04b9d857..15f00498 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java @@ -6,9 +6,24 @@ * This class will be used to hold all Piece relevant data. */ public class Piece { + /** + * The shield state of the piece. + */ private ShieldState shield; + + /** + * The current state of the piece. + */ private PieceState state; + + /** + * The color of the piece. + */ private final Color color; + + /** + * The unique identifier of the piece. + */ private final UUID uuid = UUID.randomUUID(); /** diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/PieceState.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/PieceState.java index 006e6fbd..abab1258 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/PieceState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/PieceState.java @@ -1,8 +1,23 @@ package pp.mdga.game; +/** + * Represents the state of a piece. + */ public enum PieceState { + /** + * The piece is active. + */ ACTIVE, + /** + * The piece is waiting. + */ WAITING, + /** + * The piece is in the home. + */ HOME, + /** + * The piece is finished. + */ HOMEFINISHED } 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 6029e396..7537333b 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 @@ -6,11 +6,34 @@ * This class will be used to handle general PlayerData */ public class Player { + /** + * The name of the player. + */ private String name; + + /** + * The statistics of the player. + */ private Statistic playerStatistic; + + /** + * The hand cards of the player. + */ private ArrayList handCards; + + /** + * The color of the player. + */ private Color color = Color.NONE; + + /** + * Indicates if the player is ready. + */ private boolean isReady; + + /** + * Indicates if the player is active. + */ private boolean active = true; /** @@ -72,7 +95,7 @@ public ArrayList getHandCards() { * * @param card the card to be added to the players hand */ - public void addHandCards(BonusCard card) { + public void addHandCard(BonusCard card) { handCards.add(card); } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/PlayerData.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/PlayerData.java index c68bc8ff..f03559fa 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/PlayerData.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/PlayerData.java @@ -4,9 +4,24 @@ * This class is used to represent PlayerData related to the board */ public class PlayerData { + /** + * An array of HomeNode objects representing the home nodes of the player. + */ private HomeNode[] homeNodes; + + /** + * The index of the start node for the player. + */ private int startNodeIndex; + + /** + * An array of Piece objects representing the waiting area of the player. + */ private Piece[] waitingArea; + + /** + * An array of Piece objects representing all the pieces of the player. + */ private Piece[] pieces; /** @@ -75,7 +90,7 @@ public Piece[] getPieces() { * @param piece the piece to be added to the waiting area */ public void addWaitingPiece(Piece piece) { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (waitingArea[i] == null) { waitingArea[i] = piece; return; diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/ShieldState.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/ShieldState.java index 71e21c06..6bff3f52 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/ShieldState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/ShieldState.java @@ -1,7 +1,19 @@ package pp.mdga.game; +/** + * Represents the state of a piece's shield. + */ public enum ShieldState { + /** + * The shield is not active. + */ NONE, + /** + * The shield is active. + */ ACTIVE, + /** + * The shield is suppressed, when the piece is on a start node. + */ SUPPRESSED } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/StartNode.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/StartNode.java index 85e70cb3..4d2de491 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/StartNode.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/StartNode.java @@ -1,24 +1,21 @@ package pp.mdga.game; +/** + * Represents a start node. + */ public class StartNode extends Node { - - public StartNode(Color color) { - this.color = color; - } - + /** + * The color of the node. + */ private Color color; /** - * This method is used to set a new Occupant + * Creates a new start node with the given color. * - * @param occupant the new occupant of the node + * @param color the color of the node */ - @Override - public void setOccupant(Piece occupant) { - if (occupant.isShielded()){ - occupant.setShield(ShieldState.SUPPRESSED); - } - this.occupant = occupant; + public StartNode(Color color) { + this.color = color; } /** diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Statistic.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Statistic.java index 2b5e31aa..40675707 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Statistic.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Statistic.java @@ -4,13 +4,39 @@ * This class will be used to store Statistics during the Game; */ public class Statistic { + /** + * The number of cards played. + */ private int cardsPlayed; + + /** + * The number of pieces thrown. (enemy) + */ private int piecesThrown; + + /** + * The number of pieces being thrown. (own pieces) + */ private int piecesBeingThrown; + + /** + * The number of times a 6 was diced. + */ private int diced6; + + /** + * The number of nodes traveled. + */ private int traveledNodes; + + /** + * The number of bonus nodes activated. + */ private int activatedBonusNodes; + /** + * Constructs a new Statistic object with all values initialized to 0. + */ public Statistic() { cardsPlayed = 0; piecesThrown = 0; diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/message/client/StartGameMessage.java b/Projekte/mdga/model/src/main/java/pp/mdga/message/client/StartGameMessage.java index c2ff2ffb..bad3286c 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/message/client/StartGameMessage.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/message/client/StartGameMessage.java @@ -23,6 +23,15 @@ public StartGameMessage() { forceStartGame = false; } + /** + * Gets whether the game should be force started. + * + * @return whether the game should be force started + */ + public boolean isForceStartGame() { + return forceStartGame; + } + /** * Returns a string representation of this message. * diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyPlayerLeaveMessage.java b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyPlayerLeaveMessage.java index 75627e21..328e3242 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyPlayerLeaveMessage.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/message/server/LobbyPlayerLeaveMessage.java @@ -13,21 +13,14 @@ public class LobbyPlayerLeaveMessage extends ServerMessage { */ private final int id; - /** - * The color associated with the player leaving the lobby. - */ - private final Color color; - /** * Constructs a new LobbyPlayerLeave instance with the specified player name and color. * - * @param id the id of the player leaving the lobby - * @param color the color associated with the player leaving the lobby + * @param id the id of the player leaving the lobby. */ - public LobbyPlayerLeaveMessage(int id, Color color) { + public LobbyPlayerLeaveMessage(int id) { super(); this.id = id; - this.color = color; } /** @@ -35,7 +28,6 @@ public LobbyPlayerLeaveMessage(int id, Color color) { */ private LobbyPlayerLeaveMessage() { id = 0; - color = null; } /** @@ -47,15 +39,6 @@ public int getId() { return id; } - /** - * Returns the color associated with the player leaving the lobby. - * - * @return the color associated with the player leaving the lobby - */ - public Color getColor() { - return color; - } - /** * Accepts a visitor to process this message. * diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerSender.java b/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerSender.java index 7d628e6c..ac0b183d 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerSender.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerSender.java @@ -21,4 +21,11 @@ public interface ServerSender { * @param message as the message which will be sent to all players as a ServerMessage. */ void broadcast(ServerMessage message); + + /** + * This method will be used to disconnect the client depending on the given id parameter. + * + * @param id as the connection id of the client as an Integer. + */ + void disconnectClient(int id); } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/LobbyState.java b/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/LobbyState.java index a1bfaacf..cbc2af1b 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/LobbyState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/LobbyState.java @@ -1,13 +1,16 @@ package pp.mdga.server.automaton; +import pp.mdga.game.Color; +import pp.mdga.game.Player; import pp.mdga.message.client.*; -import pp.mdga.message.server.ServerStartGameMessage; -import pp.mdga.message.server.UpdateReadyMessage; -import pp.mdga.message.server.UpdateTSKMessage; +import pp.mdga.message.server.*; import pp.mdga.server.ServerGameLogic; +import java.util.Map; + /** - * + * This class represents the lobby state of the server. + * It will handle all join and disconnect messages, as well the selection of the color of the player. */ public class LobbyState extends ServerState { /** @@ -36,53 +39,90 @@ public void exit() { } /** - * + * @param msg as the message which was sent by the player as a JoinedLobbyMessage object. + * @param from as the client id of the player as an Integer. + */ + @Override + public void received(JoinedLobbyMessage msg, int from) { + Player player = new Player(msg.getName()); + this.logic.getGame().addPlayer(from, player); + this.logic.getServerSender().broadcast(new LobbyPlayerJoinedMessage(from, player)); + } + + /** * @param msg as the message which was sent by the player as a SelectTSK object. * @param from as the client id of the player as an Integer. */ @Override public void received(SelectTSKMessage msg, int from) { + for (Map.Entry entry : this.logic.getGame().getPlayers().entrySet()) { + if (entry.getValue().getColor() == msg.getColor()) { + return; + } + } + this.logic.getGame().getPlayerById(from).setColor(msg.getColor()); this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor())); } /** - * * @param msg as the message which was sent by the player as a DeselectTSK object. * @param from as the client id of the player as an Integer. */ @Override public void received(DeselectTSKMessage msg, int from) { - this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor())); + this.logic.getGame().getPlayerById(from).setColor(Color.NONE); + this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, Color.NONE)); } /** - * * @param msg as the message which was sent by the player as a LobbyReady object. * @param from as the client id of the player as an Integer. */ @Override public void received(LobbyReadyMessage msg, int from) { + this.logic.getGame().getPlayerById(from).setReady(true); this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, true)); + for (Map.Entry entry : this.logic.getGame().getPlayers().entrySet()) { + if (!entry.getValue().isActive()) { + return; + } + } + + this.logic.getGame().setAllReady(true); } /** - * * @param msg as the message which was sent by the player as a LobbyNotReady object. * @param from as the client id of the player as an Integer. */ @Override public void received(LobbyNotReadyMessage msg, int from) { + this.logic.getGame().getPlayerById(from).setReady(false); + this.logic.getGame().setAllReady(false); this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, false)); } /** * + * @param msg as the message which was sent by the player as a LeaveGameMessage object. + * @param from as the client id of the player as an Integer. + */ + @Override + public void received(LeaveGameMessage msg, int from) { + this.logic.getGame().removePlayer(from); + this.logic.getServerSender().broadcast(new LobbyPlayerLeaveMessage(from)); + this.logic.getServerSender().disconnectClient(from); + } + + /** * @param msg as the message which was sent by the player as a ForceStartGame object. * @param from as the client id of the player as an Integer. */ @Override public void received(StartGameMessage msg, int from) { - this.logic.getServerSender().broadcast(new ServerStartGameMessage()); - this.logic.setCurrentState(this.logic.getGameState()); + if (msg.isForceStartGame() || this.logic.getGame().allReady()) { + this.logic.getServerSender().broadcast(new ServerStartGameMessage()); + this.logic.setCurrentState(this.logic.getGameState()); + } } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/ServerState.java b/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/ServerState.java index adc4c3fd..da9ebc44 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/ServerState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/server/automaton/ServerState.java @@ -33,19 +33,19 @@ public ServerState(ServerGameLogic logic) { public abstract void exit(); /** - * This method will be called whenever the server received an AnimationEnd message. + * This method will be called whenever the server received an AnimationEndMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a AnimationEnd object. + * @param msg as the message which was sent by the player as a AnimationEndMessage object. * @param from as the client id of the player as an Integer. */ public void received(AnimationEndMessage msg, int from) {} /** - * This method will be called whenever the server received an DeselectTSK message. + * This method will be called whenever the server received a DeselectTSKMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a DeselectTSK object. + * @param msg as the message which was sent by the player as a DeselectTSKMessage object. * @param from as the client id of the player as an Integer. */ public void received(DeselectTSKMessage msg, int from) {} @@ -60,136 +60,136 @@ public void received(DeselectTSKMessage msg, int from) {} public void received(StartGameMessage msg, int from) {} /** - * This method will be called whenever the server received a JoinServer message. + * This method will be called whenever the server received a JoinedLobbyMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a JoinServer object. + * @param msg as the message which was sent by the player as a JoinedLobbyMessage object. * @param from as the client id of the player as an Integer. */ public void received(JoinedLobbyMessage msg, int from) {} /** - * This method will be called whenever the server received an LeaveGame message. + * This method will be called whenever the server received an LeaveGameMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a LeaveGame object. + * @param msg as the message which was sent by the player as a LeaveGameMessage object. * @param from as the client id of the player as an Integer. */ public void received(LeaveGameMessage msg, int from) {} /** - * This method will be called whenever the server received a LobbyReady message. + * This method will be called whenever the server received a LobbyReadyMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a LobbyReady object. + * @param msg as the message which was sent by the player as a LobbyReadyMessage object. * @param from as the client id of the player as an Integer. */ public void received(LobbyReadyMessage msg, int from) {} /** - * This method will be called whenever the server received a LobbyNotReady message. + * This method will be called whenever the server received a LobbyNotReadyMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a LobbyNotReady object. + * @param msg as the message which was sent by the player as a LobbyNotReadyMessage object. * @param from as the client id of the player as an Integer. */ public void received(LobbyNotReadyMessage msg, int from) {} /** - * This method will be called whenever the server received a Disconnected message. + * This method will be called whenever the server received a DisconnectedMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a Disconnected object. + * @param msg as the message which was sent by the player as a DisconnectedMessage object. * @param from as the client id of the player as an Integer. */ public void received(DisconnectedMessage msg, int from) {} /** - * This method will be called whenever the server received a Briefing message. + * This method will be called whenever the server received a RequestBriefingMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a Briefing object. + * @param msg as the message which was sent by the player as a RequestBriefingMessage object. * @param from as the client id of the player as an Integer. */ public void received(RequestBriefingMessage msg, int from) {} /** - * This method will be called whenever the server received a Die message. + * This method will be called whenever the server received a RequestDieMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a Die object. + * @param msg as the message which was sent by the player as a RequestDieMessage object. * @param from as the client id of the player as an Integer. */ public void received(RequestDieMessage msg, int from) {} /** - * This method will be called whenever the server received a RequestMove message. + * This method will be called whenever the server received a RequestMoveMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a RequestMove object. + * @param msg as the message which was sent by the player as a RequestMoveMessage object. * @param from as the client id of the player as an Integer. */ public void received(RequestMoveMessage msg, int from) {} /** - * This method will be called whenever the server received a PlayCard message. + * This method will be called whenever the server received a RequestPlayCardMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a PlayCard object. + * @param msg as the message which was sent by the player as a RequestPlayCardMessage object. * @param from as the client id of the player as an Integer. */ public void received(RequestPlayCardMessage msg, int from) {} /** - * This method will be called whenever the server received a SelectCard message. + * This method will be called whenever the server received a SelectCardMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a SelectCard object. + * @param msg as the message which was sent by the player as a SelectCardMessage object. * @param from as the client id of the player as an Integer. */ public void received(SelectCardMessage msg, int from) {} /** - * This method will be called whenever the server received a SelectTSK message. + * This method will be called whenever the server received a SelectTSKMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a SelectTSK object. + * @param msg as the message which was sent by the player as a SelectTSKMessage object. * @param from as the client id of the player as an Integer. */ public void received(SelectTSKMessage msg, int from) {} /** - * This method will be called whenever the server received a ForceContinueGame message. + * This method will be called whenever the server received a ForceContinueGameMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a ForceContinueGame object. + * @param msg as the message which was sent by the player as a ForceContinueGameMessage object. * @param from as the client id of the player as an Integer. */ public void received(ForceContinueGameMessage msg, int from) {} /** - * This method will be called whenever the server received a ClientStartGame message. + * This method will be called whenever the server received a ClientStartGameMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a ClientStartGame object. + * @param msg as the message which was sent by the player as a ClientStartGameMessage object. * @param from as the client id of the player as an Integer. */ public void received(ClientStartGameMessage msg, int from) {} /** - * This method will be called whenever the server received a NoPowerCard message. + * This method will be called whenever the server received a NoPowerCardMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a NoPowerCard object. + * @param msg as the message which was sent by the player as a NoPowerCardMessage object. * @param from as the client id of the player as an Integer. */ public void received(NoPowerCardMessage msg, int from) {} /** - * This method will be called whenever the server received a SelectedPieces message. + * This method will be called whenever the server received a SelectedPiecesMessage message. * It will also get the client id of the player who send this message. * - * @param msg as the message which was sent by the player as a SelectedPieces object. + * @param msg as the message which was sent by the player as a SelectedPiecesMessage object. * @param from as the client id of the player as an Integer. */ public void received(SelectedPiecesMessage msg, int from) {}