Updated client logging and started with resetBoard method
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user