move joined logic out of if statement

This commit is contained in:
Luca Conte 2024-11-30 14:14:29 +01:00
parent ff9ffbd628
commit 8bc64bd9ba
1 changed files with 28 additions and 24 deletions

View File

@ -1,42 +1,46 @@
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.util.Arrays; import java.net.Socket;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
public class GameController { public class GameController {
// TODO: fix syntax i guess public HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = new HashMap<>(Map.of(
public HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = [{ "1.1.0" : OnlinePlayer_1_1_0.class }]; "1.1.0", OnlinePlayer_1_1_0.class
));
public void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, InetSocketAddress address) { public void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, InetSocketAddress address) throws IOException {
AsyncSocket clientSocket;
if (address.getHostName() == null) { if (address.getHostName() == null) {
// SERVER MODE // SERVER MODE
try { ServerSocket serverSocket = new ServerSocket(address.getPort());
ServerSocket serverSocket = new ServerSocket(address.getPort());
System.out.println("Waiting for client connection..."); System.out.println("Waiting for client connection...");
AsyncSocket clientSocket = new AsyncSocket(serverSocket.accept(), null); clientSocket = new AsyncSocket(serverSocket.accept(), null);
clientSocket.send("VERSION", "Gruppe03 1.1.0"); // TODO: adjust versioning list
clientSocket.setHandler((message) -> {
new OnlinePlayer_1_1_0(0, clientSocket); serverSocket.close();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
return;
}
} else { } else {
// CLIENT MODE Socket socket = new Socket();
socket.connect(address);
clientSocket = new AsyncSocket(socket, null);
} }
clientSocket.send("VERSION", "Gruppe03 " + String.join(" ", supportedVersions.keySet()));
clientSocket.setHandler((message) -> {
SocketPackage socketPackage = new SocketPackage(message);
if (socketPackage.getName().equals("VERSION")) {
// TODO: check version matches - instantiate online player
} else {
// TODO: received invalid package / package out of order
}
});
// TODO: instantiate local player, set server / client roles
} }
public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) { public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) {