142 lines
9.3 KiB
Java
142 lines
9.3 KiB
Java
package games.dmg.fantasystick.events;
|
|
|
|
import games.dmg.fantasystick.persistence.CharacterRecord;
|
|
import games.dmg.fantasystick.session.EquipmentSessions;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
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.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.entity.LivingEntity;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.entity.EntityDamageEvent;
|
|
|
|
/** Audit-only final-blow snapshots; replaying a heal intent never invokes gameplay. */
|
|
public final class HealthStealEventBridge implements AutoCloseable {
|
|
public record Damage(UUID id, Instant time, UUID target, String targetType,
|
|
UUID directEntity, String cause, String amount, EventInput.Location location) { }
|
|
public record Action(UUID id, Damage damage, EventInput.Context context, EventInput.Location targetLocation,
|
|
UUID targetCharacter, UUID targetSession, BigInteger configured, BigDecimal requested,
|
|
CharacterRecord before, BigDecimal healthBefore, BigDecimal maximum) { }
|
|
|
|
private final Consumer<EventInput> sink;
|
|
private final Clock clock;
|
|
private final FeatureEventOutbox outbox;
|
|
|
|
public HealthStealEventBridge(Consumer<EventInput> sink, Clock clock) {
|
|
this.sink = Objects.requireNonNull(sink); this.clock = Objects.requireNonNull(clock); outbox = null;
|
|
}
|
|
public HealthStealEventBridge(Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink,
|
|
Clock clock, Path data, Consumer<Throwable> failures) {
|
|
this.sink = event -> sink.apply(event); this.clock = Objects.requireNonNull(clock);
|
|
outbox = new FeatureEventOutbox(data, "health-steal-event-intents", java.util.Set.of("combat.healed"), sink, failures);
|
|
}
|
|
|
|
/** Called only for an already accepted lethal candidate, before its native death callback. */
|
|
public Damage damage(EntityDamageEvent event) {
|
|
try {
|
|
var target = event.getEntity(); var direct = event.getDamageSource().getDirectEntity();
|
|
if (direct == null && event instanceof org.bukkit.event.entity.EntityDamageByEntityEvent byEntity)
|
|
direct = byEntity.getDamager();
|
|
return new Damage(UUID.randomUUID(), clock.instant(), target.getUniqueId(),
|
|
target instanceof Player ? "PLAYER" : target.getType().name(), direct == null ? null : direct.getUniqueId(),
|
|
event.getCause().name(), Double.toString(event.getFinalDamage()), location(target.getLocation()));
|
|
} catch (RuntimeException unavailable) { return null; }
|
|
}
|
|
|
|
public Action begin(Damage damage, LivingEntity target, Player player, EquipmentSessions.Active active,
|
|
CharacterRecord before, EquipmentSessions.Active targetActive,
|
|
BigInteger configured, double requested, double current, double maximum) {
|
|
if (damage == null) return null;
|
|
try {
|
|
var context = new EventInput.Context(player.getUniqueId(), player.getName(), active.character(),
|
|
before.name(), active.session(), location(player.getLocation()));
|
|
var action = new Action(id(damage.id(), "defeat"), damage, context, location(target.getLocation()),
|
|
targetActive == null ? null : targetActive.character(), targetActive == null ? null : targetActive.session(),
|
|
configured, BigDecimal.valueOf(requested), before, BigDecimal.valueOf(current), BigDecimal.valueOf(maximum));
|
|
emit("action.observed", action, "observed", null, before, null, action.healthBefore(), null, action.maximum(), null);
|
|
return action;
|
|
} catch (RuntimeException unavailable) { return null; }
|
|
}
|
|
public void refused(Action action, String reason) {
|
|
if (action != null) emit("action.resolved", action, "refused", reason, action.before(), null,
|
|
action.healthBefore(), null, action.maximum(), null);
|
|
}
|
|
public void failed(Action action, String reason) {
|
|
if (action != null) emit("action.resolved", action, "failed", reason, action.before(), null,
|
|
action.healthBefore(), null, action.maximum(), null);
|
|
}
|
|
public void healed(Action action, CharacterRecord before, CharacterRecord after,
|
|
double nativeBefore, double nativeAfter, double maximum, double accepted) {
|
|
if (action == null) return;
|
|
var prior = BigDecimal.valueOf(nativeBefore); var next = BigDecimal.valueOf(nativeAfter);
|
|
var limit = BigDecimal.valueOf(maximum); var amount = BigDecimal.valueOf(accepted);
|
|
emit("action.resolved", action, "accepted", "native_health_acknowledged", before, after, prior, next, limit, amount);
|
|
emit("combat.healed", action, "accepted", "native_health_acknowledged", before, after, prior, next, limit, amount);
|
|
}
|
|
|
|
private void emit(String type, Action action, String outcome, String reason, CharacterRecord before,
|
|
CharacterRecord after, BigDecimal prior, BigDecimal next, BigDecimal maximum, BigDecimal accepted) {
|
|
try {
|
|
var data = new LinkedHashMap<String, Object>(); var damage = action.damage();
|
|
data.put("action_kind", "health_steal_final_blow"); data.put("passive_id", "health_steal");
|
|
data.put("healing_source", "health_steal"); data.put("defeat_id", action.id().toString());
|
|
data.put("damage_causation_id", damage.id().toString()); data.put("damage_accepted_at", damage.time().toString());
|
|
data.put("damage_cause", damage.cause()); data.put("damage_amount", damage.amount());
|
|
data.put("target_id", damage.target().toString()); data.put("target_type", damage.targetType());
|
|
if (damage.directEntity() != null) data.put("direct_entity_id", damage.directEntity().toString());
|
|
if (damage.location() != null) data.put("damage_target_location", values(damage.location()));
|
|
if (action.targetLocation() != null) data.put("target_location", values(action.targetLocation()));
|
|
data.put("target_location_capture_quality", action.targetLocation() == null ? "unavailable" : "exact");
|
|
data.put("location_capture_quality", action.context().location() == null ? "unavailable" : "exact");
|
|
if (action.targetCharacter() != null) {
|
|
data.put("target_character_id", action.targetCharacter().toString());
|
|
data.put("target_session_id", action.targetSession().toString());
|
|
}
|
|
data.put("defeat_consumption", "consumed_once"); data.put("configured_amount", action.configured().toString());
|
|
data.put("requested_amount", action.requested().toPlainString()); data.put("health_before", prior.toPlainString());
|
|
data.put("maximum_health", maximum.toPlainString()); data.put("before_revision", before.revision().toString());
|
|
data.put("points_before", before.unspentPoints().toString()); data.put("outcome", outcome);
|
|
if (reason != null) data.put("reason", reason);
|
|
if (after != null && next != null) {
|
|
data.put("health_after", next.toPlainString()); data.put("final_amount", next.subtract(prior).toPlainString());
|
|
data.put("native_accepted_amount", accepted.toPlainString()); data.put("persisted_health", after.health().toPlainString());
|
|
data.put("after_revision", after.revision().toString()); data.put("points_after", after.unspentPoints().toString());
|
|
data.put("native_publication", "acknowledged");
|
|
}
|
|
var event = new EventInput(id(action.id(), type), action.id(), clock.instant(), type, action.context(), Map.copyOf(data));
|
|
if (type.equals("combat.healed") && outbox != null) outbox.accepted(event); else sink.accept(event);
|
|
} catch (RuntimeException ignored) { /* Audit failure cannot authorize another defeat, save or native heal. */ }
|
|
}
|
|
|
|
private EventInput.Location location(Location value) {
|
|
try {
|
|
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");
|
|
} catch (RuntimeException unavailable) { return null; }
|
|
}
|
|
private static Map<String, Object> values(EventInput.Location location) {
|
|
return Map.of("world_id", location.worldId().toString(), "world_name", location.worldName(),
|
|
"x", location.x(), "y", location.y(), "z", location.z(), "captured_at", location.capturedAt().toString(),
|
|
"quality", location.quality());
|
|
}
|
|
private static UUID id(UUID operation, String type) {
|
|
return UUID.nameUUIDFromBytes(("fantasystick:health-steal:" + operation + ":" + type).getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
public void replay() { if (outbox != null) outbox.replay(); }
|
|
public CompletionStage<Void> drained() {
|
|
return outbox == null ? java.util.concurrent.CompletableFuture.completedFuture(null) : outbox.drained();
|
|
}
|
|
@Override public void close() { if (outbox != null) outbox.close(); }
|
|
}
|