Initial parsing of server response

This commit is contained in:
lieght
2025-09-27 22:17:49 +02:00
committed by Bas Antonius de Jong
parent 27e7166ac7
commit a9e63b3fcc
15 changed files with 355 additions and 203 deletions

View File

@@ -1,13 +1,54 @@
package org.toop;
import org.toop.app.gui.LocalServerSelector;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.networking.NetworkingClientManager;
import org.toop.framework.networking.NetworkingInitializationException;
import org.toop.app.gui.LocalServerSelector;
import java.util.Arrays;
public class Main {
static void main(String[] args) {
initSystems();
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
EventFlow a = new EventFlow()
.addPostEvent(
NetworkEvents.StartClient.class,
"127.0.0.1",
7789)
.onResponse(Main::login)
// .onResponse(Main::sendCommand)
// .onResponse(Main::closeClient)
.asyncPostEvent();
new Thread(() -> {
while (a.getResult() == null) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
}
long clid = (Long) a.getResult().get("clientId");
new EventFlow()
.addPostEvent(new NetworkEvents.SendCommand(clid, "get playerlist"))
.listen(NetworkEvents.PlayerListResponse.class, response -> {
if (response.clientId() == clid) System.out.println(Arrays.toString(response.playerlist()));
})
.asyncPostEvent();
}).start();
new Thread(() -> javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new)).start();
}
private static void login(NetworkEvents.StartClientResponse event) {
new Thread(() -> {
try {
Thread.sleep(1000);
new EventFlow()
.addPostEvent(new NetworkEvents.SendCommand(event.clientId(), "login bas"))
.asyncPostEvent();
} catch (InterruptedException e) {}
}).start();
}
private static void initSystems() throws NetworkingInitializationException {

View File

@@ -54,14 +54,15 @@ public class RemoteGameSelector {
&& !ipTextField.getText().isEmpty()
&& !portTextField.getText().isEmpty()) {
AtomicReference<String> clientId = new AtomicReference<>();
AtomicReference<Long> clientId = new AtomicReference<>();
new EventFlow().addPostEvent(
NetworkEvents.StartClient.class,
(Supplier<NetworkingGameClientHandler>) NetworkingGameClientHandler::new,
(Supplier<NetworkingGameClientHandler>)
new NetworkingGameClientHandler(clientId.get()),
"127.0.0.1",
5001
).onResponse(
NetworkEvents.StartClientSuccess.class,
NetworkEvents.StartClientResponse.class,
(response) -> {
clientId.set(response.clientId());
}

View File

@@ -33,7 +33,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
private boolean isLocal;
private String gameId;
private String connectionId = null;
private long connectionId = -1;
private String serverId = null;
private boolean[] isAiPlayer = new boolean[2];
@@ -74,7 +74,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
// this.receivedMessageListener =
// GlobalEventBus.subscribe(this::receiveMessageAction);
// GlobalEventBus.subscribe(this.receivedMessageListener);
this.connectionId = this.createConnection(ip, port);
// this.connectionId = this.createConnection(ip, port); TODO: Refactor this
this.createGame("X", "O");
this.isLocal = false;
//this.executor.submit(this::remoteGameThread);
@@ -101,19 +101,6 @@ public class LocalTicTacToe { // TODO: Implement runnable
return new LocalTicTacToe(ip, port);
}
private String createConnection(String ip, int port) {
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
new EventFlow().addPostEvent(NetworkEvents.StartClientRequest.class,
(Supplier<NetworkingGameClientHandler>) NetworkingGameClientHandler::new,
ip, port, connectionIdFuture).asyncPostEvent(); // TODO: what if server couldn't be started with port.
try {
return connectionIdFuture.get();
} catch (InterruptedException | ExecutionException e) {
logger.error("Error getting connection ID", e);
}
return null;
}
private void createGame(String nameA, String nameB) {
nameA = nameA.trim().replace(" ", "-");
nameB = nameB.trim().replace(" ", "-");
@@ -223,7 +210,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
}
private void receiveMessageAction(NetworkEvents.ReceivedMessage receivedMessage) {
if (!receivedMessage.ConnectionUuid().equals(this.connectionId)) {
if (receivedMessage.ConnectionId() != this.connectionId) {
return;
}