This commit is contained in:
Tamino Mueller 2024-10-13 20:58:18 +02:00
parent c0a3e8b37a
commit b962444506
5 changed files with 32 additions and 3 deletions

View File

@ -9,6 +9,8 @@ dependencies {
implementation project(":battleship:model") implementation project(":battleship:model")
implementation libs.jme3.desktop implementation libs.jme3.desktop
implementation project(path: ':battleship:server')
implementation project(path: ':battleship:server')
runtimeOnly libs.jme3.awt.dialogs runtimeOnly libs.jme3.awt.dialogs
runtimeOnly libs.jme3.plugins runtimeOnly libs.jme3.plugins

View File

@ -7,10 +7,12 @@
package pp.battleship.client; package pp.battleship.client;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Container; import com.simsilica.lemur.Container;
import com.simsilica.lemur.Label; import com.simsilica.lemur.Label;
import com.simsilica.lemur.TextField; import com.simsilica.lemur.TextField;
import com.simsilica.lemur.component.SpringGridLayout; import com.simsilica.lemur.component.SpringGridLayout;
import pp.battleship.server.BattleshipServer;
import pp.dialog.Dialog; import pp.dialog.Dialog;
import pp.dialog.DialogBuilder; import pp.dialog.DialogBuilder;
import pp.dialog.SimpleDialog; import pp.dialog.SimpleDialog;
@ -35,6 +37,7 @@ class NetworkDialog extends SimpleDialog {
private final TextField port = new TextField(DEFAULT_PORT); private final TextField port = new TextField(DEFAULT_PORT);
private String hostname; private String hostname;
private int portNumber; private int portNumber;
private final Button serverButton = new Button(lookup("client.server-start"));
private Future<Object> connectionFuture; private Future<Object> connectionFuture;
private Dialog progressDialog; private Dialog progressDialog;
@ -65,7 +68,11 @@ class NetworkDialog extends SimpleDialog {
.setOkClose(false) .setOkClose(false)
.setNoClose(false) .setNoClose(false)
.build(this); .build(this);
addChild(serverButton).addClickCommands(s -> ifTopDialog(this::startServerInThread));
} }
//Add the button to start the sever
/** /**
* Handles the action for the connect button in the connection dialog. * Handles the action for the connect button in the connection dialog.
@ -150,4 +157,21 @@ class NetworkDialog extends SimpleDialog {
network.getApp().errorDialog(lookup("server.connection.failed")); network.getApp().errorDialog(lookup("server.connection.failed"));
network.getApp().setInfoText(e.getLocalizedMessage()); network.getApp().setInfoText(e.getLocalizedMessage());
} }
/**
* Starts the server in a separate thread.
*/
private void startServerInThread() {
serverButton.setEnabled(false);
Thread serverThread = new Thread(() -> {
try {
BattleshipServer.main(null);
} catch (Exception e) {
serverButton.setEnabled(true);
LOGGER.log(Level.ERROR, "Server could not be started", e);
network.getApp().errorDialog("Could not start server: " + e.getMessage());
}
});
serverThread.start();
}
} }

View File

@ -37,3 +37,4 @@ dialog.error=Error
dialog.question=Question dialog.question=Question
port.must.be.integer=Port must be an integer number port.must.be.integer=Port must be an integer number
map.doesnt.fit=The map doesn't fit to this game map.doesnt.fit=The map doesn't fit to this game
client.server-start=Start server

View File

@ -4,7 +4,7 @@
## www.unibw.de/inf2 ## www.unibw.de/inf2
## (c) Mark Minas (mark.minas@unibw.de) ## (c) Mark Minas (mark.minas@unibw.de)
######################################## ########################################
#
battleship.name=Schiffe versenken battleship.name=Schiffe versenken
button.ready=Bereit button.ready=Bereit
button.rotate=Rotiere button.rotate=Rotiere
@ -37,3 +37,4 @@ dialog.error=Fehler
dialog.question=Frage dialog.question=Frage
port.must.be.integer=Der Port muss eine ganze Zahl sein port.must.be.integer=Der Port muss eine ganze Zahl sein
map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
client.server-start=Server starten

View File

@ -1,3 +1,4 @@
//////////////////////////////////////// ////////////////////////////////////////
// Programming project code // Programming project code
// UniBw M, 2022, 2023, 2024 // UniBw M, 2022, 2023, 2024
@ -5,7 +6,7 @@
// (c) Mark Minas (mark.minas@unibw.de) // (c) Mark Minas (mark.minas@unibw.de)
//////////////////////////////////////// ////////////////////////////////////////
package pp.battleship.server; package pp.battleship.server;
import com.jme3.network.ConnectionListener; import com.jme3.network.ConnectionListener;
import com.jme3.network.HostedConnection; import com.jme3.network.HostedConnection;
@ -176,4 +177,4 @@ public class BattleshipServer implements MessageListener<HostedConnection>, Conn
else else
LOGGER.log(Level.ERROR, "there is no connection with id={0}", id); //NON-NLS LOGGER.log(Level.ERROR, "there is no connection with id={0}", id); //NON-NLS
} }
} }