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;
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<Event> 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();
}
}

View File

@ -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<Event> 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);
}
}
}

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.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<Request> {
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<Request> {
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) {
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);
}
}

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.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<Event[]> processRequests(Request[] requests) {
public Optional<Event[]> 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<Event> carrier = new ArrayList<>();
// The abort boolean describes whether an abort happened in a segment

View File

@ -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<Event> carrier, AtomicBoolean abort) {
if (packet.containsRequestOfType(RequestType.Req)) {
carrier.add(parent.game.getGameStateEvent());
carrier.add(game.getGameStateEvent());
}
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();
}
}