Added logging to game

This commit is contained in:
lieght
2025-09-17 13:03:26 +02:00
parent e1f2fe38e5
commit 5be2be56ad
5 changed files with 19 additions and 21 deletions

View File

@@ -1,11 +1,20 @@
package org.toop; package org.toop;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus;
import org.toop.game.*; import org.toop.game.*;
import org.toop.game.tictactoe.*; import org.toop.game.tictactoe.*;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class ConsoleGui { public class ConsoleGui {
private static final Logger logger = LogManager.getLogger(ConsoleGui.class);
private Scanner scanner; private Scanner scanner;
private TicTacToe game; private TicTacToe game;
@@ -14,7 +23,7 @@ public class ConsoleGui {
String ai1 = null; String ai1 = null;
String ai2 = null; String ai2 = null;
public ConsoleGui() { public ConsoleGui() throws ExecutionException, InterruptedException {
scanner = new Scanner(System.in); scanner = new Scanner(System.in);
Random random = new Random(3453498); Random random = new Random(3453498);
@@ -32,6 +41,7 @@ public class ConsoleGui {
try { try {
mode = Integer.parseInt(modeString); mode = Integer.parseInt(modeString);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage());
} }
String player1 = null; String player1 = null;

View File

@@ -19,24 +19,10 @@ import java.util.concurrent.ExecutionException;
public class Main { public class Main {
private static final Logger logger = LogManager.getLogger(Main.class); private static final Logger logger = LogManager.getLogger(Main.class);
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException { public static void main(String[] args) throws ExecutionException, InterruptedException {
initSystems(); initSystems();
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest("5001", "tictactoe", serverIdFuture));
String serverId = serverIdFuture.get();
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", connectionIdFuture));
String connectionId = connectionIdFuture.get();
CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "John", "Pim", ticTacToeGame));
String ticTacToeGameId = ticTacToeGame.get();
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId));
ConsoleGui console = new ConsoleGui(); ConsoleGui console = new ConsoleGui();
GameBase.State state = GameBase.State.INVALID; GameBase.State state = GameBase.State.INVALID;

View File

@@ -139,7 +139,7 @@ public class Events implements IEvents {
/** /**
* Triggers when the server client receives a message. * Triggers when the server client receives a message.
*/ */
public record ReceivedMessage(String message) {} public record ReceivedMessage(String ConnectionUuid, String message) {}
/** /**
* Triggers on changing the server ip. * Triggers on changing the server ip.

View File

@@ -36,7 +36,7 @@ public class ConnectionManager {
private String startConnectionRequest(String ip, String port) { private String startConnectionRequest(String ip, String port) {
String connectionId = UUID.randomUUID().toString(); String connectionId = UUID.randomUUID().toString();
ServerConnection connection = new ServerConnection(ip, port); ServerConnection connection = new ServerConnection(connectionId, ip, port);
this.serverConnections.put(connectionId, connection); this.serverConnections.put(connectionId, connection);
new Thread(connection, "Connection-" + connectionId).start(); new Thread(connection, "Connection-" + connectionId).start();
logger.info("Connected to server {} at {}:{}", connectionId, ip, port); logger.info("Connected to server {} at {}:{}", connectionId, ip, port);

View File

@@ -18,12 +18,13 @@ public final class ServerConnection implements Runnable {
private final BlockingQueue<String> commandQueue = new LinkedBlockingQueue<>(); private final BlockingQueue<String> commandQueue = new LinkedBlockingQueue<>();
private final ExecutorService executor = Executors.newFixedThreadPool(2); private final ExecutorService executor = Executors.newFixedThreadPool(2);
String uuid;
String ip; String ip;
String port; String port;
TcpClient tcpClient; TcpClient tcpClient;
volatile boolean running = false; volatile boolean running = false;
public ServerConnection(String ip, String port) { public ServerConnection(String uuid, String ip, String port) {
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
this.initEvents(); this.initEvents();
@@ -119,6 +120,7 @@ public final class ServerConnection implements Runnable {
String received = tcpClient.readLine(); // blocks String received = tcpClient.readLine(); // blocks
if (received != null) { if (received != null) {
logger.info("Received: '{}'", received); logger.info("Received: '{}'", received);
GlobalEventBus.post(new Events.ServerEvents.ReceivedMessage(this.uuid, received));
} else { } else {
break; break;
} }
@@ -205,8 +207,8 @@ public final class ServerConnection implements Runnable {
* @param ip The address of the server to contact. * @param ip The address of the server to contact.
* @param port The port of the server. * @param port The port of the server.
*/ */
public static ServerConnection startNew(String ip, String port) { public static ServerConnection startNew(String uuid, String ip, String port) {
ServerConnection serverConnection = new ServerConnection(ip, port); ServerConnection serverConnection = new ServerConnection(uuid, ip, port);
new Thread(serverConnection).start(); new Thread(serverConnection).start();
return serverConnection; return serverConnection;
} }