Updated 'MdgaServer' class.

Updated the 'MdgaServer' class by updating the 'connectionAdded' method in it. In Addition, the JavaDoc text for this method was addded.
This commit is contained in:
Daniel Grigencha
2024-12-02 21:04:36 +01:00
parent 3eef4b2a02
commit 1cf14f65bb

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