refactor: re-formated LobbyConnection

This commit is contained in:
Yannik Bretschneider 2021-06-08 02:28:58 +02:00
parent 07ce02b937
commit 80ad60a658
1 changed files with 20 additions and 18 deletions

View File

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