refactor: minor fixes

This commit is contained in:
punchready 2021-06-07 08:41:13 +02:00
parent 522558bb16
commit ca0999d06b

View File

@ -26,8 +26,8 @@ public class LobbyConnection implements Runnable {
private Participant player1, player2; private Participant player1, player2;
private final HashSet<Participant> spectators = new HashSet<>(10); private final HashSet<Participant> spectators = new HashSet<>(10);
private final HashMap<SUID, List<Integer>> selection = new HashMap<>(); private final HashMap<SUID, List<Integer>> selection = new HashMap<>(2);
public final HashMap<ParticipantType, CharacterProperties[]> options = new HashMap<>(); public final HashMap<ParticipantType, CharacterProperties[]> options = new HashMap<>(2);
private final BlockingQueue<Tuple<Participant, Request[]>> requestQueue = new LinkedBlockingQueue<>(); private final BlockingQueue<Tuple<Participant, Request[]>> requestQueue = new LinkedBlockingQueue<>();
@ -62,7 +62,11 @@ public class LobbyConnection implements Runnable {
} }
public void removeParticipant(Participant participant) { public void removeParticipant(Participant participant) {
UserManager.getInstance().removeClient(participant.getClient(), ""); removeParticipant(participant, "You have been disconnected.");
}
public void removeParticipant(Participant participant, String reason) {
UserManager.getInstance().removeClient(participant.getClient(), reason);
if(participant.type == ParticipantType.Spectator) { if(participant.type == ParticipantType.Spectator) {
spectators.remove(participant); spectators.remove(participant);
@ -85,6 +89,22 @@ public class LobbyConnection implements Runnable {
} }
} }
public Participant getPlayer1() {
return player1;
}
public Participant getPlayer2() {
return player2;
}
public boolean hasPlayer1() {
return player1 != null;
}
public boolean hasPlayer2() {
return player2 != null;
}
public void handleMessage(Participant participant, Request[] requests) { public void handleMessage(Participant participant, Request[] requests) {
try { try {
@ -128,7 +148,7 @@ public class LobbyConnection implements Runnable {
); );
while (state == LobbyConnectionState.Started) { while (state == LobbyConnectionState.Started) {
Tuple<Participant, Request[]> currentRequests = pollQueueAsync(1000); Tuple<Participant, Request[]> currentRequests = pollQueueAsync();
if(currentRequests != null) { if(currentRequests != null) {
lobby.receiveRequests(currentRequests.item2, currentRequests.item1); lobby.receiveRequests(currentRequests.item2, currentRequests.item1);
@ -170,10 +190,10 @@ public class LobbyConnection implements Runnable {
broadcastToSpectators(gameStructureMessage); broadcastToSpectators(gameStructureMessage);
} }
private Tuple<Participant, Request[]> pollQueueAsync(int timeoutMillis) { private Tuple<Participant, Request[]> pollQueueAsync() {
Tuple<Participant, Request[]> current = null; Tuple<Participant, Request[]> current = null;
try { try {
current = requestQueue.poll(timeoutMillis, TimeUnit.MILLISECONDS); current = requestQueue.poll(1000, TimeUnit.MILLISECONDS);
}catch (InterruptedException e) { }catch (InterruptedException e) {
} }
@ -181,22 +201,6 @@ public class LobbyConnection implements Runnable {
} }
public Participant getPlayer1() {
return player1;
}
public Participant getPlayer2() {
return player2;
}
public boolean hasPlayer1() {
return player1 != null;
}
public boolean hasPlayer2() {
return player2 != null;
}
private void broadcast(BasicMessage message) { private void broadcast(BasicMessage message) {
player1.sendMessage(message); player1.sendMessage(message);
player2.sendMessage(message); player2.sendMessage(message);