128 lines
6.6 KiB
Java
128 lines
6.6 KiB
Java
package games.dmg.fantasystick.events;
|
|
|
|
import games.dmg.fantasystick.persistence.RoleplaySettings;
|
|
import games.dmg.fantasystick.session.EquipmentSessions;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Path;
|
|
import java.time.Clock;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
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.BlockCommandSender;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
/** A policy publication requests ordinary exits; it never attests their later receipt completion. */
|
|
public final class RoleplayAvailabilityEventBridge implements AutoCloseable {
|
|
public record Actor(String kind, EventInput.Context context) { }
|
|
|
|
private final Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink;
|
|
private final Clock clock;
|
|
private final EquipmentSessions sessions;
|
|
private final FeatureEventOutbox outbox;
|
|
|
|
public RoleplayAvailabilityEventBridge(Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink,
|
|
Clock clock, Path data, EquipmentSessions sessions,
|
|
Consumer<Throwable> failures) {
|
|
this.sink = Objects.requireNonNull(sink);
|
|
this.clock = Objects.requireNonNull(clock);
|
|
this.sessions = Objects.requireNonNull(sessions);
|
|
outbox = new FeatureEventOutbox(data, "roleplay-availability-event-intents",
|
|
java.util.Set.of("config.changed", "game.availability_changed"), sink, failures);
|
|
}
|
|
|
|
public Actor actor(CommandSender sender) {
|
|
try {
|
|
var player = sender instanceof Player value ? value : null;
|
|
var kind = player != null ? "player" : sender instanceof BlockCommandSender ? "command_block" : "console";
|
|
var active = player == null ? null : sessions.active(player.getUniqueId()).orElse(null);
|
|
var record = active == null ? null : sessions.currentRecord(active).orElse(null);
|
|
return new Actor(kind, player == null ? null : new EventInput.Context(player.getUniqueId(),
|
|
player.getName(), record == null ? null : record.id(),
|
|
record == null ? null : record.name(), record == null ? null : active.session(),
|
|
safeLocation(player)));
|
|
} catch (RuntimeException unavailable) { return null; }
|
|
}
|
|
|
|
public void read(Actor actor, boolean disabled) {
|
|
if (actor == null) return;
|
|
var values = common(actor);
|
|
values.put("effective", disabled);
|
|
values.put("available", !disabled);
|
|
values.put("outcome", "acknowledged_read");
|
|
var operation = UUID.randomUUID();
|
|
try { sink.apply(new EventInput(id(operation, "config.read"), operation, clock.instant(),
|
|
"config.read", actor.context(), values)); }
|
|
catch (RuntimeException ignored) { /* An observation cannot change availability. */ }
|
|
}
|
|
|
|
public void changed(Actor actor, UUID operation, boolean before, RoleplaySettings.Update saved,
|
|
EquipmentSessions.DisableSummary exits) {
|
|
if (actor == null) return;
|
|
try {
|
|
var config = common(actor);
|
|
config.put("operation_id", operation.toString());
|
|
config.put("policy_revision", Long.toString(saved.sequence()));
|
|
config.put("before", before);
|
|
config.put("after", saved.disabled());
|
|
config.put("effective", saved.disabled());
|
|
config.put("outcome", "acknowledged");
|
|
durable(operation, "config.changed", actor.context(), config);
|
|
|
|
var availability = common(actor);
|
|
availability.put("origin_admin_action_id", operation.toString());
|
|
availability.put("policy_revision", Long.toString(saved.sequence()));
|
|
availability.put("available_before", !before);
|
|
availability.put("available_after", !saved.disabled());
|
|
availability.put("exit_requested_count", exits == null ? 0 : exits.exitRequested());
|
|
availability.put("exit_request_accepted_count", exits == null ? 0 : exits.exitRequested());
|
|
availability.put("exit_native_restored_pending_receipt_count",
|
|
exits == null ? 0 : exits.nativeRestoredPendingReceipt());
|
|
availability.put("exit_confirmed_count", 0);
|
|
availability.put("exit_failed_count", exits == null ? 0 : exits.exitFailed());
|
|
availability.put("exit_pending_count", exits == null ? 0 : exits.exitPending());
|
|
availability.put("pending_entry_refused_count", exits == null ? 0 : exits.entryRefused());
|
|
availability.put("receipt_state", "not_yet_observed");
|
|
availability.put("outcome", "policy_published_exit_requests_only");
|
|
durable(operation, "game.availability_changed", actor.context(), availability);
|
|
} catch (RuntimeException ignored) { /* Logging failure cannot reverse policy or reissue exits. */ }
|
|
}
|
|
|
|
private static Map<String, Object> common(Actor actor) {
|
|
var values = new LinkedHashMap<String, Object>();
|
|
values.put("key", RoleplaySettings.KEY);
|
|
values.put("actor_kind", actor.kind());
|
|
values.put("application_scope", "global_and_active_sessions");
|
|
if (actor.context() == null) values.put("location_status", "not_applicable");
|
|
else if (actor.context().location() == null) values.put("location_status", "unavailable");
|
|
return values;
|
|
}
|
|
|
|
private EventInput.Location safeLocation(Player player) {
|
|
try {
|
|
Location location = player.getLocation();
|
|
if (location == null || location.getWorld() == null) return null;
|
|
var world = location.getWorld();
|
|
return new EventInput.Location(world.getUID(), world.getName(), location.getX(),
|
|
location.getY(), location.getZ(), clock.instant(), "exact");
|
|
} catch (RuntimeException unavailable) { return null; }
|
|
}
|
|
|
|
private void durable(UUID operation, String type, EventInput.Context actor, Map<String, Object> values) {
|
|
outbox.accepted(new EventInput(id(operation, type), operation, clock.instant(), type, actor, values));
|
|
}
|
|
private static UUID id(UUID operation, String type) {
|
|
return UUID.nameUUIDFromBytes(("fantasystick:roleplay-availability:" + operation + ":" + type)
|
|
.getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
|
|
public void replay() { outbox.replay(); }
|
|
public CompletionStage<Void> drained() { return outbox.drained(); }
|
|
@Override public void close() { outbox.close(); }
|
|
}
|