Skip to content

Commit

Permalink
🐛 Add abilities to Pf2e creatures
Browse files Browse the repository at this point in the history
- ♻️ Use AppendTypeValue to determine the type of nested blocks
  instead of isAfflictionBlock
  • Loading branch information
miscoined committed Jun 16, 2024
1 parent 8e01f95 commit 438eef2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public enum Pf2eAffliction implements Pf2eJsonNodeReader {
entries,
entry;

public static boolean isAfflictionBlock(JsonNode node) {
return type.getTextFrom(node).map(s -> s.equals("affliction")).orElse(false);
}

/**
* Example JSON input, with an embedded affliction that does not have nested affliction data:
*
Expand Down Expand Up @@ -141,7 +137,7 @@ private static QuteAffliction createAffliction(
nestedAfflictionNode.isEmpty()
? List.of()
: entries.streamFrom(node)
.filter(Predicate.not(Pf2eAffliction::isAfflictionBlock))
.filter(Predicate.not(AppendTypeValue.affliction::isBlockTypeOf))
.collect(
ArrayList<String>::new,
(acc, n) -> convert.appendToText(acc, n, "##"),
Expand Down Expand Up @@ -195,7 +191,7 @@ private static JsonNode getNestedAffliction(JsonNode node) {
return null;
}
List<JsonNode> topLevelAfflictions = Pf2eAffliction.entries.streamFrom(node)
.filter(Pf2eAffliction::isAfflictionBlock).toList();
.filter(AppendTypeValue.affliction::isBlockTypeOf).toList();
if (topLevelAfflictions.size() != 1) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;

import dev.ebullient.convert.tools.Tags;
import dev.ebullient.convert.tools.pf2e.Json2QuteAffliction.Pf2eAffliction;
import dev.ebullient.convert.tools.pf2e.qute.QuteAbilityOrAffliction;
import dev.ebullient.convert.tools.pf2e.qute.QuteCreature;

public class Json2QuteCreature extends Json2QuteBase {
Expand Down Expand Up @@ -146,35 +143,16 @@ enum Pf2eCreatureLanguages implements Pf2eJsonNodeReader {
private QuteCreature.CreatureAbilities getCreatureAbilitiesFrom(JsonNode source, JsonSource convert) {
return getObjectFrom(source)
.map(n -> new QuteCreature.CreatureAbilities(
Pf2eCreatureAbilities.top.getAbilitiesFrom(n, convert),
Pf2eCreatureAbilities.mid.getAbilitiesFrom(n, convert),
Pf2eCreatureAbilities.bot.getAbilitiesFrom(n, convert)))
Pf2eCreatureAbilities.top.getAbilityOrAfflictionsFrom(n, convert),
Pf2eCreatureAbilities.mid.getAbilityOrAfflictionsFrom(n, convert),
Pf2eCreatureAbilities.bot.getAbilityOrAfflictionsFrom(n, convert)))
.orElseGet(() -> new QuteCreature.CreatureAbilities(List.of(), List.of(), List.of()));
}

enum Pf2eCreatureAbilities implements Pf2eJsonNodeReader {
top,
mid,
bot;

/**
* Example JSON input:
*
* <pre>
* [
* { &lt;ability data&gt; },
* { "type": "affliction", &lt;affliction data&gt; }
* ]
* </pre>
*/
private List<QuteAbilityOrAffliction> getAbilitiesFrom(JsonNode node, JsonSource convert) {
// The Pf2e schema doesn't match the data here - afflictions are marked with "type": "affliction", but
// abilities are unmarked.
return streamFrom(node)
.filter(Pf2eAffliction::isAfflictionBlock) // for now, we only want afflictions
.map(n -> (QuteAbilityOrAffliction) Pf2eAffliction.createInlineAffliction(n, convert))
.toList();
}
}

private List<QuteCreature.CreatureSense> getSensesFrom(JsonNode source, JsonSource convert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import dev.ebullient.convert.tools.JsonTextConverter;
import dev.ebullient.convert.tools.Tags;
import dev.ebullient.convert.tools.pf2e.Json2QuteAbility.Pf2eAbility;
import dev.ebullient.convert.tools.pf2e.Json2QuteAffliction.Pf2eAffliction;
import dev.ebullient.convert.tools.pf2e.qute.QuteAbilityOrAffliction;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataGenericStat;
import dev.ebullient.convert.tools.pf2e.qute.QuteHazard;

Expand Down Expand Up @@ -37,11 +35,7 @@ protected QuteHazard buildQuteResource() {
Pf2eHazard.abilities.streamFrom(rootNode)
.map(n -> Pf2eAbility.createEmbeddedAbility(n, this))
.toList(),
Pf2eHazard.actions.streamFrom(rootNode)
.map(n -> Pf2eAffliction.isAfflictionBlock(n)
? Pf2eAffliction.createInlineAffliction(n, this)
: (QuteAbilityOrAffliction) Pf2eAbility.createEmbeddedAbility(n, this))
.toList(),
Pf2eHazard.actions.getAbilityOrAfflictionsFrom(rootNode, this),
Pf2eHazard.stealth.getObjectFrom(rootNode)
.map(n -> Pf2eHazardAttribute.buildStealth(n, this)).orElse(null),
Pf2eHazard.perception.getObjectFrom(rootNode)
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/dev/ebullient/convert/tools/pf2e/JsonSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

import dev.ebullient.convert.config.TtrpgConfig;
import dev.ebullient.convert.io.Tui;
import dev.ebullient.convert.qute.QuteUtil;
import dev.ebullient.convert.tools.JsonNodeReader;
import dev.ebullient.convert.tools.Tags;
import dev.ebullient.convert.tools.pf2e.Json2QuteAbility.Pf2eAbility;
import dev.ebullient.convert.tools.pf2e.Json2QuteAffliction.Pf2eAffliction;
Expand Down Expand Up @@ -77,7 +75,7 @@ default void appendToText(List<String> text, JsonNode node, String heading) {

/** Internal */
default void appendObjectToText(List<String> text, JsonNode node, String heading) {
AppendTypeValue type = AppendTypeValue.valueFrom(node, SourceField.type);
AppendTypeValue type = AppendTypeValue.getBlockType(node);
String source = SourceField.source.getTextOrEmpty(node);

// entriesOtherSource handled here.
Expand Down Expand Up @@ -724,14 +722,14 @@ public boolean matches(String value) {
return this.value().equals(value) || this.name().equalsIgnoreCase(value);
}

static AppendTypeValue valueFrom(JsonNode source, JsonNodeReader field) {
String textOrNull = field.getTextOrEmpty(source);
if (textOrNull.isEmpty()) {
return null;
}
return Stream.of(AppendTypeValue.values())
.filter((t) -> t.matches(textOrNull))
.findFirst().orElse(null);
/** Return the {@link AppendTypeValue} that {@code source} represents. */
static AppendTypeValue getBlockType(JsonNode source) {
return SourceField.type.getEnumValueFrom(source, AppendTypeValue.class);
}

/** Returns true if {@code node} is a block of this type. */
boolean isBlockTypeOf(JsonNode node) {
return this == getBlockType(node);
}
}
// enum Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import static dev.ebullient.convert.StringUtil.join;
import static dev.ebullient.convert.StringUtil.pluralize;
import static dev.ebullient.convert.StringUtil.toTitleCase;
import static java.util.Objects.requireNonNullElse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
Expand All @@ -20,6 +22,10 @@

import dev.ebullient.convert.StringUtil;
import dev.ebullient.convert.tools.JsonNodeReader;
import dev.ebullient.convert.tools.pf2e.Json2QuteAbility.Pf2eAbility;
import dev.ebullient.convert.tools.pf2e.Json2QuteAffliction.Pf2eAffliction;
import dev.ebullient.convert.tools.pf2e.JsonSource.AppendTypeValue;
import dev.ebullient.convert.tools.pf2e.qute.QuteAbilityOrAffliction;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataActivity;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataArmorClass;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataDefenses;
Expand Down Expand Up @@ -123,6 +129,21 @@ default List<String> getActivationComponentsFrom(JsonNode source, Set<String> tr
return rawComponents.stream().map(convert::replaceText).toList();
}

/** Return a list of {@link QuteAbilityOrAffliction} from this field in {@code source}, or an empty list. */
default List<QuteAbilityOrAffliction> getAbilityOrAfflictionsFrom(JsonNode source, JsonSource convert) {
return streamFrom(source)
.map(n -> switch (requireNonNullElse(AppendTypeValue.getBlockType(n), AppendTypeValue.ability)) {
case affliction -> (QuteAbilityOrAffliction) Pf2eAffliction.createInlineAffliction(n, convert);
case ability -> Pf2eAbility.createEmbeddedAbility(n, convert);
default -> {
convert.tui().debugf("Unexpected block type in %s", source.toPrettyString());
yield null;
}
})
.filter(Objects::nonNull)
.toList();
}

/**
* A {@link Pf2eJsonNodeReader} which reads JSON like the following:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
/**
* Pf2eTools Creature attributes ({@code creature2md.txt})
* <p>
* Use `%%--` to mark the end of the preamble (frontmatter and
* other leading content only appropriate to the standalone case).
* </p>
* <p>
* Extension of {@link Pf2eQuteBase Pf2eQuteBase}
* </p>
*/
Expand Down

0 comments on commit 438eef2

Please sign in to comment.