mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
Is null, needs fix wip
This commit is contained in:
@@ -24,12 +24,20 @@ public class Main {
|
||||
|
||||
initSystems();
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.StartServer("5001"));
|
||||
GlobalEventBus.post(new Events.ServerEvents.StartServer("5001", "tictactoe"));
|
||||
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", future));
|
||||
String serverId = future.get();
|
||||
|
||||
CompletableFuture<String> future2 = new CompletableFuture<>();
|
||||
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGame(serverId, "John", "Pim", future2));
|
||||
String gameId = future.get();
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, gameId));
|
||||
GlobalEventBus.post(new Events.ServerEvents.Command(serverId, "MOVE", "0"));
|
||||
GlobalEventBus.post(new Events.ServerEvents.EndTicTacToeGame(serverId, gameId));
|
||||
|
||||
// for (int i = 0; i < 1; i++) {
|
||||
// Thread thread = new Thread(() -> {
|
||||
//// logger.info("Server ID: {}", serverId);
|
||||
@@ -38,10 +46,8 @@ public class Main {
|
||||
// thread.start();
|
||||
// }
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.Command(serverId, "HELP", "TEST"));
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllConnections());
|
||||
GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllServers());
|
||||
// GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllConnections());
|
||||
// GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllServers());
|
||||
|
||||
//
|
||||
// CompletableFuture<String> future2 = new CompletableFuture<>();
|
||||
|
||||
@@ -84,13 +84,17 @@ public class Events implements IEvents {
|
||||
|
||||
public record ForceCloseAllServers() {}
|
||||
|
||||
public record StartServer(String port) {}
|
||||
public record StartServer(String port, String gameType) {}
|
||||
|
||||
public record StartServerRequest(String port, CompletableFuture<String> future) {}
|
||||
public record StartServerRequest(String port, String gameType, CompletableFuture<String> future) {}
|
||||
|
||||
public record ServerStarted(String uuid, String port) {}
|
||||
|
||||
public record StartTicTacToeGame(String id, String port) {}
|
||||
public record CreateTicTacToeGame(String serverUuid, String playerA, String playerB, CompletableFuture<String> future) {}
|
||||
|
||||
public record RunTicTacToeGame(String serverUuid, String gameUuid) {}
|
||||
|
||||
public record EndTicTacToeGame(String serverUuid, String gameUuid) {}
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -5,9 +5,11 @@ import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.server.backend.tictactoe.TicTacToeServer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -29,13 +31,23 @@ public class ServerManager {
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServerRequest.class, this::handleStartServerRequest);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServer.class, this::handleStartServer);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ForceCloseAllServers.class, _ -> shutdownAll());
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartTicTacToeGame.class, this::handleStartTicTacToeGameOnAServer);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.CreateTicTacToeGame.class, this::handleStartTicTacToeGameOnAServer);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.RunTicTacToeGame.class, this::handleRunTicTacToeGameOnAServer);
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.EndTicTacToeGame.class, this::handleEndTicTacToeGameOnAServer);
|
||||
}
|
||||
|
||||
private String startServer(String port) {
|
||||
private String startServer(String port, String gameType) {
|
||||
String serverId = UUID.randomUUID().toString();
|
||||
gameType = gameType.toLowerCase();
|
||||
try {
|
||||
TcpServer server = new TcpServer(Integer.parseInt(port));
|
||||
TcpServer server = null;
|
||||
if (Objects.equals(gameType, "tictactoe")) {
|
||||
server = new TicTacToeServer(Integer.parseInt(port));
|
||||
}
|
||||
else {
|
||||
logger.error("Manager could not create a TcpServer for {}", gameType);
|
||||
return null;
|
||||
}
|
||||
this.servers.put(serverId, server);
|
||||
new Thread(server, "Server-" + serverId).start();
|
||||
logger.info("Connected to server {} at {}", serverId, port);
|
||||
@@ -47,28 +59,39 @@ public class ServerManager {
|
||||
}
|
||||
|
||||
private void handleStartServerRequest(Events.ServerEvents.StartServerRequest request) {
|
||||
request.future().complete(this.startServer(request.port())); // TODO: Maybe post StartServer event.
|
||||
request.future().complete(this.startServer(request.port(), request.gameType())); // TODO: Maybe post StartServer event.
|
||||
}
|
||||
|
||||
private void handleStartServer(Events.ServerEvents.StartServer event) {
|
||||
GlobalEventBus.post(new Events.ServerEvents.ServerStarted(
|
||||
this.startServer(event.port()),
|
||||
this.startServer(event.port(), event.gameType()),
|
||||
event.port()
|
||||
));
|
||||
}
|
||||
|
||||
private void handleStartTicTacToeGameOnAServer(Events.ServerEvents.StartTicTacToeGame event) {
|
||||
TcpServer serverThing = this.servers.get(event.id());
|
||||
private void handleStartTicTacToeGameOnAServer(Events.ServerEvents.CreateTicTacToeGame event) {
|
||||
TicTacToeServer serverThing = (TicTacToeServer) this.servers.get(event.serverUuid());
|
||||
String gameId = null;
|
||||
if (serverThing != null) {
|
||||
try {
|
||||
serverThing.runGame();
|
||||
logger.info("Started game on server {}", event.id());
|
||||
gameId = serverThing.newGame(event.playerA(), event.playerB());
|
||||
logger.info("Created game on server {}", event.serverUuid());
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.info("Could not start game on server {}", event.id());
|
||||
catch (Exception e) { // TODO: Error handling
|
||||
logger.info("Could not create game on server {}", event.serverUuid());
|
||||
}
|
||||
}
|
||||
event.future().complete(gameId);
|
||||
}
|
||||
|
||||
private void handleRunTicTacToeGameOnAServer(Events.ServerEvents.RunTicTacToeGame event) {
|
||||
TicTacToeServer gameServer = (TicTacToeServer) this.servers.get(event.serverUuid());
|
||||
gameServer.runGame(event.gameUuid());
|
||||
}
|
||||
|
||||
private void handleEndTicTacToeGameOnAServer(Events.ServerEvents.EndTicTacToeGame event) {
|
||||
TicTacToeServer gameServer = (TicTacToeServer) this.servers.get(event.serverUuid());
|
||||
gameServer.endGame(event.gameUuid());
|
||||
}
|
||||
|
||||
private void getAllServers(Events.ServerEvents.RequestsAllServers request) {
|
||||
|
||||
@@ -50,6 +50,10 @@ public class TcpServer implements Runnable {
|
||||
|
||||
public void runGame() {}
|
||||
|
||||
public void endGame() {}
|
||||
|
||||
public void newGame() {}
|
||||
|
||||
protected String sendServerMessage() {
|
||||
try { return sendQueue.poll(this.WAIT_TIME, TimeUnit.MILLISECONDS); }
|
||||
catch (InterruptedException e) {
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
package org.toop.server.backend.tictactoe;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.server.backend.tictactoe.game.TicTacToe;
|
||||
import org.toop.server.backend.TcpServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectStreamException;
|
||||
import java.net.Socket;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class TicTacToeServer extends TcpServer {
|
||||
|
||||
protected static final Logger logger = LogManager.getLogger(TicTacToeServer.class);
|
||||
/**
|
||||
* Map of gameId -> Game instances
|
||||
*/
|
||||
@@ -36,11 +38,13 @@ public class TicTacToeServer extends TcpServer {
|
||||
}
|
||||
}
|
||||
|
||||
public void newGame(String playerA, String playerB) {
|
||||
public String newGame(String playerA, String playerB) {
|
||||
logger.info("Creating a new game: {} vs {}", playerA, playerB);
|
||||
String gameId = UUID.randomUUID().toString();
|
||||
TicTacToe game = new TicTacToe(playerA, playerB);
|
||||
this.games.put(gameId, game);
|
||||
logger.info("Created a new game: {}. {} vs {}", gameId, playerA, playerB);
|
||||
return gameId;
|
||||
}
|
||||
|
||||
public void runGame(String gameId) {
|
||||
@@ -52,7 +56,7 @@ public class TicTacToeServer extends TcpServer {
|
||||
public void endGame(String gameId) {
|
||||
TicTacToe game = this.games.get(gameId);
|
||||
this.games.remove(gameId);
|
||||
logger.info("Removed game: {}", gameId);
|
||||
logger.info("Ended game: {}", gameId);
|
||||
// TODO: Multithreading, close game in a graceful matter, etc.
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,9 @@ public class TicTacToe extends GameBase implements Runnable {
|
||||
|
||||
private void gameThread() {
|
||||
while (true) {
|
||||
String command = getNewestCommand();
|
||||
command = this.parseCommand(command).toString();
|
||||
if (command == null) { continue; }
|
||||
// String command = getNewestCommand();
|
||||
// command = this.parseCommand(command).toString();
|
||||
// if (command == null) { continue; }
|
||||
|
||||
// TODO: Game
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user