feat(model): Combination recipe record + order-independent key
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package thb.jeanluc.adventure.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A data-driven item-combination recipe. Identified by an unordered pair of
|
||||
* item ids ({@code a}, {@code b}). When applied it may require conditions,
|
||||
* consume items, produce an item, apply effects, and print a response.
|
||||
*/
|
||||
public record Combination(
|
||||
String a,
|
||||
String b,
|
||||
List<Condition> requires,
|
||||
List<String> consume,
|
||||
String produce,
|
||||
List<Effect> effects,
|
||||
String response,
|
||||
String failText
|
||||
) {
|
||||
public Combination {
|
||||
requires = requires == null ? List.of() : List.copyOf(requires);
|
||||
consume = consume == null ? List.of() : List.copyOf(consume);
|
||||
effects = effects == null ? List.of() : List.copyOf(effects);
|
||||
}
|
||||
|
||||
/** Order-independent key for an item pair (sorted, '|'-joined). */
|
||||
public static String key(String x, String y) {
|
||||
return x.compareTo(y) <= 0 ? x + "|" + y : y + "|" + x;
|
||||
}
|
||||
|
||||
/** This recipe's canonical pair key. */
|
||||
public String key() {
|
||||
return key(a, b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package thb.jeanluc.adventure.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class CombinationTest {
|
||||
|
||||
@Test
|
||||
void keyIsOrderIndependentAndSorted() {
|
||||
assertThat(Combination.key("matches", "torch"))
|
||||
.isEqualTo(Combination.key("torch", "matches"))
|
||||
.isEqualTo("matches|torch");
|
||||
assertThat(Combination.key("b", "a")).isEqualTo("a|b");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullCollectionComponentsBecomeEmpty() {
|
||||
Combination c = new Combination("a", "b", null, null, null, null, "ok", null);
|
||||
assertThat(c.requires()).isEmpty();
|
||||
assertThat(c.consume()).isEmpty();
|
||||
assertThat(c.effects()).isEmpty();
|
||||
assertThat(c.produce()).isNull();
|
||||
assertThat(c.key()).isEqualTo("a|b");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user