diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2024-10-15 02:09:12 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2024-10-15 02:09:12 -0400 |
commit | 70d3f3a9d45fdb12d1eaf937b4c7ab90df6a8545 (patch) | |
tree | f5e7a02f4273bb8d4dd02e77067d2c433def4ca2 /src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java | |
parent | 84b072a945bb5b0b541c9ca6c30af56dc39ff631 (diff) | |
download | NPEhero-70d3f3a9d45fdb12d1eaf937b4c7ab90df6a8545.tar.gz NPEhero-70d3f3a9d45fdb12d1eaf937b4c7ab90df6a8545.tar.bz2 NPEhero-70d3f3a9d45fdb12d1eaf937b4c7ab90df6a8545.zip |
Improve GameOver and other fixes
Diffstat (limited to 'src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java')
-rw-r--r-- | src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java b/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java index 82f9aed..395e2c1 100644 --- a/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java +++ b/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java @@ -10,6 +10,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.time.LocalDate; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,10 +19,10 @@ public class Leaderboard { public final ObservableList<LeaderboardEntry> entries = FXCollections.observableArrayList(); private final Gson json = new GsonBuilder().serializeNulls().setPrettyPrinting().create(); - private final File file; + private final File jsonFile; public Leaderboard(File file) throws IOException{ - this.file = file; + this.jsonFile = file; read(); } @@ -33,17 +34,22 @@ public class Leaderboard { */ public void add(String name, int score) throws IOException { entries.add(new LeaderboardEntry(name, score, LocalDate.now().toString())); - save(); + write(); } /** * Writes leaderboard to json file * @throws IOException If there are problems writing to the file. */ - public void save() throws IOException { - file.createNewFile(); + public void write() throws IOException { + if (!jsonFile.exists() && !jsonFile.createNewFile()) { + throw new IOException("Could not create file " + jsonFile.getAbsolutePath()); + } @SuppressWarnings("unchecked") - List<Map<String, Object>> data = json.fromJson(new FileReader(file), List.class); + List<Map<String, Object>> data = json.fromJson(new FileReader(jsonFile), List.class); + if (data == null) { + data = new ArrayList<>(); + } for (LeaderboardEntry cur : entries) { Map<String, Object> obj = new HashMap<>(); obj.put("name", cur.name); @@ -51,7 +57,7 @@ public class Leaderboard { obj.put("date", cur.date); data.add(obj); } - FileWriter fileWriter = new FileWriter(file); + FileWriter fileWriter = new FileWriter(jsonFile); json.toJson(data, fileWriter); fileWriter.close(); } @@ -61,11 +67,11 @@ public class Leaderboard { * @throws IOException If there are problems reading the file */ public void read() throws IOException { - if (!file.exists()) { + if (!jsonFile.exists()) { return; } @SuppressWarnings("unchecked") - List<Map<String, Object>> data = json.fromJson(new FileReader(file), List.class); + List<Map<String, Object>> data = json.fromJson(new FileReader(jsonFile), List.class); if (data == null) { return; } |