implemented self host for server

This commit is contained in:
Johannes Schmelz 2024-11-18 23:01:34 +01:00
parent 0b46d14650
commit e2126a7ea7
6 changed files with 58 additions and 37 deletions

View File

@ -0,0 +1,18 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="MonopolyApp" type="Application" factoryName="Application" singleton="false"
nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="pp.monopoly.client.MonopolyApp"/>
<module name="Projekte.monopoly.client.main"/>
<option name="VM_PARAMETERS" value="-Djava.util.logging.config.file=logging.properties"/>
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$"/>
<extension name="coverage">
<pattern>
<option name="PATTERN" value="pp.monopoly.client.*"/>
<option name="ENABLED" value="true"/>
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true"/>
</method>
</configuration>
</component>

View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="MonopolyServer" type="Application" factoryName="Application"
nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="pp.monopoly.server.MonopolyServer"/>
<module name="Projekte.monopoly.server.main"/>
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$"/>
<extension name="coverage">
<pattern>
<option name="PATTERN" value="pp.monopoly.server.*"/>
<option name="ENABLED" value="true"/>
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true"/>
</method>
</configuration>
</component>

View File

@ -7,6 +7,7 @@ description = 'Monopoly Client'
dependencies {
implementation project(":jme-common")
implementation project(":monopoly:model")
implementation project(":monopoly:server")
implementation libs.jme3.desktop

View File

@ -9,8 +9,10 @@ import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.system.AppSettings;
import com.simsilica.lemur.GuiGlobals;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.style.BaseStyles;
import pp.dialog.DialogBuilder;
@ -23,6 +25,7 @@ import pp.monopoly.game.client.MonopolyClient;
import pp.monopoly.game.client.ServerConnection;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.InfoTextEvent;
import pp.monopoly.server.MonopolyServer;
public class MonopolyApp extends SimpleApplication implements MonopolyClient, GameEventListener {
private BitmapText topText;
@ -37,6 +40,8 @@ public class MonopolyApp extends SimpleApplication implements MonopolyClient, Ga
private TestWorld testWorld;
private boolean isSettingsMenuOpen = false;
private boolean inputBlocked = false;
private MonopolyServer monopolyServer;
/**
* Path to the styles script for GUI elements.
*/
@ -207,5 +212,20 @@ public class MonopolyApp extends SimpleApplication implements MonopolyClient, Ga
StartMenu.createStartMenu(this); // Zeige das Startmenü erneut
}
/**
* Startet den Server in einem neuen Thread.
*/
public void startServer() {
new Thread(() -> {
try {
monopolyServer = new MonopolyServer(); // Erstelle Serverinstanz
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
public MonopolyServer getMonopolyServer() {
return monopolyServer;
}
}

View File

@ -29,8 +29,6 @@ public class CreateGameMenu {
private final Container menuContainer;
private Geometry background;
private Label serverStatusLabel;
private MonopolyServer monopolyServer;
private Client client;
public CreateGameMenu(MonopolyApp app) {
this.app = app;
@ -72,7 +70,7 @@ public class CreateGameMenu {
// "Selber hosten"-Button
Button hostButton = buttonContainer.addChild(new Button("Selber hosten"));
hostButton.setPreferredSize(new Vector3f(120, 40, 0));
hostButton.addClickCommands(source -> startServer());
hostButton.addClickCommands(source -> app.startServer());
// "Beitreten"-Button (vorerst funktionslos)
Button joinButton = buttonContainer.addChild(new Button("Beitreten"));
@ -125,37 +123,4 @@ public class CreateGameMenu {
app.getGuiNode().detachChild(background); // Entfernt das Hintergrundbild
StartMenu.createStartMenu(app);
}
/**
* Startet den Server in einem neuen Thread.
*/
private void startServer() {
if (monopolyServer == null) {
serverStatusLabel.setText("Serverstatus: Wird gestartet...");
new Thread(() -> {
try {
monopolyServer = new MonopolyServer(); // Erstelle Serverinstanz
serverStatusLabel.setText("Serverstatus: Läuft auf Port " + monopolyServer.getConfig().getPort());
} catch (Exception e) {
serverStatusLabel.setText("Serverstatus: Fehler beim Starten");
e.printStackTrace();
}
}).start();
} else {
serverStatusLabel.setText("Serverstatus: Bereits gestartet!");
}
}
private void joinGame(String host, String port) {
try {
int portNumber = Integer.parseInt(port);
client = Network.connectToServer(host, portNumber); // Verbindet sich mit Server
client.start();
JOptionPane.showMessageDialog(null, "Erfolgreich mit dem Server verbunden: " + host + ":" + port);
// Hier kannst du zusätzliche Logik für das Verbinden hinzufügen
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Fehler beim Verbinden mit dem Server: " + e.getMessage(),
"Verbindungsfehler", JOptionPane.ERROR_MESSAGE);
}
}
}

View File

@ -65,7 +65,7 @@ public class MonopolyServer implements MessageListener<HostedConnection>, Connec
/**
* Creates the server.
*/
MonopolyServer() {
public MonopolyServer() {
config.readFromIfExists(CONFIG_FILE);
LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS
logic = new ServerGameLogic(this, config);