feat(loader): ConditionDto/EffectDto with toModel mapping

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 21:47:11 +02:00
parent 5546b63d47
commit 228efaffcb
3 changed files with 117 additions and 0 deletions

View File

@@ -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<Condition> toModelList(List<ConditionDto> dtos) {
List<Condition> out = new ArrayList<>();
if (dtos != null) {
for (ConditionDto d : dtos) {
out.add(d.toModel());
}
}
return out;
}
}

View File

@@ -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<Effect> toModelList(List<EffectDto> dtos) {
List<Effect> out = new ArrayList<>();
if (dtos != null) {
for (EffectDto d : dtos) {
out.add(d.toModel());
}
}
return out;
}
}

View File

@@ -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);
}
}