diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java index b47419daa11eea14e292ecab5dac7e0ae483f2b6..ec33d875df97fe74ee69a806c5842c00bc416414 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java @@ -1,5 +1,7 @@ package de.hdm_stuttgart.battlearena.Model.Multiplayer; +import de.hdm_stuttgart.battlearena.Model.Entity.EntityClass; +import de.hdm_stuttgart.battlearena.Model.Entity.EntityDirection; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -14,7 +16,16 @@ public class ConnectionHandler { private static int enemyX = 0; private static int enemyY = 0; private static int playerID = 0; - private static int[] coordinates = {pX,pY,playerID}; + + private static int playerAttacking = 1; + private static int enemyAttacking = 0; + private static int playerClass = 0; + private static int enemyClass = 0; + private static int playerWalkDirection = 2; + private static int enemyWalkDirection = 0; + private static int playerHealth = 10; + private static int enemyHealth = 10; + private static int[] coordinates = {pX,pY,playerID,playerAttacking,playerClass,playerWalkDirection,playerHealth}; private static String ipaddress = "localhost"; private static final Logger log = LogManager.getLogger(ConnectionHandler.class); @@ -41,16 +52,20 @@ public class ConnectionHandler { int[] cords = client.sendcords(coordinates); enemyX = cords[0]; enemyY = cords[1]; - // System.out.println("Enemy X: " + enemyX); - // System.out.println("Enemy Y: " + enemyY); - // System.out.println("Your PlayerID is " + cords[2]); playerID = cords[2]; + enemyAttacking = cords[3]; + enemyClass = cords[4]; + enemyWalkDirection = cords[5]; + enemyHealth = cords[6]; // Assign the Values to the message to send: coordinates[0] = pX; coordinates[1] = pY; coordinates[2] = playerID; - - Thread.sleep(16); + coordinates[3] = playerAttacking; + coordinates[4] = playerClass; + coordinates[5] = playerWalkDirection; + coordinates[6] = playerHealth; + Thread.sleep(50); } } catch (InterruptedException | IOException e) { e.printStackTrace(); // Handle the exception as needed @@ -79,12 +94,160 @@ public class ConnectionHandler { } public int getEnemyX() { + return enemyX; } public int getEnemyY() { + return enemyY; } + //Getters and Setter for all other information that are not coordinates: + + public static boolean getPlayerAttacking() { + if (playerAttacking == 1){ + return true; + } else { + return false; + } + } + + public static void setPlayerAttacking(boolean attack) { + if (attack){ + ConnectionHandler.playerAttacking = 1; + } else { + ConnectionHandler.playerAttacking = 0; + } + } + + public static boolean getEnemyAttacking() { + if (enemyAttacking == 1){ + return true; + } else { + return false; + } + } + + public static void setEnemyAttacking(boolean attack) { + if (attack){ + ConnectionHandler.enemyAttacking = 1; + } else { + ConnectionHandler.enemyAttacking = 0; + } + } + + public static EntityClass getPlayerClass() { + if (playerClass == 1) { + return EntityClass.HIGH_BORN; + } else if (playerClass == 2) { + return EntityClass.LOW_BORN; + } else if (playerClass == 3) { + return EntityClass.SENTINELS; + } else { + return EntityClass.HUMAN; + } + } + + public static void setPlayerClass(EntityClass player) { + if (player == EntityClass.HUMAN){ + ConnectionHandler.playerClass = 0; + } else if (player == EntityClass.HIGH_BORN) { + ConnectionHandler.playerClass = 1; + } else if (player == EntityClass.LOW_BORN){ + ConnectionHandler.playerClass = 2; + } else { + ConnectionHandler.playerClass = 3; + } + } + + public static EntityClass getEnemyClass() { + if (enemyClass == 1) { + return EntityClass.HIGH_BORN; + } else if (enemyClass == 2) { + return EntityClass.LOW_BORN; + } else if (enemyClass == 3) { + return EntityClass.SENTINELS; + } else { + return EntityClass.HUMAN; + } + } + + public static void setEnemyClass(EntityClass player) { + if (player == EntityClass.HUMAN){ + ConnectionHandler.enemyClass = 0; + } else if (player == EntityClass.HIGH_BORN) { + ConnectionHandler.enemyClass = 1; + } else if (player == EntityClass.LOW_BORN){ + ConnectionHandler.enemyClass = 2; + } else { + ConnectionHandler.enemyClass = 3; + } + } + + public static EntityDirection getPlayerWalkDirection() { + if (playerWalkDirection == 1){ + return EntityDirection.RIGHT; + } else if (playerWalkDirection == 2){ + return EntityDirection.DOWN; + } else if (playerWalkDirection == 3){ + return EntityDirection.LEFT; + } else { + return EntityDirection.UP; + } + } + + public static void setPlayerWalkDirection(EntityDirection walk) { + if (walk == EntityDirection.RIGHT) { + ConnectionHandler.playerWalkDirection = 1; + } else if (walk == EntityDirection.DOWN) { + ConnectionHandler.playerWalkDirection = 2; + } else if (walk == EntityDirection.LEFT) { + ConnectionHandler.playerWalkDirection = 3; + } else { + ConnectionHandler.playerWalkDirection = 0; + } + } + + public static EntityDirection getEnemyWalkDirection() { + if (enemyWalkDirection == 1){ + return EntityDirection.RIGHT; + } else if (enemyWalkDirection == 2){ + return EntityDirection.DOWN; + } else if (enemyWalkDirection == 3){ + return EntityDirection.LEFT; + } else { + return EntityDirection.UP; + } + } + + public static void setEnemyWalkDirection(EntityDirection walk) { + if (walk == EntityDirection.RIGHT) { + ConnectionHandler.enemyWalkDirection = 1; + } else if (walk == EntityDirection.DOWN) { + ConnectionHandler.enemyWalkDirection = 2; + } else if (walk == EntityDirection.LEFT) { + ConnectionHandler.enemyWalkDirection = 3; + } else { + ConnectionHandler.enemyWalkDirection = 0; + } + } + + public static int getPlayerHealth() { + return playerHealth; + } + + public static void setPlayerHealth(int playerHealth) { + ConnectionHandler.playerHealth = playerHealth; + } + + public static int getEnemyHealth() { + return enemyHealth; + } + + public static void setEnemyHealth(int enemyHealth) { + ConnectionHandler.enemyHealth = enemyHealth; + } + public static void setIpaddress(String ipaddress) { ConnectionHandler.ipaddress = ipaddress; } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java index fddc2fae68590bac4fc51d13f466b7bd8684012c..b8ee8830f6ac315a9ba7802a667c1ed473b43222 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java @@ -17,22 +17,29 @@ public class Server { private static boolean isServerRunning = true; private static int px; private static int py; - private static int enemyx = 0; private static int enemyy = 0; private static int playerID; + + private static int playerAttacking = 0; + private static int enemyAttacking = 0; + private static int playerClass = 0; + private static int enemyClass = 0; + private static int playerWalkDirection = 0; + private static int enemyWalkDirection = 0; + private static int playerHealth = 0; + private static int enemyHealth = 0; private static int playercount = 0; private static int[] cords; - private static int[] returncords = {enemyx,enemyy,playerID}; + private static int[] returncords = {enemyx,enemyy,playerID,enemyAttacking,enemyClass,enemyWalkDirection,enemyHealth}; private boolean started = false; - private static String ipaddress = "localhost"; //Uncomment these lines to test the server starting manually: /* public static void main(String[] args) throws IOException { //main method for testing purposes Server server = new Server(); log.info("server starting..."); - server.start(ipaddress, 4444); - } */ + server.start( 4444); + } //use this method to start the server from another class public void startServer() throws IOException { @@ -41,13 +48,24 @@ public class Server { server.start(ipaddress,4444); } - public void start(String host_ip, int port) throws IOException { + public void start(int port) throws IOException { serverSocket = new ServerSocket(port); log.info("server started!"); started = true; - while (true){ - new ServerHandler(serverSocket.accept()).start(); - } + + log.info("server accepting Thread starting..."); + Thread serverThread = new Thread(() -> { + while (true) { + try { + new ServerHandler(serverSocket.accept()).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + serverThread.start(); + log.info("requests accepting Thread started!"); + } private static class ServerHandler extends Thread { @@ -95,28 +113,45 @@ public class Server { } else if(playercount == 2){ //check which client has connected - if (cords[2] == 1) { //player + if (cords[2] == 1) { + //player px = cords[0]; py = cords[1]; + playerAttacking = cords[3]; + playerClass = cords[4]; + playerWalkDirection = cords[5]; + playerHealth = cords[6]; //set the cords to return: returncords[0] = enemyx; returncords[1] = enemyy; + returncords[3] = enemyAttacking; + returncords[4] = enemyClass; + returncords[5] = enemyWalkDirection; + returncords[6] = enemyHealth; //set playerID: returncords[2] = 1; - } else if(cords[2] == 2) { //enemy + } else if(cords[2] == 2) { + //enemy enemyx = cords[0]; enemyy = cords[1]; + enemyAttacking = cords[3]; + enemyClass = cords[4]; + enemyWalkDirection = cords[5]; + enemyHealth = cords[6]; //set the cords to return: returncords[0] = px; returncords[1] = py; + returncords[3] = playerAttacking; + returncords[4] = playerClass; + returncords[5] = playerWalkDirection; + returncords[6] = playerHealth; //set playerID: returncords[2] = 2; - } } @@ -124,10 +159,10 @@ public class Server { localOut.flush(); } - log.info("Player X / Y : " + px + " / " + py + " Enemy X / Y : " + enemyx + " / " + enemyy); - + log.info("Player (X Y) : " + px + " " + py + " Attacking : " + playerAttacking + " direction : " + playerWalkDirection + " HP: " + playerHealth); + log.info(" Enemy (X Y) : " + enemyx + " " + enemyy + " Attacking : " + enemyAttacking + " direction : " + enemyWalkDirection + " HP: " + enemyHealth); - //check if server was shut down: + //check if server was shut down:W if (!isServerRunning) { executorService.shutdown(); try { @@ -158,7 +193,6 @@ public class Server { public boolean isStarted() { return started; } - public static void setIpaddress(String ipaddress) { - Server.ipaddress = ipaddress; - } + + }