feat(adventure): export acknowledged day-reset owner events
Release / release (push) Failing after 12m53s
Release / release (push) Failing after 12m53s
This commit is contained in:
1 parent
e83c6fbabc
commit
c6252e8805
10 files changed
+473
-3
No files matched your search
@@ -191,6 +191,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
private java.util.concurrent.CompletionStage<Void> stickKnockbackAuditDrain = java.util.concurrent.CompletableFuture.completedFuture(null);
|
||||
private games.dmg.fantasystick.events.CampEventBridge campAuditEvents;
|
||||
private java.util.concurrent.CompletionStage<Void> campAuditDrain = java.util.concurrent.CompletableFuture.completedFuture(null);
|
||||
private games.dmg.fantasystick.events.AdventureDayResetEventBridge adventureDayResetEvents;
|
||||
private java.util.concurrent.CompletionStage<Void> adventureDayResetDrain = java.util.concurrent.CompletableFuture.completedFuture(null);
|
||||
private games.dmg.fantasystick.events.ChampionEventBridge championAuditEvents;
|
||||
private java.util.concurrent.CompletionStage<Void> championAuditDrain = java.util.concurrent.CompletableFuture.completedFuture(null);
|
||||
private games.dmg.fantasystick.events.HardMobXpEventBridge hardMobXpAuditEvents;
|
||||
@@ -933,6 +935,12 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
adventureQuestController.attachChampionEvents(championAuditEvents);
|
||||
adventureQuests.attachQuestExpiryEvents(campAuditEvents::questExpired);
|
||||
adventureQuests.attachChampionQuestExpiryEvents(championAuditEvents::questExpired);
|
||||
adventureDayResetEvents = new games.dmg.fantasystick.events.AdventureDayResetEventBridge(eventLog::emit, getDataFolder().toPath(),
|
||||
failure -> getLogger().log(Level.WARNING, "Adventure-day reset event export failed", failure), adventureDayResetDrain);
|
||||
adventureDayResetEvents.recover(adventureQuests.snapshot());
|
||||
adventureQuests.attachDayResetEvents(adventureDayResetEvents::prepared);
|
||||
dungeons.attachResetCleanupEvents(adventureDayResetEvents::participantCleanup);
|
||||
adventureQuestController.attachDayResetEvents(adventureDayResetEvents);
|
||||
trainingAuditEvents = new games.dmg.fantasystick.events.TrainingEventBridge(eventLog::emit, getDataFolder().toPath(),
|
||||
failure -> getLogger().log(Level.WARNING, "Training event intent failed", failure));
|
||||
admin.attachTrainingEvents(trainingAuditEvents);
|
||||
@@ -1138,6 +1146,11 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
if (failure != null) getLogger().log(Level.WARNING, "Previous champion event drain failed", failure);
|
||||
if (replayChampionEvents != null) replayChampionEvents.replay();
|
||||
});
|
||||
var replayDayResetEvents = adventureDayResetEvents;
|
||||
adventureDayResetDrain.whenComplete((ignored, failure) -> {
|
||||
if (failure != null) getLogger().log(Level.WARNING, "Previous adventure-day reset event drain failed", failure);
|
||||
if (replayDayResetEvents != null) replayDayResetEvents.replay();
|
||||
});
|
||||
var replayTrainingEvents = trainingAuditEvents;
|
||||
trainingAuditDrain.whenComplete((ignored, failure) -> {
|
||||
if (failure != null) getLogger().log(Level.WARNING, "Previous training event drain failed", failure);
|
||||
@@ -1255,6 +1268,7 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
var closingHardMobXpEvents = hardMobXpAuditEvents; hardMobXpAuditEvents = null;
|
||||
var closingCampEvents = campAuditEvents; campAuditEvents = null;
|
||||
var closingChampionEvents = championAuditEvents; championAuditEvents = null;
|
||||
var closingDayResetEvents = adventureDayResetEvents; adventureDayResetEvents = null;
|
||||
var closingTrainingEvents = trainingAuditEvents; trainingAuditEvents = null;
|
||||
var closingArmorEvents = armorAuditEvents; armorAuditEvents = null;
|
||||
var closingCoinEvents = coinEvents;
|
||||
@@ -1652,6 +1666,14 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
closingChampionEvents.close(); return null;
|
||||
}).thenCompose(ignored -> closingChampionEvents.drained());
|
||||
});
|
||||
if (closingDayResetEvents != null) cleanup("adventure-day reset event intent drain", () -> {
|
||||
var authorityDrained = closingAdventureQuests == null
|
||||
? java.util.concurrent.CompletableFuture.<Void>completedFuture(null) : closingAdventureQuests.drained();
|
||||
adventureDayResetDrain = authorityDrained.handle((ignored, failure) -> {
|
||||
if (failure != null) getLogger().log(Level.WARNING, "Adventure-day reset authority drain failed", failure);
|
||||
closingDayResetEvents.close(); return null;
|
||||
}).thenCompose(ignored -> closingDayResetEvents.drained());
|
||||
});
|
||||
if (closingTrainingEvents != null) cleanup("training event intent drain", () -> {
|
||||
var registrationsDrained = closingTrainingDummies == null
|
||||
? java.util.concurrent.CompletableFuture.<Void>completedFuture(null) : closingTrainingDummies.drained();
|
||||
|
||||
@@ -105,9 +105,11 @@ public final class AdventureQuestController implements Listener,AutoCloseable {
|
||||
public void attachCampEvents(games.dmg.fantasystick.events.CampEventBridge value){campEvents=Objects.requireNonNull(value);}
|
||||
private games.dmg.fantasystick.events.ChampionEventBridge championEvents;
|
||||
public void attachChampionEvents(games.dmg.fantasystick.events.ChampionEventBridge value){championEvents=Objects.requireNonNull(value);}
|
||||
private games.dmg.fantasystick.events.AdventureDayResetEventBridge dayResetEvents;
|
||||
public void attachDayResetEvents(games.dmg.fantasystick.events.AdventureDayResetEventBridge value){dayResetEvents=Objects.requireNonNull(value);}
|
||||
public void registerResetParticipant(AdventureResetParticipant participant){if(closed)throw new IllegalStateException("Adventure controller closed");resetParticipants.add(Objects.requireNonNull(participant));}
|
||||
public void registerDungeonRoomPresence(java.util.function.BiPredicate<UUID,Location> presence){if(closed)throw new IllegalStateException("Adventure controller closed");dungeonRoomPresence=Objects.requireNonNull(presence);}
|
||||
public CompletionStage<Boolean> publishPendingReset(){var reset=directory.snapshot().reset();if(reset==null)return CompletableFuture.completedFuture(true);if(resetWork!=null)return resetWork;for(var attempt:new ArrayList<>(attempts.values()))cleanup(attempt,"catalog_reset");attempts.clear();starting.clear();readyAt.clear();for(var world:plugin.getServer().getWorlds())for(var entity:safe(world.getEntities()))if(managed(entity))entity.remove();refreshTrackingPlayers();var work=resetParticipants.stream().map(participant->{try{return participant.reset(reset).toCompletableFuture();}catch(RuntimeException failure){return CompletableFuture.<Void>failedFuture(failure);}}).toArray(CompletableFuture[]::new);resetWork=CompletableFuture.allOf(work).thenCompose(ignored->directory.completeReset(reset.operation())).whenComplete((ignored,failure)->resetWork=null);return resetWork;}
|
||||
public CompletionStage<Boolean> publishPendingReset(){var reset=directory.snapshot().reset();if(reset==null)return CompletableFuture.completedFuture(true);if(resetWork!=null)return resetWork;var audit=dayResetEvents;if(audit!=null)audit.cleanupObserved(reset,attempts.size(),resetParticipants.size());for(var attempt:new ArrayList<>(attempts.values()))cleanup(attempt,"catalog_reset");attempts.clear();starting.clear();readyAt.clear();for(var world:plugin.getServer().getWorlds())for(var entity:safe(world.getEntities()))if(managed(entity))entity.remove();refreshTrackingPlayers();var work=resetParticipants.stream().map(participant->{try{return participant.reset(reset).toCompletableFuture();}catch(RuntimeException failure){return CompletableFuture.<Void>failedFuture(failure);}}).toArray(CompletableFuture[]::new);resetWork=CompletableFuture.allOf(work).thenCompose(ignored->{if(audit!=null)audit.cleanupAcknowledged(reset,resetParticipants.size());return directory.completeReset(reset.operation()).whenComplete((published,failure)->{if(failure==null&&Boolean.TRUE.equals(published)&&audit!=null)audit.completed(reset);});}).whenComplete((ignored,failure)->resetWork=null);return resetWork;}
|
||||
private void spawnGiver(AdventureQuestState.Giver value){var world=plugin.getServer().getWorld(value.world());if(world==null||!world.isChunkLoaded(section(value.x()),section(value.z())))return;world.spawn(location(value),Villager.class,villager->configure(villager,value));}
|
||||
private void configure(Villager villager,AdventureQuestState.Giver value){var data=villager.getPersistentDataContainer();data.set(typeKey,PersistentDataType.STRING,GIVER);data.set(idKey,PersistentDataType.STRING,value.id().toString());villager.setAI(false);villager.setGravity(false);villager.setSilent(true);villager.setInvulnerable(true);villager.setCollidable(false);villager.setCanPickupItems(false);villager.setLeashHolder(null);villager.setRemoveWhenFarAway(false);villager.setAdult();villager.setAgeLock(true);villager.customName(net.kyori.adventure.text.Component.text(value.displayName()));villager.setCustomNameVisible(true);}
|
||||
public void open(Player player){open(player,0);}
|
||||
|
||||
@@ -25,6 +25,11 @@ public final class AdventureQuestDirectory implements AutoCloseable {
|
||||
private volatile java.util.function.Consumer<CatalogPublication> catalogObserver=ignored->{};
|
||||
private CatalogPublication pendingCatalogPublication;
|
||||
public void attachCatalogEvents(java.util.function.Consumer<CatalogPublication> observer){catalogObserver=Objects.requireNonNull(observer);}
|
||||
public record ResetPublication(AdventureQuestState.Reset reset,AdventureQuestState.Catalog previous,
|
||||
AdventureQuestState.Catalog catalog,String reason,int expiredQuests,
|
||||
games.dmg.fantasystick.events.CampEventBridge.Actor actor){}
|
||||
private volatile java.util.function.Consumer<ResetPublication> resetObserver=ignored->{};
|
||||
public void attachDayResetEvents(java.util.function.Consumer<ResetPublication> observer){resetObserver=Objects.requireNonNull(observer);}
|
||||
public record QuestExpiry(UUID operation,String reason,AdventureQuestState.Quest quest,AdventureQuestState.Site site,
|
||||
games.dmg.fantasystick.events.CampEventBridge.Actor actor){}
|
||||
private volatile java.util.function.Consumer<QuestExpiry> expiryObserver=ignored->{};
|
||||
@@ -127,6 +132,14 @@ public final class AdventureQuestDirectory implements AutoCloseable {
|
||||
}
|
||||
}catch(RuntimeException ignored){/* Metadata observation cannot change durable catalog authority. */}
|
||||
}
|
||||
private void observeResetCommit(AdventureQuestState before,Result<?> result){
|
||||
try{
|
||||
var after=result.state();var publication=result.publication();
|
||||
if(before.reset()==null&&after.reset()!=null&&publication!=null)
|
||||
resetObserver.accept(new ResetPublication(after.reset(),before.catalog(),after.catalog(),
|
||||
publication.reason().equals("admin_full_reroll")?"manual":"utc_rollover",before.quests().size(),publication.actor()));
|
||||
}catch(RuntimeException ignored){/* Observing reset installation cannot change its durable authority. */}
|
||||
}
|
||||
private void observeQuestCommit(AdventureQuestState before,Result<?> result,String reason,UUID operation){
|
||||
try{
|
||||
if(reason.equals("player_abandoned"))return;
|
||||
@@ -148,7 +161,7 @@ public final class AdventureQuestDirectory implements AutoCloseable {
|
||||
}catch(RuntimeException ignored){/* Removed authority is not reversible by audit metadata. */}
|
||||
}
|
||||
private <T>CompletableFuture<T> mutate(java.util.function.Function<AdventureQuestState,Result<T>> operation){return mutate("authoritative_state_transition",operation);}
|
||||
private <T>CompletableFuture<T> mutate(String reason,java.util.function.Function<AdventureQuestState,Result<T>> operation){var future=new CompletableFuture<T>();UUID observation=UUID.randomUUID();synchronized(this){if(closed){future.completeExceptionally(new IOException("Adventure directory closed"));return future;}pending++;}worker.execute(()->{try{var before=snapshot;var result=operation.apply(before);if(result.state()!=before){repository.save(result.state());snapshot=result.state();observeQuestCommit(before,result,reason,observation);observeCatalogCommit(before,result);}future.complete(result.value());}catch(Throwable failure){future.completeExceptionally(failure);}finally{finished();}});return future;}
|
||||
private <T>CompletableFuture<T> mutate(String reason,java.util.function.Function<AdventureQuestState,Result<T>> operation){var future=new CompletableFuture<T>();UUID observation=UUID.randomUUID();synchronized(this){if(closed){future.completeExceptionally(new IOException("Adventure directory closed"));return future;}pending++;}worker.execute(()->{try{var before=snapshot;var result=operation.apply(before);if(result.state()!=before){repository.save(result.state());snapshot=result.state();observeResetCommit(before,result);observeQuestCommit(before,result,reason,observation);observeCatalogCommit(before,result);}future.complete(result.value());}catch(Throwable failure){future.completeExceptionally(failure);}finally{finished();}});return future;}
|
||||
private synchronized void finished(){pending--;if(closed&&pending==0)drained.complete(null);}
|
||||
public CompletionStage<Void> drained(){return drained.minimalCompletionStage();}
|
||||
@Override public synchronized void close(){closed=true;worker.shutdown();if(pending==0)drained.complete(null);}
|
||||
|
||||
@@ -59,7 +59,10 @@ public final class DungeonDirectory implements AutoCloseable,AdventureResetParti
|
||||
public CompletableFuture<Boolean> acknowledgeCompletion(UUID dungeon,UUID generation,UUID boss){return mutate(state->{var found=state.runs().stream().filter(v->v.dungeon().equals(dungeon)&&v.generation().equals(generation)).findFirst();if(found.isEmpty())return result(state,false);var run=found.orElseThrow();if(!run.defeated()||!boss.toString().equals(run.completionAudit().get("boss_id")))return result(state,false);if(run.completionPublished())return result(state,true);var published=new DungeonState.Run(run.dungeon(),run.generation(),run.defeatedSlots(),run.clearedRooms(),run.unitClaims(),run.characterClaims(),true,run.completionAudit(),true);var runs=state.runs().stream().map(value->value==run?published:value).toList();return result(new DungeonState(state.dungeons(),runs),true);});}
|
||||
/** Reconcile prior exported proof before replaying a missing completion, on this I/O worker. */
|
||||
public CompletableFuture<List<DungeonState.Run>> pendingCompletions(java.util.function.Predicate<DungeonState.Run> exported){return mutate(state->{var runs=new ArrayList<DungeonState.Run>();var pending=new ArrayList<DungeonState.Run>();boolean changed=false;for(var run:state.runs()){if(!run.defeated()||run.completionPublished()||run.completionAudit().isEmpty()){runs.add(run);continue;}if(exported.test(run)){runs.add(new DungeonState.Run(run.dungeon(),run.generation(),run.defeatedSlots(),run.clearedRooms(),run.unitClaims(),run.characterClaims(),true,run.completionAudit(),true));changed=true;}else{runs.add(run);pending.add(run);}}return result(changed?new DungeonState(state.dungeons(),runs):state,List.copyOf(pending));});}
|
||||
@Override public CompletionStage<Void> reset(AdventureQuestState.Reset reset){currentGeneration=reset.generation();return mutate(state->result(new DungeonState(state.dungeons(),List.of()),null));}
|
||||
public record ResetCleanup(AdventureQuestState.Reset reset,int runs){}
|
||||
private volatile java.util.function.Consumer<ResetCleanup> resetCleanupObserver=ignored->{};
|
||||
public void attachResetCleanupEvents(java.util.function.Consumer<ResetCleanup> observer){resetCleanupObserver=Objects.requireNonNull(observer);}
|
||||
@Override public CompletionStage<Void> reset(AdventureQuestState.Reset reset){currentGeneration=reset.generation();return mutate(state->result(new DungeonState(state.dungeons(),List.of()),state.runs().size())).thenAccept(count->{try{resetCleanupObserver.accept(new ResetCleanup(reset,count));}catch(RuntimeException ignored){/* Successful source cleanup does not depend on audit. */}});}
|
||||
private <T>CompletableFuture<T> updateRoom(UUID id,UUID roomId,java.util.function.UnaryOperator<DungeonState.Room> operation,T result){return mutate(state->{var dungeon=editable(state,id);var found=dungeon.rooms().stream().filter(v->v.id().equals(roomId)).findFirst().orElseThrow(()->new IllegalArgumentException("Room not found"));var updated=operation.apply(found);var rooms=dungeon.rooms().stream().map(v->v.id().equals(roomId)?updated:v).toList();return result(replace(state,definition(dungeon,rooms,dungeon.entrance(),false)),result);});}
|
||||
private CompletableFuture<DungeonState.Run> updateRun(UUID dungeon,UUID generation,java.util.function.UnaryOperator<DungeonState.Run> operation){return mutate(state->{requireGeneration(generation);var existing=state.runs().stream().filter(v->v.dungeon().equals(dungeon)&&v.generation().equals(generation)).findFirst().orElse(new DungeonState.Run(dungeon,generation,Set.of(),Set.of(),Set.of(),Set.of(),false));var updated=operation.apply(existing);var runs=state.runs().stream().filter(v->!(v.dungeon().equals(dungeon)&&v.generation().equals(generation))).collect(java.util.stream.Collectors.toCollection(ArrayList::new));runs.add(updated);return result(new DungeonState(state.dungeons(),runs),updated);});}
|
||||
private CompletableFuture<DungeonState.Dungeon> change(UUID id,java.util.function.UnaryOperator<DungeonState.Dungeon> operation){return mutate(state->{var updated=operation.apply(editable(state,id));return result(replace(state,updated),updated);});}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package games.dmg.fantasystick.events;
|
||||
|
||||
import games.dmg.fantasystick.adventure.AdventureQuestDirectory;
|
||||
import games.dmg.fantasystick.adventure.AdventureQuestState;
|
||||
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.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Independent observations; no audit fact is reset authority and no replay calls gameplay. */
|
||||
public final class AdventureDayResetEventBridge implements AutoCloseable {
|
||||
private final FeatureEventOutbox outbox;
|
||||
private final AdventureDayResetFactsStore store;
|
||||
private final Consumer<Throwable> failures;
|
||||
private final Clock clock=Clock.systemUTC();
|
||||
private final ExecutorService worker=Executors.newSingleThreadExecutor(task->new Thread(task,"fantasy-adventure-day-reset-facts"));
|
||||
private final Map<UUID,AdventureDayResetFactsStore.Facts> facts=new LinkedHashMap<>();
|
||||
private final Map<UUID,Map<String,Object>> cleanup=new LinkedHashMap<>();
|
||||
private final Set<UUID> handedOff=new java.util.HashSet<>();
|
||||
private final Set<UUID> recoveredPending=new java.util.HashSet<>();
|
||||
private final Map<UUID,Integer> participantRuns=new LinkedHashMap<>();
|
||||
private final CompletableFuture<Void> drained=new CompletableFuture<>();
|
||||
private boolean closed;
|
||||
public AdventureDayResetEventBridge(Function<EventInput,CompletionStage<LocalEventLog.Delivery>> sink,Path data,Consumer<Throwable> failures){
|
||||
this(sink,data,failures,CompletableFuture.completedFuture(null));
|
||||
}
|
||||
public AdventureDayResetEventBridge(Function<EventInput,CompletionStage<LocalEventLog.Delivery>> sink,Path data,Consumer<Throwable> failures,CompletionStage<Void> previousDrain){
|
||||
this.failures=failures;store=new AdventureDayResetFactsStore(data);
|
||||
outbox=new FeatureEventOutbox(data,"adventure-day-reset-event-intents",Set.of("adventure.day.reset"),event->sink.apply(event).whenComplete((delivery,failure)->{
|
||||
if(failure==null&&delivery!=LocalEventLog.Delivery.REJECTED)submit(()->{store.remove(event.actionId());facts.remove(event.actionId());cleanup.remove(event.actionId());handedOff.remove(event.actionId());recoveredPending.remove(event.actionId());participantRuns.remove(event.actionId());});
|
||||
}),failures);
|
||||
submit(()->{previousDrain.toCompletableFuture().join();load();});
|
||||
}
|
||||
public void prepared(AdventureQuestDirectory.ResetPublication publication){submit(()->{
|
||||
var reset=publication.reset();if(facts.containsKey(reset.operation()))return;var values=publicationData(publication);var work=cleanup.get(reset.operation());if(work!=null)values.putAll(work);if(participantRuns.containsKey(reset.operation())){values.put("cleaned_run_count",participantRuns.get(reset.operation()));values.put("run_cleanup_capture_quality","acknowledged_dungeon_source_save");}
|
||||
put(new AdventureDayResetFactsStore.Facts(reset.operation(),reset.generation(),reset.startedAt(),publication.actor()==null?null:publication.actor().context(),values,null));
|
||||
});}
|
||||
public void cleanupObserved(AdventureQuestState.Reset reset,int encounters,int participants){submit(()->{
|
||||
var prior=facts.get(reset.operation());boolean unknown=recoveredPending.contains(reset.operation())&&(prior==null||!prior.data().containsKey("cleaned_encounter_count"));
|
||||
var original=cleanup.computeIfAbsent(reset.operation(),ignored->Map.of(unknown?"recovery_cleaned_encounter_count":"cleaned_encounter_count",encounters,"participant_count",participants,"cleanup_capture_quality",unknown?"recovery_current_work_only":"native_work_observed_before_cleanup"));
|
||||
if(prior!=null&&prior.completedAt()==null){var values=new LinkedHashMap<>(prior.data());values.putAll(original);put(withData(prior,values));}
|
||||
});}
|
||||
public void participantCleanup(games.dmg.fantasystick.dungeon.DungeonDirectory.ResetCleanup observed){submit(()->{
|
||||
var operation=observed.reset().operation();participantRuns.putIfAbsent(operation,observed.runs());var prior=facts.get(operation);if(prior==null||prior.completedAt()!=null)return;
|
||||
var values=new LinkedHashMap<>(prior.data());boolean unknown=recoveredPending.contains(operation)&&!values.containsKey("cleaned_run_count");
|
||||
values.putIfAbsent(unknown?"recovery_cleaned_run_count":"cleaned_run_count",participantRuns.get(operation));values.putIfAbsent("run_cleanup_capture_quality",unknown?"recovery_current_work_only":"acknowledged_dungeon_source_save");put(withData(prior,values));
|
||||
});}
|
||||
public void cleanupAcknowledged(AdventureQuestState.Reset reset,int participants){submit(()->{
|
||||
var prior=facts.get(reset.operation());if(prior==null||prior.completedAt()!=null)return;var values=new LinkedHashMap<>(prior.data());
|
||||
values.put("participant_count",participants);values.put("participant_outcomes",participantOutcomes(participants));put(withData(prior,values));
|
||||
});}
|
||||
/** Only called following the final successful source save, not initial installation or cleanup alone. */
|
||||
public void completed(AdventureQuestState.Reset reset){submit(()->complete(reset.operation(),"original_final_source_acknowledgment"));}
|
||||
/** A cleared source reset marker at the same captured generation proves final publication, not a new reset. */
|
||||
public void recover(AdventureQuestState state){submit(()->{
|
||||
load();
|
||||
if(state.reset()!=null&&!facts.containsKey(state.reset().operation())){
|
||||
var reset=state.reset();var values=new LinkedHashMap<String,Object>();values.put("operation_id",reset.operation().toString());values.put("catalog_id",reset.generation().toString());values.put("generation_id",reset.generation().toString());values.put("reset_started_at",reset.startedAt().toString());
|
||||
if(state.catalog()!=null)catalogData(values,state.catalog());values.put("reason","unavailable");values.put("actor_kind","unavailable");values.put("original_capture_quality","unavailable");values.put("previous_capture_quality","unavailable");
|
||||
recoveredPending.add(reset.operation());put(new AdventureDayResetFactsStore.Facts(reset.operation(),reset.generation(),reset.startedAt(),null,values,null));
|
||||
}
|
||||
for(var value:java.util.List.copyOf(facts.values())){
|
||||
if(value.completedAt()!=null)handoff(value);
|
||||
else if(state.reset()==null&&state.catalog()!=null&&state.catalog().revision().equals(value.generation()))complete(value.operation(),"recovered_final_source_marker");
|
||||
}
|
||||
});}
|
||||
private void complete(UUID operation,String quality){
|
||||
var prior=facts.get(operation);if(prior==null)return;if(prior.completedAt()!=null){handoff(prior);return;}
|
||||
var values=new LinkedHashMap<>(prior.data());preservationPolicy(values);values.put("outcome","acknowledged");values.put("completion_capture_quality",quality);
|
||||
values.put("all_current_participants_acknowledged",true);
|
||||
if(!values.containsKey("participant_outcomes")){
|
||||
if(quality.equals("original_final_source_acknowledgment")&&values.get("participant_count") instanceof Integer count)values.put("participant_outcomes",participantOutcomes(count));
|
||||
else values.put("participant_outcome_capture_quality","source_final_marker_original_details_unavailable");
|
||||
}
|
||||
var completed=new AdventureDayResetFactsStore.Facts(prior.operation(),prior.generation(),prior.started(),prior.context(),values,clock.instant());put(completed);handoff(completed);
|
||||
}
|
||||
private void handoff(AdventureDayResetFactsStore.Facts value){
|
||||
if(value.completedAt()==null||!handedOff.add(value.operation()))return;
|
||||
UUID id=UUID.nameUUIDFromBytes(("fantasystick:adventure-day-reset:"+value.operation()+":completed").getBytes(StandardCharsets.UTF_8));
|
||||
outbox.accepted(new EventInput(id,value.operation(),value.completedAt(),"adventure.day.reset",value.context(),value.data()));
|
||||
}
|
||||
private void load()throws java.io.IOException{
|
||||
for(var value:store.load()){
|
||||
if(facts.putIfAbsent(value.operation(),value)==null&&value.completedAt()==null)recoveredPending.add(value.operation());
|
||||
if(value.data().get("cleaned_run_count") instanceof Integer count)participantRuns.putIfAbsent(value.operation(),count);
|
||||
if(value.data().containsKey("cleaned_encounter_count"))cleanup.putIfAbsent(value.operation(),Map.of("cleaned_encounter_count",value.data().get("cleaned_encounter_count"),"participant_count",value.data().get("participant_count"),"cleanup_capture_quality",value.data().get("cleanup_capture_quality")));
|
||||
}
|
||||
}
|
||||
private void put(AdventureDayResetFactsStore.Facts value){facts.put(value.operation(),value);try{store.save(value);}catch(java.io.IOException|RuntimeException failure){report(failure);}}
|
||||
private static AdventureDayResetFactsStore.Facts withData(AdventureDayResetFactsStore.Facts value,Map<String,Object> data){return new AdventureDayResetFactsStore.Facts(value.operation(),value.generation(),value.started(),value.context(),data,value.completedAt());}
|
||||
private static LinkedHashMap<String,Object> publicationData(AdventureQuestDirectory.ResetPublication publication){
|
||||
var reset=publication.reset();var values=new LinkedHashMap<String,Object>();values.put("reason",publication.reason());values.put("operation_id",reset.operation().toString());values.put("generation_id",reset.generation().toString());values.put("reset_started_at",reset.startedAt().toString());values.put("original_capture_quality","original_source_publication");catalogData(values,publication.catalog());
|
||||
var before=publication.previous();if(before!=null){values.put("previous_catalog_id",before.revision().toString());values.put("previous_generation_id",before.revision().toString());values.put("previous_day",before.day().toString());values.put("previous_created_at",before.createdAt().toString());values.put("previous_expires_at",before.expiresAt().toString());}else values.put("previous_capture_quality","unavailable");
|
||||
values.put("expired_quest_count",publication.expiredQuests());if(publication.actor()!=null)values.putAll(publication.actor().captured());else{values.put("actor_kind","system");values.put("location_status","not_applicable");}return values;
|
||||
}
|
||||
private static void catalogData(Map<String,Object> values,AdventureQuestState.Catalog catalog){values.put("catalog_id",catalog.revision().toString());values.put("day",catalog.day().toString());values.put("created_at",catalog.createdAt().toString());values.put("expires_at",catalog.expiresAt().toString());values.put("selected_site_count",catalog.sites().size());values.put("selected_site_ids",catalog.sites().stream().map(UUID::toString).toList());}
|
||||
private static Object participantOutcomes(int count){return java.util.stream.IntStream.range(0,count).mapToObj(index->Map.<String,Object>of("participant_index",(double)index,"outcome","acknowledged")).toList();}
|
||||
private static void preservationPolicy(Map<String,Object> values){for(String key:java.util.List.of("earned_rewards_preserved","earned_trophies_preserved","unlocked_shares_preserved","acknowledged_kill_rewards_preserved","pending_proof_preserved","fishing_stock_preserved","inventory_preserved","active_work_expired","current_generation_gates_reset"))values.put(key,true);}
|
||||
public void replay(){submit(()->{load();for(var value:facts.values())if(value.completedAt()!=null)handoff(value);outbox.replay();});}
|
||||
@FunctionalInterface private interface Work{void run()throws java.io.IOException;}
|
||||
private synchronized void submit(Work action){if(closed)return;try{worker.execute(()->{try{action.run();}catch(java.io.IOException|RuntimeException failure){report(failure);}});}catch(RuntimeException failure){report(failure);}}
|
||||
private void report(Throwable failure){try{failures.accept(failure);}catch(RuntimeException ignored){/* Failure reporting is not gameplay authority. */}}
|
||||
public CompletionStage<Void> drained(){return drained.minimalCompletionStage();}
|
||||
@Override public synchronized void close(){if(closed)return;closed=true;worker.execute(()->{outbox.close();outbox.drained().whenComplete((ignored,failure)->{if(failure==null)drained.complete(null);else drained.completeExceptionally(failure);});});worker.shutdown();}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package games.dmg.fantasystick.events;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/** Owner-local observation facts, never a gameplay phase, permission or reset receipt. Worker-only I/O. */
|
||||
final class AdventureDayResetFactsStore {
|
||||
record Facts(UUID operation,UUID generation,Instant started,EventInput.Context context,Map<String,Object> data,Instant completedAt){
|
||||
Facts{data=new EventInput(operation,operation,started,"adventure.day.reset",context,data).data();}
|
||||
}
|
||||
private static final int MAX_BYTES=65_536;
|
||||
private final Path directory;
|
||||
AdventureDayResetFactsStore(Path data){directory=data.toAbsolutePath().resolve("adventure-day-reset-facts");}
|
||||
List<Facts> load()throws IOException{
|
||||
if(!Files.exists(directory,LinkOption.NOFOLLOW_LINKS))return List.of();safeDirectory();var result=new ArrayList<Facts>();
|
||||
try(var paths=Files.list(directory)){for(var path:paths.filter(value->value.getFileName().toString().matches("[0-9a-f-]{36}\\.facts")).sorted().toList()){
|
||||
if(!Files.isRegularFile(path,LinkOption.NOFOLLOW_LINKS))throw new IOException("Unsafe reset facts file");
|
||||
byte[] bytes;try(var input=Files.newInputStream(path,LinkOption.NOFOLLOW_LINKS)){bytes=input.readNBytes(MAX_BYTES+1);}if(bytes.length>MAX_BYTES)throw new IOException("Oversized reset facts");
|
||||
try{
|
||||
var root=JsonParser.parseString(new String(bytes,StandardCharsets.UTF_8)).getAsJsonObject();if(!text(root,"format").equals("adventure_day_reset_facts_v1"))throw new IOException("Unknown reset facts format");
|
||||
UUID operation=UUID.fromString(text(root,"operation"));if(!path.getFileName().toString().equals(operation+".facts"))throw new IOException("Reset facts identity mismatch");
|
||||
EventInput.Context context=null;if(root.has("context")){
|
||||
var value=root.getAsJsonObject("context");EventInput.Location location=null;
|
||||
if(value.has("world_id"))location=new EventInput.Location(UUID.fromString(text(value,"world_id")),text(value,"world_name"),value.get("x").getAsDouble(),value.get("y").getAsDouble(),value.get("z").getAsDouble(),Instant.parse(text(value,"captured_at")),text(value,"quality"));
|
||||
context=new EventInput.Context(UUID.fromString(text(value,"player_id")),text(value,"player_name"),uuid(value,"character_id"),value.has("character_name")?text(value,"character_name"):null,uuid(value,"session_id"),location);
|
||||
}
|
||||
result.add(new Facts(operation,UUID.fromString(text(root,"generation")),Instant.parse(text(root,"started_at")),context,object(root.getAsJsonObject("data"),0),root.has("completed_at")?Instant.parse(text(root,"completed_at")):null));
|
||||
}catch(RuntimeException failure){throw new IOException("Invalid reset observation facts",failure);}
|
||||
}}return List.copyOf(result);
|
||||
}
|
||||
void save(Facts facts)throws IOException{
|
||||
if(Files.exists(directory,LinkOption.NOFOLLOW_LINKS))safeDirectory();else Files.createDirectories(directory);
|
||||
var root=new JsonObject();root.addProperty("format","adventure_day_reset_facts_v1");root.addProperty("operation",facts.operation().toString());root.addProperty("generation",facts.generation().toString());root.addProperty("started_at",facts.started().toString());
|
||||
if(facts.completedAt()!=null)root.addProperty("completed_at",facts.completedAt().toString());
|
||||
if(facts.context()!=null){var c=facts.context();var value=new JsonObject();value.addProperty("player_id",c.playerId().toString());value.addProperty("player_name",c.playerName());
|
||||
if(c.characterId()!=null){value.addProperty("character_id",c.characterId().toString());value.addProperty("character_name",c.characterName());value.addProperty("session_id",c.sessionId().toString());}
|
||||
if(c.location()!=null){var l=c.location();value.addProperty("world_id",l.worldId().toString());value.addProperty("world_name",l.worldName());value.addProperty("x",l.x());value.addProperty("y",l.y());value.addProperty("z",l.z());value.addProperty("captured_at",l.capturedAt().toString());value.addProperty("quality",l.quality());}root.add("context",value);
|
||||
}
|
||||
root.add("data",new Gson().toJsonTree(facts.data()));byte[] bytes=root.toString().getBytes(StandardCharsets.UTF_8);if(bytes.length>MAX_BYTES)throw new IOException("Oversized reset facts");
|
||||
var target=directory.resolve(facts.operation()+".facts");if(Files.exists(target,LinkOption.NOFOLLOW_LINKS)&&!Files.isRegularFile(target,LinkOption.NOFOLLOW_LINKS))throw new IOException("Unsafe reset facts target");
|
||||
var temporary=Files.createTempFile(directory,".reset-facts-",".tmp");try{
|
||||
try(var channel=FileChannel.open(temporary,StandardOpenOption.WRITE)){var buffer=java.nio.ByteBuffer.wrap(bytes);while(buffer.hasRemaining())channel.write(buffer);channel.force(true);}
|
||||
Files.move(temporary,target,StandardCopyOption.ATOMIC_MOVE,StandardCopyOption.REPLACE_EXISTING);forceDirectory();
|
||||
}finally{Files.deleteIfExists(temporary);}
|
||||
}
|
||||
void remove(UUID operation)throws IOException{if(!Files.exists(directory,LinkOption.NOFOLLOW_LINKS))return;safeDirectory();if(Files.deleteIfExists(directory.resolve(operation+".facts")))forceDirectory();}
|
||||
private void safeDirectory()throws IOException{if(!Files.isDirectory(directory,LinkOption.NOFOLLOW_LINKS))throw new IOException("Unsafe reset facts directory");}
|
||||
private void forceDirectory()throws IOException{try(var channel=FileChannel.open(directory,StandardOpenOption.READ)){channel.force(true);}}
|
||||
private static String text(JsonObject value,String key){return value.get(key).getAsString();}
|
||||
private static UUID uuid(JsonObject value,String key){return value.has(key)?UUID.fromString(text(value,key)):null;}
|
||||
private static Map<String,Object> object(JsonObject value,int depth)throws IOException{var map=new LinkedHashMap<String,Object>();for(var entry:value.entrySet())map.put(entry.getKey(),decode(entry.getValue(),depth+1));return Map.copyOf(map);}
|
||||
private static Object decode(JsonElement value,int depth)throws IOException{
|
||||
if(depth>8||value.isJsonNull())throw new IOException("Unsafe reset facts value");if(value.isJsonObject())return object(value.getAsJsonObject(),depth);if(value.isJsonArray()){var result=new ArrayList<>();for(var item:value.getAsJsonArray())result.add(decode(item,depth+1));return List.copyOf(result);}
|
||||
var primitive=value.getAsJsonPrimitive();if(primitive.isBoolean())return primitive.getAsBoolean();if(primitive.isString())return primitive.getAsString();
|
||||
if(primitive.isNumber()){String text=primitive.getAsString();if(text.matches("-?[0-9]+"))return Integer.valueOf(text);double number=primitive.getAsDouble();if(!Double.isFinite(number))throw new IOException("Non-finite reset facts number");return number;}
|
||||
throw new IOException("Unsupported reset facts value");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user