From 73859d8c81e41109e292bd6c249bde5293a6ca84 Mon Sep 17 00:00:00 2001 From: Fleischer Hanno Date: Mon, 2 Dec 2024 20:34:52 +0100 Subject: [PATCH 1/6] added methods for getting Boolean, String, Double and int --- .../model/src/main/java/pp/mdga/Resources.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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..10ae5a44 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java @@ -22,10 +22,22 @@ 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); } + public static int intLookup(String key) { + return Integer.parseInt(BUNDLE.getString(key)); + } + + public static boolean boolLookup(String key) { + return Boolean.parseBoolean(BUNDLE.getString(key)); + } + + public static double doubleLookup(String key) { + return Double.parseDouble(BUNDLE.getString(key)); + } + /** * Private constructor to prevent instantiation. */ From c48f924ead2cc27f8288f6dab18c94ee00a9df7c Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 20:43:47 +0100 Subject: [PATCH 2/6] Updated 'Resources' class. Updated the 'Resources' class by adding the 'MAX_PLAYERS' constant to it. --- Projekte/mdga/model/src/main/java/pp/mdga/Resources.java | 5 +++++ 1 file changed, 5 insertions(+) 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 10ae5a44..a7142c45 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,11 @@ * @see #BUNDLE */ public class Resources { + /** + * Create Resources constants. + */ + public static final int MAX_PLAYERS = 4; + /** * The resource bundle for the MDGA game. */ From c1fa67926180422f2f6699676a8dc83300730ff6 Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 20:45:07 +0100 Subject: [PATCH 3/6] Updated 'Resources' class. Updated the 'Resources' class by adding the 'MAX_PIECES' constant to it. --- Projekte/mdga/model/src/main/java/pp/mdga/Resources.java | 1 + 1 file changed, 1 insertion(+) 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 a7142c45..f721043d 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java @@ -12,6 +12,7 @@ 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. From 3eef4b2a02df82b092b441bf4171001e59fc5d53 Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 20:56:59 +0100 Subject: [PATCH 4/6] Updated 'PlayerData' class. Updated the 'PlayerData' class by replacing the magic constants with the 'Resources' class. In Addition, some JavaDoc texts were updated. --- .../main/java/pp/mdga/game/PlayerData.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) 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) { From ebb9f839c7bc76a245bb83605d890828e3ad8fc9 Mon Sep 17 00:00:00 2001 From: Fleischer Hanno Date: Mon, 2 Dec 2024 20:58:00 +0100 Subject: [PATCH 5/6] added JavaDocs in Resources.java --- .../src/main/java/pp/mdga/Resources.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 f721043d..13a60c48 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/Resources.java @@ -32,14 +32,41 @@ 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)); } From 1cf14f65bb840da66a83e53742774b54bec6e8fe Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Mon, 2 Dec 2024 21:04:36 +0100 Subject: [PATCH 6/6] Updated 'MdgaServer' class. Updated the 'MdgaServer' class by updating the 'connectionAdded' method in it. In Addition, the JavaDoc text for this method was addded. --- .../pp/mdga/client/server/MdgaServer.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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()); } }