From c392f80ed073eb54d53104968751e0c0ef5936fb Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 1 Jun 2026 14:21:56 +0200 Subject: [PATCH] feat: restore hooks for flags, quest progress, switch state --- .../thb/jeanluc/adventure/game/GameState.java | 7 ++++ .../thb/jeanluc/adventure/game/QuestLog.java | 9 ++++ .../adventure/model/item/SwitchableItem.java | 10 +++++ .../adventure/game/RestoreHooksTest.java | 42 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/RestoreHooksTest.java diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/GameState.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/GameState.java index 135f03f..b56d006 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/GameState.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/GameState.java @@ -1,5 +1,6 @@ package thb.jeanluc.adventure.game; +import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; @@ -24,4 +25,10 @@ public class GameState { public Set all() { return Collections.unmodifiableSet(flags); } + + /** Replaces all flags with the given collection (used by load). */ + public void restore(Collection newFlags) { + flags.clear(); + flags.addAll(newFlags); + } } diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/QuestLog.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/QuestLog.java index d0d9bb0..8ba492d 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/QuestLog.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/QuestLog.java @@ -1,5 +1,6 @@ package thb.jeanluc.adventure.game; +import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -46,4 +47,12 @@ public class QuestLog { public Set completed() { return Collections.unmodifiableSet(completed); } + + /** Replaces all progress with the given stage map and completed set (load). */ + public void restore(Map stages, Collection completedIds) { + stageIndex.clear(); + stageIndex.putAll(stages); + completed.clear(); + completed.addAll(completedIds); + } } diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java index 8f8e1a3..f2b19d5 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java @@ -53,4 +53,14 @@ public class SwitchableItem extends Item { public boolean isOn() { return state; } + + /** + * Directly sets the on/off state without running effects or printing. + * Used only when restoring a saved game. + * + * @param state the state to restore + */ + public void setState(boolean state) { + this.state = state; + } } diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/RestoreHooksTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/RestoreHooksTest.java new file mode 100644 index 0000000..613a12c --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/RestoreHooksTest.java @@ -0,0 +1,42 @@ +package thb.jeanluc.adventure.game; + +import org.junit.jupiter.api.Test; +import thb.jeanluc.adventure.model.item.SwitchableItem; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +class RestoreHooksTest { + + @Test + void gameStateRestoreReplacesFlags() { + GameState s = new GameState(); + s.set("old"); + s.restore(List.of("a", "b")); + assertThat(s.isSet("old")).isFalse(); + assertThat(s.all()).containsExactlyInAnyOrder("a", "b"); + } + + @Test + void questLogRestoreReplacesProgress() { + QuestLog q = new QuestLog(); + q.start("stale"); + q.restore(Map.of("active", 2), Set.of("done")); + assertThat(q.isActive("stale")).isFalse(); + assertThat(q.stageIndex("active")).isEqualTo(2); + assertThat(q.isCompleted("done")).isTrue(); + } + + @Test + void switchableSetStateDoesNotRunEffects() { + SwitchableItem lamp = SwitchableItem.builder() + .id("lamp").name("Lamp").description("d").light(true) + .onText("on").offText("off").state(false).effects(List.of()) + .build(); + lamp.setState(true); + assertThat(lamp.isOn()).isTrue(); + } +}