-
Notifications
You must be signed in to change notification settings - Fork 5
/
DialogueValidator.java
123 lines (106 loc) · 5.4 KB
/
DialogueValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Blabber
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package org.ladysnake.blabber.impl.common.validation;
import org.ladysnake.blabber.impl.common.model.ChoiceResult;
import org.ladysnake.blabber.impl.common.model.DialogueChoice;
import org.ladysnake.blabber.impl.common.model.DialogueState;
import org.ladysnake.blabber.impl.common.model.DialogueTemplate;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public final class DialogueValidator {
public static ValidationResult validateStructure(DialogueTemplate dialogue) {
Map<String, Map<String, Reachability>> parents = new HashMap<>();
Deque<String> waitList = new ArrayDeque<>();
Map<String, Reachability> unvalidated = new HashMap<>();
List<ValidationResult.Warning> warnings = new ArrayList<>();
for (Map.Entry<String, DialogueState> state : dialogue.states().entrySet()) {
if (state.getValue().type().equals(ChoiceResult.END_DIALOGUE)) {
waitList.add(state.getKey());
} else if (dialogue.states().get(state.getKey()).choices().isEmpty()) {
return new ValidationResult.Error.NoChoice(state.getKey());
} else {
unvalidated.put(state.getKey(), Reachability.NONE);
for (DialogueChoice choice : state.getValue().choices()) {
parents.computeIfAbsent(choice.next(), n -> new HashMap<>()).put(
state.getKey(),
choice.condition().isPresent() ? Reachability.CONDITIONAL : Reachability.PROVEN
);
}
}
// 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()) {
String state = waitList.pop();
Map<String, Reachability> stateParents = parents.get(state);
if (stateParents != null) {
for (var parent : stateParents.entrySet()) {
Reachability reachability = unvalidated.get(parent.getKey());
if (reachability != null) { // leave it alone if it was already validated through another branch
if (reachability == Reachability.NONE) { // only check once
waitList.add(parent.getKey());
}
switch (parent.getValue()) {
case PROVEN -> unvalidated.remove(parent.getKey());
case CONDITIONAL -> unvalidated.put(parent.getKey(), Reachability.CONDITIONAL);
default -> throw new IllegalStateException("Unexpected parent-child reachability " + parent.getValue());
}
}
}
} // else, state is unreachable - we log that in the next part
}
for (var bad : unvalidated.entrySet()) {
if (!Objects.equals(bad.getKey(), dialogue.start()) && !parents.containsKey(bad.getKey())) {
// Unreachable states do not cause infinite loops, but we still want to be aware of them
warnings.add(new ValidationResult.Warning.Unreachable(bad.getKey()));
} else if (bad.getValue() == Reachability.CONDITIONAL) {
warnings.add(new ValidationResult.Warning.ConditionalSoftLock(bad.getKey()));
} else {
return new ValidationResult.Error.SoftLock(bad.getKey());
}
}
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,
PROVEN,
}
}