Merge commit

This commit is contained in:
Felix Koppe
2024-12-02 21:12:56 +01:00
3 changed files with 90 additions and 21 deletions

View File

@@ -2,6 +2,7 @@
import com.jme3.network.*; import com.jme3.network.*;
import com.jme3.network.serializing.Serializer; import com.jme3.network.serializing.Serializer;
import pp.mdga.Resources;
import pp.mdga.game.*; import pp.mdga.game.*;
import pp.mdga.message.client.*; import pp.mdga.message.client.*;
import pp.mdga.message.server.*; import pp.mdga.message.server.*;
@@ -195,12 +196,29 @@ private void messageReceived(HostedConnection source, ClientMessage message) {
pendingMessages.add(new ReceivedMessage(message, source.getId())); 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 @Override
public void connectionAdded(Server server, HostedConnection hostedConnection) { public void connectionAdded(Server server, HostedConnection hostedConnection) {
System.out.println("new connection " + hostedConnection); //NON-NLS System.out.println("new connection " + hostedConnection); //NON-NLS
LOGGER.log(Level.DEBUG, "new connection {0}", 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());
} }
} }

View File

@@ -8,6 +8,12 @@
* @see #BUNDLE * @see #BUNDLE
*/ */
public class Resources { 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. * 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 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 * @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); 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. * Private constructor to prevent instantiation.
*/ */

View File

@@ -1,6 +1,7 @@
package pp.mdga.game; package pp.mdga.game;
import com.jme3.network.serializing.Serializable; import com.jme3.network.serializing.Serializable;
import pp.mdga.Resources;
/** /**
* This class is used to represent PlayerData related to the board * 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 * @param color the color of the player
*/ */
public PlayerData(Color color) { public PlayerData(Color color) {
homeNodes = new HomeNode[4]; homeNodes = new HomeNode[Resources.MAX_PIECES];
pieces = new Piece[4]; pieces = new Piece[Resources.MAX_PIECES];
waitingArea = new Piece[4]; waitingArea = new Piece[Resources.MAX_PIECES];
for (int i = 0; i < 4; i++) { for (int i = 0; i < Resources.MAX_PIECES; i++) {
homeNodes[i] = new HomeNode(); homeNodes[i] = new HomeNode();
pieces[i] = new Piece(color, PieceState.WAITING, i); pieces[i] = new Piece(color, PieceState.WAITING, i);
waitingArea[i] = pieces[i]; waitingArea[i] = pieces[i];
@@ -47,9 +48,9 @@ public PlayerData(Color color) {
* Constructor. * Constructor.
*/ */
private PlayerData() { private PlayerData() {
homeNodes = new HomeNode[4]; homeNodes = new HomeNode[Resources.MAX_PIECES];
waitingArea = new Piece[4]; waitingArea = new Piece[Resources.MAX_PIECES];
pieces = new Piece[4]; pieces = new Piece[Resources.MAX_PIECES];
} }
/** /**
@@ -113,7 +114,7 @@ public Piece[] getPieces() {
* @param piece the piece to be added to the waiting area * @param piece the piece to be added to the waiting area
*/ */
public void addWaitingPiece(Piece piece) { 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) { if (waitingArea[i] == null) {
waitingArea[i] = piece; waitingArea[i] = piece;
return; return;
@@ -127,7 +128,7 @@ public void addWaitingPiece(Piece piece) {
* @return the piece that was removed from the waiting area * @return the piece that was removed from the waiting area
*/ */
public Piece removePieceFromWaitingArea() { public Piece removePieceFromWaitingArea() {
for (int i = 0; i < 4; i++) { for (int i = 0; i < Resources.MAX_PIECES; i++) {
if (waitingArea[i] != null) { if (waitingArea[i] != null) {
Piece piece = waitingArea[i]; Piece piece = waitingArea[i];
waitingArea[i] = null; 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) { public boolean homeIncludes(Piece piece) {
for (int i = 0; i < 4; i++) { for (Node node : this.homeNodes) {
if (homeNodes[i].getOccupant() == piece) { if (node.getOccupant() == piece) {
return true; return true;
} }
} }
return false; 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) { 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) { if (homeNodes[i].getOccupant() == piece) {
return i; 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() { public boolean hasPieceInWaitingArea() {
for (Piece piece : waitingArea) { for (Piece piece : waitingArea) {