From 09c5545c6d9def1e83452027a5eedab9b80a3933 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Wed, 7 Jul 2021 18:04:19 +0200 Subject: [PATCH] fix: re-implemented the segment, this time properly --- .../marvelous/server/lobby/Lobby.java | 4 +-- .../FilterEndRoundRequestSegment.java | 24 ++++++++----- .../pipelining/RequestTurnEndSegment.java | 36 ------------------- .../FilterEndRoundRequestSegmentTest.java | 22 ++++++++---- 4 files changed, 33 insertions(+), 53 deletions(-) delete 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/Lobby.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/Lobby.java index d61e0b0..3d6e5be 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/Lobby.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/Lobby.java @@ -63,15 +63,13 @@ public class Lobby { var reqSegment = new RequestGameStateSegment(this.game); this.pauseSegment = new PauseSegment(); - var filterEndRoundRequestSegment = new FilterEndRoundRequestSegment(this::getActivePlayer); - var requestTurnEndSegment = new RequestTurnEndSegment(this.game); + var filterEndRoundRequestSegment = new FilterEndRoundRequestSegment(game.state::getActiveCharacter); var disconnectSegment = new DisconnectSegment(this); var gameLogicSegment = new GameLogicSegment(this.game); pipeline.addSegment(reqSegment) .addSegment(pauseSegment) .addSegment(filterEndRoundRequestSegment) - .addSegment(requestTurnEndSegment) .addSegment(disconnectSegment) .addSegment(gameLogicSegment); diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegment.java index d9db7c1..4d0d444 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegment.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegment.java @@ -1,7 +1,11 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import org.tinylog.Logger; +import uulm.teamname.marvelous.gamelibrary.entities.EntityID; +import uulm.teamname.marvelous.gamelibrary.entities.EntityType; import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.gamelogic.GameStateView; +import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.server.lobbymanager.Participant; import uulm.teamname.marvelous.server.lobby.Lobby; @@ -17,18 +21,18 @@ import java.util.function.Supplier; */ public class FilterEndRoundRequestSegment implements Segment { - private final Supplier activeParticipantCallback; + private final Supplier getActiveCharacterCallback; /** * Creates a new {@link FilterEndRoundRequestSegment} - * @param activeParticipantCallback is a {@link Supplier}<{@link Participant}> + * @param getActiveCharacterCallback is a {@link Supplier}<{@link Participant}> * that supplies the currently active participant * in a {@link Lobby}. It should normally be bound - * to {@link Lobby#getActivePlayer()}, by executing - * {@code new Filter...Segment(this::getActivePlayer)}. + * to {@link GameStateView#getActiveCharacter()}, by executing + * {@code new Filter...Segment(game.state::getActiveCharacter)}. */ - public FilterEndRoundRequestSegment(Supplier activeParticipantCallback) { - this.activeParticipantCallback = activeParticipantCallback; + public FilterEndRoundRequestSegment(Supplier getActiveCharacterCallback) { + this.getActiveCharacterCallback = getActiveCharacterCallback; } @@ -37,8 +41,12 @@ public class FilterEndRoundRequestSegment implements Segment { Logger.trace("FilterEndRoundSegment has received {} requests", packet.size()); if (packet.containsRequestOfType(RequestType.EndRoundRequest)) { Logger.trace("Packet contains EndRoundRequest"); - if (!packet.getOrigin().equals(activeParticipantCallback.get())) { - Logger.debug("Packet contained EndRoundRequest but was sent from non-active participant, aborting..."); + + var active = getActiveCharacterCallback.get().type; + var from = packet.getOrigin().type == ParticipantType.PlayerOne ? EntityType.P1 : EntityType.P2; + + if (packet.containsRequestOfType(RequestType.EndRoundRequest) && active != from) { + Logger.trace("Invalid endRoundRequest. Expected {} but got {}. Aborting...", active, from); abort.set(true); } } 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 deleted file mode 100644 index 64476d0..0000000 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/RequestTurnEndSegment.java +++ /dev/null @@ -1,36 +0,0 @@ -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/test/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegmentTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegmentTest.java index fce0c81..20972e8 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegmentTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegmentTest.java @@ -3,10 +3,14 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import uulm.teamname.marvelous.gamelibrary.entities.EntityID; +import uulm.teamname.marvelous.gamelibrary.entities.EntityType; +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; import uulm.teamname.marvelous.server.lobbymanager.Participant; +import uulm.teamname.marvelous.server.netconnector.Client; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicBoolean; @@ -18,21 +22,27 @@ class FilterEndRoundRequestSegmentTest { FilterEndRoundRequestSegment segment; - Participant activeParticipant; + EntityID activeCharacter; + + Participant activeParticipant, inactiveParticipant; Request[] requests; @BeforeEach void beforeEach() { - this.segment = new FilterEndRoundRequestSegment(this::getActiveParticipant); - this.activeParticipant = mock(Participant.class); + this.segment = new FilterEndRoundRequestSegment(this::getActiveCharacter); + + this.activeCharacter = new EntityID(EntityType.P1, 2); + this.activeParticipant = new Participant(mock(Client.class), null, ParticipantType.PlayerOne, false); + this.inactiveParticipant = new Participant(mock(Client.class), null, ParticipantType.PlayerTwo, false); + requests = new Request[] { new RequestBuilder(RequestType.EndRoundRequest).buildGameRequest() }; } - private Participant getActiveParticipant() { - return activeParticipant; + private EntityID getActiveCharacter() { + return activeCharacter; } @Test @@ -51,7 +61,7 @@ class FilterEndRoundRequestSegmentTest { @Test @DisplayName("Request from non-active participant gets flagged as an error") void packetFromNonActiveParticipantTest() { - var packet = new Packet(requests, mock(Participant.class)); + var packet = new Packet(requests, inactiveParticipant); var atomicBoolean = new AtomicBoolean(false); var processedPacket = (Packet) packet.clone();