From b962444506de8e69b15142448d9bc1f1b015bed3 Mon Sep 17 00:00:00 2001 From: Tamino Mueller Date: Sun, 13 Oct 2024 20:58:18 +0200 Subject: [PATCH] task 11 --- Projekte/battleship/client/build.gradle | 2 ++ .../pp/battleship/client/NetworkDialog.java | 24 +++++++++++++++++++ .../src/main/resources/battleship.properties | 1 + .../main/resources/battleship_de.properties | 3 ++- .../battleship/server/BattleshipServer.java | 5 ++-- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Projekte/battleship/client/build.gradle b/Projekte/battleship/client/build.gradle index a8aeff3..8c3e5c2 100644 --- a/Projekte/battleship/client/build.gradle +++ b/Projekte/battleship/client/build.gradle @@ -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 diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java index 3f2f5a6..ada8f1c 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java @@ -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 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(); + } + } diff --git a/Projekte/battleship/model/src/main/resources/battleship.properties b/Projekte/battleship/model/src/main/resources/battleship.properties index 47f1f6c..51f0480 100644 --- a/Projekte/battleship/model/src/main/resources/battleship.properties +++ b/Projekte/battleship/model/src/main/resources/battleship.properties @@ -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 \ No newline at end of file diff --git a/Projekte/battleship/model/src/main/resources/battleship_de.properties b/Projekte/battleship/model/src/main/resources/battleship_de.properties index 4039420..a2ad011 100644 --- a/Projekte/battleship/model/src/main/resources/battleship_de.properties +++ b/Projekte/battleship/model/src/main/resources/battleship_de.properties @@ -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 diff --git a/Projekte/battleship/server/src/main/java/pp/battleship/server/BattleshipServer.java b/Projekte/battleship/server/src/main/java/pp/battleship/server/BattleshipServer.java index 1ad0a2e..dd572f0 100644 --- a/Projekte/battleship/server/src/main/java/pp/battleship/server/BattleshipServer.java +++ b/Projekte/battleship/server/src/main/java/pp/battleship/server/BattleshipServer.java @@ -1,3 +1,4 @@ + //////////////////////////////////////// // Programming project code // UniBw M, 2022, 2023, 2024 @@ -5,7 +6,7 @@ // (c) Mark Minas (mark.minas@unibw.de) //////////////////////////////////////// -package pp.battleship.server; + package pp.battleship.server; import com.jme3.network.ConnectionListener; import com.jme3.network.HostedConnection; @@ -176,4 +177,4 @@ public class BattleshipServer implements MessageListener, Conn else LOGGER.log(Level.ERROR, "there is no connection with id={0}", id); //NON-NLS } -} \ No newline at end of file +}