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 libs.jme3.desktop
implementation project(path: ':battleship:server')
implementation project(path: ':battleship:server')
runtimeOnly libs.jme3.awt.dialogs
runtimeOnly libs.jme3.plugins

View File

@ -7,10 +7,12 @@
package pp.battleship.client;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Container;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.TextField;
import com.simsilica.lemur.component.SpringGridLayout;
import pp.battleship.server.BattleshipServer;
import pp.dialog.Dialog;
import pp.dialog.DialogBuilder;
import pp.dialog.SimpleDialog;
@ -35,6 +37,7 @@ class NetworkDialog extends SimpleDialog {
private final TextField port = new TextField(DEFAULT_PORT);
private String hostname;
private int portNumber;
private final Button serverButton = new Button(lookup("client.server-start"));
private Future<Object> connectionFuture;
private Dialog progressDialog;
@ -65,7 +68,11 @@ class NetworkDialog extends SimpleDialog {
.setOkClose(false)
.setNoClose(false)
.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.
@ -150,4 +157,21 @@ class NetworkDialog extends SimpleDialog {
network.getApp().errorDialog(lookup("server.connection.failed"));
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
port.must.be.integer=Port must be an integer number
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
## (c) Mark Minas (mark.minas@unibw.de)
########################################
#
battleship.name=Schiffe versenken
button.ready=Bereit
button.rotate=Rotiere
@ -37,3 +37,4 @@ dialog.error=Fehler
dialog.question=Frage
port.must.be.integer=Der Port muss eine ganze Zahl sein
map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
client.server-start=Server starten

View File

@ -1,3 +1,4 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024