aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/levelapi/Difficulty.java
blob: 2e99a7afd0ffa8acc1c07ed3b2ff0c33cdb03848 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package net.sowgro.npehero.levelapi;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;

import java.io.*;
import java.util.Map;

/**
 * Represents a difficulty
 * Responsible for the data in metadata.yml
 */
public class Difficulty implements Comparable<Difficulty>
{
    public final File thisDir;
    public final Level level;

    public String title = "Unnamed";
    public Double bpm = 0.0;
    public double endTime = 0;
    public int order = 0;

    public final Leaderboard leaderboard;
    public final Notes notes;

    private final Gson jsonParser = new GsonBuilder().serializeNulls().setPrettyPrinting().setNumberToNumberStrategy(ToNumberPolicy.DOUBLE).create();
    private final File jsonFile;

    /**
     * Creates a new Difficulty
     * @param newDir: The file path of the Difficulty
     * @throws IOException If there are any problems reading the metadata or leaderboard files
     */
    public Difficulty(File newDir, Level level) throws IOException {
        thisDir = newDir;
        this.level = level;
        jsonFile = new File(thisDir, "metadata.json");
        readMetadata();
        notes = new Notes(new File(thisDir, "notes.txt"), this); // needs metadata first
        leaderboard = new Leaderboard(new File(thisDir, "leaderboard.json"));
    }

    /**
     * Read in the data from metadata.json
     * @throws IOException If there are any problems loading the file.
     */
    public void readMetadata() throws IOException {
        if (!jsonFile.exists()) {
            return;
        }
        Map<String, Object> data = jsonParser.fromJson(new FileReader(jsonFile), Map.class);

        title = (String) data.getOrDefault("title", title);
        bpm = (Double) data.getOrDefault("bpm", bpm);
        endTime = (double) data.getOrDefault("endTime", endTime);
        if (endTime == 0) {
            int tmp = (int) (double) data.getOrDefault("numBeats", 0.0);
            if (tmp != 0) {
                endTime = Notes.beatToSecond(tmp, bpm);
            }
        }
        order = (int) (double) data.getOrDefault("priority", (double) order);
    }

    /**
     * Checks the validity of the difficulty
     * <p>
     * A valid difficulty has at least one note
     * @return True if the difficulty is valid
     */
    public boolean isValid() {
        return !notes.list.isEmpty();
    }

    /**
     * Writes metadata to json file
     * @throws IOException If there is a problem writing to the file
     */
    public void writeMetadata() throws IOException {
        jsonFile.createNewFile();
        Map<String, Object> data = jsonParser.fromJson(new FileReader(jsonFile), Map.class); // start with previous values
        data.put("title", title);
        data.put("endTime", endTime);
        data.put("priority", order);
        FileWriter fileWriter = new FileWriter(jsonFile);
        jsonParser.toJson(data, fileWriter);
        fileWriter.close();
    }

    @Override
    public int compareTo(Difficulty d) {
        return order - d.order;
    }
}