From 8f120843084313e05debd578e289028dffa2eebe Mon Sep 17 00:00:00 2001 From: punchready Date: Tue, 6 Jul 2021 13:14:00 +0200 Subject: [PATCH] fix: prevent EndRoundRequests and invalid pausing --- .../server/lobby/pipelining/PauseSegment.java | 11 ++++++ .../pipelining/RequestTurnEndSegment.java | 36 +++++++++++++++++++ .../server/lobbymanager/LobbyManager.java | 4 +-- .../server/lobbymanager/Participant.java | 4 ++- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestTurnEndSegment.java 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 d6241c4..01a7cbe 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 @@ -4,6 +4,7 @@ import org.tinylog.Logger; 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; @@ -72,6 +73,11 @@ public class PauseSegment implements Segment { // check if there is a pause request (either start or stop) if (packet.contains(new RequestBuilder(RequestType.PauseStartRequest).buildGameRequest())) { Logger.trace("PauseStartRequest found"); + if(packet.getOrigin().type == ParticipantType.Spectator || packet.getOrigin().isAI) { + Logger.trace("Invalid pause start request. Aborting"); + abort.set(true); + return; + } if (!paused) { // pause the game pauseGame(); @@ -85,6 +91,11 @@ public class PauseSegment implements Segment { } } else if (packet.contains(new RequestBuilder(RequestType.PauseStopRequest).buildGameRequest())) { Logger.trace("PauseStopRequest found"); + if(packet.getOrigin().type == ParticipantType.Spectator || packet.getOrigin().isAI) { + Logger.trace("Invalid pause stop request. Aborting"); + abort.set(true); + return; + } if (paused) { pauseEnd(); Logger.debug("Game unpaused."); diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestTurnEndSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestTurnEndSegment.java new file mode 100644 index 0000000..64476d0 --- /dev/null +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestTurnEndSegment.java @@ -0,0 +1,36 @@ +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.gamelogic.GameInstance; +import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; +import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage; +import uulm.teamname.marvelous.gamelibrary.requests.RequestType; + +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * The {@link RequestTurnEndSegment} handles requests of {@link RequestType} EndRoundRequest. It filters invalid turn end requests. + */ +public class RequestTurnEndSegment implements Segment { + + private final GameInstance game; + + + public RequestTurnEndSegment(GameInstance game) { + this.game = game; + } + + @Override + public void processRequests(Packet packet, List carrier, AtomicBoolean abort) { + Logger.trace("RequestTurnEndSegment received {} requests", packet.size()); + var active = game.state.getActiveCharacter().type; + var from = packet.getOrigin().type == ParticipantType.PlayerOne ? EntityType.P1 : EntityType.P2; + if (packet.containsRequestOfType(RequestType.EndRoundRequest) && active != from) { + Logger.trace("Invalid end turn request. Aborting"); + abort.set(true); + } + } +} 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 3b104c0..ade2391 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 @@ -212,7 +212,7 @@ public class LobbyManager { public void removeLobby(String lobbyID) { lobbies.remove(lobbyID); } - + /** * Adds a participant to a lobby. If the maximum amount of lobbies is already filled, or if the lobby requested * isn't free, the participant is disconnected. @@ -242,7 +242,7 @@ public class LobbyManager { Logger.trace("New participant '{}' has the role '{}'", client.getId(), type); - Participant participant = new Participant(client, lobbyID, type); + Participant participant = new Participant(client, lobbyID, type, role == RoleEnum.KI); participants.put(client.getId(), participant); lobby.addParticipant(participant); 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 54574e0..b0b9d4e 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 @@ -13,12 +13,14 @@ public class Participant { public ParticipantState state = ParticipantState.Assigned; public final ParticipantType type; public boolean disconnected = false; + public final boolean isAI; - public Participant(Client client, String lobby, ParticipantType type) { + public Participant(Client client, String lobby, ParticipantType type, boolean ai) { this.client = client; this.id = client.getId(); this.lobby = lobby; this.type = type; + this.isAI = ai; } public void setClient(Client client) {