Refactor and fixes

This commit is contained in:
lieght
2025-09-24 18:37:13 +02:00
parent 9df467c0d3
commit e6e11a3604
16 changed files with 129 additions and 241 deletions

View File

@@ -7,6 +7,10 @@
<properties>
<main-class>org.toop.Main</main-class>
<maven.compiler.source>25</maven.compiler.source>
<maven.compiler.target>25</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
@@ -15,17 +19,22 @@
<version>0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.toop</groupId>
<artifactId>pism_game</artifactId>
<version>0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
<source>25</source>
<target>25</target>
</configuration>
</plugin>
</plugins>

View File

@@ -1,6 +1,16 @@
package org.toop;
import org.toop.framework.networking.NetworkingClientManager;
import org.toop.framework.networking.NetworkingInitializationException;
public class Main {
public static void main(String[] args) {
initSystems();
}
private static void initSystems() throws NetworkingInitializationException {
new NetworkingClientManager();
}
}

View File

@@ -1,14 +1,12 @@
package org.toop.app.gui;
import java.awt.event.ActionEvent;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import javax.swing.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.events.Events;
import org.toop.framework.eventbus.GlobalEventBus;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.eventbus.events.NetworkEvents;
import org.toop.tictactoe.LocalTicTacToe;
import org.toop.framework.networking.NetworkingGameClientHandler;
@@ -56,35 +54,18 @@ public class RemoteGameSelector {
&& !ipTextField.getText().isEmpty()
&& !portTextField.getText().isEmpty()) {
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
GlobalEventBus.post(
new Events.ServerEvents.StartServerRequest(
Integer.parseInt(portTextField.getText()), // TODO: Unsafe parse
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 NetworkEvents.StartClientRequest(
NetworkingGameClientHandler::new,
ipTextField.getText(),
Integer.parseInt(portTextField.getText()), // TODO: Not safe parsing
connectionIdFuture));
String connectionId;
try {
connectionId = connectionIdFuture.get();
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
} // TODO: Better error handling to not crash the system.
AtomicReference<String> clientId = new AtomicReference<>();
new EventFlow().addPostEvent(
NetworkEvents.StartClient.class,
(Supplier<NetworkingGameClientHandler>) NetworkingGameClientHandler::new,
"127.0.0.1",
5001
).onResponse(
NetworkEvents.StartClientSuccess.class,
(response) -> {
clientId.set(response.clientId());
}
).asyncPostEvent();
// GlobalEventBus.subscribeAndRegister(
// NetworkEvents.ReceivedMessage.class,
@@ -103,35 +84,7 @@ public class RemoteGameSelector {
// logger.info("{}", event.message());
// }
// });
GlobalEventBus.post(
new Events.ServerEvents.SendCommand(
connectionId,
"create_game",
nameTextField.getText(),
name2TextField.getText()));
// 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(),
// name2TextField.getText(),
// ticTacToeGame
// ));
// String ticTacToeGameId;
// try {
// ticTacToeGameId = ticTacToeGame.get();
// } catch (InterruptedException | ExecutionException ex) {
// throw new RuntimeException(ex);
// } // TODO: Better error handling to not crash the system.
frame.remove(mainMenu);
localTicTacToe =
LocalTicTacToe.createRemote(
ipTextField.getText(), portTextField.getText());
UIGameBoard ttt = new UIGameBoard(localTicTacToe, this);
localTicTacToe.startThreads();
frame.add(ttt.getTTTPanel()); // TODO: Fix later

View File

@@ -4,13 +4,13 @@ import java.util.concurrent.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.EventPublisher;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.eventbus.events.Events;
import org.toop.framework.eventbus.events.NetworkEvents;
import org.toop.game.GameBase;
import org.toop.tictactoe.gui.UIGameBoard;
import org.toop.framework.networking.NetworkingGameClientHandler;
import org.toop.game.GameBase;
import org.toop.game.tictactoe.ai.MinMaxTicTacToe;
import org.toop.tictactoe.TicTacToeAI;
import java.util.function.Supplier;
@@ -37,7 +37,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
private String serverId = null;
private boolean isAiPlayer[] = new boolean[2];
private MinMaxTicTacToe[] aiPlayers = new MinMaxTicTacToe[2];
private TicTacToeAI[] aiPlayers = new TicTacToeAI[2];
private TicTacToe ticTacToe;
private UIGameBoard ui;
@@ -85,7 +85,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
for (int i = 0; i < aiFlags.length && i < this.aiPlayers.length; i++) {
if (aiFlags[i]) {
this.aiPlayers[i] = new MinMaxTicTacToe(); // create AI for that player
this.aiPlayers[i] = new TicTacToeAI(); // create AI for that player
} else {
this.aiPlayers[i] = null; // not an AI player
}
@@ -110,23 +110,11 @@ public class LocalTicTacToe { // TODO: Implement runnable
return new LocalTicTacToe(ip, port);
}
private String createServer(int port) {
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
new EventPublisher<>(Events.ServerEvents.StartServerRequest.class, port, "tictactoe", serverIdFuture)
.postEvent();
try {
return serverIdFuture.get();
} catch (Exception e) {
logger.error("Error getting server ID", e);
}
return null;
}
private String createConnection(String ip, int port) {
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
new EventPublisher<>(NetworkEvents.StartClientRequest.class,
new EventFlow().addPostEvent(NetworkEvents.StartClientRequest.class,
(Supplier<NetworkingGameClientHandler>) NetworkingGameClientHandler::new,
ip, port, connectionIdFuture).postEvent(); // TODO: what if server couldn't be started with port.
ip, port, connectionIdFuture).asyncPostEvent(); // TODO: what if server couldn't be started with port.
try {
return connectionIdFuture.get();
} catch (InterruptedException | ExecutionException e) {
@@ -258,7 +246,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
}
private void sendCommand(String... args) {
new EventPublisher<>(NetworkEvents.SendCommand.class, this.connectionId, args).postEvent();
new EventFlow().addPostEvent(NetworkEvents.SendCommand.class, this.connectionId, args).asyncPostEvent();
}
// private void endListeners() {