155 lines
8.2 KiB
Java
155 lines
8.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. */
|
|
public 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));
|
|
}
|
|
|
|
public 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());
|
|
root.put("sheet-generation", record.sheetGeneration().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());
|
|
root.put("learned-passives", record.passives());
|
|
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);
|
|
List<Map<String, String>> restrictedKills = new ArrayList<>();
|
|
record.restrictedKills().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
|
|
var position = entry.getValue();
|
|
restrictedKills.add(Map.of("world", entry.getKey().toString(), "x", position.x().toString(),
|
|
"y", position.y().toString(), "z", position.z().toString()));
|
|
});
|
|
root.put("restricted-kills", restrictedKills);
|
|
root.put("coins-initialized", record.coinsInitialized());
|
|
root.put("coins", record.coins().toString());
|
|
root.put("reward-receipts", new ArrayList<>(record.rewardReceipts().stream().sorted().map(UUID::toString).toList()));
|
|
root.put("pvp-coin-escrow", record.pvpCoinEscrow().toString());
|
|
root.put("gear",Map.of("chestplate",record.gear().chestplate().name(),"leggings",record.gear().leggings().name(),"boots",record.gear().boots().name()));
|
|
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);
|
|
}
|
|
|
|
public 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");
|
|
}
|
|
}
|
|
Map<UUID, games.dmg.fantasystick.progression.CombatRewardRules.KillPosition> restrictedKills = new LinkedHashMap<>();
|
|
for (Object value : root.containsKey("restricted-kills") ? list(root.get("restricted-kills")) : List.of()) {
|
|
Map<?, ?> row = object(value);
|
|
UUID world = UUID.fromString(text(row.get("world")));
|
|
var position = new games.dmg.fantasystick.progression.CombatRewardRules.KillPosition(world,
|
|
new BigDecimal(text(row.get("x"))), new BigDecimal(text(row.get("y"))), new BigDecimal(text(row.get("z"))));
|
|
if (restrictedKills.putIfAbsent(world, position) != null) throw new IllegalArgumentException("Duplicate restricted-kill world");
|
|
}
|
|
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")),
|
|
root.containsKey("learned-passives") ? strings(root.get("learned-passives")) : List.of("", "", ""),
|
|
cooldowns, restrictedKills, dead, root.containsKey("death-id") ? UUID.fromString(text(root.get("death-id"))) : null,
|
|
root.containsKey("died-at") ? instant(root.get("died-at")) : null,
|
|
root.containsKey("coins") ? number(root.get("coins")) : BigInteger.ZERO,
|
|
root.containsKey("coins-initialized") && bool(root.get("coins-initialized")),
|
|
root.containsKey("reward-receipts") ? strings(root.get("reward-receipts")).stream()
|
|
.map(UUID::fromString).collect(java.util.stream.Collectors.toUnmodifiableSet()) : java.util.Set.of(),
|
|
root.containsKey("pvp-coin-escrow") ? number(root.get("pvp-coin-escrow")) : BigInteger.ZERO,
|
|
root.containsKey("gear")?gear(root.get("gear")):CharacterGear.none(),
|
|
root.containsKey("sheet-generation") ? number(root.get("sheet-generation")) : BigInteger.ZERO);
|
|
}
|
|
|
|
private static CharacterGear gear(Object value){var gear=object(value);return new CharacterGear(GearTier.valueOf(text(gear.get("chestplate"))),GearTier.valueOf(text(gear.get("leggings"))),GearTier.valueOf(text(gear.get("boots"))));}
|
|
|
|
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 boolean bool(Object value) {
|
|
if (value instanceof Boolean result) return result;
|
|
throw new IllegalArgumentException("Expected YAML boolean");
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|