diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/CombinationFactory.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/CombinationFactory.java new file mode 100644 index 0000000..81234b4 --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/CombinationFactory.java @@ -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 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 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 + "')"); + } + } +} diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/CombinationDto.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/CombinationDto.java new file mode 100644 index 0000000..8541dab --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/CombinationDto.java @@ -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 requires, + List consume, + String produce, + List effects, + String response, + String failText +) { +} diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/CombinationFactoryTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/CombinationFactoryTest.java new file mode 100644 index 0000000..88db084 --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/CombinationFactoryTest.java @@ -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 items() { + Map 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"); + } +}