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++; badRequests++;
//if the player sends 2 bad messages after one another, the player gets kicked out of the lobby. //if the player sends 2 bad messages after one another, the player gets kicked out of the lobby.
if (badRequests >= 5) { if (badRequests >= 5) {
Logger.info("Participant '{}' has sent too many bad requests, disconnecting...", source.id.getName());
connection.removeParticipant(source); connection.removeParticipant(source);
if (connection.hasPlayer1()) { if (connection.hasPlayer1()) {
Logger.debug("Triggering win for Player 1");
triggerWin(connection.getPlayer1()); triggerWin(connection.getPlayer1());
} else if (connection.hasPlayer2()) { } else if (connection.hasPlayer2()) {
Logger.debug("Triggering win for Player 2");
triggerWin(connection.getPlayer2()); triggerWin(connection.getPlayer2());
} }
} }
@ -251,6 +254,7 @@ public class Lobby {
source, source,
new EventBuilder(EventType.TimeoutWarningEvent) new EventBuilder(EventType.TimeoutWarningEvent)
.withTimeLeft(timeLeft) .withTimeLeft(timeLeft)
.withMessage("If you do not send a message soon, you will be time-outed")
.buildGameEvent()); .buildGameEvent());
} }
@ -289,6 +293,7 @@ public class Lobby {
* @param winner is the {@link Participant} that won * @param winner is the {@link Participant} that won
*/ */
public synchronized void triggerWin(Participant winner) { public synchronized void triggerWin(Participant winner) {
Logger.debug("Triggering win. Building events and broadcasting for winner of type '{}'", winner);
connection.broadcastEvents( connection.broadcastEvents(
new EventBuilder(EventType.WinEvent) new EventBuilder(EventType.WinEvent)
.withPlayerWon(winner.type.equals(ParticipantType.PlayerOne) ? 1 : 2) .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 */ /** Disconnects a participant from the LobbyConnection */
public void removeParticipant(Participant participant, String reason) { 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); LobbyManager.getInstance().removeParticipant(participant);
UserManager.getInstance().removeClient(participant.getClient(), reason); UserManager.getInstance().removeClient(participant.getClient(), reason);
if(participant.type == ParticipantType.Spectator) { if (participant.type == ParticipantType.Spectator) {
spectators.remove(participant); spectators.remove(participant);
} else if(participant.type == ParticipantType.PlayerOne) { } else if (participant.type == ParticipantType.PlayerOne) {
player1 = null; player1 = null;
}else { } else {
player2 = null; player2 = null;
} }
} }
}
/** Returns the next free slot in the lobby as a {@link ParticipantType} */ /** Returns the next free slot in the lobby as a {@link ParticipantType} */
public ParticipantType freeSlot() { public ParticipantType freeSlot() {
@ -181,6 +185,9 @@ public class LobbyConnection implements Runnable {
public void terminate() { public void terminate() {
state = LobbyConnectionState.Aborted; 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; Tuple<Participant, Request[]> current = null;
try { try {
current = requestQueue.poll(1000, TimeUnit.MILLISECONDS); current = requestQueue.poll(1000, TimeUnit.MILLISECONDS);
}catch (InterruptedException e) { } catch (InterruptedException ignored) {}
}
return current; return current;
} }
private void broadcast(BasicMessage message) { private void broadcast(BasicMessage message) {
player1.sendMessage(message); Logger.trace("Broadcasting message of type {} to all members of lobby", message.messageType);
player2.sendMessage(message); if (player1 != null) player1.sendMessage(message);
if (player2 != null) player2.sendMessage(message);
spectators.forEach(spectator -> spectator.sendMessage(message)); spectators.forEach(spectator -> spectator.sendMessage(message));
} }
private void broadcastToSpectators(BasicMessage message) { private void broadcastToSpectators(BasicMessage message) {
Logger.trace("Broadcasting message of type {} to all spectators", message.messageType);
spectators.forEach(spectator -> spectator.sendMessage(message)); spectators.forEach(spectator -> spectator.sendMessage(message));
} }
private void broadcastToAllExcept(Participant except, BasicMessage 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(player1)) player1.sendMessage(message);
if (!except.equals(player2)) player2.sendMessage(message); if (!except.equals(player2)) player2.sendMessage(message);
for(Participant spectator: spectators) { for (Participant spectator : spectators) {
if(!except.equals(spectator)) { if (!except.equals(spectator)) {
spectator.sendMessage(message); spectator.sendMessage(message);
} }
} }
} else {
broadcast(message);
}
} }
public void sendEvents(Participant recipient, Event... events) { 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) { if (recipient != null) {
EventMessage message = new EventMessage(); EventMessage message = new EventMessage();
message.messages = events; message.messages = events;
@ -266,6 +281,7 @@ public class LobbyConnection implements Runnable {
} }
public void broadcastEvents(Event... events) { public void broadcastEvents(Event... events) {
Logger.trace("Broadcasting {} events", events.length);
EventMessage message = new EventMessage(); EventMessage message = new EventMessage();
message.messages = events; message.messages = events;
@ -273,6 +289,8 @@ public class LobbyConnection implements Runnable {
} }
public void broadcastToAllExcept(Participant except, Event... events) { 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(); var message = new EventMessage();
message.messages = events; message.messages = events;

View File

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