Basics of client server communication and drawing of gamestates finished
This commit is contained in:
3
Server/res/META-INF/MANIFEST.MF
Normal file
3
Server/res/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: networking.Server
|
||||
|
||||
41
Server/src/game/TicTacToe_Server.java
Normal file
41
Server/src/game/TicTacToe_Server.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package game;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class TicTacToe_Server {
|
||||
|
||||
private String gameState;
|
||||
|
||||
public TicTacToe_Server(){
|
||||
gameState = "---------";
|
||||
}
|
||||
|
||||
public void setGameState(String gameState) {
|
||||
this.gameState = gameState;
|
||||
}
|
||||
|
||||
public String getGameState() {
|
||||
return gameState;
|
||||
}
|
||||
|
||||
public int makeClientMove(String input) {
|
||||
int column = Double.valueOf(input.split("\\|")[0]).intValue() / 300;
|
||||
int row = Double.valueOf(input.split("\\|")[1]).intValue() / 300;
|
||||
int index = column * 3 + row;
|
||||
|
||||
if (isLegalMove(column, row)) {
|
||||
byte[] gameStateBytes = gameState.getBytes();
|
||||
gameStateBytes[index] = (byte) 'x';
|
||||
gameState = new String(gameStateBytes, StandardCharsets.UTF_8);
|
||||
return 200;
|
||||
} else {
|
||||
return 404;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isLegalMove(int column, int row){
|
||||
int index = column * 3 + row;
|
||||
return gameState.charAt(index) == '-';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package networking;
|
||||
|
||||
import game.TicTacToe_Server;
|
||||
import logging.LogType;
|
||||
import logging.ServerLogger;
|
||||
|
||||
@@ -15,7 +16,8 @@ public class Server {
|
||||
private HashMap<Socket, String> clientNames;
|
||||
private HashMap<Socket, DataOutputStream> outstreams;
|
||||
private HashMap<Socket, DataInputStream> instreams;
|
||||
private ServerLogger logger;
|
||||
private TicTacToe_Server ticTacToe_server;
|
||||
private ServerLogger serverLogger;
|
||||
private Scanner scanner;
|
||||
private int requiredConnections;
|
||||
|
||||
@@ -26,11 +28,12 @@ public class Server {
|
||||
clientNames = new HashMap<>();
|
||||
outstreams = new HashMap<>();
|
||||
instreams = new HashMap<>();
|
||||
ticTacToe_server = new TicTacToe_Server();
|
||||
scanner = new Scanner(System.in);
|
||||
logger = new ServerLogger();
|
||||
serverLogger = new ServerLogger();
|
||||
requiredConnections = 1;
|
||||
|
||||
logger.printLog("Server started successfully", LogType.Log);
|
||||
serverLogger.printLog("Server started successfully", LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -39,7 +42,7 @@ public class Server {
|
||||
public void connectClients(){
|
||||
try {
|
||||
int id = 0;
|
||||
logger.printLog(String.format("Waiting for %d clients to connect ...", requiredConnections), LogType.Log);
|
||||
serverLogger.printLog(String.format("Waiting for %d clients to connect ...", requiredConnections), LogType.Log);
|
||||
while(clients.size() < requiredConnections) {
|
||||
Socket momentaryClient = serverSocket.accept();
|
||||
clients.put(id, momentaryClient);
|
||||
@@ -62,7 +65,7 @@ public class Server {
|
||||
clientNames.put(client, instreams.get(client).readUTF());
|
||||
outstreams.get(client).writeUTF(serverSocket.getInetAddress().getHostAddress()+":"+serverSocket.getLocalPort());
|
||||
outstreams.get(client).flush();
|
||||
logger.printLog(String.format("%s got connected", clientNames.get(client)), LogType.Log);
|
||||
serverLogger.printLog(String.format("%s got connected", clientNames.get(client)), LogType.Log);
|
||||
} else {
|
||||
outstreams.get(client).writeInt(403);
|
||||
outstreams.get(client).flush();
|
||||
@@ -74,19 +77,16 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
public void getMessages(){
|
||||
public void ticTacToe_gameloop(){
|
||||
for (Socket client: clients.values()) {
|
||||
try {
|
||||
while (true) {
|
||||
while (!client.isClosed()) {
|
||||
String message = instreams.get(client).readUTF();
|
||||
if (!message.equalsIgnoreCase("exit()")) {
|
||||
logger.printLog(message, clientNames.get(client), LogType.Message);
|
||||
} else {
|
||||
outstreams.get(client).writeInt(200);
|
||||
logger.printLog(String.format("%s closed the connection",clientNames.get(client)), LogType.Log);
|
||||
break;
|
||||
}
|
||||
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);
|
||||
this.gameFlow(message, client);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -97,10 +97,56 @@ public class Server {
|
||||
}
|
||||
}
|
||||
}
|
||||
clients.clear();
|
||||
System.out.println("Do you want to keep the server alive and wait for other clients ? [y]/[n]");
|
||||
if (scanner.nextLine().equalsIgnoreCase("y")){
|
||||
this.connectClients();
|
||||
}
|
||||
|
||||
public void gameFlow(String input, Socket client){
|
||||
switch (input){
|
||||
case "gameState":
|
||||
try {
|
||||
outstreams.get(client).writeUTF(ticTacToe_server.getGameState());
|
||||
outstreams.get(client).flush();
|
||||
serverLogger.printLog("Sent gameState", clientNames.get(client), LogType.Log);
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
case "update":
|
||||
try {
|
||||
String position = instreams.get(client).readUTF();
|
||||
serverLogger.printLog(position, clientNames.get(client), LogType.Message);
|
||||
outstreams.get(client).writeInt(200);
|
||||
outstreams.get(client).flush();
|
||||
serverLogger.printLog("Sent verification code", clientNames.get(client), LogType.Log);
|
||||
int verificationCode = ticTacToe_server.makeClientMove(position);
|
||||
if (verificationCode == 200) {
|
||||
String gameState = ticTacToe_server.getGameState();
|
||||
outstreams.get(client).writeUTF(gameState);
|
||||
outstreams.get(client).flush();
|
||||
serverLogger.printLog(String.format("Sent gameState: %s", gameState), clientNames.get(client), LogType.Log);
|
||||
break;
|
||||
} else {
|
||||
outstreams.get(client).writeUTF(" ");
|
||||
outstreams.get(client).flush();
|
||||
serverLogger.printLog(String.format("Move is not allowed!"), clientNames.get(client), LogType.Error);
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
case "exit()":
|
||||
try {
|
||||
outstreams.get(client).writeInt(200);
|
||||
outstreams.get(client).flush();
|
||||
outstreams.get(client).close();
|
||||
instreams.get(client).close();
|
||||
client.close();
|
||||
serverLogger.printLog(String.format("%s closed the connection",clientNames.get(client)), LogType.Log);
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,6 +154,6 @@ public class Server {
|
||||
Server server = new Server(2589);
|
||||
server.connectClients();
|
||||
server.handshake();
|
||||
server.getMessages();
|
||||
server.ticTacToe_gameloop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user