Fixed framework and moved window events to app

This commit is contained in:
lieght
2025-09-24 16:53:29 +02:00
parent 05e2b27330
commit e590941e2f
9 changed files with 54 additions and 141 deletions

View File

@@ -8,6 +8,14 @@
<properties> <properties>
<main-class>org.toop.Main</main-class> <main-class>org.toop.Main</main-class>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.toop</groupId>
<artifactId>pism_framework</artifactId>
<version>0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build> <build>

View File

@@ -0,0 +1,24 @@
package org.toop.events;
import org.toop.framework.eventbus.events.Events;
public class WindowEvents extends Events {
/** Triggers when a cell is clicked in one of the game boards. */
public record CellClicked(int cell) {}
/** Triggers when the window wants to quit. */
public record OnQuitRequested() implements IEvent {}
/** Triggers when the window is resized. */
// public record OnResize(Window.Size size) {}
/** Triggers when the mouse is moved within the window. */
public record OnMouseMove(int x, int y) implements IEvent {}
/** Triggers when the mouse is clicked within the window. */
public record OnMouseClick(int button) {}
/** Triggers when the mouse is released within the window. */
public record OnMouseRelease(int button) {}
}

View File

@@ -1,3 +1,3 @@
package org.toop.framework.eventbus.events; package org.toop.framework.eventbus.events;
public interface IEvent {} public interface EventType {}

View File

@@ -2,7 +2,7 @@ package org.toop.framework.eventbus.events;
import java.util.Map; import java.util.Map;
public interface EventWithUuid extends IEvent { public interface EventWithUuid extends EventType {
Map<String, Object> result(); Map<String, Object> result();
String eventId(); String eventId();
} }

View File

@@ -0,0 +1,3 @@
package org.toop.framework.eventbus.events;
public interface EventWithoutUuid extends EventType {}

View File

@@ -5,7 +5,7 @@ import java.util.Arrays;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
/** Events that are used in the GlobalEventBus class. */ /** Events that are used in the GlobalEventBus class. */
public class Events implements IEvent { public class Events {
/** /**
* WIP, DO NOT USE! * WIP, DO NOT USE!
@@ -68,115 +68,9 @@ public class Events implements IEvent {
return constructor.newInstance(args); return constructor.newInstance(args);
} }
public static class ServerEvents {
/**
* BLOCKING Requests all active servers. The result is returned via the provided
* CompletableFuture.
*
* @param future List of all servers in string form.
*/
public record RequestsAllServers(CompletableFuture<String> future) {}
/** Forces closing all active servers immediately. */
public record ForceCloseAllServers() implements IEvent {}
/**
* Requests starting a server with a specific port and game type.
*
* @param port The port to open the server.
* @param gameType Either "tictactoe" or ...
*/
public record StartServer(int port, String gameType) implements IEvent {}
/**
* BLOCKING Requests starting a server with a specific port and game type, and returns a
* CompletableFuture that completes when the server has started.
*
* @param port The port to open the server.
* @param gameType Either "tictactoe" or ...
* @param future The uuid of the server.
*/
public record StartServerRequest(
int port, String gameType, CompletableFuture<String> future) implements IEvent{}
/**
* Represents a server that has successfully started.
*
* @param uuid The unique identifier of the server.
* @param port The port the server is listening on.
*/
public record ServerStarted(String uuid, int port) implements IEvent {}
/**
* BLOCKING Requests creation of a TicTacToe game on a specific server.
*
* @param serverUuid The unique identifier of the server where the game will be created.
* @param playerA The name of the first player.
* @param playerB The name of the second player.
* @param future The game UUID when the game is created.
*/
public record CreateTicTacToeGameRequest(
String serverUuid,
String playerA,
String playerB,
CompletableFuture<String> future) implements IEvent {}
/**
* Requests running a TicTacToe game on a specific server.
*
* @param serverUuid The unique identifier of the server.
* @param gameUuid The UUID of the game to run.
*/
public record RunTicTacToeGame(String serverUuid, String gameUuid) implements IEvent {}
/**
* Requests ending a TicTacToe game on a specific server.
*
* @param serverUuid The UUID of the server the game is running on.
* @param gameUuid The UUID of the game to end.
*/
public record EndTicTacToeGame(String serverUuid, String gameUuid) implements IEvent {}
// public record StartGameConnectionRequest(String ip, String port,
// CompletableFuture<String> future) {}
/**
* Triggers on changing the server IP.
*
* @param ip The new IP address.
*/
public record OnChangingServerIp(String ip) {}
/**
* Triggers on changing the server port.
*
* @param port The new port.
*/
public record OnChangingServerPort(int port) {}
/** Triggers when a cell is clicked in one of the game boards. */
public record CellClicked(int cell) {}
}
public static class EventBusEvents {} public static class EventBusEvents {}
public static class WindowEvents {
/** Triggers when the window wants to quit. */
public record OnQuitRequested() implements IEvent {}
/** Triggers when the window is resized. */
// public record OnResize(Window.Size size) {}
/** Triggers when the mouse is moved within the window. */
public record OnMouseMove(int x, int y) implements IEvent {}
/** Triggers when the mouse is clicked within the window. */
public record OnMouseClick(int button) {}
/** Triggers when the mouse is released within the window. */
public record OnMouseRelease(int button) {}
}
public static class TttEvents {} public static class TttEvents {}

View File

@@ -1,6 +1,5 @@
package org.toop.framework.eventbus.events; package org.toop.framework.eventbus.events;
import org.toop.backend.tictactoe.TicTacToeServer;
import org.toop.framework.networking.NetworkingGameClientHandler; import org.toop.framework.networking.NetworkingGameClientHandler;
import java.lang.reflect.RecordComponent; import java.lang.reflect.RecordComponent;
@@ -18,14 +17,14 @@ public class NetworkEvents extends Events {
* *
* @param future List of all connections in string form. * @param future List of all connections in string form.
*/ */
public record RequestsAllClients(CompletableFuture<String> future) implements IEvent {} public record RequestsAllClients(CompletableFuture<String> future) implements EventWithoutUuid {}
/** Forces closing all active connections immediately. */ /** Forces closing all active connections immediately. */
public record ForceCloseAllClients() implements IEvent {} public record ForceCloseAllClients() implements EventWithoutUuid {}
public record CloseClientRequest(CompletableFuture<String> future) {} public record CloseClientRequest(CompletableFuture<String> future) implements EventWithoutUuid {}
public record CloseClient(String connectionId) implements IEvent {} public record CloseClient(String connectionId) implements EventWithoutUuid {}
/** /**
* Event to start a new client connection to a server. * Event to start a new client connection to a server.
@@ -50,7 +49,7 @@ public class NetworkEvents extends Events {
* @param ip The IP address of the server to connect to. * @param ip The IP address of the server to connect to.
* @param port The port number of the server to connect to. * @param port The port number of the server to connect to.
* @param eventId A unique identifier for this event, typically injected * @param eventId A unique identifier for this event, typically injected
* automatically by the {@link org.toop.framework.eventbus.EventPublisher}. * automatically by the {@link org.toop.framework.eventbus.EventFlow}.
*/ */
public record StartClient( public record StartClient(
Supplier<? extends NetworkingGameClientHandler> handlerFactory, Supplier<? extends NetworkingGameClientHandler> handlerFactory,
@@ -102,7 +101,7 @@ public class NetworkEvents extends Events {
*/ */
public record StartClientRequest( public record StartClientRequest(
Supplier<? extends NetworkingGameClientHandler> handlerFactory, Supplier<? extends NetworkingGameClientHandler> handlerFactory,
String ip, int port, CompletableFuture<String> future) implements IEvent {} String ip, int port, CompletableFuture<String> future) implements EventWithoutUuid {}
/** /**
* *
@@ -138,18 +137,7 @@ public class NetworkEvents extends Events {
* @param connectionId The UUID of the connection to send the command on. * @param connectionId The UUID of the connection to send the command on.
* @param args The command arguments. * @param args The command arguments.
*/ */
public record SendCommand(String connectionId, String... args) implements IEvent {} public record SendCommand(String connectionId, String... args) implements EventWithoutUuid {}
/**
* WIP Triggers when a command is sent to a server.
*
* @param command The TicTacToeServer instance that executed the command.
* @param args The command arguments.
* @param result The result returned from executing the command.
*/
public record OnCommand(
TicTacToeServer command, String[] args, String result) {} // TODO old
/** /**
* Triggers reconnecting to a previous address. * Triggers reconnecting to a previous address.
* *
@@ -164,7 +152,7 @@ public class NetworkEvents extends Events {
* @param ConnectionUuid The UUID of the connection that received the message. * @param ConnectionUuid The UUID of the connection that received the message.
* @param message The message received. * @param message The message received.
*/ */
public record ReceivedMessage(String ConnectionUuid, String message) implements IEvent {} public record ReceivedMessage(String ConnectionUuid, String message) implements EventWithoutUuid {}
/** /**
* Triggers changing connection to a new address. * Triggers changing connection to a new address.

View File

@@ -1,4 +0,0 @@
package org.toop.framework.eventbus.events;
public class ServerEvents {
}

View File

@@ -6,7 +6,7 @@ import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.EventPublisher; import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.eventbus.events.NetworkEvents; import org.toop.framework.eventbus.events.NetworkEvents;
public class NetworkingClientManager { public class NetworkingClientManager {
@@ -18,12 +18,12 @@ public class NetworkingClientManager {
/** Starts a connection manager, to manage, connections. */ /** Starts a connection manager, to manage, connections. */
public NetworkingClientManager() { public NetworkingClientManager() {
new EventPublisher<>(NetworkEvents.StartClientRequest.class, this::handleStartClientRequest); new EventFlow().listen(NetworkEvents.StartClientRequest.class, this::handleStartClientRequest);
new EventPublisher<>(NetworkEvents.StartClient.class, this::handleStartClient); new EventFlow().listen(NetworkEvents.StartClient.class, this::handleStartClient);
new EventPublisher<>(NetworkEvents.SendCommand.class, this::handleCommand); new EventFlow().listen(NetworkEvents.SendCommand.class, this::handleCommand);
new EventPublisher<>(NetworkEvents.CloseClient.class, this::handleCloseClient); new EventFlow().listen(NetworkEvents.CloseClient.class, this::handleCloseClient);
new EventPublisher<>(NetworkEvents.RequestsAllClients.class, this::getAllConnections); new EventFlow().listen(NetworkEvents.RequestsAllClients.class, this::getAllConnections);
new EventPublisher<>(NetworkEvents.ForceCloseAllClients.class, this::shutdownAll); new EventFlow().listen(NetworkEvents.ForceCloseAllClients.class, this::shutdownAll);
} }
private String startClientRequest(Supplier<? extends NetworkingGameClientHandler> handlerFactory, private String startClientRequest(Supplier<? extends NetworkingGameClientHandler> handlerFactory,
@@ -54,7 +54,7 @@ public class NetworkingClientManager {
private void handleStartClient(NetworkEvents.StartClient event) { private void handleStartClient(NetworkEvents.StartClient event) {
String uuid = this.startClientRequest(event.handlerFactory(), event.ip(), event.port()); String uuid = this.startClientRequest(event.handlerFactory(), event.ip(), event.port());
new EventPublisher<>(NetworkEvents.StartClientSuccess.class, new EventFlow().addPostEvent(NetworkEvents.StartClientSuccess.class,
uuid, event.eventId() uuid, event.eventId()
).asyncPostEvent(); ).asyncPostEvent();
} }