mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Added logging to game
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
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.tictactoe.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class ConsoleGui {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(ConsoleGui.class);
|
||||
|
||||
private Scanner scanner;
|
||||
|
||||
private TicTacToe game;
|
||||
@@ -14,7 +23,7 @@ public class ConsoleGui {
|
||||
String ai1 = null;
|
||||
String ai2 = null;
|
||||
|
||||
public ConsoleGui() {
|
||||
public ConsoleGui() throws ExecutionException, InterruptedException {
|
||||
scanner = new Scanner(System.in);
|
||||
Random random = new Random(3453498);
|
||||
|
||||
@@ -32,6 +41,7 @@ public class ConsoleGui {
|
||||
try {
|
||||
mode = Integer.parseInt(modeString);
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
|
||||
String player1 = null;
|
||||
|
||||
@@ -19,24 +19,10 @@ import java.util.concurrent.ExecutionException;
|
||||
public class Main {
|
||||
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();
|
||||
|
||||
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();
|
||||
GameBase.State state = GameBase.State.INVALID;
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ public class Events implements IEvents {
|
||||
/**
|
||||
* 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.
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ConnectionManager {
|
||||
|
||||
private String startConnectionRequest(String ip, String port) {
|
||||
String connectionId = UUID.randomUUID().toString();
|
||||
ServerConnection connection = new ServerConnection(ip, port);
|
||||
ServerConnection connection = new ServerConnection(connectionId, ip, port);
|
||||
this.serverConnections.put(connectionId, connection);
|
||||
new Thread(connection, "Connection-" + connectionId).start();
|
||||
logger.info("Connected to server {} at {}:{}", connectionId, ip, port);
|
||||
|
||||
@@ -18,12 +18,13 @@ public final class ServerConnection implements Runnable {
|
||||
private final BlockingQueue<String> commandQueue = new LinkedBlockingQueue<>();
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
|
||||
String uuid;
|
||||
String ip;
|
||||
String port;
|
||||
TcpClient tcpClient;
|
||||
volatile boolean running = false;
|
||||
|
||||
public ServerConnection(String ip, String port) {
|
||||
public ServerConnection(String uuid, String ip, String port) {
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
this.initEvents();
|
||||
@@ -119,6 +120,7 @@ public final class ServerConnection implements Runnable {
|
||||
String received = tcpClient.readLine(); // blocks
|
||||
if (received != null) {
|
||||
logger.info("Received: '{}'", received);
|
||||
GlobalEventBus.post(new Events.ServerEvents.ReceivedMessage(this.uuid, received));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -205,8 +207,8 @@ public final class ServerConnection implements Runnable {
|
||||
* @param ip The address of the server to contact.
|
||||
* @param port The port of the server.
|
||||
*/
|
||||
public static ServerConnection startNew(String ip, String port) {
|
||||
ServerConnection serverConnection = new ServerConnection(ip, port);
|
||||
public static ServerConnection startNew(String uuid, String ip, String port) {
|
||||
ServerConnection serverConnection = new ServerConnection(uuid, ip, port);
|
||||
new Thread(serverConnection).start();
|
||||
return serverConnection;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user