Server/Server/src/main/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegmen...

47 lines
2.0 KiB
Java

package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import uulm.teamname.marvelous.server.lobby.Lobby;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
/**
* {@link Segment} that checks for an {@link RequestType#EndRoundRequest}, and
* if it exists, checks for the player that has sent it. If the player sending
* this request is not the active player, it flags the message as an error.
*/
public class FilterEndRoundRequestSegment implements Segment {
private final Supplier<Participant> activeParticipantCallback;
/**
* Creates a new {@link FilterEndRoundRequestSegment}
* @param activeParticipantCallback 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)}.
*/
public FilterEndRoundRequestSegment(Supplier<Participant> activeParticipantCallback) {
this.activeParticipantCallback = activeParticipantCallback;
}
@Override
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) {
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...");
abort.set(true);
}
}
}
}