diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TimeoutTimer.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TimeoutTimer.java index fe13903..96e62b9 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TimeoutTimer.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TimeoutTimer.java @@ -1,7 +1,6 @@ package uulm.teamname.marvelous.server.lobby; import org.tinylog.Logger; -import uulm.teamname.marvelous.server.Server; import uulm.teamname.marvelous.server.lobbymanager.Participant; import java.util.concurrent.*; @@ -12,13 +11,16 @@ public class TimeoutTimer { private final ScheduledExecutorService timer; - private ScheduledFuture player1AlmostTimeout, player1Timeout; - private ScheduledFuture player2AlmostTimeout, player2Timeout; + private ScheduledFuture player1AlmostTimeout; + private ScheduledFuture player1Timeout; + private ScheduledFuture player2AlmostTimeout; + private ScheduledFuture player2Timeout; private final BiConsumer almostTimeoutCallback; private final Consumer 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. diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TurnTimeoutTimer.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TurnTimeoutTimer.java index 32cc414..9e39aa5 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TurnTimeoutTimer.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/TurnTimeoutTimer.java @@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobby; import org.tinylog.Logger; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; -import uulm.teamname.marvelous.server.Server; import uulm.teamname.marvelous.server.lobbymanager.Participant; import java.util.concurrent.*; @@ -20,12 +19,7 @@ public class TurnTimeoutTimer { public TurnTimeoutTimer(int maxRoundTime, Consumer callback) { String lobbyThreadName = Thread.currentThread().getName(); - ThreadFactory threadFactory = new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - return new Thread(r, lobbyThreadName + "-TurnTimerThread"); - } - }; + ThreadFactory threadFactory = r -> new Thread(r, lobbyThreadName + "-TurnTimerThread"); this.timer = Executors.newSingleThreadScheduledExecutor(threadFactory); this.maxRoundTime = maxRoundTime; this.callback = callback; diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameLogicSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameLogicSegment.java index b87e295..42593cf 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameLogicSegment.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameLogicSegment.java @@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import org.tinylog.Logger; 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.requests.Request; diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PauseSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PauseSegment.java index 01a7cbe..1a9a5f8 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PauseSegment.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PauseSegment.java @@ -5,7 +5,6 @@ import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventType; 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.RequestType; diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Pipeline.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Pipeline.java index 6578d96..6b7c518 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Pipeline.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Pipeline.java @@ -51,7 +51,7 @@ public class Pipeline { for (Segment segment : segments) { // Give the segment the packet, carrier and abort, and let it process requests 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 } } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PlayerFilterSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PlayerFilterSegment.java index e8be38a..801f661 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PlayerFilterSegment.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/PlayerFilterSegment.java @@ -3,7 +3,6 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import org.tinylog.Logger; import uulm.teamname.marvelous.gamelibrary.entities.EntityType; 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.Request; import uulm.teamname.marvelous.server.lobbymanager.Participant; diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestGameStateSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestGameStateSegment.java index fc9d4a1..ec09d1b 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestGameStateSegment.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestGameStateSegment.java @@ -2,12 +2,9 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import org.tinylog.Logger; 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.messages.server.EventMessage; -import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; -import uulm.teamname.marvelous.server.lobby.Lobby; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java index 0163f46..b25f55d 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java @@ -26,7 +26,9 @@ public class LobbyConnection implements Runnable { public final String gameID; public LobbyConnectionState state = LobbyConnectionState.Waiting; - private Participant player1, player2; + private Participant player1; + private Participant player2; + private final HashSet spectators = new HashSet<>(10); private final HashMap> selection = new HashMap<>(2); public final HashMap options = new HashMap<>(2); @@ -59,7 +61,6 @@ public class LobbyConnection implements Runnable { if (participant.type == ParticipantType.Spectator) { Logger.trace("Adding spectator"); spectators.add(participant); - return; } else if (participant.type == ParticipantType.PlayerOne) { player1 = participant; } else { diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java index ade2391..824beea 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManager.java @@ -120,7 +120,7 @@ public class LobbyManager { int n = 0; for (int i = 0; i < 12; i++) { - if (message.characters[i]) { + if (Boolean.TRUE.equals(message.characters[i])) { selected[n++] = options[i].characterID; } } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java index b0b9d4e..f8d7f48 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java @@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobbymanager; import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; 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.SUID; @@ -52,4 +51,12 @@ public class Participant { Participant other = (Participant) o; 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; + } } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java b/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java index 9f69e1c..ada923e 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java @@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class UserManager { private static UserManager instance; + private static final String errorInvalidMessage = "Invalid message."; /** * @return the current instance of the UserManager @@ -175,7 +176,7 @@ public class UserManager { return; } - if(message.reconnect) { + if(Boolean.TRUE.equals(message.reconnect)) { Logger.trace("Reconnecting to lobby. Forwarding reconnect instruction to the LobbyManager"); if(LobbyManager.getInstance().handleReconnect(client)) { Logger.trace("Successfully reconnected client, changing state to Playing..."); @@ -200,12 +201,12 @@ public class UserManager { } Logger.trace("Relaying message to LobbyManager"); - if(message.startGame) { + if(Boolean.TRUE.equals(message.startGame)) { if(LobbyManager.getInstance().handleReady(client, message)) { client.setState(ClientState.Assigned); } else { Logger.trace("Sending error to client as message couldn't be processed properly"); - client.sendError("Invalid message."); + client.sendError(errorInvalidMessage); } } else { Logger.debug("Disconnecting client as game couldn't be started"); @@ -227,7 +228,7 @@ public class UserManager { if(LobbyManager.getInstance().handleSelection(client, message)) { Logger.trace("Handled successfully"); } else { - client.sendError("Invalid message."); + client.sendError(errorInvalidMessage); } } @@ -247,7 +248,7 @@ public class UserManager { //"i approve" - the server } else { Logger.debug("Message couldn't be handled, sending error to client"); - client.sendError("Invalid message."); + client.sendError(errorInvalidMessage); } }