aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/levelapi/Leaderboard.java
blob: bb1f30c5b0479ba2269b140b2e09a20124840bf4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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));
        }
    }
}