feat(adventure): add site participation modes
Release / release (push) Successful in 11m36s

This commit is contained in:
dmg committed 2026-09-23 15:23:18 -04:00
1 parent 871258070b
commit 421e44d86f
13 files changed
+474 -32

No files matched your search

@@ -0,0 +1,178 @@
package games.dmg.fantasystick.adventure;
import static org.junit.jupiter.api.Assertions.*;
import games.dmg.fantasystick.dungeon.DungeonDirectory;
import games.dmg.fantasystick.persistence.AdventureQuestSettings;
import games.dmg.fantasystick.persistence.YamlAdventureQuestRepository;
import games.dmg.fantasystick.persistence.YamlDungeonRepository;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
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 AdventureParticipationModeTest {
@TempDir Path data;
@Test void sitesDefaultPersistUpdateAndCaptureParticipationMode() throws Exception {
var rules = new AdventureQuestSettings.Rules(3, 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-23T19:00:00Z"), ZoneOffset.UTC);
UUID siteId;
try (var directory = new AdventureQuestDirectory(new YamlAdventureQuestRepository(data), clock,
() -> rules, new Random(1))) {
var site = directory.placeSite(AdventureQuestState.SiteType.CAMP, "Mode Camp", UUID.randomUUID(),
0, 64, 0).get(5, TimeUnit.SECONDS);
siteId = site.id();
assertEquals(AdventureQuestState.ParticipationMode.PARTY_AVAILABLE, site.participationMode());
var changed = directory.setSiteMode(site.id(), site.participationMode(),
AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY).get(5, TimeUnit.SECONDS);
assertEquals(AdventureQuestDirectory.ModeStatus.APPLIED, changed.status());
assertEquals(AdventureQuestDirectory.ModeStatus.STALE, directory.setSiteMode(site.id(),
AdventureQuestState.ParticipationMode.PARTY_AVAILABLE,
AdventureQuestState.ParticipationMode.PARTY_REQUIRED).get(5, TimeUnit.SECONDS).status());
var catalog = directory.ensureToday().get(5, TimeUnit.SECONDS);
UUID character = UUID.randomUUID(), party = UUID.randomUUID();
assertEquals(AdventureQuestDirectory.Acceptance.POLICY, directory.accept(catalog.revision(), site.id(),
AdventureQuestState.UnitType.PARTY, party, Set.of(character),
AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY).get(5, TimeUnit.SECONDS).status());
var quest = directory.accept(catalog.revision(), site.id(), AdventureQuestState.UnitType.SOLO,
character, Set.of(character), AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY)
.get(5, TimeUnit.SECONDS).quest().orElseThrow();
assertEquals(AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY, quest.participationMode());
}
try (var restarted = new AdventureQuestDirectory(new YamlAdventureQuestRepository(data), clock,
() -> rules, new Random(2))) {
assertEquals(AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY,
restarted.findSite(siteId).orElseThrow().participationMode());
assertEquals(AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY,
restarted.active().getFirst().participationMode());
}
}
@Test void compatibleSiteAndDungeonSavesDefaultToPartyAvailable() throws Exception {
Files.writeString(data.resolve("adventure-quests.yml"), """
schema-version: 1
sites:
- id: '00000000-0000-0000-0000-000000000001'
type: CAMP
name: Legacy
world: '00000000-0000-0000-0000-000000000002'
x: '0.0'
y: '64.0'
z: '0.0'
givers: []
catalog: null
quests: []
unit-claims: []
character-claims: []
rosters: []
tracking: []
rewards: {allocations: [], operations: [], receipts: []}
reset: null
""");
assertEquals(AdventureQuestState.ParticipationMode.PARTY_AVAILABLE,
new YamlAdventureQuestRepository(data).load().sites().getFirst().participationMode());
UUID dungeonId;
try (var dungeons = new DungeonDirectory(new YamlDungeonRepository(data))) {
var dungeon = dungeons.create("Modes", UUID.randomUUID(), 0, 64, 0, 0, 0,
AdventureQuestState.ParticipationMode.PARTY_REQUIRED).get(5, TimeUnit.SECONDS);
dungeonId = dungeon.id();
assertEquals(AdventureQuestState.ParticipationMode.PARTY_REQUIRED, dungeon.participationMode());
var updated = dungeons.setMode(dungeon.id(), dungeon.revision(),
AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY).get(5, TimeUnit.SECONDS);
assertEquals(AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY, updated.participationMode());
assertThrows(java.util.concurrent.ExecutionException.class, () -> dungeons.setMode(dungeon.id(),
dungeon.revision(), AdventureQuestState.ParticipationMode.PARTY_AVAILABLE).get(5, TimeUnit.SECONDS));
}
var dungeonFile = data.resolve("dungeons.yml");
Files.writeString(dungeonFile, Files.readString(dungeonFile)
.replaceFirst("(?m)^ participation-mode: individual-only\\R", ""));
assertEquals(AdventureQuestState.ParticipationMode.PARTY_AVAILABLE,
new YamlDungeonRepository(data).load().dungeons().stream()
.filter(value -> value.id().equals(dungeonId)).findFirst().orElseThrow().participationMode());
}
@Test void failedModeWritesRetainLastAcknowledgedSiteAndDungeonState() 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));
try (var directory = new AdventureQuestDirectory(new YamlAdventureQuestRepository(data), Clock.systemUTC(),
() -> rules, new Random(1))) {
var site = directory.placeSite(AdventureQuestState.SiteType.CAMP, "Retained", UUID.randomUUID(),
0, 64, 0).get(5, TimeUnit.SECONDS);
var file = data.resolve("adventure-quests.yml");
Files.delete(file); Files.createDirectory(file);
assertThrows(java.util.concurrent.ExecutionException.class, () -> directory.setSiteMode(site.id(),
AdventureQuestState.ParticipationMode.PARTY_AVAILABLE,
AdventureQuestState.ParticipationMode.INDIVIDUAL_ONLY).get(5, TimeUnit.SECONDS));
assertEquals(AdventureQuestState.ParticipationMode.PARTY_AVAILABLE,
directory.findSite(site.id()).orElseThrow().participationMode());
}
var dungeonRoot = data.resolve("dungeon-failure");
try (var dungeons = new DungeonDirectory(new YamlDungeonRepository(dungeonRoot))) {
var dungeon = dungeons.create("Retained", UUID.randomUUID(), 0, 64, 0, 0, 0)
.get(5, TimeUnit.SECONDS);
var file = dungeonRoot.resolve("dungeons.yml");
Files.delete(file); Files.createDirectory(file);
assertThrows(java.util.concurrent.ExecutionException.class, () -> dungeons.setMode(dungeon.id(),
dungeon.revision(), AdventureQuestState.ParticipationMode.PARTY_REQUIRED)
.get(5, TimeUnit.SECONDS));
assertEquals(AdventureQuestState.ParticipationMode.PARTY_AVAILABLE,
dungeons.find(dungeon.id()).orElseThrow().participationMode());
}
}
@Test void liveModeChangesGovernNewAcceptanceWithoutReinterpretingExistingQuest() 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-23T19:00:00Z"), ZoneOffset.UTC);
try (var directory = new AdventureQuestDirectory(new YamlAdventureQuestRepository(data), clock,
() -> rules, new Random(1))) {
var site = directory.placeSite(AdventureQuestState.SiteType.CHAMPION, "Changing", UUID.randomUUID(),
0, 64, 0).get(5, TimeUnit.SECONDS);
var catalog = directory.ensureToday().get(5, TimeUnit.SECONDS);
UUID first = UUID.randomUUID(), second = UUID.randomUUID(), party = UUID.randomUUID();
var existing = directory.accept(catalog.revision(), site.id(), AdventureQuestState.UnitType.SOLO,
first, Set.of(first), AdventureQuestState.ParticipationMode.PARTY_AVAILABLE)
.get(5, TimeUnit.SECONDS).quest().orElseThrow();
directory.setSiteMode(site.id(), AdventureQuestState.ParticipationMode.PARTY_AVAILABLE,
AdventureQuestState.ParticipationMode.PARTY_REQUIRED).get(5, TimeUnit.SECONDS);
assertEquals(AdventureQuestState.ParticipationMode.PARTY_AVAILABLE, existing.participationMode());
assertEquals(AdventureQuestDirectory.Acceptance.STALE, directory.accept(catalog.revision(), site.id(),
AdventureQuestState.UnitType.PARTY, party, Set.of(second),
AdventureQuestState.ParticipationMode.PARTY_AVAILABLE).get(5, TimeUnit.SECONDS).status());
var replacement = directory.accept(catalog.revision(), site.id(), AdventureQuestState.UnitType.PARTY,
party, Set.of(second), AdventureQuestState.ParticipationMode.PARTY_REQUIRED)
.get(5, TimeUnit.SECONDS).quest().orElseThrow();
assertEquals(AdventureQuestState.ParticipationMode.PARTY_REQUIRED, replacement.participationMode());
}
}
@Test void personalAndPartyCommitmentsCannotOverlapCharacterSnapshots() throws Exception {
var rules = new AdventureQuestSettings.Rules(2, 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-23T19:00:00Z"), ZoneOffset.UTC);
try (var directory = new AdventureQuestDirectory(new YamlAdventureQuestRepository(data), clock,
() -> rules, new Random(1))) {
var first = directory.placeSite(AdventureQuestState.SiteType.CAMP, "First", UUID.randomUUID(),
0, 64, 0).get(5, TimeUnit.SECONDS);
var second = directory.placeSite(AdventureQuestState.SiteType.CHAMPION, "Second", UUID.randomUUID(),
20, 64, 0).get(5, TimeUnit.SECONDS);
var catalog = directory.ensureToday().get(5, TimeUnit.SECONDS);
UUID leader = UUID.randomUUID(), member = UUID.randomUUID(), party = UUID.randomUUID();
assertEquals(AdventureQuestDirectory.Acceptance.APPLIED, directory.accept(catalog.revision(), first.id(),
AdventureQuestState.UnitType.PARTY, party, Set.of(leader, member)).get(5, TimeUnit.SECONDS).status());
assertEquals(AdventureQuestDirectory.Acceptance.CONFLICT, directory.accept(catalog.revision(), second.id(),
AdventureQuestState.UnitType.SOLO, member, Set.of(member)).get(5, TimeUnit.SECONDS).status());
}
}
}