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.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
|
|
||||||
|
serverSocket.close();
|
||||||
|
} else {
|
||||||
|
Socket socket = new Socket();
|
||||||
|
socket.connect(address);
|
||||||
|
|
||||||
|
clientSocket = new AsyncSocket(socket, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
clientSocket.send("VERSION", "Gruppe03 " + String.join(" ", supportedVersions.keySet()));
|
||||||
clientSocket.setHandler((message) -> {
|
clientSocket.setHandler((message) -> {
|
||||||
|
|
||||||
new OnlinePlayer_1_1_0(0, clientSocket);
|
SocketPackage socketPackage = new SocketPackage(message);
|
||||||
|
if (socketPackage.getName().equals("VERSION")) {
|
||||||
try {
|
// TODO: check version matches - instantiate online player
|
||||||
serverSocket.close();
|
} else {
|
||||||
} catch (IOException e) {
|
// TODO: received invalid package / package out of order
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// TODO: instantiate local player, set server / client roles
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// CLIENT MODE
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|
Loading…
Reference in New Issue