diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2024-07-28 01:07:41 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2024-07-28 01:07:41 -0400 |
commit | 0ce09f72f4af26412356b9699d402b52dbcfc94f (patch) | |
tree | b01b94b1b80d1f3fc5aea559b3718024b79cfe91 /src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java | |
parent | d04c277edff957d14b6261dd38da43c18b7ba189 (diff) | |
download | NPEhero-0ce09f72f4af26412356b9699d402b52dbcfc94f.tar.gz NPEhero-0ce09f72f4af26412356b9699d402b52dbcfc94f.tar.bz2 NPEhero-0ce09f72f4af26412356b9699d402b52dbcfc94f.zip |
Finalize level API and new Json library
Diffstat (limited to 'src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java')
-rw-r--r-- | src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java b/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java new file mode 100644 index 0000000..bb1f30c --- /dev/null +++ b/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java @@ -0,0 +1,81 @@ +package net.sowgro.npehero.levelapi; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Leaderboard { + + public final ObservableList<LeaderboardEntry> entries = FXCollections.observableArrayList(); + private final Gson json = new GsonBuilder().serializeNulls().setPrettyPrinting().create(); + private final File file; + + public Leaderboard(File file) throws IOException{ + this.file = file; + read(); + } + + /** + * Adds new leaderboardEntry to list and updates json file + * @param name: The players name + * @param score The players score + * @throws IOException If there is a problem updating the leaderboard file. + */ + public void add(String name, int score) throws IOException { + entries.add(new LeaderboardEntry(name, score, LocalDate.now().toString())); + save(); + } + + /** + * Writes leaderboard to json file + * @throws IOException If there are problems writing to the file. + */ + public void save() throws IOException { + file.createNewFile(); + List<Map<String, Object>> data = json.fromJson(new FileReader(file), List.class); + for (LeaderboardEntry cur : entries) { + Map<String, Object> obj = new HashMap<>(); + obj.put("name", cur.name); + obj.put("score", cur.score); + obj.put("date", cur.date); + data.add(obj); + } + FileWriter fileWriter = new FileWriter(file); + json.toJson(data, fileWriter); + fileWriter.close(); + } + + /** + * Reads in json leaderboard and assigns populates list with leaderboardEntries + * @throws IOException If there are problems reading the file + */ + public void read() throws IOException { + if (!file.exists()) { + return; + } + List<Map<String, Object>> data = json.fromJson(new FileReader(file), List.class); + if (data == null) { + return; + } + for (Map<String, Object> cur: data) { + String name = (String) cur.getOrDefault("name", null); + int score = (int) (double) cur.getOrDefault("score", -1); + String date = (String) cur.getOrDefault("date", null); + if (name == null || score == -1 || date == null) { + System.out.println("dbg: bad entry skipped"); + continue; // discard invalid entries + } + entries.add(new LeaderboardEntry(name, score, date)); + } + } +} |