mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Fixed no game found bug
This commit is contained in:
@@ -24,19 +24,22 @@ public class Main {
|
|||||||
|
|
||||||
initSystems();
|
initSystems();
|
||||||
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.StartServer("5001", "tictactoe"));
|
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest("5001", "tictactoe", serverIdFuture));
|
||||||
|
String serverId = serverIdFuture.get();
|
||||||
|
|
||||||
CompletableFuture<String> future = new CompletableFuture<>();
|
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
|
||||||
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", future));
|
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", connectionIdFuture));
|
||||||
String serverId = future.get();
|
String connectionId = connectionIdFuture.get();
|
||||||
|
|
||||||
CompletableFuture<String> future2 = new CompletableFuture<>();
|
CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
|
||||||
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGame(serverId, "John", "Pim", future2));
|
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "John", "Pim", ticTacToeGame));
|
||||||
String gameId = future.get();
|
String ticTacToeGameId = ticTacToeGame.get();
|
||||||
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, gameId));
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.Command(serverId, "MOVE", "0"));
|
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId));
|
||||||
GlobalEventBus.post(new Events.ServerEvents.EndTicTacToeGame(serverId, gameId));
|
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "0"));
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.EndTicTacToeGame(serverId, ticTacToeGameId));
|
||||||
|
|
||||||
// for (int i = 0; i < 1; i++) {
|
// for (int i = 0; i < 1; i++) {
|
||||||
// Thread thread = new Thread(() -> {
|
// Thread thread = new Thread(() -> {
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ public class Events implements IEvents {
|
|||||||
|
|
||||||
public record ServerStarted(String uuid, String port) {}
|
public record ServerStarted(String uuid, String port) {}
|
||||||
|
|
||||||
public record CreateTicTacToeGame(String serverUuid, String playerA, String playerB, CompletableFuture<String> future) {}
|
public record CreateTicTacToeGameRequest(String serverUuid, String playerA, String playerB, CompletableFuture<String> future) {}
|
||||||
|
|
||||||
public record RunTicTacToeGame(String serverUuid, String gameUuid) {}
|
public record RunTicTacToeGame(String serverUuid, String gameUuid) {}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class ServerManager {
|
|||||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServerRequest.class, this::handleStartServerRequest);
|
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServerRequest.class, this::handleStartServerRequest);
|
||||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServer.class, this::handleStartServer);
|
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServer.class, this::handleStartServer);
|
||||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ForceCloseAllServers.class, _ -> shutdownAll());
|
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ForceCloseAllServers.class, _ -> shutdownAll());
|
||||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.CreateTicTacToeGame.class, this::handleStartTicTacToeGameOnAServer);
|
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.CreateTicTacToeGameRequest.class, this::handleStartTicTacToeGameOnAServer);
|
||||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.RunTicTacToeGame.class, this::handleRunTicTacToeGameOnAServer);
|
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.RunTicTacToeGame.class, this::handleRunTicTacToeGameOnAServer);
|
||||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.EndTicTacToeGame.class, this::handleEndTicTacToeGameOnAServer);
|
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.EndTicTacToeGame.class, this::handleEndTicTacToeGameOnAServer);
|
||||||
}
|
}
|
||||||
@@ -69,18 +69,18 @@ public class ServerManager {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleStartTicTacToeGameOnAServer(Events.ServerEvents.CreateTicTacToeGame event) {
|
private void handleStartTicTacToeGameOnAServer(Events.ServerEvents.CreateTicTacToeGameRequest event) {
|
||||||
TicTacToeServer serverThing = (TicTacToeServer) this.servers.get(event.serverUuid());
|
TicTacToeServer serverThing = (TicTacToeServer) this.servers.get(event.serverUuid());
|
||||||
String gameId = null;
|
String gameId = null;
|
||||||
if (serverThing != null) {
|
if (serverThing != null) {
|
||||||
try {
|
try {
|
||||||
gameId = serverThing.newGame(event.playerA(), event.playerB());
|
gameId = serverThing.newGame(event.playerA(), event.playerB());
|
||||||
logger.info("Created game on server {}", event.serverUuid());
|
logger.info("Created game on server: {}", event.serverUuid());
|
||||||
}
|
}
|
||||||
catch (Exception e) { // TODO: Error handling
|
catch (Exception e) { // TODO: Error handling
|
||||||
logger.info("Could not create game on server {}", event.serverUuid());
|
logger.error("Could not create game on server: {}", event.serverUuid());
|
||||||
}
|
}
|
||||||
}
|
} else { logger.warn("Could not find server: {}", event.serverUuid()); }
|
||||||
event.future().complete(gameId);
|
event.future().complete(gameId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user