fix: fixed major bug where lobbyConnection termination wouldn't remove clients

This commit is contained in:
Yannik Bretschneider 2021-06-08 02:02:50 +02:00
parent 96ce2ebde2
commit 8dd4a4b60f
3 changed files with 44 additions and 19 deletions

View File

@ -232,10 +232,13 @@ public class Lobby {
badRequests++;
//if the player sends 2 bad messages after one another, the player gets kicked out of the lobby.
if (badRequests >= 5) {
Logger.info("Participant '{}' has sent too many bad requests, disconnecting...", source.id.getName());
connection.removeParticipant(source);
if (connection.hasPlayer1()) {
Logger.debug("Triggering win for Player 1");
triggerWin(connection.getPlayer1());
} else if (connection.hasPlayer2()) {
Logger.debug("Triggering win for Player 2");
triggerWin(connection.getPlayer2());
}
}
@ -251,6 +254,7 @@ public class Lobby {
source,
new EventBuilder(EventType.TimeoutWarningEvent)
.withTimeLeft(timeLeft)
.withMessage("If you do not send a message soon, you will be time-outed")
.buildGameEvent());
}
@ -289,6 +293,7 @@ public class Lobby {
* @param winner is the {@link Participant} that won
*/
public synchronized void triggerWin(Participant winner) {
Logger.debug("Triggering win. Building events and broadcasting for winner of type '{}'", winner);
connection.broadcastEvents(
new EventBuilder(EventType.WinEvent)
.withPlayerWon(winner.type.equals(ParticipantType.PlayerOne) ? 1 : 2)

View File

@ -72,17 +72,21 @@ public class LobbyConnection implements Runnable {
/** Disconnects a participant from the LobbyConnection */
public void removeParticipant(Participant participant, String reason) {
if (participant != null) {
Logger.debug("Removing participant '{}' with role {} from lobby",
participant.id.getName(), participant.type);
LobbyManager.getInstance().removeParticipant(participant);
UserManager.getInstance().removeClient(participant.getClient(), reason);
if(participant.type == ParticipantType.Spectator) {
if (participant.type == ParticipantType.Spectator) {
spectators.remove(participant);
} else if(participant.type == ParticipantType.PlayerOne) {
} else if (participant.type == ParticipantType.PlayerOne) {
player1 = null;
}else {
} else {
player2 = null;
}
}
}
/** Returns the next free slot in the lobby as a {@link ParticipantType} */
public ParticipantType freeSlot() {
@ -181,6 +185,9 @@ public class LobbyConnection implements Runnable {
public void terminate() {
state = LobbyConnectionState.Aborted;
removeParticipant(player1);
removeParticipant(player2);
spectators.forEach(this::removeParticipant);
}
@ -224,35 +231,43 @@ public class LobbyConnection implements Runnable {
Tuple<Participant, Request[]> current = null;
try {
current = requestQueue.poll(1000, TimeUnit.MILLISECONDS);
}catch (InterruptedException e) {
}
} catch (InterruptedException ignored) {}
return current;
}
private void broadcast(BasicMessage message) {
player1.sendMessage(message);
player2.sendMessage(message);
Logger.trace("Broadcasting message of type {} to all members of lobby", message.messageType);
if (player1 != null) player1.sendMessage(message);
if (player2 != null) player2.sendMessage(message);
spectators.forEach(spectator -> spectator.sendMessage(message));
}
private void broadcastToSpectators(BasicMessage message) {
Logger.trace("Broadcasting message of type {} to all spectators", message.messageType);
spectators.forEach(spectator -> spectator.sendMessage(message));
}
private void broadcastToAllExcept(Participant except, BasicMessage message) {
Logger.trace("Sending message of type {} to all except participant with role {}",
message.messageType, except == null ? "NONE" : except.type);
if (except != null) {
if (!except.equals(player1)) player1.sendMessage(message);
if (!except.equals(player2)) player2.sendMessage(message);
for(Participant spectator: spectators) {
if(!except.equals(spectator)) {
for (Participant spectator : spectators) {
if (!except.equals(spectator)) {
spectator.sendMessage(message);
}
}
} else {
broadcast(message);
}
}
public void sendEvents(Participant recipient, Event... events) {
Logger.trace("Sending {} events to participant with role {}",
events.length, recipient == null ? "NONE" : recipient.type);
if (recipient != null) {
EventMessage message = new EventMessage();
message.messages = events;
@ -266,6 +281,7 @@ public class LobbyConnection implements Runnable {
}
public void broadcastEvents(Event... events) {
Logger.trace("Broadcasting {} events", events.length);
EventMessage message = new EventMessage();
message.messages = events;
@ -273,6 +289,8 @@ public class LobbyConnection implements Runnable {
}
public void broadcastToAllExcept(Participant except, Event... events) {
Logger.trace("Broadcasting {} events to all except participant with role {}",
events.length, except == null ? "NONE" : except.type);
var message = new EventMessage();
message.messages = events;

View File

@ -251,6 +251,8 @@ public class LobbyManager {
* Lobby because of a timeout.
*/
public void removeParticipant(Participant participant) {
if (participant != null) {
participants.remove(participant.id);
}
}
}