Combined SingleServer and MultiServer into one class

This commit is contained in:
2021-03-20 16:30:54 +01:00
parent fa7bda4230
commit 1b4bcd1ec2
16 changed files with 115 additions and 339 deletions

View File

@@ -13,21 +13,21 @@ public class Client {
private Socket serverSocket;
private DataOutputStream out;
private DataInputStream in;
private static Scanner scanner;
private ClientLogger clientLogger;
private static String name;
private String serverName;
private boolean success;
private boolean authorizedToMove;
public Client(String ip, int port, String name) {
try {
scanner = new Scanner(System.in);
serverSocket = new Socket(ip, port);
serverName = serverSocket.getRemoteSocketAddress().toString();
out = new DataOutputStream(serverSocket.getOutputStream());
in = new DataInputStream(serverSocket.getInputStream());
clientLogger = new ClientLogger();
success = true;
authorizedToMove = false;
this.name = name;
clientLogger.printLog(String.format("Client with the name %s successfully initialized", name), success, LogType.Log);
} catch (IOException e) {
@@ -64,16 +64,6 @@ public class Client {
}
}
public void sendConfirmation(){
try {
out.writeInt(200);
out.flush();
clientLogger.printLog("Sent verification code", serverName, true, LogType.Output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getResponse() {
try {
String message = in.readUTF();
@@ -108,12 +98,6 @@ public class Client {
return serverType;
}
public String getGameState() {
this.sendToServer("gameState");
String gameState = this.getResponse();
return gameState;
}
public boolean getGameEnded() {
this.sendToServer("gameEnded");
boolean gameEnded = false;
@@ -126,10 +110,6 @@ public class Client {
return gameEnded;
}
public void waitForInput() {
}
public void exitProcess(){
try {
out.writeUTF("exit");
@@ -152,12 +132,16 @@ public class Client {
}
}
public void printLog(String message, boolean success){
clientLogger.printLog(message, success, LogType.Log);
public boolean isAuthorizedToMove(){
return authorizedToMove;
}
public String getName() {
return name;
public void setAuthorizedToMove(boolean isAuthorizedToMove){
authorizedToMove = isAuthorizedToMove;
}
public void printLog(String message, boolean success){
clientLogger.printLog(message, success, LogType.Log);
}
public boolean isConnected(){