Files
purpur-fantasy-stick/src/main/java/games/dmg/fantasystick/events/ActiveInspectionEventBridge.java
T

74 lines
3.7 KiB
Java

package games.dmg.fantasystick.events;
import games.dmg.fantasystick.persistence.CharacterRecord;
import games.dmg.fantasystick.session.EquipmentSessions;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.util.LinkedHashMap;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/** One bounded read outcome per captured administrator action; never serializes book/record contents. */
public final class ActiveInspectionEventBridge {
public static final class Request {
private final AdminActionEventBridge.Action action;
private final UUID target;
private final int offset;
private final String entryPoint;
private final AtomicBoolean resolved = new AtomicBoolean();
private Request(AdminActionEventBridge.Action action, UUID target, int offset, String entryPoint) {
this.action = action;
this.target = action.authorized() ? target : null;
this.offset = offset;
this.entryPoint = entryPoint;
}
public UUID eventId() {
return UUID.nameUUIDFromBytes(("fantasystick:active-inspection:" + action.id())
.getBytes(StandardCharsets.UTF_8));
}
}
private final Consumer<EventInput> sink;
private final Clock clock;
public ActiveInspectionEventBridge(Consumer<EventInput> sink, Clock clock) {
this.sink = Objects.requireNonNull(sink);
this.clock = Objects.requireNonNull(clock);
}
public Request request(AdminActionEventBridge.Action action, UUID target, int offset, String entryPoint) {
if (action == null) return null;
if (offset < 0 || !(entryPoint.equals("command") || entryPoint.equals("book_navigation")))
throw new IllegalArgumentException("Invalid inspection metadata");
return new Request(action, target, offset, entryPoint);
}
/** Protected resolved character metadata is supplied only after native open and session revalidation. */
public void resolved(Request request, String outcome, String reason, CharacterRecord record,
EquipmentSessions.Active active, int pages, int volumes) {
if (request == null || !request.resolved.compareAndSet(false, true)) return;
var data = new LinkedHashMap<String, Object>();
boolean denied = !request.action.authorized() || reason.equals("permission_changed") || reason.equals("unauthorized");
data.put("route", "character.view");
data.put("actor_kind", request.action.actorKind());
data.put("authorization", denied ? "denied" : "authorized");
data.put("entry_point", request.entryPoint);
data.put("requested_offset", request.offset);
data.put("requested_volume", request.offset / 30 + 1);
data.put("outcome", outcome);
data.put("reason", reason);
if (!denied && request.target != null) data.put("target_player_id", request.target);
if (!denied && outcome.equals("completed") && record != null && active != null) {
data.put("target_character_id", record.id());
data.put("target_session_id", active.session());
data.put("target_revision", record.revision().toString());
data.put("presented_pages", pages);
data.put("presented_volumes", volumes);
}
try {
sink.accept(new EventInput(request.eventId(), request.action.id(), clock.instant(),
"character.active_inspection_resolved", request.action.context(), data));
} catch (RuntimeException ignored) { /* Audit delivery cannot authorize, fail or replay inspection. */ }
}
}