fix: prevent EndRoundRequests and invalid pausing

This commit is contained in:
punchready 2021-07-06 13:14:00 +02:00
parent f8aa2c9bf2
commit 8f12084308
4 changed files with 52 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import org.tinylog.Logger;
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.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
@ -72,6 +73,11 @@ public class PauseSegment implements Segment {
// check if there is a pause request (either start or stop)
if (packet.contains(new RequestBuilder(RequestType.PauseStartRequest).buildGameRequest())) {
Logger.trace("PauseStartRequest found");
if(packet.getOrigin().type == ParticipantType.Spectator || packet.getOrigin().isAI) {
Logger.trace("Invalid pause start request. Aborting");
abort.set(true);
return;
}
if (!paused) {
// pause the game
pauseGame();
@ -85,6 +91,11 @@ public class PauseSegment implements Segment {
}
} else if (packet.contains(new RequestBuilder(RequestType.PauseStopRequest).buildGameRequest())) {
Logger.trace("PauseStopRequest found");
if(packet.getOrigin().type == ParticipantType.Spectator || packet.getOrigin().isAI) {
Logger.trace("Invalid pause stop request. Aborting");
abort.set(true);
return;
}
if (paused) {
pauseEnd();
Logger.debug("Game unpaused.");

View File

@ -0,0 +1,36 @@
package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* The {@link RequestTurnEndSegment} handles requests of {@link RequestType} EndRoundRequest. It filters invalid turn end requests.
*/
public class RequestTurnEndSegment implements Segment {
private final GameInstance game;
public RequestTurnEndSegment(GameInstance game) {
this.game = game;
}
@Override
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort) {
Logger.trace("RequestTurnEndSegment received {} requests", packet.size());
var active = game.state.getActiveCharacter().type;
var from = packet.getOrigin().type == ParticipantType.PlayerOne ? EntityType.P1 : EntityType.P2;
if (packet.containsRequestOfType(RequestType.EndRoundRequest) && active != from) {
Logger.trace("Invalid end turn request. Aborting");
abort.set(true);
}
}
}

View File

@ -212,7 +212,7 @@ public class LobbyManager {
public void removeLobby(String lobbyID) {
lobbies.remove(lobbyID);
}
/**
* Adds a participant to a lobby. If the maximum amount of lobbies is already filled, or if the lobby requested
* isn't free, the participant is disconnected.
@ -242,7 +242,7 @@ public class LobbyManager {
Logger.trace("New participant '{}' has the role '{}'", client.getId(), type);
Participant participant = new Participant(client, lobbyID, type);
Participant participant = new Participant(client, lobbyID, type, role == RoleEnum.KI);
participants.put(client.getId(), participant);
lobby.addParticipant(participant);

View File

@ -13,12 +13,14 @@ public class Participant {
public ParticipantState state = ParticipantState.Assigned;
public final ParticipantType type;
public boolean disconnected = false;
public final boolean isAI;
public Participant(Client client, String lobby, ParticipantType type) {
public Participant(Client client, String lobby, ParticipantType type, boolean ai) {
this.client = client;
this.id = client.getId();
this.lobby = lobby;
this.type = type;
this.isAI = ai;
}
public void setClient(Client client) {