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

View File

@@ -20,10 +20,10 @@ public class ClientLogger {
switch (logType){
case Log:
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);
} 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);
}
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);
break;
case Message:
System.out.printf(ANSI_WHITE + " (%s) [%s] %s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
case Input:
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;
default:

View File

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

View File

@@ -58,7 +58,7 @@ public class Client {
out.writeUTF(message);
out.flush();
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) {
e.printStackTrace();
}
@@ -68,7 +68,7 @@ public class Client {
try {
out.writeInt(200);
out.flush();
clientLogger.printLog("Sent verification code", serverName, true, LogType.Log);
clientLogger.printLog("Sent verification code", serverName, true, LogType.Output);
} catch (IOException e) {
e.printStackTrace();
}
@@ -77,7 +77,7 @@ public class Client {
public String getResponse() {
try {
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;
} catch (IOException e) {
e.printStackTrace();
@@ -88,7 +88,7 @@ public class Client {
public boolean getBooleanResponse(String message) {
try {
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;
} catch (IOException e) {
e.printStackTrace();
@@ -119,7 +119,7 @@ public class Client {
boolean gameEnded = false;
try {
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) {
e.printStackTrace();
}
@@ -132,7 +132,7 @@ public class Client {
public void exitProcess(){
try {
out.writeUTF("exit()");
out.writeUTF("exit");
out.flush();
success = in.readInt()==200;
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() {
return name;
}

View File

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