aboutsummaryrefslogtreecommitdiff
path: root/src/main/Difficulty.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/Difficulty.java')
-rw-r--r--src/main/Difficulty.java106
1 files changed, 71 insertions, 35 deletions
diff --git a/src/main/Difficulty.java b/src/main/Difficulty.java
index 2c74956..abb12f4 100644
--- a/src/main/Difficulty.java
+++ b/src/main/Difficulty.java
@@ -1,62 +1,64 @@
package main;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
-
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
-
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class Difficulty
{
public File thisDir;
- public String title;
+ public String title = "Unnamed";
private ObservableList<LeaderboardEntry> leaderboard = FXCollections.observableArrayList();
public File notes;
- public int bpm;
- public File song;
+ public Double bpm = 0.0;
public int numBeats;
- private File leaderboardFile;
+ public Level level;
- public Difficulty(File file)
+ /**
+ * Creates a new Difficulty and gives it a file path
+ * @param newDir: The file path of the Difficulty
+ */
+ public Difficulty(File newDir, Level level)
{
- thisDir = file;
- readData();
+ thisDir = newDir;
+ this.level = level;
}
+ /**
+ * Checks for files in the difficulty folder and runs cooresponding actions
+ */
public void readData()
{
for(File cur: thisDir.listFiles()) //iterates through all files/folders in src/assets/levels/LEVEL/DIFFICULTY
{
if (cur.getName().equals("metadata.json"))
{
- parseMetadata(cur);
+ parseMetadata();
}
if (cur.getName().equals("leaderboard.json"))
{
- parseLeaderboard(cur);
+ parseLeaderboard();
}
if (cur.getName().equals("notes.txt"))
{
notes = cur;
}
- if (cur.getName().equals("song.wav"))
- {
- song = cur;
- }
}
}
-
- public void parseMetadata(File file)
+ /**
+ * Reads in json metadata and assigns values to variables
+ */
+ public void parseMetadata()
{
+ File file = new File(thisDir, "metadata.json");
JSONParser jsonParser = new JSONParser(); //parser to read the file
try(FileReader reader = new FileReader(file))
@@ -65,20 +67,44 @@ public class Difficulty
JSONObject diffStuff = (JSONObject)(obj); //converts read object to a JSONObject
title = (String) diffStuff.get("title");
- bpm = (int)(diffStuff.get("bpm"));
- numBeats = (int)(diffStuff.get("numBeats"));
-
+ bpm = Double.parseDouble(diffStuff.get("bpm")+"");
+ numBeats = Integer.parseInt(diffStuff.get("numBeats")+"");
}
catch (Exception e)
{
- System.out.println("Error in json file "+file);
- //e.printStackTrace();
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Writes metadata to json file
+ */
+ public void writeMetadata()
+ {
+ FileWriter fileWriter;
+ try
+ {
+ File file = new File(thisDir, "metadata.json");
+ fileWriter = new FileWriter(file);
+ JSONObject obj = new JSONObject();
+ obj.put("title", title);
+ obj.put("bpm", bpm);
+ obj.put("numBeats", numBeats);
+ obj.writeJSONString(fileWriter);
+ fileWriter.flush();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
}
}
- public void parseLeaderboard(File file)
+ /**
+ * Reads in json leaderboard and assigns populates list with leaderboardEntries
+ */
+ public void parseLeaderboard()
{
- leaderboardFile = file;
+ File file = new File(thisDir, "leaderboard.json");
JSONParser jsonParser = new JSONParser(); //parser to read the file
try(FileReader reader = new FileReader(file))
@@ -99,17 +125,20 @@ public class Difficulty
}
catch (Exception e)
{
- System.out.println("Error in json file "+leaderboardFile);
- //e.printStackTrace();
+ e.printStackTrace();
}
}
- public void addToLeaderboard(String name, int score)
+ /**
+ * Writes leaderboard to json file
+ */
+ public void writeLeaderboard()
{
- leaderboard.add(new LeaderboardEntry(name, score, ""+LocalDate.now())); //do not delete this tho its not a placeholder
-
- try (FileWriter fileWriter = new FileWriter(leaderboardFile))
+ FileWriter fileWriter;
+ try
{
+ File file = new File(thisDir, "leaderboard.json");
+ fileWriter = new FileWriter(file);
//write the settings JSONObject instance to the file
JSONArray jsonArray = new JSONArray();
for (LeaderboardEntry cur: leaderboard)
@@ -129,6 +158,17 @@ public class Difficulty
}
}
+ /**
+ * Adds new leaderboardEntry to list and updates json file
+ * @param name: the players name
+ * @param score the players 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
+ writeLeaderboard();
+ }
+
public ObservableList<LeaderboardEntry> getLeaderboard()
{
return leaderboard;
@@ -138,8 +178,4 @@ public class Difficulty
{
return title;
}
-
- public void writeMetadata() {
-
- }
}