Combined SingleServer and MultiServer into one class
This commit is contained in:
@@ -24,7 +24,7 @@ public class TicTacToe_Client {
|
|||||||
if (isSingleServer){
|
if (isSingleServer){
|
||||||
this.setWindowTitle(client.isPlayerOne());
|
this.setWindowTitle(client.isPlayerOne());
|
||||||
client.sendToServer("ready");
|
client.sendToServer("ready");
|
||||||
while (client.isConnected()) {
|
while (client.isConnected() && !renderEngine.isWindowClosed()) {
|
||||||
String message = client.getResponse();
|
String message = client.getResponse();
|
||||||
//Check if message is gamestate
|
//Check if message is gamestate
|
||||||
if (message.charAt(0) == 'x' || message.charAt(0) == '-' || message.charAt(0) == 'o') {
|
if (message.charAt(0) == 'x' || message.charAt(0) == '-' || message.charAt(0) == 'o') {
|
||||||
@@ -36,6 +36,11 @@ public class TicTacToe_Client {
|
|||||||
this.gameFlow(message);
|
this.gameFlow(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
client.exitProcess();
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.setWindowTitle(client.isPlayerOne());
|
this.setWindowTitle(client.isPlayerOne());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,21 +13,21 @@ public class Client {
|
|||||||
private Socket serverSocket;
|
private Socket serverSocket;
|
||||||
private DataOutputStream out;
|
private DataOutputStream out;
|
||||||
private DataInputStream in;
|
private DataInputStream in;
|
||||||
private static Scanner scanner;
|
|
||||||
private ClientLogger clientLogger;
|
private ClientLogger clientLogger;
|
||||||
private static String name;
|
private static String name;
|
||||||
private String serverName;
|
private String serverName;
|
||||||
private boolean success;
|
private boolean success;
|
||||||
|
private boolean authorizedToMove;
|
||||||
|
|
||||||
public Client(String ip, int port, String name) {
|
public Client(String ip, int port, String name) {
|
||||||
try {
|
try {
|
||||||
scanner = new Scanner(System.in);
|
|
||||||
serverSocket = new Socket(ip, port);
|
serverSocket = new Socket(ip, port);
|
||||||
serverName = serverSocket.getRemoteSocketAddress().toString();
|
serverName = serverSocket.getRemoteSocketAddress().toString();
|
||||||
out = new DataOutputStream(serverSocket.getOutputStream());
|
out = new DataOutputStream(serverSocket.getOutputStream());
|
||||||
in = new DataInputStream(serverSocket.getInputStream());
|
in = new DataInputStream(serverSocket.getInputStream());
|
||||||
clientLogger = new ClientLogger();
|
clientLogger = new ClientLogger();
|
||||||
success = true;
|
success = true;
|
||||||
|
authorizedToMove = false;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
clientLogger.printLog(String.format("Client with the name %s successfully initialized", name), success, LogType.Log);
|
clientLogger.printLog(String.format("Client with the name %s successfully initialized", name), success, LogType.Log);
|
||||||
} catch (IOException e) {
|
} 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() {
|
public String getResponse() {
|
||||||
try {
|
try {
|
||||||
String message = in.readUTF();
|
String message = in.readUTF();
|
||||||
@@ -108,12 +98,6 @@ public class Client {
|
|||||||
return serverType;
|
return serverType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getGameState() {
|
|
||||||
this.sendToServer("gameState");
|
|
||||||
String gameState = this.getResponse();
|
|
||||||
return gameState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getGameEnded() {
|
public boolean getGameEnded() {
|
||||||
this.sendToServer("gameEnded");
|
this.sendToServer("gameEnded");
|
||||||
boolean gameEnded = false;
|
boolean gameEnded = false;
|
||||||
@@ -126,10 +110,6 @@ public class Client {
|
|||||||
return gameEnded;
|
return gameEnded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void waitForInput() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void exitProcess(){
|
public void exitProcess(){
|
||||||
try {
|
try {
|
||||||
out.writeUTF("exit");
|
out.writeUTF("exit");
|
||||||
@@ -152,12 +132,16 @@ public class Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printLog(String message, boolean success){
|
public boolean isAuthorizedToMove(){
|
||||||
clientLogger.printLog(message, success, LogType.Log);
|
return authorizedToMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public void setAuthorizedToMove(boolean isAuthorizedToMove){
|
||||||
return name;
|
authorizedToMove = isAuthorizedToMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printLog(String message, boolean success){
|
||||||
|
clientLogger.printLog(message, success, LogType.Log);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected(){
|
public boolean isConnected(){
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public class Engine extends Application {
|
|||||||
private static final CountDownLatch latch = new CountDownLatch(1);
|
private static final CountDownLatch latch = new CountDownLatch(1);
|
||||||
private static Engine engine = null;
|
private static Engine engine = null;
|
||||||
private boolean mouseClicked = false;
|
private boolean mouseClicked = false;
|
||||||
|
private boolean windowClosed = false;
|
||||||
private Point coordinates = new Point();
|
private Point coordinates = new Point();
|
||||||
private Stage primaryStage;
|
private Stage primaryStage;
|
||||||
|
|
||||||
@@ -112,6 +113,8 @@ public class Engine extends Application {
|
|||||||
coordinates.setLocation(event.getX(), event.getY());
|
coordinates.setLocation(event.getX(), event.getY());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isWindowClosed() {return windowClosed;}
|
||||||
|
|
||||||
public boolean isMouseClicked() {
|
public boolean isMouseClicked() {
|
||||||
return mouseClicked;
|
return mouseClicked;
|
||||||
}
|
}
|
||||||
@@ -134,6 +137,10 @@ public class Engine extends Application {
|
|||||||
primaryStage.setScene(this.setScene());
|
primaryStage.setScene(this.setScene());
|
||||||
primaryStage.sizeToScene();
|
primaryStage.sizeToScene();
|
||||||
primaryStage.show();
|
primaryStage.show();
|
||||||
|
|
||||||
|
primaryStage.setOnCloseRequest(event -> {
|
||||||
|
windowClosed = true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
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.TicTacToe_Server
|
||||||
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
Manifest-Version: 1.0
|
|
||||||
Main-Class: networking.SinglePlayerServer
|
|
||||||
|
|
||||||
@@ -1,229 +0,0 @@
|
|||||||
package networking;
|
|
||||||
|
|
||||||
import res.TicTacToe_Server;
|
|
||||||
import logging.LogType;
|
|
||||||
import logging.ServerLogger;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.DataInputStream;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class MultiPlayerServer {
|
|
||||||
private ServerSocket serverSocket;
|
|
||||||
private HashMap<Integer, Socket> clients;
|
|
||||||
private HashMap<Socket, String> clientNames;
|
|
||||||
private HashMap<Socket, DataOutputStream> outstreams;
|
|
||||||
private HashMap<Socket, DataInputStream> instreams;
|
|
||||||
private TicTacToe_Server ticTacToe_server;
|
|
||||||
private ServerLogger serverLogger;
|
|
||||||
private Scanner scanner;
|
|
||||||
private int requiredConnections;
|
|
||||||
|
|
||||||
public MultiPlayerServer(int port) {
|
|
||||||
try {
|
|
||||||
serverSocket = new ServerSocket(port);
|
|
||||||
clients = new HashMap<>();
|
|
||||||
clientNames = new HashMap<>();
|
|
||||||
outstreams = new HashMap<>();
|
|
||||||
instreams = new HashMap<>();
|
|
||||||
ticTacToe_server = new TicTacToe_Server();
|
|
||||||
scanner = new Scanner(System.in);
|
|
||||||
serverLogger = new ServerLogger();
|
|
||||||
requiredConnections = 2;
|
|
||||||
|
|
||||||
serverLogger.printLog("Server started successfully", LogType.Log);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connectClients() {
|
|
||||||
try {
|
|
||||||
int id = 0;
|
|
||||||
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);
|
|
||||||
outstreams.put(momentaryClient, new DataOutputStream(momentaryClient.getOutputStream()));
|
|
||||||
instreams.put(momentaryClient, new DataInputStream(momentaryClient.getInputStream()));
|
|
||||||
id++;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handshake() {
|
|
||||||
for (Socket client : clients.values()) {
|
|
||||||
try {
|
|
||||||
int handshakeValue = instreams.get(client).readInt();
|
|
||||||
if (handshakeValue == 165313125) {
|
|
||||||
outstreams.get(client).writeInt(200);
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
clientNames.put(client, instreams.get(client).readUTF());
|
|
||||||
serverLogger.printLog(String.format("Client \"%s\" got connected", clientNames.get(client)), LogType.Log);
|
|
||||||
} else {
|
|
||||||
outstreams.get(client).writeInt(403);
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ticTacToe_gameloop() {
|
|
||||||
while (clients.size() == 2) {
|
|
||||||
for (Socket client : clients.values()) {
|
|
||||||
try {
|
|
||||||
String message = instreams.get(client).readUTF();
|
|
||||||
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();
|
|
||||||
try {
|
|
||||||
client.close();
|
|
||||||
} catch (IOException ioException) {
|
|
||||||
ioException.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendGameState(){
|
|
||||||
try {
|
|
||||||
int index = 0;
|
|
||||||
for (DataOutputStream outstream: outstreams.values()) {
|
|
||||||
outstream.writeUTF(ticTacToe_server.getGameState());
|
|
||||||
outstream.flush();
|
|
||||||
serverLogger.printLog("Sent gameState", clientNames.get(index), LogType.Log);
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void gameFlow(String input, Socket client){
|
|
||||||
switch (input){
|
|
||||||
case "ready":
|
|
||||||
gameFlow("gameState", client);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "gameState":
|
|
||||||
sendGameState();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "clientMove":
|
|
||||||
try {
|
|
||||||
//Get position (X|Y)
|
|
||||||
String position = instreams.get(client).readUTF();
|
|
||||||
serverLogger.printLog(position, clientNames.get(client), LogType.Message);
|
|
||||||
//Send confirmation (2ßß)
|
|
||||||
outstreams.get(client).writeInt(200);
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
serverLogger.printLog("Sent verification code", clientNames.get(client), LogType.Log);
|
|
||||||
boolean moveAllowed = ticTacToe_server.makeClientMove(position);
|
|
||||||
if (moveAllowed) {
|
|
||||||
sendGameState();
|
|
||||||
} else {
|
|
||||||
//send " "
|
|
||||||
outstreams.get(client).writeUTF("userInput");
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
serverLogger.printLog("Requested userInput", LogType.Log);
|
|
||||||
serverLogger.printLog(String.format("Move is not allowed!"), clientNames.get(client), LogType.Error);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "computerMove":
|
|
||||||
ticTacToe_server.makeComputerMove();
|
|
||||||
sendGameState();
|
|
||||||
try {
|
|
||||||
outstreams.get(client).writeUTF("userInput");
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
serverLogger.printLog("Requested userInput", LogType.Log);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "serverType":
|
|
||||||
try {
|
|
||||||
outstreams.get(client).writeBoolean(true);
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
serverLogger.printLog("Sent serverType", clientNames.get(client), LogType.Log);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "isClientOne":
|
|
||||||
for (Map.Entry<Integer, Socket> entry : clients.entrySet()) {
|
|
||||||
if (Objects.equals(client, entry.getValue())) {
|
|
||||||
try {
|
|
||||||
outstreams.get(client).writeBoolean(entry.getKey() == 0);
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
serverLogger.printLog("Sent isPlayerOne", clientNames.get(client), LogType.Log);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "gameEnded":
|
|
||||||
try {
|
|
||||||
boolean gameEnded = ticTacToe_server.gameEnded();
|
|
||||||
outstreams.get(client).writeBoolean(gameEnded);
|
|
||||||
if (gameEnded) {
|
|
||||||
//send coordinates
|
|
||||||
String coordinates = "";
|
|
||||||
for (Point point: ticTacToe_server.getWinCoordinates()) {
|
|
||||||
coordinates += point.x + ";" + point.y + ";";
|
|
||||||
}
|
|
||||||
//send winning fields
|
|
||||||
outstreams.get(client).writeUTF(coordinates);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
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);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
MultiPlayerServer server = new MultiPlayerServer(2589);
|
|
||||||
server.connectClients();
|
|
||||||
server.handshake();
|
|
||||||
server.ticTacToe_gameloop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package networking;
|
package networking;
|
||||||
|
|
||||||
import res.TicTacToe_Server;
|
|
||||||
import logging.LogType;
|
import logging.LogType;
|
||||||
import logging.ServerLogger;
|
import logging.ServerLogger;
|
||||||
|
import res.TicTacToe_GameRules;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
@@ -13,30 +13,31 @@ import java.net.Socket;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class SinglePlayerServer {
|
public class TicTacToe_Server {
|
||||||
|
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private HashMap<Integer, Socket> clients;
|
private HashMap<Integer, Socket> clients;
|
||||||
private HashMap<Socket, String> clientNames;
|
private HashMap<Socket, String> clientNames;
|
||||||
|
private HashMap<Socket, Boolean> clientMoveAuthorizations;
|
||||||
private HashMap<Socket, DataOutputStream> outstreams;
|
private HashMap<Socket, DataOutputStream> outstreams;
|
||||||
private HashMap<Socket, DataInputStream> instreams;
|
private HashMap<Socket, DataInputStream> instreams;
|
||||||
private TicTacToe_Server ticTacToe_server;
|
private TicTacToe_GameRules ticTacToe_gameRules;
|
||||||
private ServerLogger serverLogger;
|
private ServerLogger serverLogger;
|
||||||
private Scanner scanner;
|
|
||||||
private int requiredConnections;
|
private int requiredConnections;
|
||||||
|
|
||||||
public SinglePlayerServer(int port){
|
|
||||||
|
public TicTacToe_Server(int port, int requiredConnections) {
|
||||||
try {
|
try {
|
||||||
serverSocket = new ServerSocket(port);
|
serverSocket = new ServerSocket(port);
|
||||||
clients = new HashMap<>();
|
clients = new HashMap<>();
|
||||||
clientNames = new HashMap<>();
|
clientNames = new HashMap<>();
|
||||||
|
clientMoveAuthorizations = new HashMap<>();
|
||||||
outstreams = new HashMap<>();
|
outstreams = new HashMap<>();
|
||||||
instreams = new HashMap<>();
|
instreams = new HashMap<>();
|
||||||
ticTacToe_server = new TicTacToe_Server();
|
ticTacToe_gameRules = new TicTacToe_GameRules();
|
||||||
scanner = new Scanner(System.in);
|
|
||||||
serverLogger = new ServerLogger();
|
serverLogger = new ServerLogger();
|
||||||
requiredConnections = 1;
|
this.requiredConnections = requiredConnections;
|
||||||
|
|
||||||
serverLogger.printLog("Server started successfully", LogType.Log);
|
serverLogger.printLog("Server started successfully", LogType.Log);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -44,6 +45,10 @@ public class SinglePlayerServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isSingleServer() {
|
||||||
|
return requiredConnections == 1;
|
||||||
|
}
|
||||||
|
|
||||||
public void connectClients() {
|
public void connectClients() {
|
||||||
try {
|
try {
|
||||||
int id = 0;
|
int id = 0;
|
||||||
@@ -68,7 +73,7 @@ public class SinglePlayerServer {
|
|||||||
outstreams.get(client).writeInt(200);
|
outstreams.get(client).writeInt(200);
|
||||||
outstreams.get(client).flush();
|
outstreams.get(client).flush();
|
||||||
clientNames.put(client, instreams.get(client).readUTF());
|
clientNames.put(client, instreams.get(client).readUTF());
|
||||||
serverLogger.printLog(String.format("Client: \"%s\" got connected", clientNames.get(client)), LogType.Log);
|
serverLogger.printLog(String.format("Client \"%s\" got connected", clientNames.get(client)), LogType.Log);
|
||||||
} else {
|
} else {
|
||||||
outstreams.get(client).writeInt(403);
|
outstreams.get(client).writeInt(403);
|
||||||
outstreams.get(client).flush();
|
outstreams.get(client).flush();
|
||||||
@@ -80,32 +85,15 @@ public class SinglePlayerServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ticTacToe_gameloop(){
|
public void sendGameState() {
|
||||||
for (Socket client : clients.values()) {
|
for (Socket client : clients.values()) {
|
||||||
try {
|
sendGameState(client);
|
||||||
while (!client.isClosed()) {
|
|
||||||
//Get instruction
|
|
||||||
String message = instreams.get(client).readUTF();
|
|
||||||
serverLogger.printLog(message, clientNames.get(client), LogType.Message);
|
|
||||||
outstreams.get(client).writeInt(200);
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
serverLogger.printLog("Sent verification code", "200", clientNames.get(client), LogType.Log);
|
|
||||||
this.gameFlow(message, client);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
try {
|
|
||||||
client.close();
|
|
||||||
} catch (IOException ioException) {
|
|
||||||
ioException.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendGameState(Socket client) {
|
public void sendGameState(Socket client) {
|
||||||
try {
|
try {
|
||||||
String gameState = ticTacToe_server.getGameState();
|
String gameState = ticTacToe_gameRules.getGameState();
|
||||||
outstreams.get(client).writeUTF(gameState);
|
outstreams.get(client).writeUTF(gameState);
|
||||||
outstreams.get(client).flush();
|
outstreams.get(client).flush();
|
||||||
serverLogger.printLog("Sent gameState", gameState, clientNames.get(client), LogType.Log);
|
serverLogger.printLog("Sent gameState", gameState, clientNames.get(client), LogType.Log);
|
||||||
@@ -114,28 +102,59 @@ public class SinglePlayerServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String handleInput(Socket client){
|
||||||
|
String message = null;
|
||||||
|
try {
|
||||||
|
message = instreams.get(client).readUTF();
|
||||||
|
serverLogger.printLog(message, clientNames.get(client), LogType.Message);
|
||||||
|
outstreams.get(client).writeInt(200);
|
||||||
|
outstreams.get(client).flush();
|
||||||
|
serverLogger.printLog("Sent verification code", "200", clientNames.get(client), LogType.Log);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ticTacToe_gameloop() {
|
||||||
|
if (isSingleServer()) {
|
||||||
|
Socket client = clients.get(0);
|
||||||
|
//SingleServer GameLoop
|
||||||
|
while (!client.isClosed()) {
|
||||||
|
//Get instruction
|
||||||
|
this.gameFlow(handleInput(client), client);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//MultiServer GameLoop
|
||||||
|
gameFlow("gameState");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void gameFlow(String input){
|
||||||
|
this.gameFlow(input, null);
|
||||||
|
}
|
||||||
|
|
||||||
public void gameFlow(String input, Socket client) {
|
public void gameFlow(String input, Socket client) {
|
||||||
switch (input) {
|
switch (input) {
|
||||||
case "ready":
|
case "ready":
|
||||||
gameFlow("gameState", client);
|
if (isSingleServer()){
|
||||||
|
sendGameState();
|
||||||
|
} else {
|
||||||
|
sendGameState(client);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "gameState":
|
case "gameState":
|
||||||
sendGameState(client);
|
sendGameState();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "clientMove":
|
case "clientMove":
|
||||||
try {
|
try {
|
||||||
//Get position (X|Y)
|
//Get position (X|Y)
|
||||||
String position = instreams.get(client).readUTF();
|
String position = handleInput(client);
|
||||||
serverLogger.printLog(position, clientNames.get(client), LogType.Message);
|
boolean moveAllowed = ticTacToe_gameRules.makeClientMove(position);
|
||||||
//Send confirmation (2ßß)
|
|
||||||
outstreams.get(client).writeInt(200);
|
|
||||||
outstreams.get(client).flush();
|
|
||||||
serverLogger.printLog("Sent verification code", "200", clientNames.get(client), LogType.Log);
|
|
||||||
boolean moveAllowed = ticTacToe_server.makeClientMove(position);
|
|
||||||
if (moveAllowed) {
|
if (moveAllowed) {
|
||||||
sendGameState(client);
|
sendGameState();
|
||||||
} else {
|
} else {
|
||||||
//send " "
|
//send " "
|
||||||
outstreams.get(client).writeUTF("userInput");
|
outstreams.get(client).writeUTF("userInput");
|
||||||
@@ -149,8 +168,8 @@ public class SinglePlayerServer {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "computerMove":
|
case "computerMove":
|
||||||
ticTacToe_server.makeComputerMove();
|
ticTacToe_gameRules.makeComputerMove();
|
||||||
sendGameState(client);
|
sendGameState();
|
||||||
try {
|
try {
|
||||||
outstreams.get(client).writeUTF("userInput");
|
outstreams.get(client).writeUTF("userInput");
|
||||||
outstreams.get(client).flush();
|
outstreams.get(client).flush();
|
||||||
@@ -189,12 +208,12 @@ public class SinglePlayerServer {
|
|||||||
|
|
||||||
case "gameEnded":
|
case "gameEnded":
|
||||||
try {
|
try {
|
||||||
boolean gameEnded = ticTacToe_server.gameEnded();
|
boolean gameEnded = ticTacToe_gameRules.gameEnded();
|
||||||
outstreams.get(client).writeBoolean(gameEnded);
|
outstreams.get(client).writeBoolean(gameEnded);
|
||||||
if (gameEnded) {
|
if (gameEnded) {
|
||||||
//send coordinates
|
//send coordinates
|
||||||
String coordinates = "";
|
String coordinates = "";
|
||||||
for (Point point: ticTacToe_server.getWinCoordinates()) {
|
for (Point point : ticTacToe_gameRules.getWinCoordinates()) {
|
||||||
coordinates += point.x + ";" + point.y + ";";
|
coordinates += point.x + ";" + point.y + ";";
|
||||||
}
|
}
|
||||||
//send winning fields
|
//send winning fields
|
||||||
@@ -218,16 +237,17 @@ public class SinglePlayerServer {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "reset":
|
case "reset":
|
||||||
ticTacToe_server.resetGameState();
|
ticTacToe_gameRules.resetGameState();
|
||||||
this.sendGameState(client);
|
this.sendGameState();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SinglePlayerServer server = new SinglePlayerServer(2589);
|
TicTacToe_Server server = new TicTacToe_Server(2589, Integer.valueOf(args[0]));
|
||||||
server.connectClients();
|
server.connectClients();
|
||||||
server.handshake();
|
server.handshake();
|
||||||
server.ticTacToe_gameloop();
|
server.ticTacToe_gameloop();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,13 +5,13 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class TicTacToe_Server {
|
public class TicTacToe_GameRules {
|
||||||
|
|
||||||
private String gameState;
|
private String gameState;
|
||||||
private Integer[][] board;
|
private Integer[][] board;
|
||||||
Point startWin, endWin;
|
Point startWin, endWin;
|
||||||
|
|
||||||
public TicTacToe_Server(){
|
public TicTacToe_GameRules(){
|
||||||
gameState = "---------";
|
gameState = "---------";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
cd /home/cato447/Code/ArcadeMachine/out/production/Server/
|
|
||||||
jar cmvf META-INF/multiServerManifest.MF multiServer.jar *
|
|
||||||
scp multiServer.jar root@server:/root
|
|
||||||
rm multiServer.jar
|
|
||||||
cd /home/cato447/Code/ArcadeMachine/deployment/multiplayer/
|
|
||||||
./startMultiServer
|
|
||||||
6
deployment/server/buildServer
Executable file
6
deployment/server/buildServer
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
cd /home/cato447/Code/ArcadeMachine/out/production/Server/
|
||||||
|
jar cmvf /home/cato447/Code/ArcadeMachine/Server/res/META-INF/MANIFEST.MF TicTacToe_Server.jar *
|
||||||
|
scp TicTacToe_Server.jar root@server:/root
|
||||||
|
ssh root@server chmod +x TicTacToe_Server.jar
|
||||||
|
rm TicTacToe_Server.jar
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
cd /home/cato447/Code/ArcadeMachine/out/production/Server/
|
|
||||||
jar cmvf META-INF/MANIFEST.MF singleServer.jar *
|
|
||||||
scp singleServer.jar root@server:/root
|
|
||||||
rm singleServer.jar
|
|
||||||
cd /home/cato447/Code/ArcadeMachine/deployment/singleplayer/
|
|
||||||
./startSingleServer
|
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
Manifest-Version: 1.0
|
Manifest-Version: 1.0
|
||||||
Main-Class: networking.SinglePlayerServer
|
Main-Class: networking.TicTacToe_Server
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
Manifest-Version: 1.0
|
|
||||||
Main-Class: networking.MultiPlayerServer
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
Manifest-Version: 1.0
|
|
||||||
Main-Class: networking.SinglePlayerServer
|
|
||||||
Reference in New Issue
Block a user