Updated client logging and started with resetBoard method

This commit is contained in:
2021-03-18 11:39:49 +01:00
parent f1fc88511b
commit b72e03711a
9 changed files with 75 additions and 34 deletions

View File

@@ -10,6 +10,7 @@ public class TicTacToe_Client {
private Engine renderEngine; private Engine renderEngine;
private Client client; private Client client;
private boolean isSingleServer; private boolean isSingleServer;
private static String clientName;
public TicTacToe_Client(){ public TicTacToe_Client(){
renderEngine = Engine.waitForEngine(); renderEngine = Engine.waitForEngine();
@@ -17,7 +18,7 @@ public class TicTacToe_Client {
private void ticTacToe_gameloop(){ private void ticTacToe_gameloop(){
//Setup //Setup
client = new Client("server", 2589, "second"); client = new Client("server", 2589, clientName);
client.handshake(); client.handshake();
isSingleServer = client.getServerType(); isSingleServer = client.getServerType();
if (isSingleServer){ if (isSingleServer){
@@ -80,8 +81,8 @@ public class TicTacToe_Client {
winCoordinates.add(Integer.valueOf(s) * 300); winCoordinates.add(Integer.valueOf(s) * 300);
} }
//this.drawWinningLine(winCoordinates); //this.drawWinningLine(winCoordinates);
client.exitProcess(); //client.exitProcess();
System.exit(0); client.resetBoard();
} }
} else { } else {
int column = (int) renderEngine.getCoordinates().getX() / 300; int column = (int) renderEngine.getCoordinates().getX() / 300;
@@ -125,6 +126,11 @@ public class TicTacToe_Client {
} }
}.start(); }.start();
TicTacToe_Client test = new TicTacToe_Client(); TicTacToe_Client test = new TicTacToe_Client();
try{
clientName = args[0];
} catch (Exception e){
clientName = "testing";
}
test.ticTacToe_gameloop(); test.ticTacToe_gameloop();
} }

View File

@@ -20,10 +20,10 @@ public class ClientLogger {
switch (logType){ switch (logType){
case Log: case Log:
if (success){ if (success){
System.out.printf( ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_GREEN + "%s%n" + ANSI_RESET, System.out.printf(ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_WHITE + "[status] " + ANSI_GREEN + "%s%n" + ANSI_RESET,
new Timestamp(System.currentTimeMillis()), name, message); new Timestamp(System.currentTimeMillis()), name, message);
} else { } else {
System.out.printf(ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_RED + "%s%n" + ANSI_RESET, System.out.printf(ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_WHITE + "[status] " + ANSI_RED + "%s%n" + ANSI_RESET,
new Timestamp(System.currentTimeMillis()), name, message); new Timestamp(System.currentTimeMillis()), name, message);
} }
break; break;
@@ -32,8 +32,17 @@ public class ClientLogger {
System.out.printf(ANSI_PURPLE + "(%s) [%s] %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message); System.out.printf(ANSI_PURPLE + "(%s) [%s] %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
break; break;
case Message: case Input:
System.out.printf(ANSI_WHITE + " (%s) [%s] %s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message); System.out.printf(ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_WHITE + "[input] " + ANSI_YELLOW +"%s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
break;
case Output:
if (success){
System.out.printf(ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_WHITE + "[output] " + ANSI_GREEN +"%s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
}else {
System.out.printf(ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_WHITE + "[output] " + ANSI_RED +"%s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
}
break; break;
default: default:

View File

@@ -2,6 +2,7 @@ package logging;
public enum LogType { public enum LogType {
Log, Log,
Message, Input,
Output,
Error Error
} }

View File

@@ -58,7 +58,7 @@ public class Client {
out.writeUTF(message); out.writeUTF(message);
out.flush(); out.flush();
success = in.readInt() == 200; success = in.readInt() == 200;
clientLogger.printLog(String.format("Sent the message: %s", message), serverName, success, LogType.Log); clientLogger.printLog(String.format("Sent the message: %s", message), serverName, success, LogType.Output);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -68,7 +68,7 @@ public class Client {
try { try {
out.writeInt(200); out.writeInt(200);
out.flush(); out.flush();
clientLogger.printLog("Sent verification code", serverName, true, LogType.Log); clientLogger.printLog("Sent verification code", serverName, true, LogType.Output);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -77,7 +77,7 @@ public class Client {
public String getResponse() { public String getResponse() {
try { try {
String message = in.readUTF(); String message = in.readUTF();
clientLogger.printLog(String.format("Message recieved: %s", message), serverName, true, LogType.Log); clientLogger.printLog(String.format("Message recieved: %s", message), serverName, true, LogType.Input);
return message; return message;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -88,7 +88,7 @@ public class Client {
public boolean getBooleanResponse(String message) { public boolean getBooleanResponse(String message) {
try { try {
boolean state = in.readBoolean(); boolean state = in.readBoolean();
clientLogger.printLog(String.format("%s: %b", message, state), serverName, true, LogType.Log); clientLogger.printLog(String.format("%s: %b", message, state), serverName, true, LogType.Input);
return state; return state;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -119,7 +119,7 @@ public class Client {
boolean gameEnded = false; boolean gameEnded = false;
try { try {
gameEnded = in.readBoolean(); gameEnded = in.readBoolean();
clientLogger.printLog(String.format("Game ended: %b", gameEnded), serverName, gameEnded, LogType.Log); clientLogger.printLog(String.format("Game ended: %b", gameEnded), serverName, gameEnded, LogType.Input);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -132,7 +132,7 @@ public class Client {
public void exitProcess(){ public void exitProcess(){
try { try {
out.writeUTF("exit()"); out.writeUTF("exit");
out.flush(); out.flush();
success = in.readInt()==200; success = in.readInt()==200;
clientLogger.printLog("Closing connection to server", serverName, success, LogType.Log); clientLogger.printLog("Closing connection to server", serverName, success, LogType.Log);
@@ -141,6 +141,17 @@ public class Client {
} }
} }
public void resetBoard(){
try {
out.writeUTF("reset");
out.flush();
success = in.readInt()==200;
clientLogger.printLog("Resetting board", serverName, success, LogType.Log);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getName() { public String getName() {
return name; return name;
} }

View File

@@ -108,7 +108,6 @@ public class Engine extends Application {
private void onMouseClick(MouseEvent event) { private void onMouseClick(MouseEvent event) {
mouseClicked = true; mouseClicked = true;
coordinates.setLocation(event.getX(), event.getY()); coordinates.setLocation(event.getX(), event.getY());
System.out.println(coordinates.getX() + ":" + coordinates.getY());
} }
public boolean isMouseClicked() { public boolean isMouseClicked() {

View File

@@ -14,14 +14,14 @@ public class ServerLogger {
private static final String ANSI_CYAN = "\u001B[36m"; private static final String ANSI_CYAN = "\u001B[36m";
private static final String ANSI_WHITE = "\u001B[37m"; private static final String ANSI_WHITE = "\u001B[37m";
public void printLog(String message, String name, LogType logType){ public void printLog(String message, String value, String name, LogType logType){
switch (logType){ switch (logType){
case Log: case Log:
System.out.printf(ANSI_CYAN + "%s %s%n"+ANSI_RESET, new Timestamp(System.currentTimeMillis()), message); System.out.printf(ANSI_CYAN + "%s %s: %s%n"+ANSI_RESET, new Timestamp(System.currentTimeMillis()), message, value);
break; break;
case Error: case Error:
System.out.printf(ANSI_RED + "%s %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), message); System.out.printf(ANSI_RED + "%s %s: %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), message, value);
break; break;
case Message: case Message:
@@ -33,7 +33,11 @@ public class ServerLogger {
} }
} }
public void printLog(String message, String name, LogType logType){
this.printLog(message, "", name, logType);
}
public void printLog(String message, LogType logType){ public void printLog(String message, LogType logType){
this.printLog(message, "", logType); this.printLog(message, "", "", logType);
} }
} }

View File

@@ -37,7 +37,6 @@ public class MultiPlayerServer {
scanner = new Scanner(System.in); scanner = new Scanner(System.in);
serverLogger = new ServerLogger(); serverLogger = new ServerLogger();
requiredConnections = 2; requiredConnections = 2;
serverLogger.printLog(ticTacToe_server.getGameState(), this.getClass().getName(), LogType.Log);
serverLogger.printLog("Server started successfully", LogType.Log); serverLogger.printLog("Server started successfully", LogType.Log);
} catch (IOException e) { } catch (IOException e) {
@@ -69,7 +68,7 @@ public class MultiPlayerServer {
outstreams.get(client).writeInt(200); outstreams.get(client).writeInt(200);
outstreams.get(client).flush(); outstreams.get(client).flush();
clientNames.put(client, instreams.get(client).readUTF()); clientNames.put(client, instreams.get(client).readUTF());
serverLogger.printLog(String.format("%s got connected", clientNames.get(client)), LogType.Log); serverLogger.printLog(String.format("Client \"%s\" got connected", clientNames.get(client)), LogType.Log);
} else { } else {
outstreams.get(client).writeInt(403); outstreams.get(client).writeInt(403);
outstreams.get(client).flush(); outstreams.get(client).flush();
@@ -184,8 +183,6 @@ public class MultiPlayerServer {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else {
serverLogger.printLog("Current client not in clients!", LogType.Error);
} }
} }
break; break;

View File

@@ -68,7 +68,7 @@ public class SinglePlayerServer {
outstreams.get(client).writeInt(200); outstreams.get(client).writeInt(200);
outstreams.get(client).flush(); outstreams.get(client).flush();
clientNames.put(client, instreams.get(client).readUTF()); clientNames.put(client, instreams.get(client).readUTF());
serverLogger.printLog(String.format("%s got connected", clientNames.get(client)), LogType.Log); serverLogger.printLog(String.format("Client: \"%s\" got connected", clientNames.get(client)), LogType.Log);
} else { } else {
outstreams.get(client).writeInt(403); outstreams.get(client).writeInt(403);
outstreams.get(client).flush(); outstreams.get(client).flush();
@@ -89,7 +89,7 @@ public class SinglePlayerServer {
serverLogger.printLog(message, clientNames.get(client), LogType.Message); serverLogger.printLog(message, clientNames.get(client), LogType.Message);
outstreams.get(client).writeInt(200); outstreams.get(client).writeInt(200);
outstreams.get(client).flush(); outstreams.get(client).flush();
serverLogger.printLog("Sent verification code", clientNames.get(client), LogType.Log); serverLogger.printLog("Sent verification code", "200", clientNames.get(client), LogType.Log);
this.gameFlow(message, client); this.gameFlow(message, client);
} }
} catch (IOException e) { } catch (IOException e) {
@@ -105,9 +105,10 @@ public class SinglePlayerServer {
public void sendGameState(Socket client){ public void sendGameState(Socket client){
try { try {
outstreams.get(client).writeUTF(ticTacToe_server.getGameState()); String gameState = ticTacToe_server.getGameState();
outstreams.get(client).writeUTF(gameState);
outstreams.get(client).flush(); outstreams.get(client).flush();
serverLogger.printLog("Sent gameState", clientNames.get(client), LogType.Log); serverLogger.printLog("Sent gameState", gameState, clientNames.get(client), LogType.Log);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -131,7 +132,7 @@ public class SinglePlayerServer {
//Send confirmation (2ßß) //Send confirmation (2ßß)
outstreams.get(client).writeInt(200); outstreams.get(client).writeInt(200);
outstreams.get(client).flush(); outstreams.get(client).flush();
serverLogger.printLog("Sent verification code", clientNames.get(client), LogType.Log); serverLogger.printLog("Sent verification code", "200", clientNames.get(client), LogType.Log);
boolean moveAllowed = ticTacToe_server.makeClientMove(position); boolean moveAllowed = ticTacToe_server.makeClientMove(position);
if (moveAllowed) { if (moveAllowed) {
sendGameState(client); sendGameState(client);
@@ -163,7 +164,7 @@ public class SinglePlayerServer {
try { try {
outstreams.get(client).writeBoolean(true); outstreams.get(client).writeBoolean(true);
outstreams.get(client).flush(); outstreams.get(client).flush();
serverLogger.printLog("Sent serverType", clientNames.get(client), LogType.Log); serverLogger.printLog("Sent serverType", Boolean.toString(true), clientNames.get(client), LogType.Log);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -173,9 +174,10 @@ public class SinglePlayerServer {
for (Map.Entry<Integer, Socket> entry : clients.entrySet()) { for (Map.Entry<Integer, Socket> entry : clients.entrySet()) {
if (Objects.equals(client, entry.getValue())) { if (Objects.equals(client, entry.getValue())) {
try { try {
outstreams.get(client).writeBoolean(entry.getKey() == 0); boolean isClientOne = entry.getKey() == 0;
outstreams.get(client).writeBoolean(isClientOne);
outstreams.get(client).flush(); outstreams.get(client).flush();
serverLogger.printLog("Sent isPlayerOne", clientNames.get(client), LogType.Log); serverLogger.printLog("Sent isPlayerOne", Boolean.toString(isClientOne), clientNames.get(client), LogType.Log);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -197,13 +199,14 @@ public class SinglePlayerServer {
} }
//send winning fields //send winning fields
outstreams.get(client).writeUTF(coordinates); outstreams.get(client).writeUTF(coordinates);
serverLogger.printLog("Winning coordinates got sent", coordinates, clientNames.get(client), LogType.Log);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
break; break;
case "exit()": case "exit":
try { try {
outstreams.get(client).writeInt(200); outstreams.get(client).writeInt(200);
outstreams.get(client).flush(); outstreams.get(client).flush();
@@ -215,6 +218,17 @@ public class SinglePlayerServer {
e.printStackTrace(); e.printStackTrace();
} }
break; break;
case "reset":
try {
outstreams.get(client).writeInt(200);
outstreams.get(client).flush();
ticTacToe_server.resetGameState();
this.gameFlow("gameState", client);
} catch (IOException e) {
e.printStackTrace();
}
break;
} }
} }

View File

@@ -15,8 +15,8 @@ public class TicTacToe_Server {
gameState = "---------"; gameState = "---------";
} }
public void setGameState(String gameState) { public void resetGameState() {
this.gameState = gameState; this.gameState = "---------";
} }
public String getGameState() { public String getGameState() {