57 lines
1.5 KiB
Java
57 lines
1.5 KiB
Java
import java.io.IOException;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.ServerSocket;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
|
|
public class GameController {
|
|
|
|
// TODO: fix syntax i guess
|
|
public HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = [{ "1.1.0" : OnlinePlayer_1_1_0.class }];
|
|
|
|
public void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, InetSocketAddress address) {
|
|
if (address.getHostName() == null) {
|
|
// SERVER MODE
|
|
try {
|
|
ServerSocket serverSocket = new ServerSocket(address.getPort());
|
|
|
|
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) -> {
|
|
|
|
new OnlinePlayer_1_1_0(0, clientSocket);
|
|
|
|
try {
|
|
serverSocket.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return;
|
|
}
|
|
} else {
|
|
// CLIENT MODE
|
|
}
|
|
}
|
|
|
|
public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) {
|
|
|
|
LocalPlayer localPlayer;
|
|
AiPlayer aiPlayer;
|
|
try {
|
|
localPlayer = localPlayerClass.getDeclaredConstructor().newInstance();
|
|
aiPlayer = enemyClass.getDeclaredConstructor().newInstance();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return;
|
|
}
|
|
localPlayer.setEnemy(aiPlayer);
|
|
aiPlayer.setEnemy(localPlayer);
|
|
}
|
|
|
|
} |