sonarqube: fixed some smells

This commit is contained in:
Richard Reiber 2021-08-05 22:19:27 +02:00
parent 7fb4f41326
commit 6f19056fde
11 changed files with 26 additions and 27 deletions

View File

@ -1,7 +1,6 @@
package uulm.teamname.marvelous.server.lobby; package uulm.teamname.marvelous.server.lobby;
import org.tinylog.Logger; import org.tinylog.Logger;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobbymanager.Participant; import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -12,13 +11,16 @@ public class TimeoutTimer {
private final ScheduledExecutorService timer; private final ScheduledExecutorService timer;
private ScheduledFuture<Participant> player1AlmostTimeout, player1Timeout; private ScheduledFuture<Participant> player1AlmostTimeout;
private ScheduledFuture<Participant> player2AlmostTimeout, player2Timeout; private ScheduledFuture<Participant> player1Timeout;
private ScheduledFuture<Participant> player2AlmostTimeout;
private ScheduledFuture<Participant> player2Timeout;
private final BiConsumer<Participant, Integer> almostTimeoutCallback; private final BiConsumer<Participant, Integer> almostTimeoutCallback;
private final Consumer<Participant> timeoutCallback; private final Consumer<Participant> timeoutCallback;
private final int almostTimeoutTime, timeoutTime; private final int almostTimeoutTime;
private final int timeoutTime;
/** /**
* Class that manages timeouts of players after not sending a message for a long time. * Class that manages timeouts of players after not sending a message for a long time.

View File

@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobby;
import org.tinylog.Logger; import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobbymanager.Participant; import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -20,12 +19,7 @@ public class TurnTimeoutTimer {
public TurnTimeoutTimer(int maxRoundTime, Consumer<Participant> callback) { public TurnTimeoutTimer(int maxRoundTime, Consumer<Participant> callback) {
String lobbyThreadName = Thread.currentThread().getName(); String lobbyThreadName = Thread.currentThread().getName();
ThreadFactory threadFactory = new ThreadFactory() { ThreadFactory threadFactory = r -> new Thread(r, lobbyThreadName + "-TurnTimerThread");
@Override
public Thread newThread(Runnable r) {
return new Thread(r, lobbyThreadName + "-TurnTimerThread");
}
};
this.timer = Executors.newSingleThreadScheduledExecutor(threadFactory); this.timer = Executors.newSingleThreadScheduledExecutor(threadFactory);
this.maxRoundTime = maxRoundTime; this.maxRoundTime = maxRoundTime;
this.callback = callback; this.callback = callback;

View File

@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger; import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance; import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance;
import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.Request;

View File

@ -5,7 +5,6 @@ import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType; import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder; import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.gamelibrary.requests.RequestType;

View File

@ -51,7 +51,7 @@ public class Pipeline {
for (Segment segment : segments) { for (Segment segment : segments) {
// Give the segment the packet, carrier and abort, and let it process requests // Give the segment the packet, carrier and abort, and let it process requests
segment.processRequests(packet, carrier, abort); segment.processRequests(packet, carrier, abort);
if (packet.size() == 0 || abort.get()) { // if packet is empty (all requests processed) or abort initiated if (packet.isEmpty() || abort.get()) { // if packet is empty (all requests processed) or abort initiated
break; // (abort boolean true), break out of the loop break; // (abort boolean true), break out of the loop
} }
} }

View File

@ -3,7 +3,6 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger; import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType; import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.requests.CharacterRequest; import uulm.teamname.marvelous.gamelibrary.requests.CharacterRequest;
import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.server.lobbymanager.Participant; import uulm.teamname.marvelous.server.lobbymanager.Participant;

View File

@ -2,12 +2,9 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger; import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance; import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance;
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage; import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobby.Lobby;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;

View File

@ -26,7 +26,9 @@ public class LobbyConnection implements Runnable {
public final String gameID; public final String gameID;
public LobbyConnectionState state = LobbyConnectionState.Waiting; public LobbyConnectionState state = LobbyConnectionState.Waiting;
private Participant player1, player2; private Participant player1;
private Participant 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<>(2); private final HashMap<SUID, List<Integer>> selection = new HashMap<>(2);
public final HashMap<ParticipantType, CharacterProperties[]> options = new HashMap<>(2); public final HashMap<ParticipantType, CharacterProperties[]> options = new HashMap<>(2);
@ -59,7 +61,6 @@ public class LobbyConnection implements Runnable {
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;
} else if (participant.type == ParticipantType.PlayerOne) { } else if (participant.type == ParticipantType.PlayerOne) {
player1 = participant; player1 = participant;
} else { } else {

View File

@ -120,7 +120,7 @@ public class LobbyManager {
int n = 0; int n = 0;
for (int i = 0; i < 12; i++) { for (int i = 0; i < 12; i++) {
if (message.characters[i]) { if (Boolean.TRUE.equals(message.characters[i])) {
selected[n++] = options[i].characterID; selected[n++] = options[i].characterID;
} }
} }

View File

@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobbymanager;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.server.lobby.Lobby;
import uulm.teamname.marvelous.server.netconnector.Client; import uulm.teamname.marvelous.server.netconnector.Client;
import uulm.teamname.marvelous.server.netconnector.SUID; import uulm.teamname.marvelous.server.netconnector.SUID;
@ -52,4 +51,12 @@ public class Participant {
Participant other = (Participant) o; Participant other = (Participant) o;
return other.id.equals(id); return other.id.equals(id);
} }
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((client == null) ? 0 : client.hashCode()) + ((id == null) ? 0 : id.hashCode());
return result;
}
} }

View File

@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/ */
public class UserManager { public class UserManager {
private static UserManager instance; private static UserManager instance;
private static final String errorInvalidMessage = "Invalid message.";
/** /**
* @return the current instance of the UserManager * @return the current instance of the UserManager
@ -175,7 +176,7 @@ public class UserManager {
return; return;
} }
if(message.reconnect) { if(Boolean.TRUE.equals(message.reconnect)) {
Logger.trace("Reconnecting to lobby. Forwarding reconnect instruction to the LobbyManager"); Logger.trace("Reconnecting to lobby. Forwarding reconnect instruction to the LobbyManager");
if(LobbyManager.getInstance().handleReconnect(client)) { if(LobbyManager.getInstance().handleReconnect(client)) {
Logger.trace("Successfully reconnected client, changing state to Playing..."); Logger.trace("Successfully reconnected client, changing state to Playing...");
@ -200,12 +201,12 @@ public class UserManager {
} }
Logger.trace("Relaying message to LobbyManager"); Logger.trace("Relaying message to LobbyManager");
if(message.startGame) { if(Boolean.TRUE.equals(message.startGame)) {
if(LobbyManager.getInstance().handleReady(client, message)) { if(LobbyManager.getInstance().handleReady(client, message)) {
client.setState(ClientState.Assigned); client.setState(ClientState.Assigned);
} else { } else {
Logger.trace("Sending error to client as message couldn't be processed properly"); Logger.trace("Sending error to client as message couldn't be processed properly");
client.sendError("Invalid message."); client.sendError(errorInvalidMessage);
} }
} else { } else {
Logger.debug("Disconnecting client as game couldn't be started"); Logger.debug("Disconnecting client as game couldn't be started");
@ -227,7 +228,7 @@ public class UserManager {
if(LobbyManager.getInstance().handleSelection(client, message)) { if(LobbyManager.getInstance().handleSelection(client, message)) {
Logger.trace("Handled successfully"); Logger.trace("Handled successfully");
} else { } else {
client.sendError("Invalid message."); client.sendError(errorInvalidMessage);
} }
} }
@ -247,7 +248,7 @@ public class UserManager {
//"i approve" - the server //"i approve" - the server
} else { } else {
Logger.debug("Message couldn't be handled, sending error to client"); Logger.debug("Message couldn't be handled, sending error to client");
client.sendError("Invalid message."); client.sendError(errorInvalidMessage);
} }
} }