118 lines
5.2 KiB
Java
118 lines
5.2 KiB
Java
package games.dmg.fantasystick.persistence;
|
|
|
|
import games.dmg.fantasystick.progression.Stat;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.time.Instant;
|
|
import java.util.ArrayList;
|
|
import java.util.EnumMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import org.yaml.snakeyaml.LoaderOptions;
|
|
import org.yaml.snakeyaml.Yaml;
|
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
|
|
|
/** Plain values only: no Bukkit item deserialization or arbitrary Java YAML tags. */
|
|
final class CharacterYaml {
|
|
private CharacterYaml() { }
|
|
|
|
private static Yaml yaml() {
|
|
var options = new LoaderOptions();
|
|
options.setAllowDuplicateKeys(false);
|
|
options.setMaxAliasesForCollections(0);
|
|
options.setCodePointLimit(1_048_576);
|
|
return new Yaml(new SafeConstructor(options));
|
|
}
|
|
|
|
static String encode(CharacterRecord record) {
|
|
Map<String, Object> root = new LinkedHashMap<>();
|
|
root.put("schema-version", 1);
|
|
root.put("character-id", record.id().toString());
|
|
root.put("name", record.name());
|
|
root.put("revision", record.revision().toString());
|
|
Map<String, String> stats = new LinkedHashMap<>();
|
|
for (Stat stat : Stat.values()) stats.put(stat.name(), record.stats().get(stat).toString());
|
|
root.put("stats", stats);
|
|
root.put("level", record.level());
|
|
root.put("experience", record.experience().toString());
|
|
root.put("unspent-points", record.unspentPoints().toString());
|
|
root.put("health", record.health().toString());
|
|
root.put("learned-abilities", record.learned().stream().sorted().toList());
|
|
root.put("loadout", record.loadout());
|
|
List<Map<String, String>> cooldowns = new ArrayList<>();
|
|
record.cooldowns().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry ->
|
|
cooldowns.add(Map.of("ability", entry.getKey(), "ready-at", entry.getValue().toString())));
|
|
root.put("cooldowns", cooldowns);
|
|
root.put("dead", record.dead());
|
|
if (record.deathId() != null) {
|
|
root.put("death-id", record.deathId().toString());
|
|
root.put("died-at", record.diedAt().toString());
|
|
}
|
|
return yaml().dump(root);
|
|
}
|
|
|
|
static CharacterRecord decode(String input) {
|
|
Map<?, ?> root = object(yaml().load(input));
|
|
if (!number(root.get("schema-version")).equals(BigInteger.ONE)) {
|
|
throw new IllegalArgumentException("Unsupported character schema");
|
|
}
|
|
Map<?, ?> storedStats = object(root.get("stats"));
|
|
var stats = new EnumMap<Stat, BigInteger>(Stat.class);
|
|
for (var entry : storedStats.entrySet()) {
|
|
stats.put(Stat.valueOf(text(entry.getKey())), number(entry.getValue()));
|
|
}
|
|
List<String> learnedList = strings(root.get("learned-abilities"));
|
|
var learned = new LinkedHashSet<>(learnedList);
|
|
if (learned.size() != learnedList.size()) throw new IllegalArgumentException("Duplicate learned ability");
|
|
Map<String, Instant> cooldowns = new LinkedHashMap<>();
|
|
for (Object value : list(root.get("cooldowns"))) {
|
|
Map<?, ?> row = object(value);
|
|
if (cooldowns.putIfAbsent(text(row.get("ability")), instant(row.get("ready-at"))) != null) {
|
|
throw new IllegalArgumentException("Duplicate cooldown");
|
|
}
|
|
}
|
|
if (!(root.get("dead") instanceof Boolean dead)) throw new IllegalArgumentException("Missing dead flag");
|
|
return new CharacterRecord(UUID.fromString(text(root.get("character-id"))), text(root.get("name")),
|
|
number(root.get("revision")), stats, number(root.get("level")).intValueExact(),
|
|
number(root.get("experience")), number(root.get("unspent-points")),
|
|
new BigDecimal(text(root.get("health"))), learned, strings(root.get("loadout")), cooldowns,
|
|
dead, root.containsKey("death-id") ? UUID.fromString(text(root.get("death-id"))) : null,
|
|
root.containsKey("died-at") ? instant(root.get("died-at")) : null);
|
|
}
|
|
|
|
private static Map<?, ?> object(Object value) {
|
|
if (value instanceof Map<?, ?> map) return map;
|
|
throw new IllegalArgumentException("Expected YAML mapping");
|
|
}
|
|
|
|
private static List<?> list(Object value) {
|
|
if (value instanceof List<?> result) return result;
|
|
throw new IllegalArgumentException("Expected YAML list");
|
|
}
|
|
|
|
private static List<String> strings(Object value) {
|
|
return list(value).stream().map(CharacterYaml::text).toList();
|
|
}
|
|
|
|
private static String text(Object value) {
|
|
if (value instanceof String string) return string;
|
|
throw new IllegalArgumentException("Expected YAML string");
|
|
}
|
|
|
|
private static BigInteger number(Object value) {
|
|
if (value instanceof Integer || value instanceof Long || value instanceof BigInteger) {
|
|
return new BigInteger(value.toString());
|
|
}
|
|
return new BigInteger(text(value));
|
|
}
|
|
|
|
private static Instant instant(Object value) {
|
|
String string = text(value);
|
|
if (!string.endsWith("Z")) throw new IllegalArgumentException("Expected UTC timestamp");
|
|
return Instant.parse(string);
|
|
}
|
|
}
|