diff --git a/Gamelib b/Gamelib index 4802b01..0572450 160000 --- a/Gamelib +++ b/Gamelib @@ -1 +1 @@ -Subproject commit 4802b0113d64d333d892185dc72f0b8589481bd0 +Subproject commit 0572450ced119f18a11363e02356d9afe1d880a7 diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegment.java index 3138dfc..dfae228 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegment.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegment.java @@ -1,32 +1,36 @@ package uulm.teamname.marvelous.server.lobby.pipelining; 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.requests.RequestBuilder; +import uulm.teamname.marvelous.gamelibrary.gamelogic.ParticipantType; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.server.lobby.Lobby; -import uulm.teamname.marvelous.server.lobbymanager.Participant; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; public class DisconnectSegment implements Segment { + private final Lobby parent; - private final Participant player; - public DisconnectSegment(Lobby parent, Participant player){ + + public DisconnectSegment(Lobby parent) { this.parent = parent; - this.player = player; } @Override public void processRequests(Packet packet, List carrier, AtomicBoolean abort) { - if (packet.contains(new RequestBuilder(RequestType.DisconnectRequest).buildGameRequest())) { - carrier.add(new EventBuilder(EventType.DisconnectEvent).buildGameEvent()); - parent.connection.removePlayer(player); - } + if (packet.containsRequestOfType(RequestType.DisconnectRequest)) { + parent.connection.removePlayer(packet.getOrigin()); + if (packet.getOrigin().type != ParticipantType.Spectator) { + if(parent.connection.hasPlayer1()){ + parent.generateWin(parent.connection.getPlayer1()); + } + else if(parent.connection.hasPlayer2()) { + parent.generateWin(parent.connection.getPlayer2()); + } + } + } packet.clear(); } } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameStateSegment.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameStateSegment.java index 17be751..fad74be 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameStateSegment.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/GameStateSegment.java @@ -1,42 +1,30 @@ package uulm.teamname.marvelous.server.lobby.pipelining; 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; +import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.server.lobby.Lobby; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; public class GameStateSegment implements Segment { - private Lobby parent; + private GameInstance game; - public GameStateSegment(Lobby parent){ - this.parent = parent; + public GameStateSegment(GameInstance game){ + this.game = game; } @Override public void processRequests(Packet packet, List carrier, AtomicBoolean abort) { - - parent.game.checkRequestsAndApply(packet.toArray(new Request[0])); - -// if (packet.contains(new RequestBuilder(RequestType.MeleeAttackRequest).buildGameRequest())) { -// carrier.add(new EventBuilder(EventType.MeleeAttackEvent).buildGameEvent()); -// } -// if (packet.contains(new RequestBuilder(RequestType.RangedAttackRequest).buildGameRequest())) { -// carrier.add(new EventBuilder(EventType.RangedAttackEvent).buildGameEvent()); -// } -// if (packet.contains(new RequestBuilder(RequestType.MoveRequest).buildGameRequest())) { -// carrier.add(new EventBuilder(EventType.MoveEvent).buildGameEvent()); -// } -// if (packet.contains(new RequestBuilder(RequestType.ExchangeInfinityStoneRequest).buildGameRequest())) { -// carrier.add(new EventBuilder(EventType.ExchangeInfinityStoneEvent).buildGameEvent()); -// } -// if (packet.contains(new RequestBuilder(RequestType.UseInfinityStoneRequest).buildGameRequest())) { -// carrier.add(new EventBuilder(EventType.UseInfinityStoneEvent).buildGameEvent()); -// } -// if (packet.contains(new RequestBuilder(RequestType.EndRoundRequest).buildGameRequest())) { -// //TODO what to do after EndRoundRequest -// //carrier.add(new EventBuilder(EventType.EndRoundEvent).buildGameEvent()); -// } + var result = game.checkRequestsAndApply(packet.toArray(new Request[0])); + if (result.isPresent()) { + carrier.addAll(result.get()); + packet.clear(); + } else { + abort.set(true); + } } } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Packet.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Packet.java index 12a6e68..e3cab2f 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Packet.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/Packet.java @@ -2,6 +2,7 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; +import uulm.teamname.marvelous.server.lobbymanager.Participant; import java.util.ArrayList; import java.util.Arrays; @@ -10,7 +11,10 @@ import java.util.List; public class Packet extends ArrayList { - public Packet (Request[] requests) { + private final Participant origin; + + public Packet (Request[] requests, Participant origin) { + this.origin = origin; addAll(Arrays.asList(requests)); } @@ -23,4 +27,18 @@ public class Packet extends ArrayList { return false; } + public void removeRequestsOfTypes(RequestType... types) { + var listOfTypes = Arrays.asList(types); + this.removeIf(request -> listOfTypes.contains(request.type)); + } + + public void removeRequestsNotOfTypes(RequestType... types) { + var listOfTypes = Arrays.asList(types); + this.removeIf(request -> !listOfTypes.contains(request.type)); + } + + public Participant getOrigin() { + return origin; + } + } 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 3bc11aa..9d349ca 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 @@ -77,7 +77,7 @@ public class PauseSegment implements Segment { } } - // filter the events + /* filter the events if (paused) { for (Request request: packet) { switch (request.type) { @@ -90,9 +90,19 @@ public class PauseSegment implements Segment { PauseStopRequest, PauseStartRequest -> packet.remove(request); - case DisconnectRequest, Req -> { /* do nothing */ } + case DisconnectRequest, Req -> { /* do nothing } } } - } + } */ + + packet.removeRequestsOfTypes( + RequestType.MeleeAttackRequest, + RequestType.RangedAttackRequest, + RequestType.MoveRequest, + RequestType.ExchangeInfinityStoneRequest, + RequestType.UseInfinityStoneRequest, + RequestType.EndRoundRequest, + RequestType.PauseStopRequest, + RequestType.PauseStartRequest); } } 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 37c0d34..9d7a767 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 @@ -2,6 +2,7 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.requests.Request; +import uulm.teamname.marvelous.server.lobbymanager.Participant; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -35,9 +36,9 @@ public class Pipeline { * out of the {@link Optional}, first check whether the {@link Optional} is empty by doing * {@link Optional#isEmpty()} or {@link Optional#isPresent()}, and act accordingly. */ - public Optional processRequests(Request[] requests) { + public Optional processRequests(Request[] requests, Participant origin) { // The packet carries the requests, and gets smaller per segment - Packet packet = new Packet(requests); + Packet packet = new Packet(requests, origin); // The packet is filled by the requests resulting from events per segment List carrier = new ArrayList<>(); // The abort boolean describes whether an abort happened in a segment 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 fbde3d7..c29b0df 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 @@ -1,6 +1,7 @@ package uulm.teamname.marvelous.server.lobby.pipelining; import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance; import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.server.lobby.Lobby; @@ -10,16 +11,16 @@ import java.util.concurrent.atomic.AtomicBoolean; public class RequestGameStateSegment implements Segment { - private Lobby parent; + private final GameInstance game; - public RequestGameStateSegment(Lobby parent) { - this.parent = parent; + public RequestGameStateSegment(GameInstance game) { + this.game = game; } @Override public void processRequests(Packet packet, List carrier, AtomicBoolean abort) { if (packet.containsRequestOfType(RequestType.Req)) { - carrier.add(parent.game.getGameStateEvent()); + carrier.add(game.getGameStateEvent()); } packet.clear(); } diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/GameStateSegmentTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/GameStateSegmentTest.java new file mode 100644 index 0000000..e5412cd --- /dev/null +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/GameStateSegmentTest.java @@ -0,0 +1,48 @@ +package uulm.teamname.marvelous.server.lobby.pipelining; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; +import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder; +import uulm.teamname.marvelous.gamelibrary.events.EventType; +import uulm.teamname.marvelous.gamelibrary.events.GameEvent; +import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance; +import uulm.teamname.marvelous.gamelibrary.requests.Request; +import uulm.teamname.marvelous.gamelibrary.requests.RequestType; +import uulm.teamname.marvelous.server.lobbymanager.Participant; + +import java.util.List; +import java.util.ArrayList; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.*; + +class GameStateSegmentTest { + + @Test + void processRequests() { + var game = mock(GameInstance.class); + var event = new EventBuilder(EventType.DisconnectEvent).buildGameEvent(); + when(game.checkRequestsAndApply(any(Request.class))).thenReturn(Optional.of(List.of(event))); + var abort = new AtomicBoolean(false); + + var segment = new GameStateSegment(game); + + // note that DisconnectRequests are actually never passed to the GameLogic, ever. + var packet = new Packet( + new Request[] {new RequestBuilder(RequestType.DisconnectRequest).buildGameRequest()}, + null); + + var carrier = new ArrayList(1); + + segment.processRequests(packet, carrier, abort); + + assertThat(carrier.contains(event)).isTrue(); + verify(game).checkRequestsAndApply(packet.get(0)); + assertThat(abort.get()).isFalse(); + } +}