Merge branch 'dev/client' into dev/client_beck

This commit is contained in:
Cedric Beck
2024-12-01 21:55:37 +01:00
23 changed files with 491 additions and 200 deletions

View File

@@ -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);
}
/**

View File

@@ -127,6 +127,7 @@ public void setReady(boolean ready) {
test++;
if(test > 2) {
testColor = null;
test = 0;
enter(MdgaState.GAME);
}

View File

@@ -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<HostedConnection>, 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<ReceivedMessage> pendingMessages = new LinkedBlockingQueue<>();
@@ -40,37 +42,38 @@ public class MdgaServer implements MessageListener<HostedConnection>, 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<Integer, Player> 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("");
}
}