move joined logic out of if statement
This commit is contained in:
parent
ff9ffbd628
commit
8bc64bd9ba
|
@ -1,42 +1,46 @@
|
|||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.Arrays;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GameController {
|
||||
|
||||
// TODO: fix syntax i guess
|
||||
public HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = [{ "1.1.0" : OnlinePlayer_1_1_0.class }];
|
||||
public HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = new HashMap<>(Map.of(
|
||||
"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) {
|
||||
// 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.send("VERSION", "Gruppe03 1.1.0"); // TODO: adjust versioning list
|
||||
clientSocket.setHandler((message) -> {
|
||||
clientSocket = new AsyncSocket(serverSocket.accept(), null);
|
||||
|
||||
new OnlinePlayer_1_1_0(0, clientSocket);
|
||||
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
serverSocket.close();
|
||||
} 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) {
|
||||
|
|
Loading…
Reference in New Issue