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() {

View File

@@ -14,14 +14,14 @@ public class ServerLogger {
private static final String ANSI_CYAN = "\u001B[36m";
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){
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;
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;
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){
this.printLog(message, "", logType);
this.printLog(message, "", "", logType);
}
}

View File

@@ -37,7 +37,6 @@ public class MultiPlayerServer {
scanner = new Scanner(System.in);
serverLogger = new ServerLogger();
requiredConnections = 2;
serverLogger.printLog(ticTacToe_server.getGameState(), this.getClass().getName(), LogType.Log);
serverLogger.printLog("Server started successfully", LogType.Log);
} catch (IOException e) {
@@ -69,7 +68,7 @@ public class MultiPlayerServer {
outstreams.get(client).writeInt(200);
outstreams.get(client).flush();
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 {
outstreams.get(client).writeInt(403);
outstreams.get(client).flush();
@@ -184,8 +183,6 @@ public class MultiPlayerServer {
} catch (IOException e) {
e.printStackTrace();
}
} else {
serverLogger.printLog("Current client not in clients!", LogType.Error);
}
}
break;

View File

@@ -68,7 +68,7 @@ public class SinglePlayerServer {
outstreams.get(client).writeInt(200);
outstreams.get(client).flush();
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 {
outstreams.get(client).writeInt(403);
outstreams.get(client).flush();
@@ -89,7 +89,7 @@ public class SinglePlayerServer {
serverLogger.printLog(message, clientNames.get(client), LogType.Message);
outstreams.get(client).writeInt(200);
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);
}
} catch (IOException e) {
@@ -105,9 +105,10 @@ public class SinglePlayerServer {
public void sendGameState(Socket client){
try {
outstreams.get(client).writeUTF(ticTacToe_server.getGameState());
String gameState = ticTacToe_server.getGameState();
outstreams.get(client).writeUTF(gameState);
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) {
e.printStackTrace();
}
@@ -131,7 +132,7 @@ public class SinglePlayerServer {
//Send confirmation (2ßß)
outstreams.get(client).writeInt(200);
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);
if (moveAllowed) {
sendGameState(client);
@@ -163,7 +164,7 @@ public class SinglePlayerServer {
try {
outstreams.get(client).writeBoolean(true);
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) {
e.printStackTrace();
}
@@ -173,9 +174,10 @@ public class SinglePlayerServer {
for (Map.Entry<Integer, Socket> entry : clients.entrySet()) {
if (Objects.equals(client, entry.getValue())) {
try {
outstreams.get(client).writeBoolean(entry.getKey() == 0);
boolean isClientOne = entry.getKey() == 0;
outstreams.get(client).writeBoolean(isClientOne);
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) {
e.printStackTrace();
}
@@ -197,13 +199,14 @@ public class SinglePlayerServer {
}
//send winning fields
outstreams.get(client).writeUTF(coordinates);
serverLogger.printLog("Winning coordinates got sent", coordinates, clientNames.get(client), LogType.Log);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
case "exit()":
case "exit":
try {
outstreams.get(client).writeInt(200);
outstreams.get(client).flush();
@@ -215,6 +218,17 @@ public class SinglePlayerServer {
e.printStackTrace();
}
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 = "---------";
}
public void setGameState(String gameState) {
this.gameState = gameState;
public void resetGameState() {
this.gameState = "---------";
}
public String getGameState() {