|
|
@ -1,10 +1,8 @@ |
|
|
|
package org.bitbiome.shop; |
|
|
|
package org.bitbiome.classes; |
|
|
|
|
|
|
|
import org.bitbiome.classes.Colors; |
|
|
|
import org.bitbiome.classes.JsonParser; |
|
|
|
import org.bitbiome.classes.TravelEngine; |
|
|
|
import org.bitbiome.commands.BlackJackCommand; |
|
|
|
import org.bitbiome.commands.QuizCommand; |
|
|
|
import org.bitbiome.entities.Item; |
|
|
|
import org.json.JSONArray; |
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
@ -19,7 +17,6 @@ import java.util.Scanner; |
|
|
|
public class Shop { |
|
|
|
public ArrayList<Item> allItems; |
|
|
|
public ArrayList<Item> currentShopItems; |
|
|
|
public JsonParser jsonParser = new JsonParser(); |
|
|
|
public QuizCommand quizCommand = new QuizCommand(); |
|
|
|
public BlackJackCommand blackJackCommand = new BlackJackCommand(); |
|
|
|
public Scanner scanner; |
|
|
@ -66,18 +63,21 @@ public class Shop { |
|
|
|
} |
|
|
|
} |
|
|
|
if (itemIndex == -1) { |
|
|
|
System.out.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Dieses Item gibt es nicht!" + Colors.ANSI_RESET); |
|
|
|
System.out.println( |
|
|
|
Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Dieses Item gibt es nicht!" + Colors.ANSI_RESET); |
|
|
|
return false; |
|
|
|
} |
|
|
|
if(!(currentShopItems.get(itemIndex).amount > 0)){ |
|
|
|
System.out.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Es gibt zu wenige Items!" + Colors.ANSI_RESET); |
|
|
|
if (!(currentShopItems.get(itemIndex).getAmount() > 0)) { |
|
|
|
System.out.println( |
|
|
|
Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Es gibt zu wenige Items!" + Colors.ANSI_RESET); |
|
|
|
return false; |
|
|
|
} |
|
|
|
// Test if the player has enough gold |
|
|
|
int costs = currentShopItems.get(itemIndex).gold * amount; |
|
|
|
int costs = currentShopItems.get(itemIndex).getGold() * amount; |
|
|
|
int gold = (int) playerConfig.get("gold"); |
|
|
|
if (!(gold >= costs)) { |
|
|
|
System.out.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Du hast zu wenig Gold!" + Colors.ANSI_RESET); |
|
|
|
System.out |
|
|
|
.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Du hast zu wenig Gold!" + Colors.ANSI_RESET); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
@ -96,7 +96,7 @@ public class Shop { |
|
|
|
tempJSON.put("amount", intNewAmount); |
|
|
|
jsonArray2.put(tempJSON); |
|
|
|
gameConfig.put("shopitems", jsonArray2); |
|
|
|
jsonParser.writeObject("gameconfig.json", gameConfig); |
|
|
|
JsonParser.writeObject("src/main/resources/gameconfig.json", gameConfig); |
|
|
|
currentShopItems = loadCurrentShopItems(); |
|
|
|
break; |
|
|
|
} |
|
|
@ -113,7 +113,7 @@ public class Shop { |
|
|
|
tempJSON.put("amount", newAmount); |
|
|
|
jsonArray.put(tempJSON); |
|
|
|
playerConfig.put("inventory", jsonArray); |
|
|
|
jsonParser.writeObject("playerconfig.json", playerConfig); |
|
|
|
JsonParser.writeObject("src/main/resources/playerconfig.json", playerConfig); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
@ -134,7 +134,7 @@ public class Shop { |
|
|
|
|
|
|
|
jsonArray.put(inventory); |
|
|
|
playerConfig.put("inventory", jsonArray); |
|
|
|
jsonParser.writeObject("playerconfig.json", playerConfig); |
|
|
|
JsonParser.writeObject("src/main/resources/playerconfig.json", playerConfig); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
@ -143,51 +143,70 @@ public class Shop { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
private ArrayList loadAllItems(){ |
|
|
|
private ArrayList<Item> loadAllItems() { |
|
|
|
ArrayList<Item> arrayList = new ArrayList<>(); |
|
|
|
JSONArray itemJSON = returnJSONArrayOfAllItems(); |
|
|
|
|
|
|
|
arrayList = addToList(itemJSON, arrayList, "amountShop"); |
|
|
|
return arrayList; |
|
|
|
} |
|
|
|
|
|
|
|
public JSONArray returnJSONArrayOfAllItems() { |
|
|
|
File file = new File("src/main/resources/items.json"); |
|
|
|
ArrayList arrayList = new ArrayList<Item>(); |
|
|
|
JSONArray itemJSON = null; |
|
|
|
try { |
|
|
|
String content3 = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8"); |
|
|
|
JSONArray itemJSON = new JSONArray(content3); |
|
|
|
|
|
|
|
for(int i = 0; i < itemJSON.length(); i++){ |
|
|
|
JSONObject tempJSON = itemJSON.getJSONObject(i); |
|
|
|
arrayList.add(new Item(tempJSON.getString("name"), tempJSON.getInt("amountShop"), tempJSON.getInt("gold"))); |
|
|
|
} |
|
|
|
itemJSON = new JSONArray(content3); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
return arrayList; |
|
|
|
return itemJSON; |
|
|
|
} |
|
|
|
|
|
|
|
public ArrayList loadCurrentShopItems(){ |
|
|
|
File file = new File("src/main/resources/gameconfig.json"); |
|
|
|
ArrayList arrayList = new ArrayList<Item>(); |
|
|
|
try { |
|
|
|
String content = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8"); |
|
|
|
JSONObject jsonObject = new JSONObject(content); |
|
|
|
public ArrayList<Item> loadCurrentShopItems() { |
|
|
|
|
|
|
|
ArrayList<Item> arrayList = new ArrayList<>(); |
|
|
|
JSONObject jsonObject = JsonParser.getJSONObject("src/main/resources/gameconfig.json"); |
|
|
|
JSONArray jsonArray = jsonObject.getJSONArray("shopitems"); |
|
|
|
for(int i = 0; i < jsonArray.length(); i++){ |
|
|
|
JSONObject tempJSON = jsonArray.getJSONObject(i); |
|
|
|
arrayList.add(new Item(tempJSON.getString("name"), tempJSON.getInt("amount"), tempJSON.getInt("gold"))); |
|
|
|
|
|
|
|
return addToList(jsonArray, arrayList, "amount"); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
public JSONObject getItemByName(String itemName, JSONArray itemArray) { |
|
|
|
for (int i = 0; i < itemArray.length(); i++) { |
|
|
|
if (itemArray.getJSONObject(i).getString("name").equals(itemName)) { |
|
|
|
return itemArray.getJSONObject(i); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public ArrayList<Item> addToList(JSONArray itemJSON, ArrayList<Item> arrayList, String key) { |
|
|
|
JSONArray allItems = returnJSONArrayOfAllItems(); |
|
|
|
for (int i = 0; i < itemJSON.length(); i++) { |
|
|
|
JSONObject tempJSON = itemJSON.getJSONObject(i); |
|
|
|
String damage; |
|
|
|
boolean doesDmg; |
|
|
|
if (key.equals("amount")) { |
|
|
|
damage = getItemByName(tempJSON.getString("name"), allItems).getString("damage"); |
|
|
|
} else { |
|
|
|
damage = tempJSON.getString("damage"); |
|
|
|
} |
|
|
|
doesDmg = !damage.equals("0") ? false : true; |
|
|
|
arrayList.add(new Item(tempJSON.getString("name"), doesDmg, damage, tempJSON.getInt(key), |
|
|
|
tempJSON.getInt("gold"))); |
|
|
|
} |
|
|
|
return arrayList; |
|
|
|
} |
|
|
|
|
|
|
|
private ArrayList loadPartofItems(ArrayList<Item> alleItems, int itemCount) { |
|
|
|
ArrayList arrayList = new ArrayList<Item>(); |
|
|
|
private ArrayList<Item> loadPartofItems(ArrayList<Item> alleItems, int itemCount) { |
|
|
|
ArrayList<Item> arrayList = new ArrayList<>(); |
|
|
|
try { |
|
|
|
File fileGameConfig = new File("src/main/resources/gameconfig.json"); |
|
|
|
String content2 = new String(Files.readAllBytes(Paths.get(fileGameConfig.toURI())), "UTF-8"); |
|
|
|
JSONObject gameConfig = new JSONObject(content2); |
|
|
|
|
|
|
|
JSONArray jsonArray = gameConfig.getJSONArray("shopitems"); |
|
|
|
//JSONArray jsonArray = gameConfig.getJSONArray("shopitems"); |
|
|
|
HashSet<Integer> hashSet = new HashSet<>(); |
|
|
|
JSONArray shopitems = new JSONArray(); |
|
|
|
Random random = new Random(); |
|
|
@ -197,23 +216,21 @@ public class Shop { |
|
|
|
hashSet.add(rand); |
|
|
|
arrayList.add(alleItems.get(rand)); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put("name", alleItems.get(rand).name); |
|
|
|
jsonObject.put("amount", alleItems.get(rand).amount); |
|
|
|
jsonObject.put("gold", alleItems.get(rand).gold); |
|
|
|
jsonObject.put("name", alleItems.get(rand).getName()); |
|
|
|
jsonObject.put("amount", alleItems.get(rand).getAmount()); |
|
|
|
jsonObject.put("gold", alleItems.get(rand).getGold()); |
|
|
|
shopitems.put(jsonObject); |
|
|
|
} |
|
|
|
} |
|
|
|
// write in gameconfig.json |
|
|
|
gameConfig.remove("shopitems"); |
|
|
|
gameConfig.put("shopitems", shopitems); |
|
|
|
jsonParser.writeObject("gameconfig.json", gameConfig); |
|
|
|
|
|
|
|
JsonParser.writeObject("src/main/resources/gameconfig.json", gameConfig); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return arrayList; |
|
|
|
} |
|
|
|
|
|
|
@ -238,11 +255,13 @@ public class Shop { |
|
|
|
blackJackCommand.performCommand(scanner, isRunning, message, travelEngine); |
|
|
|
System.out.println(""); |
|
|
|
} |
|
|
|
|
|
|
|
private void printArrayList(ArrayList<Item> arrayList) { |
|
|
|
System.out.println(""); |
|
|
|
for (int i = 0; i < arrayList.size(); i++) { |
|
|
|
if (arrayList.get(i).getAmount() != 0) { |
|
|
|
System.out.println(arrayList.get(i).getName() + " | Anzahl: " + arrayList.get(i).getAmount() + " | Kosten: " + arrayList.get(i).getGold()); |
|
|
|
System.out.println(arrayList.get(i).getName() + " | Anzahl: " + arrayList.get(i).getAmount() |
|
|
|
+ " | Kosten: " + arrayList.get(i).getGold()); |
|
|
|
} |
|
|
|
} |
|
|
|
System.out.println(""); |