Skip to content
Snippets Groups Projects
Commit 5949dee9 authored by Elrabu's avatar Elrabu
Browse files

Update: Player cords are stored in server, enemy cords are sent back.

parent 1464c490
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
......@@ -24,7 +24,21 @@ public class Client {
String message = String.join(",", Arrays.stream(cords).mapToObj(String::valueOf).toArray(String[]::new));
out.println(message);
log.info("Sent coordinates: " + message);
return cords;
String resp = in.readLine();
return convertStringToArray(resp);
}
public static int[] convertStringToArray(String inputString) {
// Remove brackets and split by comma
String[] parts = inputString.substring(1, inputString.length() - 1).split(",");
// Convert each part to integer
int[] result = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
result[i] = Integer.parseInt(parts[i].trim());
}
return result;
}
......
package de.hdm_stuttgart.battlearena.Model.Multiplayer;
import java.io.IOException;
import java.util.Arrays;
public class ConnectionHandler {
......@@ -16,7 +17,8 @@ public class ConnectionHandler {
Client client = new Client();
client.startConnection("localhost", 4444);
int[] cords = client.sendcords(coordinates);
System.out.println(cords);
System.out.println("Enemy X: " + cords[0]);
System.out.println("Enemy Y: " + cords[1]);
//stop the connection:
client.stopConnection();
......
......@@ -5,6 +5,7 @@ import org.apache.logging.log4j.Logger;
import java.net.*;
import java.io.*;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
......@@ -21,6 +22,7 @@ public class Server {
private static int enemyy;
private static int[] cords;
private static int[] enemycords = {7,7};
public static void main(String[] args) throws IOException {
Server server = new Server();
......@@ -28,14 +30,14 @@ public class Server {
server.start("localhost",4444);
}
public void start(String lolcalhost, int port) throws IOException {
public void start(String localhost, int port) throws IOException {
serverSocket = new ServerSocket(port);
log.info("server started!");
new ServerHandler(serverSocket.accept()).start();
}
private static class ServerHandler extends Thread {
private Socket clientSocket;
private final Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
......@@ -53,7 +55,7 @@ public class Server {
String inputLine = localIn.readLine();
log.info(inputLine);
// log.info(inputLine);
if (inputLine != null) {
// Assuming that the input line is a comma-separated list of integers
......@@ -62,9 +64,15 @@ 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));
localOut.flush();
}
log.info(cords);
log.info("Player X: " + px + " Player Y: " + py);
//check if server was shut down:
if (!isServerRunning) {
......
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