aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZach Jordan <zxjordan5@gmail.com>2023-05-30 19:49:48 -0400
committerZach Jordan <zxjordan5@gmail.com>2023-05-30 19:49:48 -0400
commit010eebb242d9e229e7c3c30ea6396b650c7ace9c (patch)
tree9cf45f91409f6f9c971a1f91e08313cb8062e5ec
parent262c74969ac492a0d41cb968a3afbaa24671d92e (diff)
downloadNPEhero-010eebb242d9e229e7c3c30ea6396b650c7ace9c.tar.gz
NPEhero-010eebb242d9e229e7c3c30ea6396b650c7ace9c.tar.bz2
NPEhero-010eebb242d9e229e7c3c30ea6396b650c7ace9c.zip
finished difficulty stuff, hope it works
-rw-r--r--src/main/Difficulty.java45
1 files changed, 24 insertions, 21 deletions
diff --git a/src/main/Difficulty.java b/src/main/Difficulty.java
index 03460ee..9812c04 100644
--- a/src/main/Difficulty.java
+++ b/src/main/Difficulty.java
@@ -3,12 +3,14 @@ package main;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import javax.lang.model.util.ElementScanner14;
+import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@@ -25,8 +27,12 @@ public class Difficulty
public int numBeats;
public JSONObject diffStuff;
public JSONObject leaderboardStuff;
+ public LeaderboardEntry[] fileLeaderboard;
+ private String filepath;
+
- public void parseMetadata(File file) {
+ public void parseMetadata(File file)
+ {
JSONParser jsonParser = new JSONParser(); //parser to read the file
try(FileReader reader = new FileReader(file))
@@ -57,30 +63,19 @@ public class Difficulty
//and here
//leaderboard.add(new LeaderboardEntry("placeholderScore", 0, "0/0/0"));
- String tracker = "leaderBoardEntry1"; //you gotta follow this format in ascending order in order for the file to be read properly
- //basically this tracker is going to be used as the changing key name to be read in from the file. the substring later on is to be used to increase its value.
- boolean jsonHasNext = true;
JSONParser jsonParser = new JSONParser(); //parser to read the file
+
+ filepath = file.getPath();
try(FileReader reader = new FileReader(file))
{
Object obj = jsonParser.parse(reader);
- leaderboardStuff = (JSONObject)(obj); //converts read object to a JSONObject
- while(jsonHasNext)
- {
- if(leaderboardStuff.containsKey(tracker))
- {
- int num = (tracker.charAt(tracker.length()-1)) + 1; //get the number at the end of tracker and increase it by one
- tracker = tracker.substring(0, tracker.length()-1) + num; //substring tracker to take off the end number and add the new end number.
+ leaderboardStuff = (JSONObject)(obj); //converts read object to a JSONArray
+
+ fileLeaderboard = (LeaderboardEntry[]) leaderboardStuff.get("leaderboard");
- //read in the actual leaderboard stuff now
- }
- else
- {
- jsonHasNext = false;
- }
- }
+ leaderboard.addAll(fileLeaderboard);
}
catch (FileNotFoundException e)
{
@@ -93,12 +88,20 @@ public class Difficulty
{
e.printStackTrace();
}
-
}
- public void addToLeaderboard(String name, int score) {
+ public void addToLeaderboard(String name, int score)
+ {
leaderboard.add(new LeaderboardEntry(name, score, ""+LocalDate.now())); //do not delete this tho its not a placeholder
- //and make this write to the json also
+ try (FileWriter fileWriter = new FileWriter(filepath))
+ {
+ //write the settings JSONObject instance to the file
+ fileWriter.write(((JSONArray) leaderboard).toJSONString());
+ fileWriter.flush();
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
public ObservableList<LeaderboardEntry> getLeaderboard() {