201 lines
10 KiB
Java
201 lines
10 KiB
Java
package games.dmg.fantasystick.events;
|
|
|
|
import games.dmg.fantasystick.persistence.AdventureSettings;
|
|
import games.dmg.fantasystick.persistence.EquipmentSessionStore;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Path;
|
|
import java.time.Clock;
|
|
import java.time.Instant;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.CompletionStage;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Function;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
/** Captured administrative/session recovery observations; never authorizes native recovery. */
|
|
public final class SessionRecoveryEventBridge implements AutoCloseable {
|
|
public record Action(UUID id, Instant observedAt, EventInput.Context context, String actorKind,
|
|
String locationStatus, UUID target) { }
|
|
|
|
private final Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink;
|
|
private final FeatureEventOutbox outbox;
|
|
private final Consumer<Throwable> failures;
|
|
private final Clock clock;
|
|
|
|
public SessionRecoveryEventBridge(Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink,
|
|
Clock clock, Path data, Consumer<Throwable> failures) {
|
|
this.sink = Objects.requireNonNull(sink);
|
|
this.clock = Objects.requireNonNull(clock);
|
|
this.failures = Objects.requireNonNull(failures);
|
|
outbox = new FeatureEventOutbox(data, "session-recovery-event-intents",
|
|
Set.of("recovery.resolved", "config.changed"), sink, failures);
|
|
}
|
|
|
|
public void replay() { outbox.replay(); }
|
|
public CompletionStage<Void> drained() { return outbox.drained(); }
|
|
@Override public void close() { outbox.close(); }
|
|
|
|
public Action capture(CommandSender sender, UUID target) {
|
|
try {
|
|
var now = clock.instant();
|
|
if (sender instanceof Player player) {
|
|
var location = player.getLocation();
|
|
EventInput.Location origin = location == null || location.getWorld() == null ? null
|
|
: new EventInput.Location(location.getWorld().getUID(), location.getWorld().getName(),
|
|
location.getX(), location.getY(), location.getZ(), now, "exact");
|
|
return new Action(UUID.randomUUID(), now,
|
|
new EventInput.Context(player.getUniqueId(), player.getName(), null, null, null, origin),
|
|
"player", origin == null ? "unavailable" : null, target);
|
|
}
|
|
return new Action(UUID.randomUUID(), now, null,
|
|
sender instanceof org.bukkit.command.BlockCommandSender ? "command_block" : "console",
|
|
"not_applicable", target);
|
|
} catch (RuntimeException ignored) { return null; }
|
|
}
|
|
|
|
public void observed(Action action, String kind) {
|
|
if (action == null) return;
|
|
var data = fields(action);
|
|
data.put("action_kind", kind);
|
|
data.put("outcome", "observed");
|
|
emit(action, "action.observed", "observed", data);
|
|
}
|
|
|
|
public void resolved(Action action, String kind, String outcome, String reason) {
|
|
if (action == null) return;
|
|
var data = fields(action);
|
|
if (outcome.equals("refused")) data.remove("target_player_id");
|
|
data.put("action_kind", kind);
|
|
data.put("outcome", outcome);
|
|
data.put("reason", reason);
|
|
emit(action, "action.resolved", "resolved:" + outcome + ':' + reason, data);
|
|
}
|
|
|
|
public void inspected(Action action, Optional<EquipmentSessionStore.State> state,
|
|
String nativePhase, boolean online) {
|
|
if (action == null) return;
|
|
var data = fields(action);
|
|
data.put("action_kind", "equipment_inspect");
|
|
data.put("outcome", "authorized");
|
|
var saved = state.orElse(null);
|
|
var receipt = saved == null ? null : saved.pending();
|
|
data.put("receipt_phase", receipt != null ? "pending" : saved == null ? "absent" : "completed");
|
|
if (saved != null) data.put("receipt_revision", saved.revision().toString());
|
|
if (receipt != null) {
|
|
data.put("session_id", receipt.sessionId().toString());
|
|
data.put("character_id", receipt.characterId().toString());
|
|
}
|
|
data.put("native_phase", online ? nativePhase : "offline_unavailable");
|
|
data.put("custody_quality", online ? "receipt_and_native_observation" : "receipt_only");
|
|
emit(action, "recovery.inspected", "inspected", data);
|
|
}
|
|
|
|
/** One result per proof-based reconciliation attempt, distinct from native equipment custody. */
|
|
public void reconciled(Action action, UUID session, UUID character, String priorPhase,
|
|
String currentPhase, String outcome, String quality, String reason,
|
|
java.math.BigInteger priorRevision, java.math.BigInteger afterRevision,
|
|
UUID equipmentOperation, EventInput.Location original, EventInput.Location current) {
|
|
if (action == null) return;
|
|
var data = fields(action);
|
|
data.put("recovery_operation_id", action.id().toString());
|
|
if (session != null) data.put("session_id", session.toString());
|
|
if (character != null) data.put("character_id", character.toString());
|
|
data.put("prior_phase", priorPhase);
|
|
data.put("current_phase", currentPhase);
|
|
data.put("custody_quality", quality);
|
|
data.put("outcome", outcome);
|
|
data.put("reason", reason);
|
|
if (priorRevision != null) data.put("prior_receipt_revision", priorRevision.toString());
|
|
if (afterRevision != null) data.put("receipt_revision", afterRevision.toString());
|
|
if (equipmentOperation != null) data.put("equipment_recovery_operation_id", equipmentOperation.toString());
|
|
nativeLocation(data, "original", original);
|
|
nativeLocation(data, "current", current);
|
|
data.put("changed_components", outcome.equals("restored") && quality.equals("verified_box_and_marker")
|
|
? "verified_native_equipment" : quality.equals("native_restored_receipt_pending")
|
|
? "native_equipment_unacknowledged" : "not_independently_verified");
|
|
emit(action, "recovery.resolved", "reconciled", data);
|
|
if (!action.actorKind().equals("system")) resolved(action, "equipment_recovery", outcome, reason);
|
|
}
|
|
|
|
/** Must be called on the native thread before any asynchronous receipt acknowledgment. */
|
|
public EventInput.Location captureLocation(Location value) {
|
|
if (value == null || value.getWorld() == null) return null;
|
|
return new EventInput.Location(value.getWorld().getUID(), value.getWorld().getName(),
|
|
value.getX(), value.getY(), value.getZ(), clock.instant(), "exact");
|
|
}
|
|
|
|
private void nativeLocation(LinkedHashMap<String, Object> data, String name, EventInput.Location location) {
|
|
if (location == null) {
|
|
data.put(name + "_location_status", "unavailable");
|
|
return;
|
|
}
|
|
data.put(name + "_world_id", location.worldId().toString());
|
|
data.put(name + "_world_name", location.worldName());
|
|
data.put(name + "_x", location.x());
|
|
data.put(name + "_y", location.y());
|
|
data.put(name + "_z", location.z());
|
|
data.put(name + "_captured_at", location.capturedAt().toString());
|
|
data.put(name + "_location_status", name.equals("original") ? "captured_at_entry" : "captured_at_reconciliation");
|
|
}
|
|
|
|
public Action system(Player player) {
|
|
try {
|
|
var now = clock.instant();
|
|
var location = player.getLocation();
|
|
var action = new Action(UUID.randomUUID(), now, null, "system",
|
|
location == null || location.getWorld() == null ? "unavailable" : "captured_current_only",
|
|
player.getUniqueId());
|
|
return action;
|
|
} catch (RuntimeException ignored) { return null; }
|
|
}
|
|
|
|
public void configRead(Action action, boolean effective, Optional<String> revision) {
|
|
if (action == null) return;
|
|
var data = fields(action);
|
|
data.put("key", "adventure-mode");
|
|
data.put("effective_value", Boolean.toString(effective));
|
|
if (revision.isPresent()) data.put("persistence_revision", revision.orElseThrow());
|
|
else data.put("persistence_revision_status", "not_verified_at_read");
|
|
data.put("outcome", "authorized");
|
|
emit(action, "config.read", "read", data);
|
|
}
|
|
|
|
public void configChanged(Action action, AdventureSettings.Saved saved) {
|
|
if (action == null || saved == null) return;
|
|
var data = fields(action);
|
|
data.put("key", "adventure-mode");
|
|
data.put("before_value", Boolean.toString(saved.beforeValue()));
|
|
data.put("after_value", Boolean.toString(saved.value()));
|
|
data.put("effective_value", Boolean.toString(saved.value()));
|
|
data.put("persistence_revision", saved.revision());
|
|
data.put("application", "future_sessions");
|
|
data.put("outcome", "acknowledged");
|
|
emit(action, "config.changed", "changed", data);
|
|
}
|
|
|
|
private LinkedHashMap<String, Object> fields(Action action) {
|
|
var data = new LinkedHashMap<String, Object>();
|
|
data.put("actor_kind", action.actorKind());
|
|
if (action.locationStatus() != null) data.put("location_status", action.locationStatus());
|
|
if (action.target() != null) data.put("target_player_id", action.target().toString());
|
|
return data;
|
|
}
|
|
|
|
private void emit(Action action, String type, String stage, LinkedHashMap<String, Object> data) {
|
|
try {
|
|
var event = new EventInput(UUID.nameUUIDFromBytes(("fantasystick:session-recovery:" + action.id()
|
|
+ ':' + stage).getBytes(StandardCharsets.UTF_8)), action.id(),
|
|
stage.equals("observed") ? action.observedAt() : clock.instant(), type, action.context(), data);
|
|
if (type.equals("config.changed") || type.equals("recovery.resolved")) outbox.accepted(event);
|
|
else sink.apply(event);
|
|
}
|
|
catch (RuntimeException failure) { try { failures.accept(failure); } catch (RuntimeException ignored) { } }
|
|
}
|
|
}
|