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 20264ef9..a23348ee 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 @@ -2,6 +2,7 @@ import com.jme3.network.*; import com.jme3.network.serializing.Serializer; +import pp.mdga.Resources; import pp.mdga.game.*; import pp.mdga.message.client.*; import pp.mdga.message.server.*; @@ -195,12 +196,29 @@ private void messageReceived(HostedConnection source, ClientMessage message) { pendingMessages.add(new ReceivedMessage(message, source.getId())); } + /** + * This method will be used to handle all connections which are connected to the server. + * It will check if the maximum number of connected clients are already reached. If yes it will send a + * LobbyDenyMessage to the given hostedConnection parameter and close it, otherwise it will send a + * LobbyAcceptMessage to the given hostedConnection parameter. In Addition, if the number of connected clients is + * equal to 1 it will set the host of the game to the id of the given hostedConnection parameter. + * + * @param server as the server which is contains all connections as a Server object. + * @param hostedConnection as the connection which is added to the server as a HostedConnection object. + */ @Override public void connectionAdded(Server server, HostedConnection hostedConnection) { System.out.println("new connection " + hostedConnection); //NON-NLS LOGGER.log(Level.DEBUG, "new connection {0}", hostedConnection); //NON-NLS - if (this.myServer.getConnections().size() == 1) { - this.logic.getGame().setHost(hostedConnection.getId()); + + if (this.myServer.getConnections().size() > Resources.MAX_PLAYERS) { + this.logic.getServerSender().send(hostedConnection.getId(), new LobbyDenyMessage()); + hostedConnection.close(""); + } else { + if (this.myServer.getConnections().size() == 1) { + this.logic.getGame().setHost(hostedConnection.getId()); + } + this.logic.getServerSender().send(hostedConnection.getId(), new LobbyAcceptMessage()); } } diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java b/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java index a8c3bf82..13a60c48 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java @@ -8,6 +8,12 @@ * @see #BUNDLE */ public class Resources { + /** + * Create Resources constants. + */ + public static final int MAX_PLAYERS = 4; + public static final int MAX_PIECES = 4; + /** * The resource bundle for the MDGA game. */ @@ -22,10 +28,49 @@ public class Resources { * @throws java.util.MissingResourceException if no object for the given key can be found * @throws ClassCastException if the object found for the given key is not a string */ - public static String lookup(String key) { + public static String stringLookup(String key) { return BUNDLE.getString(key); } + /** + * Gets a int for the given key from the resource bundle in {@linkplain #BUNDLE}. + * + * @param key the key for the desired string + * @return the string for the given key + * @throws NullPointerException if {@code key} is {@code null} + * @throws java.util.MissingResourceException if no object for the given key can be found + * @throws ClassCastException if the object found for the given key is not a string + */ + public static int intLookup(String key) { + return Integer.parseInt(BUNDLE.getString(key)); + } + + /** + * Gets a boolean for the given key from the resource bundle in {@linkplain #BUNDLE}. + * + * @param key the key for the desired string + * @return the string for the given key + * @throws NullPointerException if {@code key} is {@code null} + * @throws java.util.MissingResourceException if no object for the given key can be found + * @throws ClassCastException if the object found for the given key is not a string + */ + public static boolean boolLookup(String key) { + return Boolean.parseBoolean(BUNDLE.getString(key)); + } + + /** + * Gets a double for the given key from the resource bundle in {@linkplain #BUNDLE}. + * + * @param key the key for the desired string + * @return the string for the given key + * @throws NullPointerException if {@code key} is {@code null} + * @throws java.util.MissingResourceException if no object for the given key can be found + * @throws ClassCastException if the object found for the given key is not a string + */ + public static double doubleLookup(String key) { + return Double.parseDouble(BUNDLE.getString(key)); + } + /** * Private constructor to prevent instantiation. */ 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 cb2a788b..ae3387e4 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 @@ -1,6 +1,7 @@ package pp.mdga.game; import com.jme3.network.serializing.Serializable; +import pp.mdga.Resources; /** * This class is used to represent PlayerData related to the board @@ -33,10 +34,10 @@ public class PlayerData { * @param color the color of the player */ public PlayerData(Color color) { - homeNodes = new HomeNode[4]; - pieces = new Piece[4]; - waitingArea = new Piece[4]; - for (int i = 0; i < 4; i++) { + homeNodes = new HomeNode[Resources.MAX_PIECES]; + pieces = new Piece[Resources.MAX_PIECES]; + waitingArea = new Piece[Resources.MAX_PIECES]; + for (int i = 0; i < Resources.MAX_PIECES; i++) { homeNodes[i] = new HomeNode(); pieces[i] = new Piece(color, PieceState.WAITING, i); waitingArea[i] = pieces[i]; @@ -47,9 +48,9 @@ public PlayerData(Color color) { * Constructor. */ private PlayerData() { - homeNodes = new HomeNode[4]; - waitingArea = new Piece[4]; - pieces = new Piece[4]; + homeNodes = new HomeNode[Resources.MAX_PIECES]; + waitingArea = new Piece[Resources.MAX_PIECES]; + pieces = new Piece[Resources.MAX_PIECES]; } /** @@ -113,7 +114,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 < Resources.MAX_PIECES; i++) { if (waitingArea[i] == null) { waitingArea[i] = piece; return; @@ -127,7 +128,7 @@ public void addWaitingPiece(Piece piece) { * @return the piece that was removed from the waiting area */ public Piece removePieceFromWaitingArea() { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < Resources.MAX_PIECES; i++) { if (waitingArea[i] != null) { Piece piece = waitingArea[i]; waitingArea[i] = null; @@ -148,26 +149,30 @@ public void setPieceInHome(int index, Piece piece) { } /** - * This method returns the homeNodes + * This method will be used to return if the given piece parameter is inside the homNodes attribute of PlayerData + * class. + * If yes it will return true, otherwise false. * - * @return the homeNodes + * @return true or false. */ public boolean homeIncludes(Piece piece) { - for (int i = 0; i < 4; i++) { - if (homeNodes[i].getOccupant() == piece) { + for (Node node : this.homeNodes) { + if (node.getOccupant() == piece) { return true; } } + return false; } /** - * This method returns the homeNodes + * This method will be used to return the index of the given piece parameter in the homeNodes attribute of + * PlayerData class. * - * @return the homeNodes + * @return index as an Integer. */ public int getIndexInHome(Piece piece) { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < Resources.MAX_PIECES; i++) { if (homeNodes[i].getOccupant() == piece) { return i; } @@ -176,9 +181,10 @@ public int getIndexInHome(Piece piece) { } /** - * This method returns the homeNodes + * This method will be usd to check if the waitingArea attribute of PlayerData class is empty. + * If yes it will return false, otherwise true. * - * @return the homeNodes + * @return true or false. */ public boolean hasPieceInWaitingArea() { for (Piece piece : waitingArea) {