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

55 lines
2.5 KiB
Java

package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.gamelogic.GameStateView;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
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<EntityID> getActiveCharacterCallback;
/**
* Creates a new {@link FilterEndRoundRequestSegment}
* @param getActiveCharacterCallback is a {@link Supplier}<{@link Participant}>
* that supplies the currently active participant
* in a {@link Lobby}. It should normally be bound
* to {@link GameStateView#getActiveCharacter()}, by executing
* {@code new Filter...Segment(game.state::getActiveCharacter)}.
*/
public FilterEndRoundRequestSegment(Supplier<EntityID> getActiveCharacterCallback) {
this.getActiveCharacterCallback = getActiveCharacterCallback;
}
@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");
var active = getActiveCharacterCallback.get().type;
var from = packet.getOrigin().type == ParticipantType.PlayerOne ? EntityType.P1 : EntityType.P2;
if (packet.containsRequestOfType(RequestType.EndRoundRequest) && active != from) {
Logger.trace("Invalid endRoundRequest. Expected {} but got {}. Aborting...", active, from);
abort.set(true);
}
}
}
}