Skip to content
Snippets Groups Projects
Commit 33770c29 authored by Elrabu's avatar Elrabu
Browse files

Update: the client can now connect to the server and exchange coordinates...

Update: the client can now connect to the server and exchange coordinates depending on the client addressing the server.
parent 5949dee9
No related branches found
No related tags found
2 merge requests!35Update: Added more Stuff to be transported over the server.,!2Merge of Multiplayer Feature into development branch
......@@ -12,6 +12,7 @@ public class Client {
private PrintWriter out;
private BufferedReader in;
private static final Logger log = LogManager.getLogger(Client.class);
public void startConnection(String ip, int port) throws IOException {
......
package de.hdm_stuttgart.battlearena.Model.Multiplayer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class ConnectionHandler {
......@@ -9,19 +13,41 @@ public class ConnectionHandler {
private int pY;
private int enemyX;
private int enemyY;
private static int playerID = 0;
private static int[] coordinates = {5,10,playerID};
private static int[] coordinates = {5,10};
private static final Logger log = LogManager.getLogger(ConnectionHandler.class);
public static void main(String[] args) throws IOException, InterruptedException {
Client client = new Client();
client.startConnection("localhost", 4444);
int[] cords = client.sendcords(coordinates);
System.out.println("Enemy X: " + cords[0]);
System.out.println("Enemy Y: " + cords[1]);
while (true){
int[] cords = client.sendcords(coordinates);
System.out.println("Enemy X: " + cords[0]);
System.out.println("Enemy Y: " + cords[1]);
System.out.println("Your PlayerID is " + cords[2]);
playerID = cords[2];
coordinates[2] = playerID;
TimeUnit.SECONDS.sleep(1);
}
//stop the connection:
client.stopConnection();
//client.stopConnection();
}
class ConnectionThread implements Runnable {
public void run() {
try {
Thread.sleep(250);
} catch (InterruptedException e){
log.error("Error in Thread.sleep()");
}
}
}
public int getpX() {
......
......@@ -18,11 +18,14 @@ public class Server {
private static int px;
private static int py;
private static int enemyx;
private static int enemyy;
private static int enemyx = 0;
private static int enemyy = 0;
private static int playerID;
private static int playercount = 0;
private static int[] cords;
private static int[] enemycords = {7,7};
private static int[] returncords = {enemyx,enemyy,playerID};
public static void main(String[] args) throws IOException {
Server server = new Server();
......@@ -55,7 +58,6 @@ public class Server {
String inputLine = localIn.readLine();
// log.info(inputLine);
if (inputLine != null) {
// Assuming that the input line is a comma-separated list of integers
......@@ -64,10 +66,51 @@ public class Server {
for (int i = 0; i < coordinates.length; i++) {
cords[i] = Integer.parseInt(coordinates[i]);
}
px = cords[0];
py = cords[1];
localOut.println(Arrays.toString(enemycords));
//first connect of a client:
if(playercount == 0){
//first player to connect
playercount++;
returncords[2] = 1;
} else if(playercount == 1){
//second player to connect
if(cords[2] == 1){
returncords[2] = 1;
} else {
playercount++;
returncords[2] = 2;
}
} else if(playercount == 2){
//check which client has connected
if (cords[2] == 1) { //player
px = cords[0];
py = cords[1];
//set the cords to return:
returncords[0] = enemyx;
returncords[1] = enemyy;
//set playerID:
returncords[2] = 1;
} else if(cords[2] == 2) { //enemy
enemyx = cords[0];
enemyy = cords[1];
//set the cords to return:
returncords[0] = px;
returncords[1] = py;
//set playerID:
returncords[2] = 2;
}
}
localOut.println(Arrays.toString(returncords));
localOut.flush();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment