207 lines
9.0 KiB
Java
207 lines
9.0 KiB
Java
package games.dmg.purpursentinel;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.UUID;
|
|
|
|
final class FlightDetector {
|
|
private final Map<UUID, State> players = new HashMap<>();
|
|
|
|
Optional<AlertDecision> observe(UUID playerId, FlightFrame frame, SentinelSettings settings) {
|
|
var state = players.computeIfAbsent(playerId, ignored -> new State());
|
|
state.expire(frame.observedAtNanos());
|
|
if (ineligible(frame)) {
|
|
state.resetSequence();
|
|
state.authorized = frame.authorized();
|
|
state.supported = frame.supportState() == SupportState.SUPPORTED;
|
|
state.currentExclusion = frame.exclusionReason() != ExclusionReason.NONE
|
|
? frame.exclusionReason() : frame.authorized() ? ExclusionReason.FLYING : ExclusionReason.ENVIRONMENT;
|
|
state.graceThroughTick = Math.max(state.graceThroughTick,
|
|
frame.tick() + settings.exemptionGraceTicks());
|
|
return Optional.empty();
|
|
}
|
|
if (frame.tick() <= state.graceThroughTick) return Optional.empty();
|
|
if (!state.connected(frame)) state.startSequence(frame);
|
|
|
|
state.lastTick = frame.tick();
|
|
state.authorized = false;
|
|
state.supported = false;
|
|
state.currentExclusion = ExclusionReason.NONE;
|
|
state.eligibleTicks++;
|
|
double horizontal = Math.hypot(frame.toX() - frame.fromX(), frame.toZ() - frame.fromZ());
|
|
double vertical = frame.toY() - frame.fromY();
|
|
state.horizontalDistance += horizontal;
|
|
state.verticalDisplacement = frame.toY() - state.startY;
|
|
if (frame.flying()) state.unauthorizedFlightTicks++;
|
|
if (vertical > 0.01) state.ascentTicks++;
|
|
if (Math.abs(vertical) <= 0.01 && horizontal <= 0.03) state.hoverTicks++;
|
|
if (Math.abs(vertical) <= 0.01 && horizontal > 0.03) state.cruiseTicks++;
|
|
state.signal = signal(state, frame, settings);
|
|
if (!thresholdReached(state, settings)) return Optional.empty();
|
|
state.evidenceExpiresAtNanos = frame.observedAtNanos() + seconds(settings.evidenceWindowSeconds());
|
|
state.lastEvidence = state.evidence(frame.observedAtNanos());
|
|
state.evidenceFirstObservedAtNanos = state.firstObservedAtNanos;
|
|
if (frame.observedAtNanos() < state.cooldownUntilNanos) return Optional.empty();
|
|
state.cooldownUntilNanos = frame.observedAtNanos() + seconds(settings.alertCooldownSeconds());
|
|
state.lastEvidence = state.evidence(frame.observedAtNanos());
|
|
return Optional.of(AlertDecision.flight(playerId, state.lastEvidence));
|
|
}
|
|
|
|
void exempt(UUID playerId, int currentTick, long nowNanos, ExclusionReason reason,
|
|
SentinelSettings settings) {
|
|
var state = players.computeIfAbsent(playerId, ignored -> new State());
|
|
state.expire(nowNanos);
|
|
state.resetSequence();
|
|
state.currentExclusion = reason;
|
|
state.graceThroughTick = Math.max(state.graceThroughTick,
|
|
currentTick + settings.exemptionGraceTicks());
|
|
}
|
|
|
|
Optional<FlightEvidence> inspect(UUID playerId, long nowNanos) {
|
|
var state = players.get(playerId);
|
|
if (state == null) return Optional.empty();
|
|
state.expire(nowNanos);
|
|
if (state.lastEvidence == null) return Optional.empty();
|
|
return Optional.of(state.evidence(nowNanos));
|
|
}
|
|
|
|
void cleanup(int currentTick, long nowNanos) {
|
|
players.entrySet().removeIf(entry -> {
|
|
var state = entry.getValue();
|
|
state.expire(nowNanos);
|
|
if (state.world != null && currentTick > state.lastTick + 1) state.resetSequence();
|
|
return state.world == null && state.lastEvidence == null
|
|
&& state.cooldownUntilNanos <= nowNanos && currentTick > state.graceThroughTick;
|
|
});
|
|
}
|
|
|
|
void forget(UUID playerId) {
|
|
players.remove(playerId);
|
|
}
|
|
|
|
void clear() {
|
|
players.clear();
|
|
}
|
|
|
|
private static boolean ineligible(FlightFrame frame) {
|
|
return frame.authorized() || frame.supportState() != SupportState.UNSUPPORTED
|
|
|| frame.exclusionReason() != ExclusionReason.NONE
|
|
|| !finite(frame.fromX(), frame.fromY(), frame.fromZ(), frame.toX(), frame.toY(), frame.toZ());
|
|
}
|
|
|
|
private static DetectionSignal signal(State state, FlightFrame frame, SentinelSettings settings) {
|
|
if (frame.flying()) return DetectionSignal.UNAUTHORIZED_FLIGHT_STATE;
|
|
if (state.verticalDisplacement >= settings.unsupportedAscentBlocks()) {
|
|
return DetectionSignal.UNSUPPORTED_ASCENT;
|
|
}
|
|
if (state.hoverTicks >= settings.unsupportedHoverTicks()) return DetectionSignal.UNSUPPORTED_HOVER;
|
|
return DetectionSignal.UNSUPPORTED_CRUISE;
|
|
}
|
|
|
|
private static boolean thresholdReached(State state, SentinelSettings settings) {
|
|
return switch (state.signal) {
|
|
case UNAUTHORIZED_FLIGHT_STATE -> state.unauthorizedFlightTicks >= settings.unsupportedFlightTicks();
|
|
case UNSUPPORTED_ASCENT -> state.ascentTicks >= 10;
|
|
case UNSUPPORTED_HOVER -> state.hoverTicks >= settings.unsupportedHoverTicks();
|
|
case UNSUPPORTED_CRUISE -> state.cruiseTicks >= settings.unsupportedFlightTicks()
|
|
&& state.horizontalDistance >= settings.unsupportedCruiseBlocks();
|
|
case ABSOLUTE_GROUND_SPEED, CONTEXTUAL_GROUND_SPEED -> false;
|
|
};
|
|
}
|
|
|
|
private static long seconds(int value) {
|
|
return value * 1_000_000_000L;
|
|
}
|
|
|
|
private static boolean finite(double... values) {
|
|
for (double value : values) {
|
|
if (!Double.isFinite(value)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static final class State {
|
|
private UUID world;
|
|
private int lastTick = Integer.MIN_VALUE;
|
|
private long firstObservedAtNanos;
|
|
private long evidenceExpiresAtNanos;
|
|
private long cooldownUntilNanos;
|
|
private double startY;
|
|
private int eligibleTicks;
|
|
private int unauthorizedFlightTicks;
|
|
private int ascentTicks;
|
|
private int hoverTicks;
|
|
private int cruiseTicks;
|
|
private int graceThroughTick = Integer.MIN_VALUE;
|
|
private double horizontalDistance;
|
|
private double verticalDisplacement;
|
|
private DetectionSignal signal = DetectionSignal.UNSUPPORTED_CRUISE;
|
|
private boolean authorized;
|
|
private boolean supported;
|
|
private ExclusionReason currentExclusion = ExclusionReason.NONE;
|
|
private FlightEvidence lastEvidence;
|
|
private long evidenceFirstObservedAtNanos;
|
|
|
|
private boolean connected(FlightFrame frame) {
|
|
return world != null && world.equals(frame.world()) && lastTick + 1 == frame.tick();
|
|
}
|
|
|
|
private void startSequence(FlightFrame frame) {
|
|
resetSequence();
|
|
world = frame.world();
|
|
lastTick = frame.tick() - 1;
|
|
firstObservedAtNanos = frame.observedAtNanos();
|
|
startY = frame.fromY();
|
|
}
|
|
|
|
private void resetSequence() {
|
|
world = null;
|
|
lastTick = Integer.MIN_VALUE;
|
|
firstObservedAtNanos = 0;
|
|
startY = 0;
|
|
eligibleTicks = 0;
|
|
unauthorizedFlightTicks = 0;
|
|
ascentTicks = 0;
|
|
hoverTicks = 0;
|
|
cruiseTicks = 0;
|
|
horizontalDistance = 0;
|
|
verticalDisplacement = 0;
|
|
signal = DetectionSignal.UNSUPPORTED_CRUISE;
|
|
}
|
|
|
|
private void expire(long nowNanos) {
|
|
if (lastEvidence != null && evidenceExpiresAtNanos <= nowNanos) lastEvidence = null;
|
|
}
|
|
|
|
private FlightEvidence evidence(long nowNanos) {
|
|
FlightEvidence source = lastEvidence;
|
|
int ticks = eligibleTicks;
|
|
int flyingTicks = unauthorizedFlightTicks;
|
|
int climbingTicks = ascentTicks;
|
|
int levelTicks = hoverTicks;
|
|
int travelTicks = cruiseTicks;
|
|
double horizontal = horizontalDistance;
|
|
double vertical = verticalDisplacement;
|
|
DetectionSignal currentSignal = signal;
|
|
long first = firstObservedAtNanos;
|
|
if (eligibleTicks == 0 && source != null) {
|
|
ticks = source.eligibleTicks();
|
|
flyingTicks = source.unauthorizedFlightTicks();
|
|
climbingTicks = source.ascentTicks();
|
|
levelTicks = source.hoverTicks();
|
|
travelTicks = source.cruiseTicks();
|
|
horizontal = source.horizontalDistance();
|
|
vertical = source.verticalDisplacement();
|
|
currentSignal = source.signal();
|
|
first = evidenceFirstObservedAtNanos;
|
|
}
|
|
return new FlightEvidence(currentSignal, authorized, supported, ticks,
|
|
flyingTicks, climbingTicks, levelTicks, travelTicks, horizontal, vertical,
|
|
Math.max(0, nowNanos - first) / 1_000_000_000.0,
|
|
Math.max(0, cooldownUntilNanos - nowNanos) / 1_000_000_000.0,
|
|
currentExclusion);
|
|
}
|
|
}
|
|
}
|