From 3ec540e41baa7c436aeae1d25f57cd6a02c36e58 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sun, 31 May 2026 22:47:38 +0200 Subject: [PATCH 1/3] feat: Ending model + endings.yaml loading (ordered, optional) Co-Authored-By: Claude Opus 4.8 (1M context) --- .../adventure/loader/EndingFactory.java | 21 ++++++++++++++++++ .../jeanluc/adventure/loader/WorldLoader.java | 11 +++++++++- .../adventure/loader/dto/EndingDto.java | 7 ++++++ .../thb/jeanluc/adventure/model/Ending.java | 10 +++++++++ .../thb/jeanluc/adventure/model/World.java | 14 ++++++++++-- .../adventure/loader/EndingLoadingTest.java | 22 +++++++++++++++++++ 6 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/EndingFactory.java create mode 100644 Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/EndingDto.java create mode 100644 Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Ending.java create mode 100644 Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/EndingLoadingTest.java diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/EndingFactory.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/EndingFactory.java new file mode 100644 index 0000000..9ac368d --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/EndingFactory.java @@ -0,0 +1,21 @@ +package thb.jeanluc.adventure.loader; + +import thb.jeanluc.adventure.loader.dto.ConditionDto; +import thb.jeanluc.adventure.loader.dto.EndingDto; +import thb.jeanluc.adventure.model.Ending; + +/** Builds {@link Ending} objects from {@link EndingDto}s. */ +public final class EndingFactory { + + private EndingFactory() { + } + + public static Ending fromDto(EndingDto dto) { + return new Ending( + dto.id(), + dto.title(), + Boolean.TRUE.equals(dto.victory()), + ConditionDto.toModelList(dto.when()), + dto.text()); + } +} diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/WorldLoader.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/WorldLoader.java index b783a63..b5c0eb1 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/WorldLoader.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/WorldLoader.java @@ -5,11 +5,13 @@ import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import thb.jeanluc.adventure.loader.dto.EndingDto; import thb.jeanluc.adventure.loader.dto.GameDto; import thb.jeanluc.adventure.loader.dto.ItemDto; import thb.jeanluc.adventure.loader.dto.NpcDto; import thb.jeanluc.adventure.loader.dto.QuestDto; import thb.jeanluc.adventure.loader.dto.RoomDto; +import thb.jeanluc.adventure.model.Ending; import thb.jeanluc.adventure.model.Npc; import thb.jeanluc.adventure.model.Player; import thb.jeanluc.adventure.model.Quest; @@ -19,6 +21,7 @@ import thb.jeanluc.adventure.model.item.Item; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -71,12 +74,14 @@ public class WorldLoader { List npcDtos = readList(basePath + "/npcs.yaml", NpcDto.class); List roomDtos = readList(basePath + "/rooms.yaml", RoomDto.class); List questDtos = readListOptional(basePath + "/quests.yaml", QuestDto.class); + List endingDtos = readListOptional(basePath + "/endings.yaml", EndingDto.class); GameDto gameDto = readSingle(basePath + "/game.yaml", GameDto.class); requireUniqueIds("item", itemDtos.stream().map(ItemDto::id).toList()); requireUniqueIds("npc", npcDtos.stream().map(NpcDto::id).toList()); requireUniqueIds("room", roomDtos.stream().map(RoomDto::id).toList()); requireUniqueIds("quest", questDtos.stream().map(QuestDto::id).toList()); + requireUniqueIds("ending", endingDtos.stream().map(EndingDto::id).toList()); Map items = new HashMap<>(); for (ItemDto dto : itemDtos) { @@ -94,6 +99,10 @@ public class WorldLoader { for (QuestDto dto : questDtos) { quests.put(dto.id(), QuestFactory.fromDto(dto)); } + List endings = new ArrayList<>(); + for (EndingDto dto : endingDtos) { + endings.add(EndingFactory.fromDto(dto)); + } ReferenceResolver resolver = new ReferenceResolver(items, npcs, rooms); resolver.resolveRooms(roomDtos); @@ -106,7 +115,7 @@ public class WorldLoader { Player player = new Player(start, gold); World world = new World(rooms, items, npcs, - gameDto.title(), gameDto.welcomeMessage(), quests); + gameDto.title(), gameDto.welcomeMessage(), quests, endings); log.info("World '{}' loaded: {} rooms, {} items, {} npcs", gameDto.title(), rooms.size(), items.size(), npcs.size()); return new LoadResult(world, player); diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/EndingDto.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/EndingDto.java new file mode 100644 index 0000000..0434956 --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/EndingDto.java @@ -0,0 +1,7 @@ +package thb.jeanluc.adventure.loader.dto; + +import java.util.List; + +/** YAML representation of a game ending. */ +public record EndingDto(String id, String title, Boolean victory, List when, String text) { +} diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Ending.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Ending.java new file mode 100644 index 0000000..83458d9 --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/Ending.java @@ -0,0 +1,10 @@ +package thb.jeanluc.adventure.model; + +import java.util.List; + +/** A game ending: shown (and ends the game) when its conditions hold. */ +public record Ending(String id, String title, boolean victory, List when, String text) { + public Ending { + when = when == null ? List.of() : List.copyOf(when); + } +} diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/World.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/World.java index b478fa8..56dffb1 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/World.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/World.java @@ -4,6 +4,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import thb.jeanluc.adventure.model.item.Item; +import java.util.List; import java.util.Map; /** @@ -33,9 +34,18 @@ public class World { /** Global lookup of quests by id. */ private final Map quests; - /** Backward-compatible constructor for worlds without quests. */ + /** Ordered list of endings (first matching one wins). */ + private final List endings; + + /** Backward-compatible constructor for worlds without quests or endings. */ public World(Map rooms, Map items, Map npcs, String title, String welcomeMessage) { - this(rooms, items, npcs, title, welcomeMessage, Map.of()); + this(rooms, items, npcs, title, welcomeMessage, Map.of(), List.of()); + } + + /** Backward-compatible constructor for worlds with quests but no endings. */ + public World(Map rooms, Map items, Map npcs, + String title, String welcomeMessage, Map quests) { + this(rooms, items, npcs, title, welcomeMessage, quests, List.of()); } } diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/EndingLoadingTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/EndingLoadingTest.java new file mode 100644 index 0000000..257d519 --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/loader/EndingLoadingTest.java @@ -0,0 +1,22 @@ +package thb.jeanluc.adventure.loader; + +import org.junit.jupiter.api.Test; +import thb.jeanluc.adventure.loader.dto.ConditionDto; +import thb.jeanluc.adventure.loader.dto.EndingDto; +import thb.jeanluc.adventure.model.Ending; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class EndingLoadingTest { + @Test + void factoryMapsFields() { + Ending e = EndingFactory.fromDto(new EndingDto("victory", "Won", true, + List.of(new ConditionDto("manor_secured", null, null)), "You win.")); + assertThat(e.id()).isEqualTo("victory"); + assertThat(e.victory()).isTrue(); + assertThat(e.when()).hasSize(1); + assertThat(e.text()).isEqualTo("You win."); + } +} From 2238aa6ff57f95bebcfad4de000a4ad6e9082379 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sun, 31 May 2026 22:49:00 +0200 Subject: [PATCH 2/3] feat(game): EndingEngine + per-turn end check with summary Co-Authored-By: Claude Opus 4.8 (1M context) --- .../jeanluc/adventure/game/EndingEngine.java | 46 +++++++++++++++++ .../java/thb/jeanluc/adventure/game/Game.java | 12 +++++ .../adventure/game/EndingEngineTest.java | 50 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/EndingEngine.java create mode 100644 Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/EndingEngineTest.java diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/EndingEngine.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/EndingEngine.java new file mode 100644 index 0000000..e70a46d --- /dev/null +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/EndingEngine.java @@ -0,0 +1,46 @@ +package thb.jeanluc.adventure.game; + +import thb.jeanluc.adventure.io.text.StyledText; +import thb.jeanluc.adventure.model.Ending; + +/** Detects the first matching ending and renders the end-of-game screen. */ +public final class EndingEngine { + + private EndingEngine() { + } + + /** First ending whose conditions hold, in list order; null if none. */ + public static Ending triggered(GameContext ctx) { + for (Ending e : ctx.getWorld().getEndings()) { + if (Conditions.all(e.when(), ctx)) { + return e; + } + } + return null; + } + + /** Builds the ending screen: title banner, text, and a run summary. */ + public static StyledText render(Ending e, GameContext ctx, int turns) { + int total = ctx.getWorld().getQuests().size(); + int done = ctx.getQuestLog().completed().size(); + String bar = "═".repeat(Math.max(12, e.title().length() + 6)); + + StyledText.Builder b = StyledText.builder(); + b.heading(bar + "\n").heading(" " + e.title() + "\n").heading(bar + "\n"); + b.plain(e.text().stripTrailing()).plain("\n\n"); + b.dim("Turns: " + turns + "\n"); + b.dim("Quests completed: " + done + " / " + total + "\n"); + b.heading("Rank: " + rank(e, total, done)); + return b.build(); + } + + private static String rank(Ending e, int total, int done) { + if (e.victory() && total > 0 && done == total) { + return "Master of the Manor"; + } + if (e.victory()) { + return "Manor Reclaimed"; + } + return "Escaped — the manor keeps its secrets"; + } +} diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/Game.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/Game.java index 0d5a5cb..20d0f02 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/Game.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/game/Game.java @@ -8,6 +8,7 @@ import thb.jeanluc.adventure.command.ParsedCommand; import thb.jeanluc.adventure.io.text.Hud; import thb.jeanluc.adventure.io.text.MapView; import thb.jeanluc.adventure.map.MapLayout; +import thb.jeanluc.adventure.model.Ending; import java.util.Optional; @@ -39,6 +40,7 @@ public class Game { */ public void run() { publishHud(); + maybeEnd(); while (running) { String input = ctx.getIo().readLine(); if (input == null) { @@ -56,6 +58,16 @@ public class Game { turn++; } publishHud(); + maybeEnd(); + } + } + + /** Ends the game if any ending's conditions are met, printing its screen. */ + private void maybeEnd() { + Ending e = EndingEngine.triggered(ctx); + if (e != null) { + ctx.getIo().print(EndingEngine.render(e, ctx, turn)); + stop(); } } diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/EndingEngineTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/EndingEngineTest.java new file mode 100644 index 0000000..f40859c --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/game/EndingEngineTest.java @@ -0,0 +1,50 @@ +package thb.jeanluc.adventure.game; + +import org.junit.jupiter.api.Test; +import thb.jeanluc.adventure.io.TestIO; +import thb.jeanluc.adventure.model.Condition; +import thb.jeanluc.adventure.model.Ending; +import thb.jeanluc.adventure.model.Player; +import thb.jeanluc.adventure.model.Room; +import thb.jeanluc.adventure.model.World; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class EndingEngineTest { + private GameContext ctx(List endings) { + Player p = new Player(new Room("k", "K", "d"), 0); + World w = new World(Map.of(), Map.of(), Map.of(), "t", "w", Map.of(), endings); + return new GameContext(w, p, new TestIO()); + } + + private Ending ending(String id, boolean victory, String flag) { + return new Ending(id, id + "-title", victory, + List.of(new Condition(Condition.Type.FLAG, flag)), id + " text"); + } + + @Test + void firstMatchingEndingWinsByOrder() { + GameContext ctx = ctx(List.of(ending("victory", true, "won"), ending("fled", false, "fled"))); + ctx.getState().set("won"); + ctx.getState().set("fled"); + assertThat(EndingEngine.triggered(ctx).id()).isEqualTo("victory"); + } + + @Test + void noEndingWhenNoConditionHolds() { + GameContext ctx = ctx(List.of(ending("victory", true, "won"))); + assertThat(EndingEngine.triggered(ctx)).isNull(); + } + + @Test + void renderIncludesTitleTextAndSummary() { + GameContext ctx = ctx(List.of(ending("victory", true, "won"))); + ctx.getState().set("won"); + String out = EndingEngine.render(EndingEngine.triggered(ctx), ctx, 7).plainText(); + assertThat(out).contains("victory-title").contains("victory text"); + assertThat(out).contains("Turns: 7").contains("Quests completed: 0 / 0"); + } +} From b23f706521d65460e476bdfb2a4e9db707a24b01 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sun, 31 May 2026 22:50:46 +0200 Subject: [PATCH 3/3] feat(content): victory & flee endings; front-door flee switch Co-Authored-By: Claude Opus 4.8 (1M context) --- Semesterprojekt/docs/enhancement-ideas.md | 9 +++++++-- .../src/main/resources/world/endings.yaml | 14 ++++++++++++++ .../src/main/resources/world/items.yaml | 12 ++++++++++++ .../src/main/resources/world/rooms.yaml | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 Semesterprojekt/src/main/resources/world/endings.yaml diff --git a/Semesterprojekt/docs/enhancement-ideas.md b/Semesterprojekt/docs/enhancement-ideas.md index 0990c2f..c404a7e 100644 --- a/Semesterprojekt/docs/enhancement-ideas.md +++ b/Semesterprojekt/docs/enhancement-ideas.md @@ -86,8 +86,13 @@ Farb-Rollen: Items / NPCs / Exits / Heading / Gefahr. > ✅ #3.2 umgesetzt (Branch `feature/quests`): condition-driven Quests > (Quest/QuestStage), QuestEngine mit Auto-Advance + Ansagen, START_QUEST-Effekt, > Konsolen-Befehl `quests` und GUI-Quest-Box unter der Karte. Demo-Quest -> `restore_power`. Offen: #3.3 Win-Condition/Enden sowie Licht/Dunkelheit & -> Item-Kombination. +> `restore_power`. +> +> ✅ #3.3 umgesetzt (Branch `feature/endings`): priorisierte, condition-driven +> Enden (`endings.yaml`) + End-Screen mit Summary (Züge, Quests X/Y, Rang). +> Demo: Sieg-Ende (`manor_secured`) und Flucht-Ende (Front Door → `fled`). +> **Damit ist das Quest-System (#3) komplett.** Offen: Licht/Dunkelheit & +> Item-Kombination sowie #4 Content-Ausbau. - Aktuelles NPC-System: Begrüßung + Geschenk-Reaktion (Item → Item). diff --git a/Semesterprojekt/src/main/resources/world/endings.yaml b/Semesterprojekt/src/main/resources/world/endings.yaml new file mode 100644 index 0000000..cab9b91 --- /dev/null +++ b/Semesterprojekt/src/main/resources/world/endings.yaml @@ -0,0 +1,14 @@ +- id: victory + title: "The Manor Reclaimed" + victory: true + when: [{ flag: manor_secured }] + text: | + The lights hold steady and the whispers fade to nothing. Whatever held + this place has loosened its grip. The manor is yours now. +- id: fled + title: "Into the Night" + victory: false + when: [{ flag: fled }] + text: | + You wrench the front door open and bolt into the dark. Safe — but the + manor keeps its secrets, and they will keep you awake for years. diff --git a/Semesterprojekt/src/main/resources/world/items.yaml b/Semesterprojekt/src/main/resources/world/items.yaml index 70a52d5..8db6c63 100644 --- a/Semesterprojekt/src/main/resources/world/items.yaml +++ b/Semesterprojekt/src/main/resources/world/items.yaml @@ -34,3 +34,15 @@ You pull the lever back; the manor falls dark again. effects: - { setFlag: power_on } + +- type: switchable + id: front_door + name: Front Door + description: The heavy front door. It would let you leave the manor for good. + initialState: false + onText: | + You haul the front door open and step out into the cold night. + offText: | + You ease the door shut again. + effects: + - { setFlag: fled } diff --git a/Semesterprojekt/src/main/resources/world/rooms.yaml b/Semesterprojekt/src/main/resources/world/rooms.yaml index 2ce13cb..0fb43ef 100644 --- a/Semesterprojekt/src/main/resources/world/rooms.yaml +++ b/Semesterprojekt/src/main/resources/world/rooms.yaml @@ -7,7 +7,7 @@ north: hallway east: cellar south: dungeon - items: [letter, lamp] + items: [letter, lamp, front_door] npcs: [old_man] exitLocks: - direction: east