feat(loader): CombinationDto + CombinationFactory with id validation
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package thb.jeanluc.adventure.loader;
|
||||
|
||||
import thb.jeanluc.adventure.loader.dto.CombinationDto;
|
||||
import thb.jeanluc.adventure.loader.dto.ConditionDto;
|
||||
import thb.jeanluc.adventure.loader.dto.EffectDto;
|
||||
import thb.jeanluc.adventure.model.Combination;
|
||||
import thb.jeanluc.adventure.model.item.Item;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Builds {@link Combination}s from {@link CombinationDto}s, validating item ids. */
|
||||
public final class CombinationFactory {
|
||||
|
||||
private CombinationFactory() {
|
||||
}
|
||||
|
||||
public static Combination fromDto(CombinationDto dto, Map<String, Item> items) {
|
||||
requireItem(items, dto.a(), "a");
|
||||
requireItem(items, dto.b(), "b");
|
||||
if (dto.consume() != null) {
|
||||
for (String id : dto.consume()) {
|
||||
requireItem(items, id, "consume");
|
||||
}
|
||||
}
|
||||
if (dto.produce() != null) {
|
||||
requireItem(items, dto.produce(), "produce");
|
||||
}
|
||||
return new Combination(
|
||||
dto.a(),
|
||||
dto.b(),
|
||||
ConditionDto.toModelList(dto.requires()),
|
||||
dto.consume() == null ? List.of() : List.copyOf(dto.consume()),
|
||||
dto.produce(),
|
||||
EffectDto.toModelList(dto.effects()),
|
||||
dto.response(),
|
||||
dto.failText());
|
||||
}
|
||||
|
||||
private static void requireItem(Map<String, Item> items, String id, String field) {
|
||||
if (id == null) {
|
||||
throw new WorldLoadException("Combination '" + field + "' is missing an item id");
|
||||
}
|
||||
if (!items.containsKey(id)) {
|
||||
throw new WorldLoadException(
|
||||
"Combination references unknown item '" + id + "' (field '" + field + "')");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package thb.jeanluc.adventure.loader.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** YAML representation of an item-combination recipe. */
|
||||
public record CombinationDto(
|
||||
String a,
|
||||
String b,
|
||||
List<ConditionDto> requires,
|
||||
List<String> consume,
|
||||
String produce,
|
||||
List<EffectDto> effects,
|
||||
String response,
|
||||
String failText
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package thb.jeanluc.adventure.loader;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import thb.jeanluc.adventure.loader.dto.CombinationDto;
|
||||
import thb.jeanluc.adventure.model.Combination;
|
||||
import thb.jeanluc.adventure.model.item.Item;
|
||||
import thb.jeanluc.adventure.model.item.PlainItem;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
class CombinationFactoryTest {
|
||||
|
||||
private Map<String, Item> items() {
|
||||
Map<String, Item> m = new HashMap<>();
|
||||
for (String id : List.of("matches", "torch", "lit_torch")) {
|
||||
m.put(id, PlainItem.builder().id(id).name(id).description("d").light(false).build());
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildsCombinationFromDto() {
|
||||
CombinationDto dto = new CombinationDto("matches", "torch", null,
|
||||
List.of("matches", "torch"), "lit_torch", null, "You light it.", null);
|
||||
Combination c = CombinationFactory.fromDto(dto, items());
|
||||
assertThat(c.key()).isEqualTo("matches|torch");
|
||||
assertThat(c.consume()).containsExactly("matches", "torch");
|
||||
assertThat(c.produce()).isEqualTo("lit_torch");
|
||||
assertThat(c.response()).isEqualTo("You light it.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnknownOperandId() {
|
||||
CombinationDto dto = new CombinationDto("matches", "ghost", null, null, null, null, "x", null);
|
||||
assertThatThrownBy(() -> CombinationFactory.fromDto(dto, items()))
|
||||
.isInstanceOf(WorldLoadException.class)
|
||||
.hasMessageContaining("ghost");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnknownProduceId() {
|
||||
CombinationDto dto = new CombinationDto("matches", "torch", null, null, "phantom", null, "x", null);
|
||||
assertThatThrownBy(() -> CombinationFactory.fromDto(dto, items()))
|
||||
.isInstanceOf(WorldLoadException.class)
|
||||
.hasMessageContaining("phantom");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user