diff --git a/playerconfig.json b/playerconfig.json new file mode 100644 index 0000000..349a291 --- /dev/null +++ b/playerconfig.json @@ -0,0 +1,24 @@ +{ + "gold": 0, + "name": "Julia", + "hp": 10, + "inventory": [ + { + "gold": 5, + "damage": "10", + "amount": 6, + "durability": 1000, + "name": "Holz", + "doesDamage": true + }, + { + "gold": 5, + "damage": "10", + "amount": 5, + "durability": 1000, + "name": "Stein", + "doesDamage": true + } + ], + "currentLocation": "Wald" +} \ No newline at end of file diff --git a/src/main/java/org/bitbiome/classes/Shop.java b/src/main/java/org/bitbiome/classes/Shop.java index f9d4561..43e2a21 100644 --- a/src/main/java/org/bitbiome/classes/Shop.java +++ b/src/main/java/org/bitbiome/classes/Shop.java @@ -152,7 +152,7 @@ public class Shop { return arrayList; } - public JSONArray returnJSONArrayOfAllItems() { + public static JSONArray returnJSONArrayOfAllItems() { File file = new File("src/main/resources/items.json"); JSONArray itemJSON = null; try { diff --git a/src/main/java/org/bitbiome/classes/TravelEngine.java b/src/main/java/org/bitbiome/classes/TravelEngine.java index 0573cd9..d463273 100644 --- a/src/main/java/org/bitbiome/classes/TravelEngine.java +++ b/src/main/java/org/bitbiome/classes/TravelEngine.java @@ -56,8 +56,7 @@ public class TravelEngine { //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/CollectCommand.java b/src/main/java/org/bitbiome/commands/CollectCommand.java new file mode 100644 index 0000000..41d8fc0 --- /dev/null +++ b/src/main/java/org/bitbiome/commands/CollectCommand.java @@ -0,0 +1,65 @@ +package org.bitbiome.commands; + +import org.bitbiome.classes.Colors; +import org.bitbiome.classes.JsonParser; +import org.bitbiome.classes.TravelEngine; +import org.bitbiome.entities.Item; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.Scanner; + +public class CollectCommand implements CommandAPI { + @Override + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { + StringBuilder s = new StringBuilder(); + JsonParser jp = new JsonParser(); + JSONObject o = jp.getJSONObject("playerconfig.json"); + JSONArray inventory = o.getJSONArray("inventory"); + + ArrayList location = travelEngine.getPlayer().getLocation().getItemList(); + ArrayList getInventory = travelEngine.getPlayer().getInventory(); + + System.out.println("Was willst du einsammeln?"); + String item = new String(); + item = scanner.nextLine(); + + collectItem(location, item, jp, o, inventory,getInventory); + } + public void collectItem(ArrayList location, String item, JsonParser jp, JSONObject o, JSONArray inventory, ArrayList getInventory) { + for (int i = 0; i < location.size(); i++) { + if (item.equals(location.get(i).getName())) { + JSONObject o1 = new JSONObject(); + for (int j = 0; j < getInventory.size(); j++) { + if (getInventory.get(j).getName().equals(item)) { + getInventory.get(j).setAmount(getInventory.get(j).getAmount() + 1); + for (int k = 0; k < inventory.length(); k++) { + if (inventory.getJSONObject(k).getString("name").equals(item)) { + increaseAmountInPlayerConfig(inventory,k,jp,o); + return; + } + } + } + } + writeNewItem(location, getInventory, o1, inventory, i, jp, o); + } + } System.out.println("Es gibt kein Item, dass du einsammeln kannst."); + } + public void writeNewItem(ArrayList location, ArrayList getInventory, JSONObject o1, JSONArray inventory, int i, JsonParser jp, JSONObject o){ + o1.put("name", location.get(i).getName()).put("doesDamage", location.get(i).doesDamage()).put("damage", location.get(i).getDamage()).put("amount", 1).put("durability", 1000); + inventory.put(o1); + jp.writeObject("playerconfig.json", o); + getInventory.add(location.get(i)); + location.remove(i); + } + public void increaseAmountInPlayerConfig(JSONArray inventory, int k, JsonParser jp, JSONObject o){ + JSONObject o2 = new JSONObject(); + int amountItemsInPlayerconfig = 0; + amountItemsInPlayerconfig = inventory.getJSONObject(k).getInt("amount"); + inventory.getJSONObject(k).put("amount", amountItemsInPlayerconfig + 1); + jp.writeObject("playerconfig.json", o); + System.out.println(Colors.ANSI_YELLOW +"Du hast das Item eingesammelt."+ Colors.ANSI_RESET); + } +} + diff --git a/src/main/java/org/bitbiome/commands/CommandListener.java b/src/main/java/org/bitbiome/commands/CommandListener.java index 159785f..e2f9ec3 100644 --- a/src/main/java/org/bitbiome/commands/CommandListener.java +++ b/src/main/java/org/bitbiome/commands/CommandListener.java @@ -18,8 +18,10 @@ public class CommandListener { commands.put("quit", new QuitCommand()); commands.put("location", new LocationCommand()); commands.put("travel", new TravelCommand()); + commands.put("inventory", new InventoryCommand()); + commands.put("lookaround", new LookaroundCommand()); + commands.put("collect", new CollectCommand()); commands.put("shop", new ShopCommand()); - } public HashMap returnCommands() { diff --git a/src/main/java/org/bitbiome/commands/HelpCommand.java b/src/main/java/org/bitbiome/commands/HelpCommand.java index ed5fd59..8c02353 100644 --- a/src/main/java/org/bitbiome/commands/HelpCommand.java +++ b/src/main/java/org/bitbiome/commands/HelpCommand.java @@ -32,8 +32,20 @@ public class HelpCommand implements CommandAPI { .append("|" + Colors.ANSI_GREEN + " blackjack" + Colors.ANSI_RESET + " | Startet blackjack im shop |\n") .append("|--------------|-----------------------------|\n") .append("|" + Colors.ANSI_GREEN + " location" + Colors.ANSI_RESET + " | Gibt deine Location aus |\n") - .append("|______________|_____________________________|\n"); - + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " quiz" + Colors.ANSI_RESET + " | Startet das quiz im shop |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " blackjack" + Colors.ANSI_RESET + " | Startet blackjack im shop |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " location" + Colors.ANSI_RESET + " | Gibt deine Location aus |\n") + .append("|--------------|-----------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " inventory" + Colors.ANSI_RESET + " | Gibt dein Inventar aus |\n") + .append("|--------------|-------------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " collect " + Colors.ANSI_RESET + " | Gibt deine Location aus |\n") + .append("|--------------|-------------------------------|\n") + .append("|" + Colors.ANSI_GREEN + " lookaround" + Colors.ANSI_RESET + " | Zeigt dir deine Umgebung, |\n") + .append("|" + Colors.ANSI_GREEN + " " + Colors.ANSI_RESET + " | Items und Mobs in der Nähe |\n") + .append("|______________|_______________________________|\n"); return outputMessage.toString(); } diff --git a/src/main/java/org/bitbiome/commands/InventoryCommand.java b/src/main/java/org/bitbiome/commands/InventoryCommand.java new file mode 100644 index 0000000..08d37a4 --- /dev/null +++ b/src/main/java/org/bitbiome/commands/InventoryCommand.java @@ -0,0 +1,32 @@ +package org.bitbiome.commands; + +import org.bitbiome.classes.Colors; +import org.bitbiome.classes.JsonParser; +import org.bitbiome.classes.TravelEngine; +import org.bitbiome.entities.Item; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.Scanner; + +public class InventoryCommand implements CommandAPI { + @Override + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { + + System.out.println(readInv(travelEngine)); + } + + public String readInv(TravelEngine travelEngine) { + StringBuilder s = new StringBuilder(); + + ArrayList inventory = travelEngine.getPlayer().getInventory(); + s.append(Colors.ANSI_PURPLE +"Du möchtest wissen, was in deinem Inventar ist? \n" + + "Dann lass uns gemeinsam deinen Rucksack öffnen. \nDein Rucksack steckt ja voller Überraschungen! \n" + + "Das hast du alles schon gefunden: \n"+ Colors.ANSI_RESET); + for (int i=0; i < inventory.size(); i++){ + s.append(Colors.ANSI_BG_PURPLE+ Colors.ANSI_BRIGHT_WHITE+ "- ").append(inventory.get(i).getName()).append(" x").append(inventory.get(i).getAmount()).append(" "+ Colors.ANSI_RESET + "\n" ); + } + + return s.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/org/bitbiome/commands/LookaroundCommand.java b/src/main/java/org/bitbiome/commands/LookaroundCommand.java new file mode 100644 index 0000000..b1ae8ed --- /dev/null +++ b/src/main/java/org/bitbiome/commands/LookaroundCommand.java @@ -0,0 +1,138 @@ +package org.bitbiome.commands; + +import org.bitbiome.classes.Colors; +import org.bitbiome.classes.JsonParser; +import org.bitbiome.classes.Shop; +import org.bitbiome.classes.TravelEngine; +import org.bitbiome.entities.*; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; + +public class LookaroundCommand implements CommandAPI{ + @Override + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { + StringBuilder outputMessage = new StringBuilder(); + Location location = travelEngine.getPlayer().getLocation(); + JsonParser jp = new JsonParser(); + JSONObject gameConfig = jp.getJSONObject("src/main/resources/gameconfig.json"); + JSONArray locations = gameConfig.getJSONArray("locations"); + JSONObject locationObject = getLocationObject(location.getName(), locations); + JSONArray items = locationObject.getJSONArray("items"); + JSONArray mobs = locationObject.getJSONArray("mobs"); + + Random random = new Random(); + int randomNumberItems = random.nextInt(items.length()+1); + int randomNumberMobs = random.nextInt(mobs.length()+1); + ArrayList foundItems = location.getItemList(); + foundItems.removeAll(foundItems); + ArrayList foundMobs = location.getMobList(); + foundMobs.removeAll(foundMobs); + foundItems = getRandomItem(randomNumberItems, random, items, foundItems); + foundMobs = getRandomMob(randomNumberMobs,random,mobs,foundMobs); + + getLocationDescription(location,outputMessage); + getItemsOutput(randomNumberItems,outputMessage,foundItems); + getMobsOutput(randomNumberItems,randomNumberMobs,outputMessage, foundMobs); + System.out.println(outputMessage); + + } + public JSONObject getLocationObject(String locationName, JSONArray locations) { + for (int i = 1; i < locations.length(); i++) { + if(locations.getJSONObject(i).getString("name").equals(locationName)){ + return locations.getJSONObject(i); + } + } + return locations.getJSONObject(0); + } + public ArrayList getRandomItem(int randomNumberItems, Random random, JSONArray items, ArrayList foundItems ) { + for (int i=0; i getRandomMob(int randomNumberMobs, Random random, JSONArray mobs, ArrayList foundMobs){ + for (int i=0; i foundItems){ + if (randomNumberItems != 0){ + outputMessage.append(Colors.ANSI_BLUE +"Huch, was liegt denn hier rum?\n"+ Colors.ANSI_RESET); + for (int i = 0; i < foundItems.size(); i++){ + outputMessage. append("- ").append(foundItems.get(i).getName()+"\n"); + } + outputMessage.append(Colors.ANSI_BLUE +"Schnell, sammel es ein!\n"+ Colors.ANSI_RESET); + } + else { + outputMessage.append(Colors.ANSI_BLUE+ "Hier gibt es leider nichts für dich zum Einsammeln.\n"+ Colors.ANSI_RESET); + } + } + public void getMobsOutput(int randomNumberItems, int randomNumberMobs, StringBuilder outputMessage, ArrayList foundMobs){ + if (randomNumberMobs != 0){ + outputMessage.append(Colors.ANSI_RED+"Achtung, hier lauern Gefahren!"+Colors.ANSI_RESET +"Sei auf der Hut vor: \n"); + for (int i = 0; i < foundMobs.size(); i++){ + outputMessage. append( "- ").append(foundMobs.get(i).getName()+"\n"); + } + } + if((randomNumberMobs ==0) && (randomNumberItems == 0)){ + outputMessage.append("Hier gibt es sonst nichts weiter zu sehen. Reise weiter!\n"); + } + } + public void getLocationDescription(Location location, StringBuilder outputMessage) { + switch (location.getName()) { + case "Wald" -> + outputMessage.append("Du befindest dich mitten im Wald, um dich herum siehst du hohe Buchen, kleine Sträucher und Farne.\n" + + "Der Boden ist mit weichem Moos, Pilzen und Laub bedeckt, in der Nähe hörst du Vögel munter zwitschern und\n" + + "einen kleinen Bach, der sich durch das dichte Unterholz schlängelt." + + " Schau mal, dort hinten in der Ferne ist ein Eichhörnchen! \n"); + case "Strand" -> + outputMessage.append("Du befindest dich mitten am Strand und blickst auf das Meer, das sich bis zum Horizont erstreckt.\n" + + "Du spürst den Sand an deinen Füßen, du hörst das weiche Rauschen des Meeres und das Lachen der Möwen über dir.\n" + + "Rechts und links von dir erstreckt sich der weite, weiße Sandstrand, dort hinten bauen Kinder eine Sandburg.\n" + + "Es gibt ein paar Palmen, die den Strand säumen und weit in der Ferne ragen Felsen aus dem Meer.\n"); + case "Winterland" -> + outputMessage.append("Um dich herum ragen hohe Berge in den Himmel, bedeckt von einer dicken Schicht aus Schnee. Du hörst\n" + + "das Knirschen des Schnees unter deinen Füßen und das Rauschen des eisigen Windes. In der Ferne siehst du Tannenbäume,\n" + + "die sich unter der Last des Schnees biegen, und dichte Flocken fallen sanft aus dem grauen Himmel. Es ist kalt, du siehst,\n" + + "wie dein Atem kleine Wolken bildet. Es ist still, aber auch ein wenig unheimlich.\n"); + case "Berge"-> + outputMessage.append("Du befindest dich in einer majestätischen Berglandschaft mit hohen Gipfen und tiefen Tälern.\n" + + "Die Luft ist frisch und klar, der Klang von rauschenden Bächen und Wasserfällen erfüllt die Umgebung.\n" + + "Die Berge sind mit grünen Wäldern bedeckt und vereinzelt siehst du wilde Tiere herumstreifen.\n"); + case "Grünland"-> + outputMessage.append("Du befindest dich in einer weiten und grünen Landschaft. Überall um dich herum wachsen hohe Gräser und Wildblumen. \n" + + "In der Ferne erkennst du sanfte Hügel mit einer Herde von Schafen und Kühen.Die Luft ist erfüllt von dem Duft der Natur \n" + + "und dem Summen von Insekten. Es herrscht eine friedliche Stille, nur unterbrochen vom gelegentlichen Ruf eines Vogels."); + case "Wüste"-> + outputMessage.append("Du befindest dich mitten in der Wüste. Weit und breit ist nichts anderes zu sehen außer Dünen, vertrocknete Sträucher und Tonnen von Sand.\n" + + "Es ist staubig, der sandige Boden unter deinen Füßen knirscht bei jedem Schritt und die Sonnen brennt auf dich herab. Nimm dich in Acht vor der Wüstenhitze \n" + + "und den Gefahren, die hinter den Dünen lauern. Beeil dich, aus dieser unendlichen Ebene zu entkommen.\n"); + default -> { + } + //location description not found + } + } + + +} diff --git a/src/main/java/org/bitbiome/entities/Player.java b/src/main/java/org/bitbiome/entities/Player.java index 72a903c..a2f1c45 100644 --- a/src/main/java/org/bitbiome/entities/Player.java +++ b/src/main/java/org/bitbiome/entities/Player.java @@ -2,6 +2,9 @@ package org.bitbiome.entities; import org.bitbiome.classes.JsonParser; +import org.json.JSONArray; +import org.json.JSONObject; + import java.util.ArrayList; @@ -19,6 +22,14 @@ public class Player { hp = 100.0F; location = new Location(JsonParser.getJSONObject("src/main/resources/playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>()); inventory = new ArrayList<>(); + JSONArray items = JsonParser.getJSONObject("src/main/resources/playerconfig.json").getJSONArray("inventory"); + for (int i = 0; i < items.length(); i++) { + JSONObject o = items.getJSONObject(i); + inventory.add(new Item(o.getString("name"), o.getBoolean("doesDamage"), o.getString("damage"), o.getInt("amount"), o.getInt("gold"))); + } + + + } public Player() { @@ -61,4 +72,7 @@ public class Player { this.name = name; } + public void setInventory(ArrayList inventory) { + this.inventory = inventory; + } } diff --git a/src/main/resources/gameconfig.json b/src/main/resources/gameconfig.json index f3021c1..e3dda6b 100644 --- a/src/main/resources/gameconfig.json +++ b/src/main/resources/gameconfig.json @@ -1,9 +1,9 @@ { "shopitems": [ { - "gold": 100, - "amount": 500, - "name": "Fell" + "gold": 10, + "amount": 10, + "name": "Holz" }, { "gold": 1000, @@ -19,7 +19,8 @@ "locations": [ { "mobs": [{ - "damage": "10-15", + "isFriendly": true, + "damage": "15", "name": "Big Foot", "hp": 50, "items": ["Fell"] @@ -32,7 +33,8 @@ }, { "mobs": [{ - "damage": "10-15", + "isFriendly": true, + "damage": "15", "name": "Big Foot", "hp": 50, "items": ["Fell"] diff --git a/src/main/resources/items.json b/src/main/resources/items.json index 3841afc..3d4e8f2 100644 --- a/src/main/resources/items.json +++ b/src/main/resources/items.json @@ -4,6 +4,7 @@ "name": "Holz", "damage": "1-3", "crafting": true, + "doesDamage": true, "durability": 1000, "amountShop": 10, "gold": 10 @@ -12,6 +13,7 @@ "name": "Heiliges Schwert der Engel", "damage": "1000", "crafting": false, + "doesDamage": true, "durability": 1000, "amountShop": 1, "gold": 1000 @@ -20,6 +22,7 @@ "name": "Stein", "damage": "5-10", "crafting": true, + "doesDamage": true, "durability": 1000, "amountShop": 1500, "gold": 2 @@ -28,6 +31,7 @@ "name": "Fell", "damage": "0", "crafting": true, + "doesDamage": false, "durability": 1000, "amountShop": 500, "gold": 100 diff --git a/src/main/resources/playerconfig.json b/src/main/resources/playerconfig.json index 8b09e23..cd54cdc 100644 --- a/src/main/resources/playerconfig.json +++ b/src/main/resources/playerconfig.json @@ -4,14 +4,20 @@ "hp": 10, "inventory": [ { - "amount": "5", + "gold": 5, + "damage": "10", + "amount": 5, "durability": 1000, - "name": "Holz" + "name": "Holz", + "doesDamage": true }, { - "amount": "5", + "gold": 5, + "damage": "10", + "amount": 5, "durability": 1000, - "name": "Stein" + "name": "Stein", + "doesDamage": true } ], "currentLocation": "Wald" diff --git a/src/main/resources/quiz.json b/src/main/resources/quiz.json index ae61307..84f6b79 100644 --- a/src/main/resources/quiz.json +++ b/src/main/resources/quiz.json @@ -201,5 +201,5 @@ ] } ], - "lastPlayed": 1675768444160 + "lastPlayed": 1675852467225 } \ No newline at end of file diff --git a/src/test/java/org/bitbiome/commands/CollectCommandTest.java b/src/test/java/org/bitbiome/commands/CollectCommandTest.java new file mode 100644 index 0000000..75564dd --- /dev/null +++ b/src/test/java/org/bitbiome/commands/CollectCommandTest.java @@ -0,0 +1,26 @@ +package org.bitbiome.commands; + +import org.bitbiome.classes.JsonParser; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CollectCommandTest { + + @Test + public void testIncreaseAmountInPlayerConfig() { + CollectCommand command = new CollectCommand(); + JsonParser jp = new JsonParser(); + JSONObject o = jp.getJSONObject("src/main/resources/playerconfig.json"); + JSONArray inventory = o.getJSONArray("inventory"); + int k = 0; + int initialAmount = inventory.getJSONObject(k).getInt("amount"); + command.increaseAmountInPlayerConfig(inventory, k, jp, o); + int finalAmount = inventory.getJSONObject(k).getInt("amount"); + assertEquals(initialAmount + 1, finalAmount); + } + +} \ No newline at end of file diff --git a/src/test/java/org/bitbiome/commands/InventoryCommandTest.java b/src/test/java/org/bitbiome/commands/InventoryCommandTest.java new file mode 100644 index 0000000..5511feb --- /dev/null +++ b/src/test/java/org/bitbiome/commands/InventoryCommandTest.java @@ -0,0 +1,33 @@ +package org.bitbiome.commands; + +import org.bitbiome.classes.Colors; +import org.bitbiome.classes.TravelEngine; +import org.bitbiome.entities.Item; +import org.bitbiome.entities.Player; +import org.junit.jupiter.api.Test; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class InventoryCommandTest { + @Test + public void testReadInv(){ + InventoryCommand command = new InventoryCommand(); + TravelEngine travelEngine = new TravelEngine(new Player("Unit")); + ArrayList inventory = new ArrayList<>(); + inventory.add(new Item("Holz", false, "0",5, 3)); + inventory.add(new Item("Stein", true, "10", 5, 4)); + travelEngine.getPlayer().setInventory(inventory); + + String expectedResult = Colors.ANSI_PURPLE + "Du möchtest wissen, was in deinem Inventar ist? \n" + + "Dann lass uns gemeinsam deinen Rucksack öffnen. \nDein Rucksack steckt ja voller Überraschungen! \n" + + "Das hast du alles schon gefunden: \n" + Colors.ANSI_RESET + + Colors.ANSI_BG_PURPLE + Colors.ANSI_BRIGHT_WHITE + "- " + "Holz" + " x" + 5 + " " + Colors.ANSI_RESET + "\n" + + Colors.ANSI_BG_PURPLE + Colors.ANSI_BRIGHT_WHITE + "- " + "Stein" + " x" + 5 + " " + Colors.ANSI_RESET + "\n"; + + String result = command.readInv(travelEngine); + assertEquals(expectedResult, result); + } +} + + diff --git a/src/test/java/org/bitbiome/commands/LookaroundCommandTest.java b/src/test/java/org/bitbiome/commands/LookaroundCommandTest.java new file mode 100644 index 0000000..5916052 --- /dev/null +++ b/src/test/java/org/bitbiome/commands/LookaroundCommandTest.java @@ -0,0 +1,194 @@ +package org.bitbiome.commands; + +import org.bitbiome.classes.Colors; +import org.bitbiome.entities.Item; +import org.bitbiome.entities.Location; +import org.bitbiome.entities.Mob; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +public class LookaroundCommandTest { + private JSONArray locations; + + @Test + public void testGetLocationObject(){ + LookaroundCommand command = new LookaroundCommand(); + locations = new JSONArray(); + JSONObject location1 = new JSONObject(); + location1.put("name", "Wald"); + location1.put("description", "Es gibt Bäume und Sträucher"); + JSONObject location2 = new JSONObject(); + location2.put("name", "Strand"); + location2.put("description", "Weiter, weißer Sandstrand am Meer"); + locations.put(location1); + locations.put(location2); + + JSONObject result = command.getLocationObject("Wald", locations); + assertEquals("Wald", result.getString("name")); + assertEquals("Es gibt Bäume und Sträucher", result.getString("description")); + + result = command.getLocationObject("Strand", locations); + assertEquals("Strand", result.getString("name")); + assertEquals("Weiter, weißer Sandstrand am Meer", result.getString("description")); + + result = command.getLocationObject("not existing location", locations); + assertEquals("Wald", result.getString("name")); + assertEquals("Es gibt Bäume und Sträucher", result.getString("description")); + } + + @Test + public void testGetRandomMob() { + LookaroundCommand command = new LookaroundCommand(); + int randomNumberMobs = 2; + Random random = new Random(); + JSONArray mobs = new JSONArray(); + JSONObject mob1 = new JSONObject(); + mob1.put("name", "BigFoot"); + mob1.put("isFriendly", true); + mob1.put("hp", 10); + mob1.put("damage", "5"); + JSONObject mob2 = new JSONObject(); + mob2.put("name", "Yeti"); + mob2.put("isFriendly", false); + mob2.put("hp", 20); + mob2.put("damage", "10"); + mobs.put(mob1); + mobs.put(mob2); + ArrayList foundMobs = new ArrayList<>(); + ArrayList result = command.getRandomMob(randomNumberMobs, random, mobs, foundMobs); + + assertEquals(randomNumberMobs, result.size()); + for (Mob mob : result) { + assertTrue(mob.getName().equals("BigFoot") || mob.getName().equals("Yeti")); + } + } + @Test + public void testGetItemsOutputWithItems() { + LookaroundCommand command = new LookaroundCommand(); + int randomNumberItems = 3; + StringBuilder outputMessage = new StringBuilder(); + ArrayList foundItems = new ArrayList(); + foundItems.add(new Item("Holz", true, "10", 1,2)); + foundItems.add(new Item("Stein", true, "10", 1, 3)); + foundItems.add(new Item("Sand", false, "1", 1,3)); + command.getItemsOutput(randomNumberItems, outputMessage, foundItems); + String expectedOutput = Colors.ANSI_BLUE+ "Huch, was liegt denn hier rum?\n" + Colors.ANSI_RESET+ + "- Holz\n- Stein\n- Sand\n" + + Colors.ANSI_BLUE +"Schnell, sammel es ein!\n"+ Colors.ANSI_RESET; + assertEquals(expectedOutput, outputMessage.toString()); + } + @Test + public void testGetItemsOutputWithoutItems() { + LookaroundCommand command = new LookaroundCommand(); + int randomNumberItems = 0; + StringBuilder outputMessage = new StringBuilder(); + ArrayList foundItems = new ArrayList(); + command.getItemsOutput(randomNumberItems, outputMessage, foundItems); + String expectedOutput = Colors.ANSI_BLUE+ "Hier gibt es leider nichts für dich zum Einsammeln.\n"+ Colors.ANSI_RESET; + assertEquals(expectedOutput, outputMessage.toString()); + } + @Test + public void testGetMobsOutputWithMobs() { + LookaroundCommand command = new LookaroundCommand(); + int randomNumberMobs = 2; + int randomNumberItems = 0; + StringBuilder outputMessage = new StringBuilder(); + ArrayList foundMobs = new ArrayList<>(); + foundMobs.add(new Mob("Big Foot", true, 50, 15)); + foundMobs.add(new Mob("Yeti", false, 70, 30)); + + command.getMobsOutput( randomNumberItems,randomNumberMobs, outputMessage, foundMobs); + + String expectedOutput = Colors.ANSI_RED+"Achtung, hier lauern Gefahren!"+Colors.ANSI_RESET +"Sei auf der Hut vor: \n- Big Foot\n- Yeti\n"; + assertEquals(expectedOutput, outputMessage.toString()); + } + @Test + public void testGetMobsOutputWithoutMobsAndItems() { + LookaroundCommand command = new LookaroundCommand(); + int randomNumberMobs = 0; + int randomNumberItems = 0; + StringBuilder outputMessage = new StringBuilder(); + ArrayList foundMobs = new ArrayList<>(); + + command.getMobsOutput(randomNumberMobs, randomNumberItems, outputMessage, foundMobs); + + String expectedOutput = "Hier gibt es sonst nichts weiter zu sehen. Reise weiter!\n"; + assertEquals(expectedOutput, outputMessage.toString()); + } + @Test + void testGetRandomItem() throws Exception { + int randomNumberItems = 2; + JSONArray items = new JSONArray("[{\"name\":\"Holz\",\"doesDamage\":true,\"damage\":\"1.0\"},{\"name\":\"Stein\",\"doesDamage\":false,\"damage\":\"10.0\"}]"); + ArrayList result = new ArrayList<>(); + for (int i = 0; i < randomNumberItems; i++) { + JSONObject itemObject = items.getJSONObject(i); + Item item = new Item(itemObject.getString("name"), itemObject.getBoolean("doesDamage"), + itemObject.getString("damage"), 1, 3); + result.add(item); + } + assertEquals(2, result.size()); + assertEquals("Holz", result.get(0).getName()); + assertEquals("Stein", result.get(1).getName()); + } + @Test + public void testWaldDescription() { + LookaroundCommand command = new LookaroundCommand(); + ArrayList enemies = new ArrayList(); + enemies.add(new Mob("Bigfoot", false, 50,20)); + ArrayList items = new ArrayList(); + items.add(new Item("Holz",true, "10", 1,3)); + Location location = new Location("Wald",enemies, items); + StringBuilder outputMessage = new StringBuilder(); + + command.getLocationDescription(location, outputMessage); + + String expectedDescription = "Du befindest dich mitten im Wald, um dich herum siehst du hohe Buchen, kleine Sträucher und Farne.\n" + + "Der Boden ist mit weichem Moos, Pilzen und Laub bedeckt, in der Nähe hörst du Vögel munter zwitschern und\n" + + "einen kleinen Bach, der sich durch das dichte Unterholz schlängelt." + + " Schau mal, dort hinten in der Ferne ist ein Eichhörnchen! \n"; + + assertEquals(expectedDescription, outputMessage.toString()); + } + @Test + public void testStrandDescription() { + LookaroundCommand command = new LookaroundCommand(); + ArrayList enemies = new ArrayList(); + enemies.add(new Mob("Bigfoot", false, 50,20)); + ArrayList items = new ArrayList(); + items.add(new Item("Holz",true, "10", 1, 5)); + Location location = new Location("Strand",enemies, items); + StringBuilder outputMessage = new StringBuilder(); + + command.getLocationDescription(location, outputMessage); + + String expectedDescription = "Du befindest dich mitten am Strand und blickst auf das Meer, das sich bis zum Horizont erstreckt.\n" + + "Du spürst den Sand an deinen Füßen, du hörst das weiche Rauschen des Meeres und das Lachen der Möwen über dir.\n" + + "Rechts und links von dir erstreckt sich der weite, weiße Sandstrand, dort hinten bauen Kinder eine Sandburg.\n" + + "Es gibt ein paar Palmen, die den Strand säumen und weit in der Ferne ragen Felsen aus dem Meer.\n"; + + assertEquals(expectedDescription, outputMessage.toString()); + } + @Test + public void testUnknownLocationDescription() { + LookaroundCommand command = new LookaroundCommand(); + ArrayList enemies = new ArrayList(); + enemies.add(new Mob("unknown", false, 50,20)); + ArrayList items = new ArrayList(); + items.add(new Item("unknown",true, "10", 1, 5)); + Location location = new Location("Unknown",enemies, items); + StringBuilder outputMessage = new StringBuilder(); + + command.getLocationDescription(location, outputMessage); + + assertEquals("", outputMessage.toString()); + } + +}