131 lines
6.8 KiB
Java
131 lines
6.8 KiB
Java
package games.dmg.fantasystick.events;
|
|
|
|
import games.dmg.fantasystick.persistence.CharacterRecord;
|
|
import games.dmg.fantasystick.session.ControlInput;
|
|
import java.math.BigInteger;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.Clock;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
import java.util.function.Consumer;
|
|
import org.bukkit.Location;
|
|
|
|
/** Stable feature-owned character-book outcomes; event delivery never authorizes or rolls back an open. */
|
|
public final class BookEventBridge {
|
|
public record Navigation(int sourceVolumeStart, int sourceNavigationPage, int destinationVolumeStart) {
|
|
public Navigation {
|
|
if (sourceVolumeStart < 0 || sourceNavigationPage < 1 || sourceNavigationPage > 100
|
|
|| destinationVolumeStart < 0 || sourceVolumeStart == destinationVolumeStart)
|
|
throw new IllegalArgumentException("Invalid book volume navigation");
|
|
}
|
|
}
|
|
|
|
public record Request(UUID actionId, EventInput.Context context, String actionKind, String viewKind,
|
|
int offset, BigInteger requestedRevision, Navigation navigation) {
|
|
public Request {
|
|
Objects.requireNonNull(actionId); Objects.requireNonNull(context); Objects.requireNonNull(actionKind);
|
|
Objects.requireNonNull(viewKind); Objects.requireNonNull(requestedRevision);
|
|
if (offset < -1) throw new IllegalArgumentException("Invalid book offset");
|
|
}
|
|
}
|
|
|
|
private final Consumer<EventInput> sink;
|
|
private final Clock clock;
|
|
|
|
public BookEventBridge(Consumer<EventInput> sink, Clock clock) {
|
|
this.sink = Objects.requireNonNull(sink);
|
|
this.clock = Objects.requireNonNull(clock);
|
|
}
|
|
|
|
public Request request(ControlInput input, CharacterRecord current, int offset) {
|
|
Objects.requireNonNull(input); Objects.requireNonNull(current);
|
|
return request(input, current, offset, null);
|
|
}
|
|
|
|
public Request request(ControlInput input, CharacterRecord current, int offset, Navigation navigation) {
|
|
Objects.requireNonNull(input); Objects.requireNonNull(current);
|
|
var request = new Request(UUID.randomUUID(), context(input, current.name()),
|
|
navigation == null ? "character_book_open" : "character_book_navigate", "active", offset,
|
|
current.revision(), navigation);
|
|
emit("action.observed", request, "observed", null, null, null, null);
|
|
return request;
|
|
}
|
|
|
|
public void refused(Request request, String reason) {
|
|
emit("action.resolved", request, "refused", reason, null, null, null);
|
|
}
|
|
|
|
public void refused(org.bukkit.entity.Player player, games.dmg.fantasystick.session.EquipmentSessions.Active active,
|
|
CharacterRecord current, String actionKind, String reason) {
|
|
Objects.requireNonNull(player); Objects.requireNonNull(active); Objects.requireNonNull(current);
|
|
var request=new Request(UUID.randomUUID(),context(player,active,current.name()),actionKind,"active",-1,current.revision(),null);
|
|
emit("action.observed",request,"observed",null,null,null,null);
|
|
refused(request,reason);
|
|
}
|
|
|
|
public void failed(Request request, String reason) {
|
|
emit("action.resolved", request, "failed", reason, null, null, null);
|
|
}
|
|
|
|
public void opened(Request request, CharacterRecord loaded, int presentedPages, int presentedVolumes) {
|
|
Objects.requireNonNull(loaded);
|
|
emit("action.resolved", request, "accepted", "acknowledged", loaded.revision(), presentedPages,
|
|
presentedVolumes);
|
|
emit(request.navigation() == null ? "character_book.opened" : "character_book.navigated", request,
|
|
"accepted", "acknowledged", loaded.revision(), presentedPages, presentedVolumes);
|
|
}
|
|
|
|
private void emit(String type, Request request, String outcome, String reason, BigInteger loadedRevision,
|
|
Integer pages, Integer volumes) {
|
|
var data = new LinkedHashMap<String, Object>();
|
|
data.put("action_kind", request.actionKind()); data.put("view_kind", request.viewKind());
|
|
data.put("section", request.navigation() == null ? "overview" : "volume_navigation");
|
|
data.put("control_kind", "book"); data.put("control_slot", "0");
|
|
if (request.navigation() != null) {
|
|
var navigation = request.navigation();
|
|
data.put("source_volume_start", Integer.toString(navigation.sourceVolumeStart()));
|
|
data.put("source_navigation_page", navigation.sourceNavigationPage());
|
|
data.put("destination_volume_start", Integer.toString(navigation.destinationVolumeStart()));
|
|
data.put("destination_page", "1");
|
|
data.put("destination_section", navigation.destinationVolumeStart() == 0 ? "overview" : "volume_start");
|
|
data.put("volume_direction", navigation.destinationVolumeStart() > navigation.sourceVolumeStart()
|
|
? "next" : "previous");
|
|
}
|
|
data.put("offset", request.offset()<0?"unknown":Integer.toString(request.offset()));
|
|
data.put("requested_revision", request.requestedRevision().toString()); data.put("outcome", outcome);
|
|
if (reason != null) data.put(type.startsWith("character_book.") ? "native_open" : "reason", reason);
|
|
if (loadedRevision != null) data.put("loaded_revision", loadedRevision.toString());
|
|
if (pages != null) data.put("presented_pages", pages);
|
|
if (volumes != null) data.put("presented_volumes", volumes);
|
|
try {
|
|
sink.accept(new EventInput(id(request.actionId(), type), request.actionId(), clock.instant(), type,
|
|
request.context(), Map.copyOf(data)));
|
|
} catch (RuntimeException ignored) {
|
|
// Observability cannot change book authorization, rendering or native opening.
|
|
}
|
|
}
|
|
|
|
private EventInput.Context context(ControlInput input, String characterName) {
|
|
return context(input.player(),input.session(),characterName);
|
|
}
|
|
|
|
private EventInput.Context context(org.bukkit.entity.Player player,
|
|
games.dmg.fantasystick.session.EquipmentSessions.Active active,
|
|
String characterName) {
|
|
return new EventInput.Context(player.getUniqueId(), player.getName(), active.character(),
|
|
characterName, active.session(), location(player.getLocation()));
|
|
}
|
|
|
|
private EventInput.Location location(Location location) {
|
|
return new EventInput.Location(location.getWorld().getUID(), location.getWorld().getName(), location.getX(),
|
|
location.getY(), location.getZ(), clock.instant(), "exact");
|
|
}
|
|
|
|
private static UUID id(UUID action, String type) {
|
|
return UUID.nameUUIDFromBytes(("fantasystick:book:" + action + ":" + type)
|
|
.getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
}
|