Closed Starlight-30036225 closed 7 months ago
The first problem is that when a third player attempts to join a room, they are never allowed in.
I need to adjust the joining code to allow spectators to be added, but without overriding players.
I have been here before, but the infrastructure keeps changing. Should be easy enough.
The issue is here:
``
if (room.WhitePlayer == null) {
room.WhitePlayer = Handler;
roomMap.put(Handler, room);
Handler.sendMessage(PacketHeader.WELCOME, "WHITE");
} else if (room.BlackPlayer == null) {
room.BlackPlayer = Handler;
roomMap.put(Handler, room);
Handler.sendMessage(PacketHeader.WELCOME, "BLACK");
}
``
Spectators will never receive a welcome packet, or be assigned to a room.
Adding an final else statement for that condition should sort it.
``
if (room.WhitePlayer == null) {
room.WhitePlayer = Handler;
Handler.sendMessage(PacketHeader.WELCOME, "WHITE");
} else if (room.BlackPlayer == null) {
room.BlackPlayer = Handler;
Handler.sendMessage(PacketHeader.WELCOME, "BLACK");
} else {
Handler.sendMessage(PacketHeader.WELCOME, "SPECT");
}
roomMap.put(Handler, room);
``
I also moved the assignment to a room to the end, as it occurs under all conditions.
``
if(room.Connections == 2) {
GameMaster finalRoom = room;
for (ConnectionHandler CH:
clients.stream()
.filter(CH -> roomMap.get(CH) == finalRoom)
.toList()) {
CH.sendMessage(PacketHeader.BOARD_STATE, room.LoadNotationFromMap());
}
} else if (room.Connections >2) {
Handler.sendMessage(PacketHeader.BOARD_STATE, room.LoadNotationFromMap());
}
``
I improved the search for relevant clients with a lambda function inside the for loop,
Other than that, when the second player joins both players are allowed to play and the gamer begins.
To ensure the windows don't re-render when new players join, any joined client past the second is handled independently.
Next I need to adjust the UI to accommodate for spectators
I need to rebuild how players are told whos turn it is. But first lets move back into main.
Now that rooms have been made, It should be easy enough to make a few adjustments to include spectators.