Added creating setting up a server for the user when the form is filled in

This commit is contained in:
lieght
2025-09-17 20:12:36 +02:00
parent 395dc8c603
commit 032d388f4f
4 changed files with 79 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
initSystems();
registerEvents();
/*
Window window = Window.setup(Window.API.GLFW, "Test", new Window.Size(1280, 720));
Renderer renderer = Renderer.setup(Renderer.API.OPENGL);
@@ -34,7 +35,9 @@ public class Main {
Window.start("");
*/
GameSelectorWindow gameSelectorWindow = new GameSelectorWindow();
new Thread(() -> {
GameSelectorWindow gameSelectorWindow = new GameSelectorWindow();
}).start();
}
/**

View File

@@ -17,7 +17,9 @@
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<properties>
<text value=""/>
</properties>
</component>
<vspacer id="4e05c">
<constraints>
@@ -46,7 +48,9 @@
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<properties>
<text value=""/>
</properties>
</component>
<component id="26a0" class="javax.swing.JTextField" binding="portTextField">
<constraints>

View File

@@ -1,9 +1,13 @@
package org.toop.UI;
import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class GameSelectorWindow extends JFrame {
@@ -34,10 +38,54 @@ public class GameSelectorWindow extends JFrame {
}
private void init() {
connectButton.addActionListener((
ActionEvent e) -> {
if(!nameTextField.getText().equals("") && !ipTextField.getText().equals("") && !portTextField.getText().equals("")) {
System.out.println(gameSelectorBox.getSelectedItem().toString()); //todo attempt connecting to the server with given ip, port and name.
connectButton.addActionListener((ActionEvent e) -> {
if( !nameTextField.getText().isEmpty() &&
!ipTextField.getText().isEmpty() &&
!portTextField.getText().isEmpty()) {
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest(
portTextField.getText(),
Objects.requireNonNull(gameSelectorBox.getSelectedItem()).toString().toLowerCase().replace(" ", ""),
serverIdFuture
));
String serverId;
try {
serverId = serverIdFuture.get();
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
} // TODO: Better error handling to not crash the system.
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest(
ipTextField.getText(),
portTextField.getText(),
connectionIdFuture
));
String connectionId;
try {
connectionId = connectionIdFuture.get();
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
} // TODO: Better error handling to not crash the system.
CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest( // TODO: Make this happen through commands send through the connection, instead of an event.
serverId,
nameTextField.getText(),
"P",
ticTacToeGame
));
String ticTacToeGameId;
try {
ticTacToeGameId = ticTacToeGame.get();
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
} // TODO: Better error handling to not crash the system.
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId)); // TODO: attempt connecting to the server with given ip, port and name.
frame.remove(mainMenu);
UIGameBoard ttt = new UIGameBoard(gameSelectorBox.getSelectedItem().toString(),this);
frame.add(ttt.getTTTPanel());