Skip to content

Commit

Permalink
Add more validation checks for illustrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Feb 14, 2024
1 parent 14a0f73 commit dd571e0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Expand Up @@ -18,5 +18,15 @@
package org.ladysnake.blabber.impl.common.model;

public enum ChoiceResult {
DEFAULT, END_DIALOGUE, ASK_CONFIRMATION
DEFAULT(true), END_DIALOGUE(false), ASK_CONFIRMATION(false);

private final boolean allowsIllustrations;

ChoiceResult(boolean allowsIllustrations) {
this.allowsIllustrations = allowsIllustrations;
}

public boolean allowsIllustrations() {
return this.allowsIllustrations;
}
}
Expand Up @@ -52,6 +52,10 @@ public static ValidationResult validateStructure(DialogueTemplate dialogue) {
);
}
}
// TODO Java 21 replace with pattern matching switch to handle all possible results
if (validateIllustrations(dialogue, state) instanceof ValidationResult.Error error) {
return error;
}
}

while (!waitList.isEmpty()) {
Expand Down Expand Up @@ -91,16 +95,31 @@ public static ValidationResult validateStructure(DialogueTemplate dialogue) {
// Verify that all illustrations are real. We're doing this here because this is a class and not a record
// So we have our own constructor.
for (Map.Entry<String, DialogueState> state : dialogue.states().entrySet()) {
for (String illustration : state.getValue().illustrations()) {
if (!dialogue.illustrations().containsKey(illustration)) {
return new ValidationResult.Error.NonexistentIllustration(state.getKey(), illustration);
}
}
}

return warnings.isEmpty() ? ValidationResult.success() : new ValidationResult.Warnings(warnings);
}

private static ValidationResult validateIllustrations(DialogueTemplate dialogue, Map.Entry<String, DialogueState> state) {
List<String> illustrations = new ArrayList<>(state.getValue().illustrations());

for (DialogueChoice c : state.getValue().choices()) {
illustrations.addAll(c.illustrations());
}

if (!illustrations.isEmpty() && !state.getValue().type().allowsIllustrations()) {
return new ValidationResult.Error.InvalidIllustratedState(state.getKey(), state.getValue().type(), illustrations);
}

for (String illustration : illustrations) {
if (!dialogue.illustrations().containsKey(illustration)) {
return new ValidationResult.Error.NonexistentIllustration(state.getKey(), illustration);
}
}

return ValidationResult.success();
}

private enum Reachability {
NONE,
CONDITIONAL,
Expand Down
Expand Up @@ -17,6 +17,8 @@
*/
package org.ladysnake.blabber.impl.common.validation;

import org.ladysnake.blabber.impl.common.model.ChoiceResult;

import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -80,5 +82,12 @@ public String message() {
return state() + " references non-existent illustration " + illustration();
}
}

record InvalidIllustratedState(String state, ChoiceResult type, List<String> illustrations) implements Error {
@Override
public String message() {
return state() + " of type " + type + " is not allowed to use illustrations " + illustrations();
}
}
}
}

0 comments on commit dd571e0

Please sign in to comment.