Browse Source

Hotfix for merge

remotes/origin/develop
David Hermann 2 years ago
parent
commit
d9d97cd7c2
  1. 3
      src/main/java/org/bitbiome/Boot.java
  2. 4
      src/main/java/org/bitbiome/classes/InteractionLoop.java
  3. 19
      src/main/java/org/bitbiome/classes/JsonParser.java
  4. 11
      src/main/java/org/bitbiome/classes/TravelEngine.java
  5. 3
      src/main/java/org/bitbiome/commands/BlackJackCommand.java
  6. 12
      src/main/java/org/bitbiome/commands/QuizCommand.java
  7. 5
      src/main/java/org/bitbiome/entities/Player.java
  8. 31
      src/main/resources/playerconfig.json
  9. 2
      src/main/resources/quiz.json

3
src/main/java/org/bitbiome/Boot.java

@ -26,8 +26,7 @@ public class Boot {
private Player getPlayerSave() { private Player getPlayerSave() {
String name; String name;
JsonParser jp = new JsonParser();
JSONObject playerconfig = jp.getJSONObject("playerconfig.json");
JSONObject playerconfig = JsonParser.getJSONObject("src/main/resources/playerconfig.json");
name = playerconfig.getString("name"); name = playerconfig.getString("name");
return new Player(name); return new Player(name);
} }

4
src/main/java/org/bitbiome/classes/InteractionLoop.java

@ -40,10 +40,10 @@ public class InteractionLoop {
+ Colors.ANSI_RESET); + Colors.ANSI_RESET);
String name = input.nextLine(); String name = input.nextLine();
JsonParser jp = new JsonParser(); JsonParser jp = new JsonParser();
JSONObject playerconf = jp.getJSONObject("playerconfig.json");
JSONObject playerconf = JsonParser.getJSONObject("src/main/resources/playerconfig.json");
playerconf.put("name", name); playerconf.put("name", name);
travelEngine.getPlayer().setName(name); travelEngine.getPlayer().setName(name);
jp.writeObject("playerconfig.json", playerconf);
JsonParser.writeObject("src/main/resources/playerconfig.json", playerconf);
} }
} }

19
src/main/java/org/bitbiome/classes/JsonParser.java

@ -1,13 +1,10 @@
package org.bitbiome.classes; package org.bitbiome.classes;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.FileReader; import java.io.FileReader;
import java.io.InputStream;
public class JsonParser { public class JsonParser {
@ -26,7 +23,7 @@ public class JsonParser {
https://github.com/stleary/JSON-java https://github.com/stleary/JSON-java
*/ */
public static JSONObject readJSONFile(String filePath) {
public static JSONObject getJSONObject(String filePath) {
StringBuilder sb = null; StringBuilder sb = null;
try { try {
FileReader reader = new FileReader(filePath); FileReader reader = new FileReader(filePath);
@ -58,18 +55,4 @@ public class JsonParser {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
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);
}
}
} }

11
src/main/java/org/bitbiome/classes/TravelEngine.java

@ -10,21 +10,19 @@ import java.util.ArrayList;
public class TravelEngine { public class TravelEngine {
private JSONArray locations; private JSONArray locations;
private JsonParser jp;
private Player player; private Player player;
public TravelEngine(Player player) { public TravelEngine(Player player) {
jp = new JsonParser();
locations = jp.getJSONObject("gameconfig.json").getJSONArray("locations");
locations = JsonParser.getJSONObject("src/main/resources/gameconfig.json").getJSONArray("locations");
this.player = player; this.player = player;
} }
public void travelTo(Location location) { public void travelTo(Location location) {
player.setLocation(location); player.setLocation(location);
JSONObject jObj = jp.getJSONObject("playerconfig.json");
JSONObject jObj = JsonParser.getJSONObject("src/main/resources/playerconfig.json");
jObj.put("currentLocation", location.getName()); jObj.put("currentLocation", location.getName());
jp.writeObject("playerconfig.json", jObj);
JsonParser.writeObject("src/main/resources/playerconfig.json", jObj);
} }
public Player getPlayer() { public Player getPlayer() {
@ -45,8 +43,7 @@ public class TravelEngine {
} }
public Location getLocationByName(String name) { public Location getLocationByName(String name) {
JsonParser jp = new JsonParser();
JSONObject gameconfig = jp.getJSONObject("gameconfig.json");
JSONObject gameconfig = JsonParser.getJSONObject("src/main/resources/gameconfig.json");
JSONArray locations = gameconfig.getJSONArray("locations"); JSONArray locations = gameconfig.getJSONArray("locations");
JSONObject location = null; JSONObject location = null;
if (locationExists(name)) { if (locationExists(name)) {

3
src/main/java/org/bitbiome/commands/BlackJackCommand.java

@ -2,6 +2,7 @@ package org.bitbiome.commands;
import org.bitbiome.classes.BlackJack; import org.bitbiome.classes.BlackJack;
import org.bitbiome.classes.TravelEngine;
import java.util.Random; import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
@ -9,7 +10,7 @@ import java.util.Scanner;
public class BlackJackCommand implements CommandAPI { public class BlackJackCommand implements CommandAPI {
private boolean over; private boolean over;
@Override @Override
public void performCommand(Scanner scanner, boolean isRunning, String message) {
public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) {
System.out.println("Du hast das Spiel BlackJack gestartet. Die Spielregeln lauten wie folgt: Du und dein Gegner bekommen jede Runde Zahlen von 4 - 11. \nDerjenige, der zuerst 21 Punkte hat gewinnt. Derjenige, der über 21 Punkte hat verliert. Möchte keiner mehr Karten ziehen, gewinnt der mit dem höchsten Blatt!\nViel Spaß!"); System.out.println("Du hast das Spiel BlackJack gestartet. Die Spielregeln lauten wie folgt: Du und dein Gegner bekommen jede Runde Zahlen von 4 - 11. \nDerjenige, der zuerst 21 Punkte hat gewinnt. Derjenige, der über 21 Punkte hat verliert. Möchte keiner mehr Karten ziehen, gewinnt der mit dem höchsten Blatt!\nViel Spaß!");

12
src/main/java/org/bitbiome/commands/QuizCommand.java

@ -4,6 +4,8 @@ package org.bitbiome.commands;
import org.bitbiome.classes.JsonParser; import org.bitbiome.classes.JsonParser;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.bitbiome.classes.TravelEngine;
import java.util.Date; import java.util.Date;
import java.util.Random; import java.util.Random;
@ -12,11 +14,11 @@ import java.util.Scanner;
public class QuizCommand implements CommandAPI { public class QuizCommand implements CommandAPI {
private Scanner quizScanner; private Scanner quizScanner;
@Override @Override
public void performCommand(Scanner scanner, boolean isRunning, String message) {
public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) {
quizScanner = new Scanner(System.in); quizScanner = new Scanner(System.in);
String path = "src\\main\\resources\\quiz.json";
JSONObject quiz = JsonParser.readJSONFile(path);
String path = "src/main/resources/quiz.json";
JSONObject quiz = JsonParser.getJSONObject(path);
long diffTime = canPlayAgain(quiz.getLong("lastPlayed")); long diffTime = canPlayAgain(quiz.getLong("lastPlayed"));
if (diffTime > 0) { if (diffTime > 0) {
@ -76,8 +78,8 @@ public class QuizCommand implements CommandAPI {
} }
public static int addGold() { public static int addGold() {
String playerpath = "src\\main\\resources\\playerconfig.json";
JSONObject playerconfig = JsonParser.readJSONFile(playerpath);
String playerpath = "src/main/resources/ssplayerconfig.json";
JSONObject playerconfig = JsonParser.getJSONObject(playerpath);
int gold = playerconfig.getInt("gold"); int gold = playerconfig.getInt("gold");
gold = gold + 5; gold = gold + 5;
playerconfig.put("gold", gold); playerconfig.put("gold", gold);

5
src/main/java/org/bitbiome/entities/Player.java

@ -12,13 +12,12 @@ public class Player {
private ArrayList<Item> inventory; private ArrayList<Item> inventory;
private JsonParser jp;
public Player(String name) { public Player(String name) {
jp = new JsonParser();
this.name = name; this.name = name;
hp = 100.0F; hp = 100.0F;
location = new Location(jp.getJSONObject("playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>());
location = new Location(JsonParser.getJSONObject("src/main/resources/playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>());
inventory = new ArrayList<>(); inventory = new ArrayList<>();
} }

31
src/main/resources/playerconfig.json

@ -1,17 +1,18 @@
{ {
"name": "null",
"gold": 0,
"hp": 10,
"currentLocation": "Wald",
"inventory": [
{
"name": "Holz",
"amount": "5",
"durability": 1000
},{
"name": "Stein",
"amount": "5",
"durability": 1000
}
]
"gold": 0,
"name": "Dave",
"hp": 10,
"inventory": [
{
"amount": "5",
"durability": 1000,
"name": "Holz"
},
{
"amount": "5",
"durability": 1000,
"name": "Stein"
}
],
"currentLocation": "Wald"
} }

2
src/main/resources/quiz.json

@ -201,5 +201,5 @@
] ]
} }
], ],
"lastPlayed": 0
"lastPlayed": 1675768444160
} }
Loading…
Cancel
Save