Skip to content

Commit

Permalink
🐛 fix monster variants
Browse files Browse the repository at this point in the history
- use versions of monsters from source data
- remove notes per conjured spell level
- look for the most specific version key for determining reprints
  • Loading branch information
ebullient committed Dec 17, 2024
1 parent 6cf0059 commit 5d92921
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 295 deletions.
21 changes: 18 additions & 3 deletions src/main/java/dev/ebullient/convert/tools/JsonNodeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,16 @@ default ArrayNode ensureArrayIn(JsonNode target) {
return target.withArray(this.nodeName());
}

default JsonNode copyFrom(JsonNode source) {
return getFrom(source).deepCopy();
}

/** Destructive! */
default void removeFrom(JsonNode target) {
default JsonNode removeFrom(JsonNode target) {
if (target == null) {
return;
return null;
}
((ObjectNode) target).remove(this.nodeName());
return ((ObjectNode) target).remove(this.nodeName());
}

/** Destructive! */
Expand Down Expand Up @@ -497,4 +501,15 @@ default void link(JsonNode source, JsonNode target) {
}
((ObjectNode) target).set(this.nodeName(), getFrom(source));
}

/** Destructive! */
default void moveFrom(JsonNode source, JsonNode target) {
if (source == null || target == null) {
return;
}
JsonNode value = removeFrom(source);
if (value != null) {
((ObjectNode) target).set(this.nodeName(), value);
}
}
}
29 changes: 24 additions & 5 deletions src/main/java/dev/ebullient/convert/tools/JsonSourceCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ public JsonNode handleCopy(T type, JsonNode copyTo) {
* @param _copy Node containing metadata about the copy
*/
protected void copyValues(T type, JsonNode copyFrom, ObjectNode copyTo, JsonNode _copy) {
JsonNode _preserve = MetaFields._preserve.getFromOrEmptyObjectNode(_copy);
JsonNode _preserve = MetaFields._preserve.getFrom(_copy); // null is ok
// Copy required values from...
for (Entry<String, JsonNode> from : iterableFields(copyFrom)) {
String k = from.getKey();
JsonNode copyToField = copyTo.get(k);
if (copyToField != null && copyToField.isNull()) {
// copyToField exists as `null`. Remove the field.
// copyToField is present / exists as an intentional `null`. Remove the field.
copyTo.remove(k);
continue;
}
if (copyToField == null) {
if (copyToField == null) { // undefined
// not already present in copyTo -- should we copyFrom?
// Do merge rules indicate the value should be preserved
if (mergePreserveKey(type, k)) {
Expand Down Expand Up @@ -166,7 +166,7 @@ protected void doModProp(
case setProps -> doSetProps(originKey, modInfo, prop, target);
case prefixSuffixStringProp -> doPrefixSuffixStringProp(originKey, modInfo, prop, target);
// Arrays
case prependArr, appendArr, replaceArr, replaceOrAppendArr, appendIfNotExistsArr, insertArr, removeArr ->
case prependArr, appendArr, renameArr, replaceArr, replaceOrAppendArr, appendIfNotExistsArr, insertArr, removeArr ->
doModArray(originKey, mode, modInfo, prop, target);
// MATH
case scalarAddProp -> doScalarAddProp(originKey, modInfo, prop, target);
Expand Down Expand Up @@ -444,7 +444,7 @@ private void doModArray(String originKey, ModFieldMode mode, JsonNode modInfo, S
String propPath = nodePath(prop);

switch (mode) {
case insertArr, removeArr, replaceArr -> {
case insertArr, removeArr, renameArr, replaceArr -> {
if (target.at(propPath).isMissingNode()) {
tui().errorf("Error (%s): Unable to %s; %s is not present: %s", originKey, mode, prop, target);
return;
Expand All @@ -464,6 +464,7 @@ private void doModArray(String originKey, ModFieldMode mode, JsonNode modInfo, S
MetaFields.index.intFrom(modInfo).filter(n -> n >= 0).orElse(targetArray.size()),
items);
case removeArr -> removeFromArray(originKey, modInfo, prop, targetArray);
case renameArr -> renameInArray(originKey, modInfo, targetArray);
case replaceArr -> replaceArray(originKey, modInfo, targetArray, items);
case replaceOrAppendArr -> {
boolean didReplace = !targetArray.isEmpty() && replaceArray(originKey, modInfo, targetArray, items);
Expand Down Expand Up @@ -557,6 +558,21 @@ public void removeFromArr(ArrayNode tgtArray, JsonNode items) {
}
}

protected void renameInArray(String originKey, JsonNode modInfo, ArrayNode tgtArray) {
JsonNode renames = ensureArray(MetaFields.renames.getFrom(modInfo));
if (renames == null || !renames.isArray()) {
return;
}

for (JsonNode renameNode : iterableElements(renames)) {
int index = findIndexByName(originKey, tgtArray, MetaFields.rename.getTextOrEmpty(renameNode));
if (index >= 0) {
JsonNode element = tgtArray.get(index);
SourceField.name.setIn(element, MetaFields.with.getFrom(renameNode));
}
}
}

protected boolean replaceArray(String originKey, JsonNode modInfo, ArrayNode tgtArray, JsonNode items) {
if (items == null || !items.isArray()) {
return false;
Expand Down Expand Up @@ -657,6 +673,8 @@ public enum MetaFields implements JsonNodeReader {
props,
range,
regex,
rename,
renames,
replace,
root,
scalar,
Expand Down Expand Up @@ -696,6 +714,7 @@ public enum ModFieldMode implements JsonNodeReader.FieldValue {

prependArr,
appendArr,
renameArr,
replaceArr,
replaceOrAppendArr,
appendIfNotExistsArr,
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/dev/ebullient/convert/tools/ToolsIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
public interface ToolsIndex {
// Special one-offs for accounting/tracking
enum TtrpgValue implements JsonNodeReader {
indexKey,
indexInputType,
indexBaseItem,
isHomebrew,
indexFluffKey,
indexInputType,
indexKey,
indexParentKey,
indexVersionKeys,
isHomebrew,
}

static ToolsIndex createIndex() {
Expand Down
Loading

0 comments on commit 5d92921

Please sign in to comment.