103 lines
3.7 KiB
Java
103 lines
3.7 KiB
Java
package games.dmg.spigottyrant;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
|
|
final class YamlTyrantStateRepositoryTest {
|
|
@TempDir
|
|
Path temporaryDirectory;
|
|
|
|
@Test
|
|
void roundTripsLifecycleRolesProgressionPlayersCooldownsItemsAndMobs() throws Exception {
|
|
UUID tyrant = UUID.fromString("11111111-1111-1111-1111-111111111111");
|
|
UUID vigilante = UUID.fromString("22222222-2222-2222-2222-222222222222");
|
|
Instant now = Instant.parse("2026-08-14T12:00:00Z");
|
|
GameState game = new GameState(
|
|
GameLifecycle.PAUSED,
|
|
Optional.of(tyrant),
|
|
Optional.of(vigilante),
|
|
Optional.empty(),
|
|
Optional.of(new PendingSelection(vigilante, now.plusSeconds(3600))),
|
|
Optional.of(now),
|
|
Duration.ofMinutes(15),
|
|
3,
|
|
2,
|
|
Set.of(TyrantUnlock.ASSASSIN, TyrantUnlock.STRENGTH)
|
|
);
|
|
PlayerState player = new PlayerState(
|
|
tyrant,
|
|
"TyrantPlayer",
|
|
Optional.of(now.minusSeconds(10)),
|
|
Optional.of(now.plusSeconds(600)),
|
|
TyrantClass.ASSASSIN,
|
|
Optional.of(vigilante),
|
|
Map.of(Ability.ASSASSIN_INVISIBILITY, now.plusSeconds(3600)),
|
|
Set.of(Ability.ASSASSIN_INVISIBILITY),
|
|
List.of(new CapturedMob("ZOMBIE", Map.of("custom-name", "Bob")))
|
|
);
|
|
PersistentState expected = new PersistentState(game, Map.of(tyrant, player));
|
|
YamlTyrantStateRepository repository = new YamlTyrantStateRepository(
|
|
temporaryDirectory.resolve("state.yml")
|
|
);
|
|
|
|
repository.save(expected);
|
|
|
|
assertEquals(expected, repository.load());
|
|
}
|
|
|
|
@Test
|
|
void preservesUnknownFieldsForRetainedState() throws Exception {
|
|
UUID playerId = UUID.fromString("11111111-1111-1111-1111-111111111111");
|
|
Path stateFile = temporaryDirectory.resolve("state.yml");
|
|
Files.writeString(stateFile, """
|
|
future-root: retained
|
|
players:
|
|
11111111-1111-1111-1111-111111111111:
|
|
name: Player
|
|
future-player-field: retained-too
|
|
""");
|
|
YamlTyrantStateRepository repository = new YamlTyrantStateRepository(stateFile);
|
|
|
|
PersistentState loaded = repository.load();
|
|
repository.save(loaded);
|
|
|
|
String saved = Files.readString(stateFile);
|
|
assertEquals(true, saved.contains("future-root: retained"));
|
|
assertEquals(true, saved.contains("future-player-field: retained-too"));
|
|
assertEquals(playerId, loaded.players().get(playerId).playerId());
|
|
}
|
|
|
|
@Test
|
|
void invalidRecordsCannotRestoreProgressOrCapturedDragons() throws Exception {
|
|
Path stateFile = temporaryDirectory.resolve("state.yml");
|
|
Files.writeString(stateFile, """
|
|
game:
|
|
lifecycle: RUNNING
|
|
tyrant-level: -10
|
|
players:
|
|
11111111-1111-1111-1111-111111111111:
|
|
name: Player
|
|
captured-mobs:
|
|
- entity-type: ENDER_DRAGON
|
|
- entity-type: NOT_REAL
|
|
""");
|
|
YamlTyrantStateRepository repository = new YamlTyrantStateRepository(stateFile);
|
|
|
|
PersistentState loaded = repository.load();
|
|
|
|
assertEquals(GameState.empty(), loaded.game());
|
|
assertEquals(List.of(), loaded.players().values().iterator().next().capturedMobs());
|
|
}
|
|
}
|