Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Pf2e creature support #447

Merged
merged 14 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.ebullient.convert.qute;

import io.quarkus.qute.TemplateExtension;

@TemplateExtension
public class TtrpgTemplateExtension {
/** Return the value formatted with a bonus with a +/- prefix */
static String asBonus(Integer value) {
return String.format("%+d", value);
}

/** Return the string capitalized */
static String capitalized(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
}
25 changes: 25 additions & 0 deletions src/main/java/dev/ebullient/convert/tools/JsonNodeReader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.ebullient.convert.tools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -113,6 +114,14 @@ default JsonNode getFrom(JsonNode source) {
return source.get(this.nodeName());
}

/**
* Return an optional of the object at this key in the node, or an empty optional if the key does not exist or is not an
* object.
*/
default Optional<JsonNode> getObjectFrom(JsonNode source) {
return this.isObjectIn(source) ? Optional.of(this.getFrom(source)) : Optional.empty();
}

default JsonNode getFromOrEmptyObjectNode(JsonNode source) {
if (source == null) {
return Tui.MAPPER.createObjectNode();
Expand Down Expand Up @@ -203,6 +212,13 @@ default String getTextOrThrow(JsonNode x) {
return text;
}

default Optional<String> getTextFrom(JsonNode x) {
if (x != null && existsIn(x) && getFrom(x).isTextual()) {
return Optional.of(getFrom(x).asText());
}
return Optional.empty();
}

default int intOrDefault(JsonNode source, int value) {
JsonNode result = getFrom(source);
return result == null || result.isNull() ? value : result.asInt();
Expand Down Expand Up @@ -256,6 +272,15 @@ default Stream<JsonNode> streamFrom(JsonNode source) {
return StreamSupport.stream(result.spliterator(), false);
}

default Stream<Entry<String, JsonNode>> streamPropsExcluding(JsonNode source, JsonNodeReader... excludingKeys) {
JsonNode result = getFrom(source);
if (result == null || !result.isObject()) {
return Stream.of();
}
return result.properties().stream()
.filter(e -> Arrays.stream(excludingKeys).noneMatch(s -> e.getKey().equalsIgnoreCase(s.name())));
}

default String transformTextFrom(JsonNode source, String join, JsonTextConverter<?> replacer) {
JsonNode target = getFrom(source);
if (target == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ default void renderInlineTemplate(List<String> text, QuteBase resource, String a

String replaceText(String s);

default String replaceText(JsonNode input) {
if (input == null) {
return null;
}
if (input.isObject() || input.isArray()) {
throw new IllegalArgumentException("Can only replace text for textual nodes: " + input);
}
return replaceText(input.asText());
}

default String slugify(String s) {
return Tui.slugify(s);
}
Expand Down Expand Up @@ -520,6 +530,14 @@ default Stream<String> streamOfFieldNames(JsonNode source) {
return StreamSupport.stream(iterableFieldNames(source).spliterator(), false);
}

default Stream<Entry<String, JsonNode>> streamPropsExcluding(JsonNode source, JsonNodeReader... excludingKeys) {
if (source == null || !source.isObject()) {
return Stream.of();
}
return source.properties().stream()
.filter(e -> Arrays.stream(excludingKeys).noneMatch(s -> e.getKey().equalsIgnoreCase(s.name())));
}

default String toAnchorTag(String x) {
return Tui.toAnchorTag(x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ default String joinAndReplace(ArrayNode array) {
return String.join(", ", list);
}

default String replaceText(JsonNode input) {
if (input == null) {
return null;
}
if (input.isObject() || input.isArray()) {
throw new IllegalArgumentException("Can only replace text for textual nodes: " + input);
}
return replaceText(input.asText());
}

default String replaceText(String input) {
return replaceTokens(input, (s, b) -> this._replaceTokenText(s, b));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ public Pf2eSources getSources() {
return sources;
}

List<String> toAlignments(JsonNode alignNode, JsonNodeReader alignmentField) {
return alignmentField.getListOfStrings(alignNode, tui()).stream()
.map(a -> a.length() > 2 ? a : linkify(Pf2eIndexType.trait, a.toUpperCase()))
.collect(Collectors.toList());
}

public Pf2eQuteBase build() {
boolean pushed = parseState().push(getSources(), rootNode);
try {
Expand Down
189 changes: 111 additions & 78 deletions src/main/java/dev/ebullient/convert/tools/pf2e/Json2QuteCreature.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;

Expand All @@ -26,108 +27,70 @@ protected QuteCreature buildQuteResource() {
appendToText(text, SourceField.entries.getFrom(rootNode), "##");

Collection<String> traits = collectTraitsFrom(rootNode, tags);
if (Pf2eCreature.alignment.existsIn(rootNode)) {
traits.addAll(toAlignments(rootNode, Pf2eCreature.alignment));
}
Optional<Integer> level = Pf2eCreature.level.getIntFrom(rootNode);
traits.addAll(getAlignments(Pf2eCreature.alignment.getFrom(rootNode)));

return new QuteCreature(sources, text, tags,
traits,
Field.alias.replaceTextFromList(rootNode, this),
Pf2eCreature.description.replaceTextFrom(rootNode, this),
level.orElse(null),
getPerception(),
buildDefenses(),
Pf2eCreatureLanguages.createCreatureLanguages(Pf2eCreature.languages.getFrom(rootNode), this),
buildSkills());
Pf2eCreature.level.getIntFrom(rootNode).orElse(null),
Pf2eCreature.perception(rootNode),
Pf2eCreature.defenses(rootNode, this),
Pf2eCreatureLanguages.create(Pf2eCreature.languages.getFrom(rootNode), this),
Pf2eCreature.skills(rootNode, this),
Pf2eCreature.senses.streamFrom(rootNode).map(n -> Pf2eCreatureSense.create(n, this)).toList(),
Pf2eCreature.abilityModifiers(rootNode),
Pf2eCreature.items.replaceTextFromList(rootNode, this),
Pf2eTypeReader.Pf2eSpeed.getSpeed(Pf2eCreature.speed.getFrom(rootNode), this));
}

/**
* Example JSON input:
*
* <pre>
* "perception": {
* "std": 6
* "languages": {
* "languages": ["Common", "Sylvan"],
* "abilities": ["{&#64;ability telepathy} 100 feet"],
* "notes": ["some other notes"],
* }
* </pre>
*/
private Integer getPerception() {
JsonNode perceptionNode = Pf2eCreature.perception.getFrom(rootNode);
if (perceptionNode == null || !perceptionNode.isObject()) {
return null;
}
return Pf2eCreature.std.getIntOrThrow(perceptionNode);
}
enum Pf2eCreatureLanguages implements JsonNodeReader {
languages,
abilities,
notes;

/**
* Example JSON input:
*
* <pre>
* "defenses": { ... }
* </pre>
*/
private QuteDataDefenses buildDefenses() {
JsonNode defenseNode = Pf2eCreature.defenses.getFrom(rootNode);
if (defenseNode == null || !defenseNode.isObject()) {
return null;
static QuteCreature.CreatureLanguages create(JsonNode node, Pf2eTypeReader convert) {
return node == null ? null
: new QuteCreature.CreatureLanguages(
languages.getListOfStrings(node, convert.tui()),
abilities.replaceTextFromList(node, convert),
notes.replaceTextFromList(node, convert));
}
return Pf2eDefenses.createInlineDefenses(defenseNode, this);
}

/**
* Example JSON input:
*
* <pre>
* "skills": {
* "athletics": 30,
* "stealth": {
* "std": 36,
* "in forests": 42,
* "note": "additional note"
* },
* "notes": [
* "some note"
* ]
* {
* "name": "scent",
* "type": "imprecise",
* "range": 60,
* }
* </pre>
*/
private QuteCreature.CreatureSkills buildSkills() {
JsonNode skillsNode = Pf2eCreature.skills.getFrom(rootNode);
if (skillsNode == null || !skillsNode.isObject()) {
return null;
}
return new QuteCreature.CreatureSkills(
skillsNode.properties().stream()
.filter(e -> !e.getKey().equals(Pf2eCreature.notes.name()))
.map(e -> Pf2eTypeReader.Pf2eSkillBonus.createSkillBonus(e.getKey(), e.getValue(), this))
.toList(),
Pf2eCreature.notes.replaceTextFromList(rootNode, this));
}
enum Pf2eCreatureSense implements JsonNodeReader {
name,
type,
range;

/**
* Example JSON input:
*
* <pre>
* "languages": {
* "languages": ["Common", "Sylvan"],
* "abilities": ["{&#64;ability telepathy} 100 feet"],
* "notes": ["some other notes"],
* }
* </pre>
*/
enum Pf2eCreatureLanguages implements JsonNodeReader {
languages,
abilities,
notes;

static QuteCreature.CreatureLanguages createCreatureLanguages(JsonNode node, Pf2eTypeReader convert) {
if (node == null) {
return null;
}
return new QuteCreature.CreatureLanguages(
languages.getListOfStrings(node, convert.tui()),
abilities.getListOfStrings(node, convert.tui()).stream().map(convert::replaceText).toList(),
notes.getListOfStrings(node, convert.tui()).stream().map(convert::replaceText).toList());
static QuteCreature.CreatureSense create(JsonNode node, Pf2eTypeReader convert) {
return node == null ? null
: new QuteCreature.CreatureSense(
name.getTextFrom(node).map(convert::replaceText).orElseThrow(),
type.getTextFrom(node).map(convert::replaceText).orElse(null),
range.getIntFrom(node).orElse(null));
}
}

Expand All @@ -154,6 +117,76 @@ enum Pf2eCreature implements JsonNodeReader {
speed,
spellcasting,
std,
traits,
traits;

/**
* Example JSON input:
*
* <pre>
* "perception": {
* "std": 6
* }
* </pre>
*/
private static Integer perception(JsonNode source) {
return perception.getObjectFrom(source).map(std::getIntOrThrow).orElse(null);
}

/**
* Example JSON input:
*
* <pre>
* "defenses": { ... }
* </pre>
*/
private static QuteDataDefenses defenses(JsonNode source, Pf2eTypeReader convert) {
return defenses.getObjectFrom(source).map(n -> Pf2eDefenses.createInlineDefenses(n, convert)).orElse(null);
}

/**
* Example JSON input:
*
* <pre>
* "skills": {
* "athletics": 30,
* "stealth": {
* "std": 36,
* "in forests": 42,
* "note": "additional note"
* },
* "notes": [
* "some note"
* ]
* }
* </pre>
*/
private static QuteCreature.CreatureSkills skills(JsonNode source, Pf2eTypeReader convert) {
return new QuteCreature.CreatureSkills(
skills.streamPropsExcluding(source, notes)
.map(e -> Pf2eTypeReader.Pf2eSkillBonus.createSkillBonus(e.getKey(), e.getValue(), convert))
.toList(),
notes.replaceTextFromList(source, convert));
}

/**
* Example JSON input:
*
* <pre>
* {
* "str": 10,
* "dex": 10,
* "con": 10,
* "int": 10,
* "wis": 10,
* "cha": 10
* }
* </pre>
*/
private static Map<String, Integer> abilityModifiers(JsonNode source) {
// Use a linked hash map to preserve insertion order
Map<String, Integer> mods = new LinkedHashMap<>();
abilityMods.streamPropsExcluding(source).forEachOrdered(e -> mods.put(e.getKey(), e.getValue().asInt()));
return mods;
}
}
}
Loading
Loading