feat(adventure): add persistent champion trophies
Release / release (push) Failing after 5m30s

This commit is contained in:
dmg committed 2026-09-23 17:19:46 -04:00
1 parent 1e3c7f28e2
commit bca3033fd9
19 files changed
+226 -36

No files matched your search

@@ -0,0 +1,41 @@
package games.dmg.fantasystick.adventure;
import static org.junit.jupiter.api.Assertions.*;
import games.dmg.fantasystick.persistence.AdventureQuestSettings;
import games.dmg.fantasystick.persistence.YamlAdventureQuestRepository;
import java.math.BigInteger;
import java.time.*;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
final class AdventureLegacyRewardRetentionTest {
@TempDir java.nio.file.Path data;
@Test void existingBankedAllocationSurvivesRerollSiteRemovalAndRestartWithoutRetroactiveTrophy() throws Exception {
var rules=new AdventureQuestSettings.Rules(1,16,30,8,BigInteger.valueOf(100),BigInteger.valueOf(20),100,8,BigInteger.valueOf(150),BigInteger.valueOf(40));
var clock=Clock.fixed(Instant.parse("2026-09-23T12:00:00Z"),ZoneOffset.UTC);UUID character=UUID.randomUUID(),siteId,claimId;
try(var directory=new AdventureQuestDirectory(new YamlAdventureQuestRepository(data),clock,()->rules,new Random(1))){
var site=directory.placeSite(AdventureQuestState.SiteType.CHAMPION,"Legacy Summit",UUID.randomUUID(),0,64,0).get(5,TimeUnit.SECONDS);siteId=site.id();
var catalog=directory.ensureToday().get(5,TimeUnit.SECONDS);var quest=directory.accept(catalog.revision(),site.id(),AdventureQuestState.UnitType.SOLO,character,Set.of(character)).get(5,TimeUnit.SECONDS).quest().orElseThrow();
assertTrue(directory.bankAndComplete(quest.id(),UUID.randomUUID(),Map.of(character,new AdventureQuestDirectory.Reward(BigInteger.valueOf(150),BigInteger.valueOf(40)))).get(5,TimeUnit.SECONDS));
assertEquals(new AdventureQuestDirectory.Reward(BigInteger.valueOf(150),BigInteger.valueOf(40)),directory.unclaimedAll(character));
claimId=directory.beginLegacyClaim(character).get(5,TimeUnit.SECONDS).orElseThrow().id();
directory.rerollToday(catalog.revision()).get(5,TimeUnit.SECONDS);directory.completeReset(directory.snapshot().reset().operation()).get(5,TimeUnit.SECONDS);directory.removeSite(site.id()).get(5,TimeUnit.SECONDS);
assertEquals(new AdventureQuestDirectory.Reward(BigInteger.valueOf(150),BigInteger.valueOf(40)),directory.unclaimedAll(character));
}
try(var restarted=new AdventureQuestDirectory(new YamlAdventureQuestRepository(data),clock,()->rules,new Random(2))){
assertTrue(restarted.findSite(siteId).isEmpty());
var claim=restarted.beginLegacyClaim(character).get(5,TimeUnit.SECONDS).orElseThrow();assertEquals(claimId,claim.id(),"in-progress claim proof survives reset and restart");
assertEquals(BigInteger.valueOf(150),claim.xp());assertEquals(BigInteger.valueOf(40),claim.coins());
assertEquals(claim,restarted.beginLegacyClaim(character).get(5,TimeUnit.SECONDS).orElseThrow());
assertTrue(restarted.finishClaim(claim.id()).get(5,TimeUnit.SECONDS));
assertEquals(AdventureQuestDirectory.Reward.empty(),restarted.unclaimedAll(character));
}
}
}
@@ -239,9 +239,9 @@ class AdventureQuestDirectoryTest {
var otherSite=catalog.sites().stream().filter(value->!value.equals(site)).findFirst().orElseThrow();UUID solo=UUID.randomUUID();var partial=restarted.accept(catalog.revision(),otherSite,AdventureQuestState.UnitType.SOLO,solo,Set.of(solo)).get(5,TimeUnit.SECONDS).quest().orElseThrow();
assertTrue(restarted.bank(partial.id(),UUID.randomUUID(),Map.of(solo,new AdventureQuestDirectory.Reward(BigInteger.ONE,BigInteger.ONE))).get(5,TimeUnit.SECONDS));
assertTrue(restarted.abandon(partial.id(),solo).get(5,TimeUnit.SECONDS));assertEquals(BigInteger.ONE,restarted.unclaimed(catalog.revision(),otherSite,solo).xp(),"ordinary quest ending retains earned allocations");
var reset=restarted.rerollToday(catalog.revision()).get(5,TimeUnit.SECONDS);assertEquals(AdventureQuestDirectory.RerollStatus.APPLIED,reset.status());assertTrue(restarted.snapshot().rewards().allocations().isEmpty());assertTrue(restarted.snapshot().rewards().operations().isEmpty());assertTrue(restarted.snapshot().rewards().receipts().isEmpty());var replacement=reset.catalog().orElseThrow();assertTrue(restarted.completeReset(restarted.snapshot().reset().operation()).get(5,TimeUnit.SECONDS));
var reset=restarted.rerollToday(catalog.revision()).get(5,TimeUnit.SECONDS);assertEquals(AdventureQuestDirectory.RerollStatus.APPLIED,reset.status());assertEquals(2,restarted.snapshot().rewards().allocations().size(),"earned legacy allocations no longer expire on full reroll");assertTrue(restarted.snapshot().rewards().operations().isEmpty());assertFalse(restarted.snapshot().rewards().receipts().isEmpty());assertEquals(BigInteger.valueOf(49),restarted.unclaimedAll(second).xp());assertEquals(BigInteger.ONE,restarted.unclaimedAll(solo).xp());var replacement=reset.catalog().orElseThrow();assertTrue(restarted.completeReset(restarted.snapshot().reset().operation()).get(5,TimeUnit.SECONDS));
UUID zero=UUID.randomUUID();var zeroQuest=restarted.accept(replacement.revision(),replacement.sites().getFirst(),AdventureQuestState.UnitType.SOLO,zero,Set.of(zero)).get(5,TimeUnit.SECONDS).quest().orElseThrow();assertTrue(restarted.bankAndComplete(zeroQuest.id(),zeroQuest.id(),Map.of(zero,new AdventureQuestDirectory.Reward(BigInteger.ZERO,BigInteger.ZERO))).get(5,TimeUnit.SECONDS));assertEquals(BigInteger.ZERO,restarted.unclaimed(replacement.revision(),zeroQuest.site(),zero).xp());assertTrue(restarted.characterClaimed(replacement.revision(),zeroQuest.site(),zero));assertFalse(restarted.rewardClaimed(replacement.revision(),zeroQuest.site(),zero));
UUID expiring=UUID.randomUUID();var expiringQuest=restarted.accept(replacement.revision(),replacement.sites().getLast(),AdventureQuestState.UnitType.SOLO,expiring,Set.of(expiring)).get(5,TimeUnit.SECONDS).quest().orElseThrow();assertTrue(restarted.bank(expiringQuest.id(),UUID.randomUUID(),Map.of(expiring,new AdventureQuestDirectory.Reward(BigInteger.TEN,BigInteger.ONE))).get(5,TimeUnit.SECONDS));clock.advance(Duration.ofDays(1));restarted.ensureToday().get(5,TimeUnit.SECONDS);assertTrue(restarted.snapshot().rewards().allocations().isEmpty(),"midnight expires unclaimed rewards");
UUID expiring=UUID.randomUUID();var expiringQuest=restarted.accept(replacement.revision(),replacement.sites().getLast(),AdventureQuestState.UnitType.SOLO,expiring,Set.of(expiring)).get(5,TimeUnit.SECONDS).quest().orElseThrow();assertTrue(restarted.bank(expiringQuest.id(),UUID.randomUUID(),Map.of(expiring,new AdventureQuestDirectory.Reward(BigInteger.TEN,BigInteger.ONE))).get(5,TimeUnit.SECONDS));clock.advance(Duration.ofDays(1));restarted.ensureToday().get(5,TimeUnit.SECONDS);assertEquals(BigInteger.TEN,restarted.unclaimedAll(expiring).xp(),"midnight preserves already earned legacy rewards");
}
}
@@ -0,0 +1,81 @@
package games.dmg.fantasystick.adventure;
import static org.junit.jupiter.api.Assertions.*;
import games.dmg.fantasystick.persistence.YamlFishingRepository;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
final class ChampionTrophyDirectoryTest {
@TempDir Path data;
@Test void earnedHeadPersistsWithFrozenCarrierBeneficiariesAndUnlocksExactlyOnce() throws Exception {
UUID trophyId=UUID.randomUUID(),carrier=UUID.randomUUID(),beneficiary=UUID.randomUUID();
var rewards=new LinkedHashMap<UUID,FishingState.Reward>();
rewards.put(beneficiary,new FishingState.Reward(BigInteger.valueOf(150),BigInteger.valueOf(40)));
var trophy=new FishingState.Trophy(trophyId,FishingState.TrophyType.CHAMPION,carrier,UUID.randomUUID(),
UUID.randomUUID(),UUID.randomUUID(),UUID.randomUUID(),"Daybreak Champion Head","Summit",rewards);
try(var directory=new FishingDirectory(new YamlFishingRepository(data))){
assertEquals(FishingDirectory.TrophyPrepareStatus.APPLIED,directory.prepareTrophy(trophy).get(5,TimeUnit.SECONDS));
assertEquals(FishingDirectory.TrophyPrepareStatus.EXISTING,directory.prepareTrophy(trophy).get(5,TimeUnit.SECONDS));
assertTrue(directory.trophies(carrier).isEmpty(),"prepared proof is not redeemable before quest completion");
var laterAttempt=new FishingState.Trophy(trophy.id(),trophy.type(),trophy.carrier(),trophy.catalog(),trophy.site(),trophy.quest(),UUID.randomUUID(),trophy.name(),trophy.origin(),trophy.rewards());
assertEquals(FishingDirectory.TrophyPrepareStatus.EXISTING,directory.prepareTrophy(laterAttempt).get(5,TimeUnit.SECONDS));assertEquals(trophy.source(),directory.snapshot().trophies().getFirst().source(),"first durable completion authority wins repeated attempt races");
assertEquals(FishingDirectory.TrophyCommitStatus.APPLIED,directory.commitTrophy(trophyId).get(5,TimeUnit.SECONDS));
assertEquals(trophy.earned(),directory.trophies(carrier).getFirst());
assertEquals(FishingState.Reward.empty(),directory.unclaimed(beneficiary),"custody does not unlock or redirect shares");
assertEquals(FishingDirectory.TrophyRedeemStatus.WRONG_CARRIER,directory.redeemTrophy(trophyId,beneficiary).get(5,TimeUnit.SECONDS));
assertEquals(FishingDirectory.TrophyRedeemStatus.APPLIED,directory.redeemTrophy(trophyId,carrier).get(5,TimeUnit.SECONDS));
assertTrue(directory.trophies(carrier).isEmpty());
assertEquals(new FishingState.Reward(BigInteger.valueOf(150),BigInteger.valueOf(40)),directory.unclaimed(beneficiary));
assertEquals(FishingDirectory.TrophyRedeemStatus.REDEEMED,directory.redeemTrophy(trophyId,carrier).get(5,TimeUnit.SECONDS));
assertEquals(FishingDirectory.TrophyPrepareStatus.REDEEMED,directory.prepareTrophy(trophy).get(5,TimeUnit.SECONDS));
}
try(var restarted=new FishingDirectory(new YamlFishingRepository(data))){
assertTrue(restarted.trophies(carrier).isEmpty());
assertEquals(new FishingState.Reward(BigInteger.valueOf(150),BigInteger.valueOf(40)),restarted.unclaimed(beneficiary));
assertEquals(FishingDirectory.TrophyRedeemStatus.REDEEMED,restarted.redeemTrophy(trophyId,carrier).get(5,TimeUnit.SECONDS));
}
}
@Test void releasedFishingSchemaLoadsWithEmptyTrophyCollections() throws Exception {
Files.writeString(data.resolve("fishing.yml"),"schema-version: 1\ninventory: []\ncatches: []\ncatch-receipts: []\nsubmissions: []\ncompletions: []\nallocations: []\nclaims: []\n");
try(var directory=new FishingDirectory(new YamlFishingRepository(data))){assertTrue(directory.snapshot().trophies().isEmpty());assertTrue(directory.snapshot().trophyReceipts().isEmpty());}
}
@Test void preparedTrophyProofSurvivesRestartWithoutBecomingRedeemable() throws Exception {
UUID carrier=UUID.randomUUID();var trophy=new FishingState.Trophy(UUID.randomUUID(),FishingState.TrophyType.CHAMPION,carrier,UUID.randomUUID(),UUID.randomUUID(),UUID.randomUUID(),UUID.randomUUID(),"Daybreak Champion Head","Restart Summit",java.util.Map.of(carrier,new FishingState.Reward(BigInteger.ONE,BigInteger.ONE)));
try(var directory=new FishingDirectory(new YamlFishingRepository(data))){assertEquals(FishingDirectory.TrophyPrepareStatus.APPLIED,directory.prepareTrophy(trophy).get(5,TimeUnit.SECONDS));assertTrue(directory.trophies(carrier).isEmpty());}
try(var restarted=new FishingDirectory(new YamlFishingRepository(data))){assertEquals(FishingState.TrophyPhase.PREPARED,restarted.snapshot().trophies().getFirst().phase());assertTrue(restarted.trophies(carrier).isEmpty());assertEquals(FishingDirectory.TrophyRedeemStatus.NOT_FOUND,restarted.redeemTrophy(trophy.id(),carrier).get(5,TimeUnit.SECONDS));assertEquals(FishingDirectory.TrophyCommitStatus.APPLIED,restarted.commitTrophy(trophy.id()).get(5,TimeUnit.SECONDS));assertEquals(trophy.earned(),restarted.trophies(carrier).getFirst());}
}
@Test void sameNameHeadsRemainDistinctAndFailedRedemptionRetainsBoth() throws Exception {
UUID carrier=UUID.randomUUID(),beneficiary=UUID.randomUUID(),site=UUID.randomUUID();
try(var directory=new FishingDirectory(new YamlFishingRepository(data))){
for(int index=0;index<2;index++){
var rewards=new LinkedHashMap<UUID,FishingState.Reward>();
rewards.put(beneficiary,new FishingState.Reward(BigInteger.valueOf(10+index),BigInteger.ONE));
var trophy=new FishingState.Trophy(UUID.randomUUID(),FishingState.TrophyType.CHAMPION,carrier,UUID.randomUUID(),
site,UUID.randomUUID(),UUID.randomUUID(),"Daybreak Champion Head","Summit",rewards);
assertEquals(FishingDirectory.TrophyPrepareStatus.APPLIED,directory.prepareTrophy(trophy).get(5,TimeUnit.SECONDS));
assertEquals(FishingDirectory.TrophyCommitStatus.APPLIED,directory.commitTrophy(trophy.id()).get(5,TimeUnit.SECONDS));
}
assertEquals(2,directory.trophies(carrier).size());
var first=directory.trophies(carrier).getFirst();
var file=data.resolve("fishing.yml");byte[] saved=Files.readAllBytes(file);Files.delete(file);Files.createDirectory(file);
assertThrows(java.util.concurrent.ExecutionException.class,()->directory.redeemTrophy(first.id(),carrier).get(5,TimeUnit.SECONDS));
assertEquals(2,directory.trophies(carrier).size());
assertEquals(FishingState.Reward.empty(),directory.unclaimed(beneficiary));
Files.delete(file);Files.write(file,saved);
assertEquals(FishingDirectory.TrophyRedeemStatus.APPLIED,directory.redeemTrophy(first.id(),carrier).get(5,TimeUnit.SECONDS));
assertEquals(1,directory.trophies(carrier).size());
assertEquals(first.total(),directory.unclaimed(beneficiary));
}
}
}