Skip to content

Commit

Permalink
🎨 Rename some methods in Pf2eJsonNodeReader
Browse files Browse the repository at this point in the history
  • Loading branch information
miscoined authored and ebullient committed Jun 2, 2024
1 parent 702b7ee commit 0ed8833
Showing 1 changed file with 60 additions and 56 deletions.
116 changes: 60 additions & 56 deletions src/main/java/dev/ebullient/convert/tools/pf2e/Pf2eJsonNodeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@
import dev.ebullient.convert.tools.pf2e.qute.QuteDataSpeed;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
Expand All @@ -44,7 +39,22 @@ default List<String> getAlignmentsFrom(JsonNode alignNode, JsonSource convert) {

/** Return a {@link QuteDataSpeed} read from this field of the {@code source} node, or null. */
default QuteDataSpeed getSpeedFrom(JsonNode source, JsonSource convert) {
return getObjectFrom(source).map(n -> Pf2eSpeed.read(n, convert)).orElse(null);
return getObjectFrom(source).map(n -> Pf2eSpeed.getSpeed(n, convert)).orElse(null);
}

/** Return a {@link QuteDataFrequency} read from {@code source}, or null. */
default QuteDataFrequency getFrequencyFrom(JsonNode source, JsonSource convert) {
return getObjectFrom(source).map(n -> Pf2eFrequency.getFrequency(n, convert)).orElse(null);
}

/** Return a {@link QuteDataDefenses} read from this field in {@code source}, or null. */
default QuteDataDefenses getDefensesFrom(JsonNode source, Pf2eTypeReader convert) {
return getObjectFrom(source).map(n -> Pf2eDefenses.getDefenses(n, convert)).orElse(null);
}

/** Return a {@link QuteDataActivity} read from this field in {@code source}, or null */
default QuteDataActivity getActivityFrom(JsonNode source, JsonSource convert) {
return getObjectFrom(source).map(n -> Pf2eNumberUnitEntry.getActivity(n, convert)).orElse(null);
}

/**
Expand All @@ -65,7 +75,7 @@ enum Pf2eSpeed implements Pf2eJsonNodeReader {
abilities;

/** Read a {@link QuteDataSpeed} from the {@code source} node. */
private static QuteDataSpeed read(JsonNode source, JsonSource convert) {
private static QuteDataSpeed getSpeed(JsonNode source, JsonSource convert) {
return new QuteDataSpeed(
walk.getIntFrom(source).orElse(null),
convert.streamPropsExcluding(source, speedNote, abilities)
Expand All @@ -80,16 +90,6 @@ private static QuteDataSpeed read(JsonNode source, JsonSource convert) {
}
}

/** Return a {@link QuteDataFrequency} read from {@code source}, or null. */
default QuteDataFrequency getFrequencyFrom(JsonNode source, JsonSource convert) {
return getObjectFrom(source).map(n -> Pf2eFrequency.read(n, convert)).orElse(null);
}

/** Return a {@link QuteDataDefenses} read from this field in {@code source}, or null. */
default QuteDataDefenses getDefensesFrom(JsonNode source, Pf2eTypeReader convert) {
return getObjectFrom(source).map(n -> Pf2eDefenses.readDefenses(n, convert)).orElse(null);
}

/**
* A {@link Pf2eJsonNodeReader} which reads JSON like the following:
*
Expand Down Expand Up @@ -131,7 +131,7 @@ enum Pf2eFrequency implements Pf2eJsonNodeReader {
customUnit;

/** Return a {@link QuteDataFrequency} read from {@code node}. */
private static QuteDataFrequency read(JsonNode node, JsonSource convert) {
private static QuteDataFrequency getFrequency(JsonNode node, JsonSource convert) {
if (special.getTextFrom(node).isPresent()) {
return new QuteDataFrequency(special.replaceTextFrom(node, convert));
}
Expand Down Expand Up @@ -191,12 +191,12 @@ enum Pf2eDefenses implements Pf2eJsonNodeReader {
* "notes": { "some key": "some value" }
* </pre>
*/
private static QuteDataDefenses readDefenses(JsonNode source, Pf2eTypeReader convert) {
private static QuteDataDefenses getDefenses(JsonNode source, JsonSource convert) {
if (notes.existsIn(source)) {
convert.tui().warnf("Defenses has notes: %s", source.toString());
}

Map<String, QuteDataHpHardnessBt> hpHardnessBt = readHpHardnessBt(source, convert);
Map<String, QuteDataHpHardnessBt> hpHardnessBt = getHpHardnessBt(source, convert);

return new QuteDataDefenses(
ac.getObjectFrom(source)
Expand Down Expand Up @@ -258,29 +258,28 @@ private static QuteDataDefenses readDefenses(JsonNode source, Pf2eTypeReader con
* ],
* </pre>
*/
private static Map<String, QuteDataHpHardnessBt> readHpHardnessBt(JsonNode source, Pf2eTypeReader convert) {
private static Map<String, QuteDataHpHardnessBt> getHpHardnessBt(JsonNode source, JsonSource convert) {
Map<String, QuteDataHpHardnessBt.HpStat> hpStats = hp.getHpFrom(source, convert);
JsonNode btNode = bt.getFromOrEmptyObjectNode(source);
JsonNode hardnessNode = hardness.getFromOrEmptyObjectNode(source);
Map<String, String> hardnessNotes = notes.getMapOfStrings(hardnessNode, convert.tui());

// Collect distinct names from the field names of the stat objects
return Stream.of(
Stream<String> names = Stream.of(
hpStats.keySet().stream(),
bt.streamPropsExcluding(source, abilities, notes).map(Map.Entry::getKey),
hardness.streamPropsExcluding(source, abilities, notes).map(Map.Entry::getKey))
.flatMap(Function.identity())
.distinct()
// Map each known name to the known stats for that name
.collect(Collectors.toMap(
String::trim,
k -> new QuteDataHpHardnessBt(
hpStats.getOrDefault(k, null),
hardnessNode.has(k)
? new QuteDataGenericStat.SimpleStat(
hardnessNode.get(k).asInt(), convert.replaceText(hardnessNotes.get(k)))
: null,
btNode.has(k) ? btNode.get(k).asInt() : null)));
.distinct();
// Map each known name to the known stats for that name
return names.collect(Collectors.toMap(
String::trim,
k -> new QuteDataHpHardnessBt(
hpStats.getOrDefault(k, null),
hardnessNode.has(k)
? new QuteDataGenericStat.SimpleStat(
hardnessNode.get(k).asInt(), convert.replaceText(hardnessNotes.get(k)))
: null,
btNode.has(k) ? btNode.get(k).asInt() : null)));
}

/**
Expand All @@ -302,7 +301,7 @@ private static Map<String, QuteDataHpHardnessBt> readHpHardnessBt(JsonNode sourc
* "abilities": ["some saving throw ability"]
* </pre>
*/
private QuteDataDefenses.QuteSavingThrows getSavingThrowsFrom(JsonNode source, Pf2eTypeReader convert) {
private QuteDataDefenses.QuteSavingThrows getSavingThrowsFrom(JsonNode source, JsonSource convert) {
JsonNode stNode = getObjectFrom(source).orElse(null);
if (stNode == null) {
return null;
Expand Down Expand Up @@ -340,11 +339,11 @@ private QuteDataDefenses.QuteSavingThrows getSavingThrowsFrom(JsonNode source, P
* Returns a map of names to {@link QuteDataHpHardnessBt.HpStat} from this field of {@code source}, or an empty
* map.
*/
private Map<String, QuteDataHpHardnessBt.HpStat> getHpFrom(JsonNode source, Pf2eTypeReader convert) {
private Map<String, QuteDataHpHardnessBt.HpStat> getHpFrom(JsonNode source, JsonSource convert) {
// We need to do HP mapping separately because creature and hazard HP are structured differently
return isArrayIn(source)
? Pf2eHpStat.readFromArray(ensureArrayIn(source), convert)
: Pf2eHpStat.readFromObject(getFromOrEmptyObjectNode(source), convert);
? Pf2eHpStat.getHpMapFromArray(ensureArrayIn(source), convert)
: Pf2eHpStat.getHpMapFromObject(getFromOrEmptyObjectNode(source), convert);
}

}
Expand Down Expand Up @@ -375,8 +374,8 @@ enum Pf2eHpStat implements Pf2eJsonNodeReader {
* [{"hp": 10, "abilities": ["some ability"], "notes": ["some note"]}]
* </pre>
*/
private static Map<String, QuteDataHpHardnessBt.HpStat> readFromArray(
JsonNode source, Pf2eTypeReader convert) {
private static Map<String, QuteDataHpHardnessBt.HpStat> getHpMapFromArray(
JsonNode source, JsonSource convert) {
return convert.streamOf(convert.ensureArray(source)).collect(Collectors.toMap(
n -> Pf2eHpStat.name.getTextFrom(n).map(StringUtil::toTitleCase).orElse(std.name()),
n -> new QuteDataHpHardnessBt.HpStat(
Expand All @@ -397,17 +396,26 @@ private static Map<String, QuteDataHpHardnessBt.HpStat> readFromArray(
* }
* </pre>
*/
private static Map<String, QuteDataHpHardnessBt.HpStat> readFromObject(
JsonNode source, Pf2eTypeReader convert) {
JsonNode notesNode = notes.getFromOrEmptyObjectNode(source);
private static Map<String, QuteDataHpHardnessBt.HpStat> getHpMapFromObject(
JsonNode source, JsonSource convert) {
Map<String, String> noteMap = notes.getMapOfStrings(source, convert.tui());
return convert.streamPropsExcluding(convert.ensureObjectNode(source), notes).collect(Collectors.toMap(
Map.Entry::getKey,
e -> new QuteDataHpHardnessBt.HpStat(
e.getValue().asInt(),
notesNode.has(e.getKey()) ? convert.replaceText(notesNode.get(e.getKey())) : null)));
convert.replaceText(noteMap.get(e.getKey())))));
}
}

/**
* A {@link Pf2eJsonNodeReader} which reads JSON structured like the following:
*
* <pre>
* "name": "physical",
* "amount": 5,
* "note": "except bludgeoning"
* </pre>
*/
enum Pf2eNameAmountNote implements Pf2eJsonNodeReader {
name,
amount,
Expand All @@ -424,11 +432,6 @@ enum Pf2eNameAmountNote implements Pf2eJsonNodeReader {
}
}

/** Return a {@link QuteDataActivity} read from this field in {@code source}, or null */
default QuteDataActivity getActivityFrom(JsonNode source, JsonSource convert) {
return getObjectFrom(source).map(n -> Pf2eNumberUnitEntry.readActivity(n, convert)).orElse(null);
}

/**
* A {@link Pf2eJsonNodeReader} which reads JSON input structured like the following:
*
Expand Down Expand Up @@ -456,14 +459,8 @@ enum Pf2eNumberUnitEntry implements Pf2eJsonNodeReader {
* {"number": 1, "unit": "varies", "entry": "{&#64;as 3} command,"}
* </pre>
*/
private static QuteDataActivity readActivity(JsonNode node, JsonSource convert) {
private static QuteDataActivity getActivity(JsonNode node, JsonSource convert) {
String actionType = unit.getTextOrNull(node);
String extra = entry.getTextFrom(node)
.filter(s -> !s.toLowerCase().contains("varies"))
.filter(Predicate.not(String::isBlank))
.map(convert::replaceText).map(StringUtil::parenthesize)
.orElse("");

Pf2eActivity activity = switch (actionType) {
case "single", "action", "free", "reaction" ->
Pf2eActivity.toActivity(actionType, number.getIntOrThrow(node));
Expand All @@ -475,6 +472,13 @@ private static QuteDataActivity readActivity(JsonNode node, JsonSource convert)
if (activity == null) {
throw new IllegalArgumentException("Can't parse activity from: %s".formatted(node));
}

String extra = entry.getTextFrom(node)
.filter(s -> !s.toLowerCase().contains("varies"))
.filter(Predicate.not(String::isBlank))
.map(convert::replaceText).map(StringUtil::parenthesize)
.orElse("");

return activity.toQuteActivity(
convert, activity == Pf2eActivity.timed ? join(" ", number.getIntOrThrow(node), actionType, extra) : extra);
}
Expand Down

0 comments on commit 0ed8833

Please sign in to comment.