diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/UseCommand.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/UseCommand.java index 642d0d6..ec9950d 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/UseCommand.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/UseCommand.java @@ -1,6 +1,7 @@ package thb.jeanluc.adventure.command.impl; import thb.jeanluc.adventure.command.Command; +import thb.jeanluc.adventure.game.Combinations; import thb.jeanluc.adventure.game.GameContext; import thb.jeanluc.adventure.model.item.Item; @@ -20,6 +21,10 @@ public class UseCommand implements Command { ctx.getIo().write("Use what?"); return; } + if (args.size() >= 2) { + Combinations.tryUse(ctx, args.get(0), args.get(1)); + return; + } String itemId = args.getFirst(); Optional item = ctx.getPlayer().findItem(itemId); if (item.isEmpty()) { @@ -34,6 +39,6 @@ public class UseCommand implements Command { @Override public String help() { - return "use - use an item (read it, switch it on/off, ...)"; + return "use - use an item; or use on to combine them"; } } diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/command/impl/UseCommandComboTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/command/impl/UseCommandComboTest.java new file mode 100644 index 0000000..65550d1 --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/command/impl/UseCommandComboTest.java @@ -0,0 +1,65 @@ +package thb.jeanluc.adventure.command.impl; + +import org.junit.jupiter.api.Test; +import thb.jeanluc.adventure.game.GameContext; +import thb.jeanluc.adventure.io.TestIO; +import thb.jeanluc.adventure.model.Combination; +import thb.jeanluc.adventure.model.Player; +import thb.jeanluc.adventure.model.Room; +import thb.jeanluc.adventure.model.World; +import thb.jeanluc.adventure.model.item.Item; +import thb.jeanluc.adventure.model.item.PlainItem; +import thb.jeanluc.adventure.model.item.SwitchableItem; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class UseCommandComboTest { + + private GameContext ctx() { + Map items = new LinkedHashMap<>(); + items.put("matches", PlainItem.builder().id("matches").name("Matches").description("d").light(false).build()); + items.put("torch", PlainItem.builder().id("torch").name("Torch").description("d").light(false).build()); + items.put("lit_torch", PlainItem.builder().id("lit_torch").name("Lit Torch").description("d").light(true).build()); + SwitchableItem lamp = SwitchableItem.builder() + .id("lamp").name("Lamp").description("d").light(true) + .onText("The lamp glows.").offText("off").state(false).effects(List.of()).build(); + items.put("lamp", lamp); + Combination recipe = new Combination("matches", "torch", List.of(), + List.of("matches", "torch"), "lit_torch", List.of(), "You light the torch.", null); + Room start = new Room("start", "Start", "d"); + World w = new World(Map.of("start", start), items, Map.of(), "t", "w", + Map.of(), List.of(), Map.of(recipe.key(), recipe)); + GameContext ctx = new GameContext(w, new Player(start, 0), new TestIO()); + ctx.getPlayer().addItem(items.get("matches")); + ctx.getPlayer().addItem(items.get("torch")); + ctx.getPlayer().addItem(lamp); + return ctx; + } + + @Test + void emptyArgsAsksWhat() { + GameContext ctx = ctx(); + new UseCommand().execute(ctx, List.of()); + assertThat(((TestIO) ctx.getIo()).allOutput()).contains("Use what?"); + } + + @Test + void twoArgsRoutesToCombination() { + GameContext ctx = ctx(); + new UseCommand().execute(ctx, List.of("matches", "torch")); + assertThat(ctx.getPlayer().hasItem("lit_torch")).isTrue(); + assertThat(((TestIO) ctx.getIo()).allOutput()).contains("light the torch"); + } + + @Test + void oneArgUsesItemDirectly() { + GameContext ctx = ctx(); + new UseCommand().execute(ctx, List.of("lamp")); + assertThat(((SwitchableItem) ctx.getWorld().getItems().get("lamp")).isOn()).isTrue(); + assertThat(((TestIO) ctx.getIo()).allOutput()).contains("lamp glows"); + } +}