feat: created packet class for pipelining

This commit is contained in:
Yannik Bretschneider 2021-06-01 15:26:30 +02:00
parent 74f3b86144
commit 0c2bc6cf6a
7 changed files with 59 additions and 41 deletions

View File

@ -1,11 +1,8 @@
package uulm.teamname.marvelous.server.lobby.pipelining;
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
import uulm.teamname.marvelous.gamelibrary.events.EntityEvent;
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.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobby.Lobby;
@ -24,7 +21,7 @@ public class DisconnectSegment implements Segment {
@Override
public void processRequests(List<Request> packet, List<Event> carrier, AtomicBoolean abort) {
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);

View File

@ -1,17 +1,13 @@
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.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
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{
public class GameStateSegment implements Segment {
private Lobby parent;
public GameStateSegment(Lobby parent){
@ -19,25 +15,28 @@ public class GameStateSegment implements Segment{
}
@Override
public void processRequests(List<Request> packet, List<Event> carrier, AtomicBoolean abort) {
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());
}
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());
// }
}
}

View File

@ -0,0 +1,26 @@
package uulm.teamname.marvelous.server.lobby.pipelining;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Packet extends ArrayList<Request> {
public Packet (Request[] requests) {
addAll(Arrays.asList(requests));
}
public boolean containsRequestOfType(RequestType requiredType) {
for (Request request : this) {
if (request.type == requiredType) {
return true;
}
}
return false;
}
}

View File

@ -53,7 +53,7 @@ public class PauseSegment implements Segment {
* @param packet
*/
@Override
public void processRequests(List<Request> packet, List<Event> carrier, AtomicBoolean abort) {
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) {
// check if there is a pause request (either start or stop)
if (packet.contains(new RequestBuilder(RequestType.PauseStartRequest).buildGameRequest())) {
if (!paused) {

View File

@ -3,10 +3,7 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@ -40,7 +37,7 @@ public class Pipeline {
*/
public Optional<Event[]> processRequests(Request[] requests) {
// The packet carries the requests, and gets smaller per segment
List<Request> packet = Arrays.asList(requests);
Packet packet = new Packet(requests);
// 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,7 +1,6 @@
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.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobby.Lobby;
@ -18,7 +17,7 @@ public class RequestGameStateSegment implements Segment {
}
@Override
public void processRequests(List<Request> packet, List<Event> carrier, AtomicBoolean abort) {
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) {
if (packet.contains(new RequestBuilder(RequestType.Req).buildGameRequest())) {
carrier.add(parent.game.getGameStateEvent());
}

View File

@ -17,9 +17,9 @@ public interface Segment {
* @param carrier is a {@link List} of {@link Event Events} that is appended to
* if new requests are generated from the execution of the {@link Segment}
* @param abort is an {@link AtomicBoolean} describing whether an error has occurred during the execution of
* the {@link Segment} Note that <b>error</b> here does <b>not describe an execution error</b>
* of the segment, but instead an error in the events passed to it, like for example moving into a Rock.
* The conventional way of setting this boolean is to write {@code abort.set(true); return;}
* the {@link Segment} Note that <b>error</b> here does <b>not describe an execution error</b>
* of the segment, but instead an error in the events passed to it, like for example moving into a Rock.
* The conventional way of setting this boolean is to write {@code abort.set(true); return;}
*/
public void processRequests(List<Request> packet, List<Event> carrier, AtomicBoolean abort);
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort);
}