feat: completed several segments

This commit is contained in:
Yannik Bretschneider 2021-06-01 17:57:05 +02:00
parent 1887a2c12d
commit 2a4fc1cef0
8 changed files with 117 additions and 47 deletions

@ -1 +1 @@
Subproject commit 4802b0113d64d333d892185dc72f0b8589481bd0 Subproject commit 0572450ced119f18a11363e02356d9afe1d880a7

View File

@ -1,32 +1,36 @@
package uulm.teamname.marvelous.server.lobby.pipelining; package uulm.teamname.marvelous.server.lobby.pipelining;
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.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
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 uulm.teamname.marvelous.server.lobby.Lobby;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class DisconnectSegment implements Segment { public class DisconnectSegment implements Segment {
private final Lobby parent; private final Lobby parent;
private final Participant player;
public DisconnectSegment(Lobby parent, Participant player){ public DisconnectSegment(Lobby parent) {
this.parent = parent; this.parent = parent;
this.player = player;
} }
@Override @Override
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) { public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) {
if (packet.contains(new RequestBuilder(RequestType.DisconnectRequest).buildGameRequest())) { if (packet.containsRequestOfType(RequestType.DisconnectRequest)) {
carrier.add(new EventBuilder(EventType.DisconnectEvent).buildGameEvent()); parent.connection.removePlayer(packet.getOrigin());
parent.connection.removePlayer(player);
}
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(); packet.clear();
} }
} }

View File

@ -1,42 +1,30 @@
package uulm.teamname.marvelous.server.lobby.pipelining; package uulm.teamname.marvelous.server.lobby.pipelining;
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.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobby.Lobby; 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;
public class GameStateSegment implements Segment { public class GameStateSegment implements Segment {
private Lobby parent; private GameInstance game;
public GameStateSegment(Lobby parent){ public GameStateSegment(GameInstance game){
this.parent = parent; this.game = game;
} }
@Override @Override
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) { public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) {
var result = game.checkRequestsAndApply(packet.toArray(new Request[0]));
parent.game.checkRequestsAndApply(packet.toArray(new Request[0])); if (result.isPresent()) {
carrier.addAll(result.get());
// if (packet.contains(new RequestBuilder(RequestType.MeleeAttackRequest).buildGameRequest())) { packet.clear();
// carrier.add(new EventBuilder(EventType.MeleeAttackEvent).buildGameEvent()); } else {
// } abort.set(true);
// 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());
// }
} }
} }

View File

@ -2,6 +2,7 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -10,7 +11,10 @@ import java.util.List;
public class Packet extends ArrayList<Request> { public class Packet extends ArrayList<Request> {
public Packet (Request[] requests) { private final Participant origin;
public Packet (Request[] requests, Participant origin) {
this.origin = origin;
addAll(Arrays.asList(requests)); addAll(Arrays.asList(requests));
} }
@ -23,4 +27,18 @@ public class Packet extends ArrayList<Request> {
return false; 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;
}
} }

View File

@ -77,7 +77,7 @@ public class PauseSegment implements Segment {
} }
} }
// filter the events /* filter the events
if (paused) { if (paused) {
for (Request request: packet) { for (Request request: packet) {
switch (request.type) { switch (request.type) {
@ -90,9 +90,19 @@ public class PauseSegment implements Segment {
PauseStopRequest, PauseStopRequest,
PauseStartRequest -> packet.remove(request); 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);
} }
} }

View File

@ -2,6 +2,7 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean; 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 * 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. * {@link Optional#isEmpty()} or {@link Optional#isPresent()}, and act accordingly.
*/ */
public Optional<Event[]> processRequests(Request[] requests) { public Optional<Event[]> processRequests(Request[] requests, Participant origin) {
// The packet carries the requests, and gets smaller per segment // 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 // The packet is filled by the requests resulting from events per segment
List<Event> carrier = new ArrayList<>(); List<Event> carrier = new ArrayList<>();
// The abort boolean describes whether an abort happened in a segment // The abort boolean describes whether an abort happened in a segment

View File

@ -1,6 +1,7 @@
package uulm.teamname.marvelous.server.lobby.pipelining; package uulm.teamname.marvelous.server.lobby.pipelining;
import uulm.teamname.marvelous.gamelibrary.events.Event; 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.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobby.Lobby; import uulm.teamname.marvelous.server.lobby.Lobby;
@ -10,16 +11,16 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class RequestGameStateSegment implements Segment { public class RequestGameStateSegment implements Segment {
private Lobby parent; private final GameInstance game;
public RequestGameStateSegment(Lobby parent) { public RequestGameStateSegment(GameInstance game) {
this.parent = parent; this.game = game;
} }
@Override @Override
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) { public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) {
if (packet.containsRequestOfType(RequestType.Req)) { if (packet.containsRequestOfType(RequestType.Req)) {
carrier.add(parent.game.getGameStateEvent()); carrier.add(game.getGameStateEvent());
} }
packet.clear(); packet.clear();
} }

View File

@ -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<Event>(1);
segment.processRequests(packet, carrier, abort);
assertThat(carrier.contains(event)).isTrue();
verify(game).checkRequestsAndApply(packet.get(0));
assertThat(abort.get()).isFalse();
}
}