diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/ConditionDto.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/ConditionDto.java new file mode 100644 index 0000000..b68550f --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/ConditionDto.java @@ -0,0 +1,34 @@ +package thb.jeanluc.adventure.loader.dto; + +import thb.jeanluc.adventure.loader.WorldLoadException; +import thb.jeanluc.adventure.model.Condition; + +import java.util.ArrayList; +import java.util.List; + +/** YAML condition: exactly one of the fields is set. */ +public record ConditionDto(String flag, String notFlag, String hasItem) { + + public Condition toModel() { + if (flag != null) { + return new Condition(Condition.Type.FLAG, flag); + } + if (notFlag != null) { + return new Condition(Condition.Type.NOT_FLAG, notFlag); + } + if (hasItem != null) { + return new Condition(Condition.Type.HAS_ITEM, hasItem); + } + throw new WorldLoadException("Condition must set one of flag/notFlag/hasItem"); + } + + public static List toModelList(List dtos) { + List out = new ArrayList<>(); + if (dtos != null) { + for (ConditionDto d : dtos) { + out.add(d.toModel()); + } + } + return out; + } +} diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/EffectDto.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/EffectDto.java new file mode 100644 index 0000000..ad57aba --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/EffectDto.java @@ -0,0 +1,40 @@ +package thb.jeanluc.adventure.loader.dto; + +import thb.jeanluc.adventure.loader.WorldLoadException; +import thb.jeanluc.adventure.model.Effect; + +import java.util.ArrayList; +import java.util.List; + +/** YAML effect: exactly one of the fields is set. */ +public record EffectDto(String setFlag, String clearFlag, String giveItem, String removeItem, String say) { + + public Effect toModel() { + if (setFlag != null) { + return new Effect(Effect.Type.SET_FLAG, setFlag); + } + if (clearFlag != null) { + return new Effect(Effect.Type.CLEAR_FLAG, clearFlag); + } + if (giveItem != null) { + return new Effect(Effect.Type.GIVE_ITEM, giveItem); + } + if (removeItem != null) { + return new Effect(Effect.Type.REMOVE_ITEM, removeItem); + } + if (say != null) { + return new Effect(Effect.Type.SAY, say); + } + throw new WorldLoadException("Effect must set one of setFlag/clearFlag/giveItem/removeItem/say"); + } + + public static List toModelList(List dtos) { + List out = new ArrayList<>(); + if (dtos != null) { + for (EffectDto d : dtos) { + out.add(d.toModel()); + } + } + return out; + } +} diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/dto/ConditionEffectDtoTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/dto/ConditionEffectDtoTest.java new file mode 100644 index 0000000..2a81cae --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/dto/ConditionEffectDtoTest.java @@ -0,0 +1,43 @@ +package thb.jeanluc.adventure.loader.dto; + +import org.junit.jupiter.api.Test; +import thb.jeanluc.adventure.loader.WorldLoadException; +import thb.jeanluc.adventure.model.Condition; +import thb.jeanluc.adventure.model.Effect; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class ConditionEffectDtoTest { + @Test + void conditionToModel() { + assertThat(new ConditionDto("power_on", null, null).toModel()) + .isEqualTo(new Condition(Condition.Type.FLAG, "power_on")); + assertThat(new ConditionDto(null, "x", null).toModel().type()) + .isEqualTo(Condition.Type.NOT_FLAG); + assertThat(new ConditionDto(null, null, "lamp").toModel().type()) + .isEqualTo(Condition.Type.HAS_ITEM); + } + + @Test + void emptyConditionThrows() { + assertThatThrownBy(() -> new ConditionDto(null, null, null).toModel()) + .isInstanceOf(WorldLoadException.class); + } + + @Test + void effectToModel() { + assertThat(new EffectDto("p", null, null, null, null).toModel()) + .isEqualTo(new Effect(Effect.Type.SET_FLAG, "p")); + assertThat(new EffectDto(null, null, "key", null, null).toModel().type()) + .isEqualTo(Effect.Type.GIVE_ITEM); + assertThat(new EffectDto(null, null, null, null, "boo").toModel().type()) + .isEqualTo(Effect.Type.SAY); + } + + @Test + void emptyEffectThrows() { + assertThatThrownBy(() -> new EffectDto(null, null, null, null, null).toModel()) + .isInstanceOf(WorldLoadException.class); + } +}