From ec70ab4803b82d109a585892b70208379846564d Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 23 Jan 2023 19:52:36 +0100 Subject: [PATCH] Summary Commit Boot.java: Added getPlayerSave() InteractionLoop: Asks for name if the player's name is null JsonParser: New method writeObject Travel Engine: getLocationByName() method Edited Help Command Added setter for Player's name New Quiz questions Edited Help unit test Added unit test for Location Command --- src/main/java/org/bitbiome/Boot.java | 12 +- .../org/bitbiome/classes/InteractionLoop.java | 13 +- .../java/org/bitbiome/classes/JsonParser.java | 23 +- .../org/bitbiome/classes/TravelEngine.java | 31 ++- .../org/bitbiome/commands/HelpCommand.java | 14 +- .../java/org/bitbiome/entities/Player.java | 10 +- src/main/resources/quiz.json | 204 ++++++++++++++++++ .../bitbiome/commands/HelpCommandTest.java | 17 +- .../commands/LocationCommandTest.java | 22 ++ 9 files changed, 336 insertions(+), 10 deletions(-) create mode 100644 src/main/resources/quiz.json create mode 100644 src/test/java/org/bitbiome/commands/LocationCommandTest.java diff --git a/src/main/java/org/bitbiome/Boot.java b/src/main/java/org/bitbiome/Boot.java index 9706de6..0f5ebdd 100644 --- a/src/main/java/org/bitbiome/Boot.java +++ b/src/main/java/org/bitbiome/Boot.java @@ -1,9 +1,11 @@ package org.bitbiome; import org.bitbiome.classes.InteractionLoop; +import org.bitbiome.classes.JsonParser; import org.bitbiome.classes.TravelEngine; import org.bitbiome.commands.CommandListener; import org.bitbiome.entities.Player; +import org.json.JSONObject; public class Boot { @@ -13,7 +15,7 @@ public class Boot { instance = this; cmdListener = new CommandListener(); InteractionLoop game = new InteractionLoop(); - Player player = new Player("Dave"); + Player player = getPlayerSave(); TravelEngine travelEngine = new TravelEngine(player); game.run(travelEngine); } @@ -22,6 +24,14 @@ public class Boot { return cmdListener; } + private Player getPlayerSave() { + String name; + JsonParser jp = new JsonParser(); + JSONObject playerconfig = jp.getJSONObject("playerconfig.json"); + name = playerconfig.getString("name"); + return new Player(name); + } + } diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index 879c345..2523644 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -1,7 +1,7 @@ package org.bitbiome.classes; import org.bitbiome.Boot; - +import org.json.JSONObject; import java.util.Scanner; public class InteractionLoop { @@ -10,7 +10,16 @@ public class InteractionLoop { public void run(TravelEngine travelEngine) { boolean isRunning = true; - System.out.println("Willkommen zu BitBiome " + travelEngine.getPlayer().getName() + "!\n\n"); + if (travelEngine.getPlayer().getName().equals("null")) { + System.out.println(Colors.ANSI_BLUE + "Oh, ein Fremder!\nBist du bereit für dein womöglich größtes Abenteuer?\nDann sag mir doch zunächst wie du heißt: " + Colors.ANSI_RESET); + String name = input.nextLine(); + JsonParser jp = new JsonParser(); + JSONObject playerconf = jp.getJSONObject("playerconfig.json"); + playerconf.put("name", name); + travelEngine.getPlayer().setName(name); + jp.writeObject("playerconfig.json", playerconf); + } + System.out.println(Colors.ANSI_BG_CYAN + Colors.ANSI_BLACK + "Willkommen zu BitBiome " + travelEngine.getPlayer().getName() + "!" + Colors.ANSI_RESET + "\n\n"); while (isRunning) { String line = input.nextLine().toLowerCase(); if (!Boot.instance.getCmdListener().perform(line.toLowerCase().split(" ")[0], input, isRunning, line, travelEngine)) { diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index e63dd87..0eb23d5 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -2,8 +2,14 @@ package org.bitbiome.classes; import org.json.JSONObject; import org.json.JSONTokener; +import org.json.JSONWriter; +import java.io.FileWriter; +import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; + + public class JsonParser { @@ -23,14 +29,27 @@ public class JsonParser { */ public JSONObject getJSONObject(String fileName) { - String resourceName = "./../../" + fileName; + String resourceName = "./../../../" + fileName; InputStream is = JsonParser.class.getResourceAsStream(resourceName); if (is == null) { throw new NullPointerException("Cannot find resource file " + resourceName); } JSONTokener tokener = new JSONTokener(is); - return new JSONObject(tokener); } + + public void writeObject(String fileName, JSONObject object) { + + String resourceName = System.getProperty("user.dir") + "/src/main/resources/" + fileName; + try { + FileWriter fw = new FileWriter(resourceName, false); + fw.write(object.toString(1)); + fw.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + + } } diff --git a/src/main/java/org/bitbiome/classes/TravelEngine.java b/src/main/java/org/bitbiome/classes/TravelEngine.java index 26a84a1..a8d3213 100644 --- a/src/main/java/org/bitbiome/classes/TravelEngine.java +++ b/src/main/java/org/bitbiome/classes/TravelEngine.java @@ -3,23 +3,28 @@ package org.bitbiome.classes; import org.bitbiome.entities.Location; import org.bitbiome.entities.Player; import org.json.JSONArray; +import org.json.JSONObject; import java.util.ArrayList; public class TravelEngine { private JSONArray locations; - JsonParser jp; + private JsonParser jp; private Player player; public TravelEngine(Player player) { jp = new JsonParser(); - locations = jp.getJSONObject("../gameconfig.json").getJSONArray("locations"); + locations = jp.getJSONObject("gameconfig.json").getJSONArray("locations"); this.player = player; } public void travelTo(Location location) { player.setLocation(location); + JSONObject jObj = jp.getJSONObject("playerconfig.json"); + jObj.put("currentLocation", location.getName()); + + jp.writeObject("playerconfig.json", jObj); } public Player getPlayer() { @@ -39,4 +44,26 @@ public class TravelEngine { return found; } + public Location getLocationByName(String name) { + JsonParser jp = new JsonParser(); + JSONObject gameconfig = jp.getJSONObject("gameconfig.json"); + JSONArray locations = gameconfig.getJSONArray("locations"); + JSONObject location = null; + if (locationExists(name)) { + for (int i = 0; i < locations.length(); i++) { + if (locations.getJSONObject(i).getString("name").equals(name)) { + location = locations.getJSONObject(i); + } + } + assert location != null; + //TODO Create Location by name and add mobs and times to the location + JSONArray items = location.getJSONArray("items"); + JSONArray mobs = location.getJSONArray("mobs"); + System.out.println(items.toString(1)); + System.out.println(mobs.toString(1)); + return new Location(name, new ArrayList<>(), new ArrayList<>()); + } else { + return null; + } + } } diff --git a/src/main/java/org/bitbiome/commands/HelpCommand.java b/src/main/java/org/bitbiome/commands/HelpCommand.java index ce85922..90702cc 100644 --- a/src/main/java/org/bitbiome/commands/HelpCommand.java +++ b/src/main/java/org/bitbiome/commands/HelpCommand.java @@ -1,6 +1,7 @@ package org.bitbiome.commands; import org.bitbiome.Boot; +import org.bitbiome.classes.Colors; import org.bitbiome.classes.TravelEngine; import java.util.HashMap; @@ -16,7 +17,18 @@ public class HelpCommand implements CommandAPI { public static String getHelpMessage() { StringBuilder outputMessage = new StringBuilder(); - outputMessage.append("Hier ist eine Liste der Commands:\n").append("- help -> Gibt diese Nachricht aus\n").append("- exit/quit -> Beendet das Spiel\n"); + outputMessage + .append("|______________|_____________________________|\n") + .append("|" + Colors.ANSI_PURPLE + " Command" + Colors.ANSI_RESET + " | " + Colors.ANSI_PURPLE + "Description" + Colors.ANSI_RESET + " |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " help" + Colors.ANSI_RESET + " | Gibt diese Nachricht aus |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " exit/quit" + Colors.ANSI_RESET + " | Beendet das Spiel |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " travel" + Colors.ANSI_RESET + " | Startet das Reise System |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " location" + Colors.ANSI_RESET + " | Gibt deine Location aus |\n") + .append("|______________|_____________________________|\n"); return outputMessage.toString(); } diff --git a/src/main/java/org/bitbiome/entities/Player.java b/src/main/java/org/bitbiome/entities/Player.java index 8f07072..6fe3152 100644 --- a/src/main/java/org/bitbiome/entities/Player.java +++ b/src/main/java/org/bitbiome/entities/Player.java @@ -1,6 +1,7 @@ package org.bitbiome.entities; import org.bitbiome.classes.CreateLocations; +import org.bitbiome.classes.JsonParser; import java.util.ArrayList; @@ -11,10 +12,13 @@ public class Player { private ArrayList inventory; + private JsonParser jp; + public Player(String name) { + jp = new JsonParser(); this.name = name; hp = 100.0F; - location = CreateLocations.createForest(); + location = new Location(jp.getJSONObject("playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>()); inventory = new ArrayList<>(); } @@ -50,4 +54,8 @@ public class Player { return inventory.remove(item); } + public void setName(String name) { + this.name = name; + } + } diff --git a/src/main/resources/quiz.json b/src/main/resources/quiz.json new file mode 100644 index 0000000..cc58a3f --- /dev/null +++ b/src/main/resources/quiz.json @@ -0,0 +1,204 @@ +{ + "Quiz": [ + { + "frage": "Wie lang ist der Äquator der Erde?", + "antworten": [ + "20.000m", + "30km", + "60.000km", + "40.000km" + ], + "korrekteAntwort": "40.000km" + }, + { + "frage": "Was ist der längste Fluss der Welt?", + "antworten": [ + "Amazonas", + "Nil", + "Rhein", + "Niger" + ], + "korrekteAntwort": "Nil" + }, + { + "frage": "Wie viele Tasten hat ein Klavier?", + "antworten": [ + "74", + "86", + "82", + "88" + ], + "korrekteAntwort": "88" + }, + { + "frage": "Von wem wird der Bundespräsident gewählt?", + "antworten": [ + "Vom Europäischen Parlament", + "Vom Bundeskanzler", + "Vom Bundestag", + "Von der Bundesversammlung" + ], + "korrekteAntwort": "von der Bundesversammlung" + }, + { + "frage": "Welches Land produziert jährlich die meisten Filme?", + "antworten": [ + "USA", + "Indien", + "Japan", + "Nigeria" + ], + "korrekteAntwort": "Indien" + }, + { + "frage": "Wie heißt der am schnellsten schwimmende Fisch auf Erden?", + "antworten": [ + "Flugfisch", + "Tigerhai", + "Segelfisch", + "Windfisch" + ], + "korrekteAntwort": "Segelfisch" + }, + { + "frage": "Was ist KEIN Gewächs?", + "antworten": [ + "Geranie", + "Moosfarn", + "Incolornis", + "Strandflieder" + ], + "korrekteAntwort": "Incolornis" + }, + { + "frage": "Was nutzt eine Fledermaus zur Orientierung in der Luft?", + "antworten": [ + "Infrarot", + "Röntgenstrahlen", + "Speichel", + "Ultraschall" + ], + "korrekteAntwort": "Ultraschall" + }, + { + "frage": "Von wem stammt der berühmte Satz: 'Ich denke, also bin ich'?", + "antworten": [ + "John Fitzgerald Kennedy", + "George Walker Bush", + "René Descartes", + "Julius Caesar" + ], + "korrekteAntwort": "René Descartes" + }, + { + "frage": "Welches Lebensmittel enthält das meiste Wasser?", + "antworten": [ + "Gurke", + "Wassermelone", + "Zitrone", + "Paprika" + ], + "korrekteAntwort": "Gurke" + }, + { + "frage": "Welches Lebensmittel gehört im botanischen Sinne zu den Früchten?", + "antworten": [ + "Möhre", + "Kartoffel", + "Tomate", + "Weißkohl" + ], + "korrekteAntwort": "Tomate" + }, + { + "frage": "Haptische Wahrnehmung beruht auf dem...?", + "antworten": [ + "Greifreflex", + "Gleichgewichtssinn", + "Hörsinn", + "Tastsinn" + ], + "korrekteAntwort": "Tastsinn" + }, + { + "frage": "Wie nennt man den letzten Tanz einer Tanzveranstaltung?", + "antworten": [ + "Voraus", + "Garaus", + "Kehraus", + "Durchaus" + ], + "korrekteAntwort": "Kehraus" + }, + { + "frage": "Wie nennt man ein tiefes, enges Tal, durch das ein Gebirgsbach fließt?", + "antworten": [ + "Klamm", + "Feucht", + "Nass", + "Schwamm" + ], + "korrekteAntwort": "Klamm" + }, + { + "frage": "Wer oder was ist Gerbera?", + "antworten": [ + "eine europäische Landschaft", + "eine Pflanze", + "die erste Präsidentin von Südafrika", + "eine Stadt in Lichtenstein" + ], + "korrekteAntwort": "eine Pflanze" + }, + { + "frage": "Nach wem wurde ein Gesellschaftsanzug benannt?", + "antworten": [ + "Richard von Weizsäcker", + "Gustav Heinemann", + "Jürgen Klinsmann", + "Gustav Stresemann" + ], + "korrekteAntwort": "Gustav Stresemann" + }, + { + "frage": "Was ist Speckstein?", + "antworten": [ + "eine Fischart, die sich als Stein tarnt", + "ein Gericht aus dem Mittelalter", + "eine Skulptur im Römischen Reich unter Nero", + "ein besonders weicher Stein" + ], + "korrekteAntwort": "ein besonders weicher Stein" + }, + { + "frage": "In welcher Religion gibt es Gurus?", + "antworten": [ + "im Christentum", + "im Hinduismus", + "im Islam", + "im Judentum" + ], + "korrekteAntwort": "im Hinduismus" + }, + { + "frage": "Was versteht man unter Brunsbüttel?", + "antworten": [ + "eine Industriestadt an der Unterelbe", + "einen Plakatkleber", + "eine 630 Mark-Kraft", + "ein Staatssekretär" + ], + "korrekteAntwort": "eine Industriestadt an der Unterelbe" + }, + { + "frage": "Welcher im 11. Jahrhundert gegründeter Orden rettet und pfelgt auch noch heute Verletzte und Kranke?", + "antworten": [ + "die Dominikaner", + "die Augustiner", + "die Zisterzienser", + "die Johanniter" + ], + "korrekteAntwort": "die Johanniter" + } + ] +} \ No newline at end of file diff --git a/src/test/java/org/bitbiome/commands/HelpCommandTest.java b/src/test/java/org/bitbiome/commands/HelpCommandTest.java index 75006ed..dab4d90 100644 --- a/src/test/java/org/bitbiome/commands/HelpCommandTest.java +++ b/src/test/java/org/bitbiome/commands/HelpCommandTest.java @@ -1,14 +1,29 @@ package org.bitbiome.commands; +import org.bitbiome.classes.Colors; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class HelpCommandTest { @Test public void testHelpCommand() { String helpMessage = HelpCommand.getHelpMessage(); - assertEquals("Hier ist eine Liste der Commands:\n- help -> Gibt diese Nachricht aus\n- exit/quit -> Beendet das Spiel\n", helpMessage); + StringBuilder outputMessage = new StringBuilder(); + outputMessage + .append("|______________|_____________________________|\n") + .append("|" + Colors.ANSI_PURPLE + " Command" + Colors.ANSI_RESET + " | " + Colors.ANSI_PURPLE + "Description" + Colors.ANSI_RESET + " |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " help" + Colors.ANSI_RESET + " | Gibt diese Nachricht aus |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " exit/quit" + Colors.ANSI_RESET + " | Beendet das Spiel |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " travel" + Colors.ANSI_RESET + " | Startet das Reise System |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " location" + Colors.ANSI_RESET + " | Gibt deine Location aus |\n") + .append("|______________|_____________________________|\n"); + assertEquals(outputMessage.toString(), helpMessage); } diff --git a/src/test/java/org/bitbiome/commands/LocationCommandTest.java b/src/test/java/org/bitbiome/commands/LocationCommandTest.java new file mode 100644 index 0000000..23c6541 --- /dev/null +++ b/src/test/java/org/bitbiome/commands/LocationCommandTest.java @@ -0,0 +1,22 @@ +package org.bitbiome.commands; + +import org.bitbiome.classes.TravelEngine; +import org.bitbiome.entities.Player; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; + +public class LocationCommandTest { + + @Test + public void testLocationCommand() { + Player p = new Player("Unit"); + TravelEngine tE = new TravelEngine(p); + String[] standorte = {"Wald", "Strand"}; + String locationMessage = LocationCommand.getLocationMessage(tE).split(": ")[1]; + assertTrue(Arrays.asList(standorte).contains(locationMessage)); + } + +}