fix online game start

This commit is contained in:
Luca Conte 2024-12-13 17:26:18 +01:00
parent 5ba5919775
commit dfbe4b3c53
3 changed files with 22 additions and 6 deletions

View File

@ -68,11 +68,18 @@ public class GameController {
throw new RuntimeException("Unable to instantiate players"); throw new RuntimeException("Unable to instantiate players");
} }
localPlayer.isServer = localPlayerIsServer; localPlayer.isServer = localPlayerIsServer;
onlinePlayer.isServer = !localPlayerIsServer; onlinePlayer.isServer = !localPlayerIsServer;
startGameWithInstancedPlayers(localPlayer, onlinePlayer, size); localPlayer.setName(localPlayerName);
localPlayer.setEnemy(onlinePlayer);
onlinePlayer.setEnemy(localPlayer);
onlinePlayer.sendIAM();
// Start game only after IAM Package was exchanged
} else { } else {
throw new RuntimeException("Unexpected Package received before game initialisation"); throw new RuntimeException("Unexpected Package received before game initialisation");
} }
@ -156,13 +163,13 @@ public class GameController {
localPlayer.createBoard(size); localPlayer.createBoard(size);
aiPlayer.createBoard(size); aiPlayer.createBoard(size);
localPlayer.setEnemy(aiPlayer);
aiPlayer.setEnemy(localPlayer);
startGameWithInstancedPlayers(localPlayer, aiPlayer, size); startGameWithInstancedPlayers(localPlayer, aiPlayer, size);
} }
private static void startGameWithInstancedPlayers(LocalPlayer p1, Player p2, int boardSize) { public static void startGameWithInstancedPlayers(LocalPlayer p1, Player p2, int boardSize) {
p1.setEnemy(p2);
p2.setEnemy(p1);
mainFrame.showPanelSLG("GameBoard", boardSizeToSemester(boardSize), p1, p2); mainFrame.showPanelSLG("GameBoard", boardSizeToSemester(boardSize), p1, p2);
// TODO: frontend configuration // TODO: frontend configuration

View File

@ -11,6 +11,8 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
//TODO Auto-generated constructor stub //TODO Auto-generated constructor stub
} }
public abstract void sendIAM();
public abstract void receive(String message); public abstract void receive(String message);
@Override @Override

View File

@ -6,7 +6,6 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
} }
@Override @Override
public void receive(String message) { public void receive(String message) {
SocketPackage p = new SocketPackage(message); SocketPackage p = new SocketPackage(message);
@ -26,6 +25,8 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
this.createBoard(usedBoardSize); this.createBoard(usedBoardSize);
this.enemy.createBoard(usedBoardSize); this.enemy.createBoard(usedBoardSize);
GameController.startGameWithInstancedPlayers((LocalPlayer)this.enemy, (Player)this, usedBoardSize);
break; break;
// TODO: IAMU // TODO: IAMU
@ -66,6 +67,12 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
} }
} }
@Override
public synchronized void sendIAM() {
if (this.enemy == null) throw new RuntimeException("enemy has not yet been defined");
socket.send(new SocketPackage("IAM", GameController.boardSizeToSemester(this.wantedBoardSize) + " " + this.enemy.name));
}
@Override @Override
public synchronized void receiveShoot(Point point){ public synchronized void receiveShoot(Point point){
super.socket.send(new SocketPackage("SHOOT",point.toString())); super.socket.send(new SocketPackage("SHOOT",point.toString()));