refactor: re-formated LobbyConnection

This commit is contained in:
Yannik Bretschneider 2021-06-08 02:28:58 +02:00
parent 07ce02b937
commit 80ad60a658

View File

@ -54,11 +54,11 @@ public class LobbyConnection implements Runnable {
participant.getClient().state = ClientState.Playing;
participant.state = ParticipantState.Playing;
}
if(participant.type == ParticipantType.Spectator) {
if (participant.type == ParticipantType.Spectator) {
Logger.trace("Adding spectator");
spectators.add(participant);
return;
} else if(participant.type == ParticipantType.PlayerOne) {
} else if (participant.type == ParticipantType.PlayerOne) {
player1 = participant;
} else {
player2 = participant;
@ -90,11 +90,11 @@ public class LobbyConnection implements Runnable {
/** Returns the next free slot in the lobby as a {@link ParticipantType} */
public ParticipantType freeSlot() {
if(player1 == null) {
if (player1 == null) {
return ParticipantType.PlayerOne;
}else if(player2 == null) {
} else if (player2 == null) {
return ParticipantType.PlayerTwo;
}else {
} else {
return ParticipantType.Spectator;
}
}
@ -113,7 +113,9 @@ public class LobbyConnection implements Runnable {
return player2;
}
public HashSet<Participant> getSpectators() {return spectators;}
public HashSet<Participant> getSpectators() {
return spectators;
}
public boolean hasPlayer1() {
return player1 != null;
@ -127,15 +129,14 @@ public class LobbyConnection implements Runnable {
public void handleMessage(Participant participant, Request[] requests) {
try {
this.requestQueue.put(Tuple.of(participant, requests));
}catch (InterruptedException e) {
} catch (InterruptedException ignored) {
}
}
/** Handles disconnect of a Participant. Hereby, the participant is made ready for reconnection */
public void handleDisconnect(Participant participant) {
participant.disconnected = true;
if(state == LobbyConnectionState.Started) {
if (state == LobbyConnectionState.Started) {
lobby.handleDisconnect(participant);
}
}
@ -143,7 +144,7 @@ public class LobbyConnection implements Runnable {
/** Handles reconnect of a Participant. Hereby, the participant is made ready for reconnection */
public void handleReconnect(Participant participant) {
participant.disconnected = false;
if(state == LobbyConnectionState.Started) {
if (state == LobbyConnectionState.Started) {
sendGameStructure(participant.equals(player1), participant.equals(player2), false);
lobby.handleReconnect(participant);
}
@ -156,7 +157,7 @@ public class LobbyConnection implements Runnable {
player1.state = ParticipantState.Playing;
player2.state = ParticipantState.Playing;
for(Participant spectator: spectators) {
for (Participant spectator : spectators) {
spectator.state = ParticipantState.Playing;
}
@ -177,7 +178,7 @@ public class LobbyConnection implements Runnable {
while (state == LobbyConnectionState.Started) {
Tuple<Participant, Request[]> currentRequests = pollQueueAsync();
if(currentRequests != null) {
if (currentRequests != null) {
lobby.receiveRequests(currentRequests.item2, currentRequests.item1);
}
}
@ -199,11 +200,11 @@ public class LobbyConnection implements Runnable {
gameStructureMessage.playerOneCharacters = new CharacterProperties[6];
gameStructureMessage.playerTwoCharacters = new CharacterProperties[6];
int i = 0;
for(Integer id: selection.get(player1.id)) {
for (Integer id : selection.get(player1.id)) {
gameStructureMessage.playerOneCharacters[i++] = Server.getCharacterConfig().getIDMap().get(id);
}
i = 0;
for(Integer id: selection.get(player2.id)) {
for (Integer id : selection.get(player2.id)) {
gameStructureMessage.playerTwoCharacters[i++] = Server.getCharacterConfig().getIDMap().get(id);
}
@ -211,17 +212,17 @@ public class LobbyConnection implements Runnable {
gameStructureMessage.scenarioconfig = Server.getScenarioConfig();
// Sending GameStructure message with fitting assignment
if(p1) {
if (p1) {
gameStructureMessage.assignment = ParticipantType.PlayerOne;
player1.sendMessage(gameStructureMessage);
}
if(p2) {
if (p2) {
gameStructureMessage.assignment = ParticipantType.PlayerTwo;
player2.sendMessage(gameStructureMessage);
}
if(spectators) {
if (spectators) {
gameStructureMessage.assignment = ParticipantType.Spectator;
broadcastToSpectators(gameStructureMessage);
}
@ -231,7 +232,8 @@ public class LobbyConnection implements Runnable {
Tuple<Participant, Request[]> current = null;
try {
current = requestQueue.poll(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignored) {}
} catch (InterruptedException ignored) {
}
return current;
}