171 lines
12 KiB
Java
171 lines
12 KiB
Java
package games.dmg.fantasystick.events;
|
|
|
|
import games.dmg.fantasystick.shop.ArmorCatalog;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.Comparator;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.CompletionStage;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Function;
|
|
|
|
/** Failure-isolated immutable records from acknowledged armor authorities, never gameplay replay. */
|
|
public final class ArmorEventBridge implements AutoCloseable {
|
|
private final FeatureEventOutbox outbox;
|
|
private final java.time.Clock clock = java.time.Clock.systemUTC();
|
|
public record Actor(String kind, UUID playerId, String playerName, EventInput.Context context) {
|
|
public Actor(String kind, EventInput.Context context) {
|
|
this(kind, context == null ? null : context.playerId(), context == null ? null : context.playerName(), context);
|
|
}
|
|
}
|
|
private static void actorData(Map<String, Object> values, Actor actor) {
|
|
values.put("actor_kind", actor == null ? "unavailable" : actor.kind());
|
|
values.put("request_actor_kind", actor == null ? "unavailable" : actor.kind());
|
|
values.put("actor_capture_quality", actor == null || actor.kind().equals("player") && actor.context() == null ? "unavailable" : "native");
|
|
if (actor != null && actor.playerId() != null) values.put("request_player_id", actor.playerId().toString());
|
|
if (actor != null && actor.playerName() != null) values.put("request_player_name", actor.playerName());
|
|
}
|
|
private final Set<UUID> catalogs = ConcurrentHashMap.newKeySet();
|
|
|
|
public ArmorEventBridge(Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink,
|
|
Path data, Consumer<Throwable> failures) {
|
|
outbox = new FeatureEventOutbox(data, "armor-event-intents", Set.of("armor.catalog_refreshed", "armorer.placed", "armorer.removed", "armor.purchased", "config.read", "config.changed"), sink, failures);
|
|
}
|
|
|
|
public void catalogRefreshed(ArmorCatalog catalog) {
|
|
try {
|
|
if (!catalogs.add(catalog.revision())) return;
|
|
var products = new ArrayList<Map<String, String>>();
|
|
catalog.products().entrySet().stream().sorted(Comparator.comparing(entry ->
|
|
entry.getKey().slot().name() + "." + entry.getKey().tier().name())).forEach(entry -> {
|
|
var product = entry.getKey(); var offer = entry.getValue();
|
|
products.add(Map.of("product_id", product.slot().name().toLowerCase(Locale.ROOT) + "."
|
|
+ product.tier().name().toLowerCase(Locale.ROOT), "price", offer.price().toString(),
|
|
"stock", Integer.toString(offer.remaining())));
|
|
});
|
|
var values = new LinkedHashMap<String, Object>();
|
|
values.put("actor_kind", "system"); values.put("catalog_revision", catalog.revision().toString());
|
|
values.put("catalog_day", catalog.day().toString()); values.put("refreshed_at", catalog.refreshedAt().toString());
|
|
values.put("refresh_reason", catalog.reason().name().toLowerCase(Locale.ROOT));
|
|
values.put("next_midnight", catalog.day().plusDays(1).atStartOfDay(java.time.ZoneOffset.UTC).toInstant().toString());
|
|
values.put("products", java.util.List.copyOf(products)); values.put("outcome", "acknowledged");
|
|
var id = UUID.nameUUIDFromBytes(("fantasystick:armor:" + catalog.revision() + ":catalog-refreshed")
|
|
.getBytes(StandardCharsets.UTF_8));
|
|
outbox.accepted(new EventInput(id, catalog.revision(), catalog.refreshedAt(),
|
|
"armor.catalog_refreshed", null, Map.copyOf(values)));
|
|
} catch (RuntimeException ignored) { /* Audit cannot interrupt the already-saved catalog. */ }
|
|
}
|
|
|
|
public Actor actor(org.bukkit.command.CommandSender sender, games.dmg.fantasystick.session.EquipmentSessions sessions,
|
|
org.bukkit.Location input) {
|
|
String kind = sender instanceof org.bukkit.entity.Player ? "player"
|
|
: sender instanceof org.bukkit.command.BlockCommandSender ? "command_block" : "console";
|
|
if (!(sender instanceof org.bukkit.entity.Player player)) return new Actor(kind, null);
|
|
UUID id = null; String name = null; UUID character = null, session = null; String characterName = null;
|
|
EventInput.Location location = null;
|
|
try { id = player.getUniqueId(); } catch (RuntimeException ignored) { }
|
|
try { name = player.getName(); } catch (RuntimeException ignored) { }
|
|
try {
|
|
var active = id == null ? null : sessions.active(id).filter(sessions::current).orElse(null);
|
|
var record = active == null ? null : sessions.currentRecord(active).orElse(null);
|
|
if (record != null) { character = record.id(); characterName = record.name(); session = active.session(); }
|
|
} catch (RuntimeException ignored) { }
|
|
try {
|
|
var nativeLocation = input == null ? player.getLocation() : input;
|
|
var world = nativeLocation.getWorld();
|
|
if (world != null) location = new EventInput.Location(world.getUID(), world.getName(), nativeLocation.getX(),
|
|
nativeLocation.getY(), nativeLocation.getZ(), clock.instant(), "native");
|
|
} catch (RuntimeException ignored) { }
|
|
try { return new Actor(kind, id, name, new EventInput.Context(id, name, character, characterName, session, location)); }
|
|
catch (RuntimeException unavailable) { return new Actor(kind, id, name, null); }
|
|
}
|
|
|
|
public void armorerChanged(String type, UUID operation, games.dmg.fantasystick.shop.FantasyArmorer shop,
|
|
Actor actor, games.dmg.fantasystick.shop.FantasyArmorerController.Observed entities,
|
|
String outcome) {
|
|
try {
|
|
var values = new LinkedHashMap<String, Object>();
|
|
actorData(values, actor);
|
|
values.put("operation_id", operation.toString()); values.put("armorer_id", shop.id().toString());
|
|
values.put("armorer_name", shop.name());
|
|
values.put("revision_capture_quality", shop.revision() == null ? "unavailable" : "persisted");
|
|
if (shop.revision() != null) values.put("armorer_revision", shop.revision().toString());
|
|
values.put("world_id", shop.world().toString()); values.put("x", shop.x()); values.put("y", shop.y());
|
|
values.put("z", shop.z()); values.put("yaw", (double) shop.yaw()); values.put("pitch", (double) shop.pitch());
|
|
values.put("chunk_status", entities.chunkStatus()); values.put("entity_capture_quality", entities.quality());
|
|
values.put("entity_ids", entities.ids().stream().map(UUID::toString).toList());
|
|
if (entities.ids().size() == 1) values.put("entity_id", entities.ids().getFirst().toString());
|
|
values.put("entity_outcome", outcome); values.put("outcome", "acknowledged");
|
|
var id = UUID.nameUUIDFromBytes(("fantasystick:armor:" + operation + ":" + type).getBytes(StandardCharsets.UTF_8));
|
|
outbox.accepted(new EventInput(id, operation, clock.instant(), type,
|
|
actor == null ? null : actor.context(), Map.copyOf(values)));
|
|
} catch (RuntimeException ignored) { /* Audit cannot undo native registration/reconciliation. */ }
|
|
}
|
|
|
|
public void purchased(games.dmg.fantasystick.shop.ArmorMarket.PurchaseProof proof, UUID catalog,
|
|
games.dmg.fantasystick.shop.FantasyArmorer shop, games.dmg.fantasystick.shop.ArmorProduct product,
|
|
Actor actor, games.dmg.fantasystick.persistence.CharacterRecord before,
|
|
games.dmg.fantasystick.persistence.CharacterRecord after) {
|
|
try {
|
|
// Bind the audit to the real consume-once receipt, never a reconstructed operation or duplicate resolution.
|
|
if (!after.rewardReceipts().contains(proof.operation()) || before.rewardReceipts().contains(proof.operation())) return;
|
|
var values = new LinkedHashMap<String, Object>();
|
|
actorData(values, actor);
|
|
values.put("purchase_operation_id", proof.operation().toString()); values.put("receipt_id", proof.operation().toString());
|
|
values.put("catalog_revision", catalog.toString()); values.put("armorer_id", shop.id().toString());
|
|
values.put("revision_capture_quality", shop.revision() == null ? "unavailable" : "persisted");
|
|
if (shop.revision() != null) values.put("armorer_revision", shop.revision().toString());
|
|
values.put("product_id", (product.slot().name() + "." + product.tier().name()).toLowerCase(Locale.ROOT));
|
|
values.put("slot", product.slot().name().toLowerCase(Locale.ROOT));
|
|
values.put("previous_tier", before.gear().tier(product.slot()).name().toLowerCase(Locale.ROOT));
|
|
values.put("new_tier", after.gear().tier(product.slot()).name().toLowerCase(Locale.ROOT));
|
|
values.put("price", proof.price().toString()); values.put("balance_before", before.coins().toString());
|
|
values.put("balance_after", after.coins().toString()); values.put("stock_before", Integer.toString(proof.stockBefore()));
|
|
values.put("stock_after", Integer.toString(proof.stockAfter()));
|
|
values.put("before_revision", before.revision().toString()); values.put("after_revision", after.revision().toString());
|
|
if (actor != null && actor.context() != null && actor.context().sessionId() != null)
|
|
values.put("session_id", actor.context().sessionId().toString());
|
|
values.put("location_capture_quality", actor != null && actor.context() != null && actor.context().location() != null ? "native" : "unavailable");
|
|
values.put("outcome", "acknowledged");
|
|
var id = UUID.nameUUIDFromBytes(("fantasystick:armor:" + proof.operation() + ":purchased").getBytes(StandardCharsets.UTF_8));
|
|
outbox.accepted(new EventInput(id, proof.operation(), clock.instant(), "armor.purchased",
|
|
actor == null ? null : actor.context(), Map.copyOf(values)));
|
|
} catch (RuntimeException ignored) { /* Audit cannot change saved coins, stock or native gear. */ }
|
|
}
|
|
|
|
public void configRead(Actor actor, String key, String value) {
|
|
configuration("config.read", UUID.randomUUID(), actor, key, null, value);
|
|
}
|
|
public void configChanged(Actor actor, String key, String before, String after) {
|
|
configuration("config.changed", UUID.randomUUID(), actor, key, before, after);
|
|
}
|
|
public void configRead(UUID operation, Actor actor, String key, String value) {
|
|
configuration("config.read", operation, actor, key, null, value);
|
|
}
|
|
public void configChanged(UUID operation, Actor actor, String key, String before, String after) {
|
|
configuration("config.changed", operation, actor, key, before, after);
|
|
}
|
|
private void configuration(String type, UUID operation, Actor actor, String key, String before, String after) {
|
|
try {
|
|
var values = new LinkedHashMap<String, Object>();
|
|
actorData(values, actor); values.put("key", key); values.put("after", after);
|
|
if (before != null) values.put("before", before);
|
|
values.put("application", "next_armor_restock"); values.put("application_scope", "next_catalog");
|
|
values.put("outcome", "acknowledged");
|
|
var id = UUID.nameUUIDFromBytes(("fantasystick:armor:" + operation + ":" + type).getBytes(StandardCharsets.UTF_8));
|
|
outbox.accepted(new EventInput(id, operation, clock.instant(), type,
|
|
actor == null ? null : actor.context(), Map.copyOf(values)));
|
|
} catch (RuntimeException ignored) { /* Audit cannot read/write effective gameplay policy or restock. */ }
|
|
}
|
|
|
|
public void replay() { outbox.replay(); }
|
|
public CompletionStage<Void> drained() { return outbox.drained(); }
|
|
@Override public void close() { outbox.close(); }
|
|
}
|