aboutsummaryrefslogtreecommitdiff
path: root/src/main/Difficulty.java
blob: 7174c3367e4d2b9171310dfc246a62181a79b39d (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main;

import java.io.File;
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 = "Unnamed";
    private ObservableList<LeaderboardEntry> leaderboard = FXCollections.observableArrayList();
    public File notes;
    public Double bpm = 0.0;
    public int numBeats;
    public Level level;
    public boolean isValid = false;
    
    /**
     * 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 = newDir;
        this.level = level;
    }

    public void readData()
    {
        boolean isValid1 = true;
        if (new File(thisDir, "metadata.json").exists())
        {
            if (!parseMetadata())
            {
                isValid1 = false;
            }
        }
        else
        {
            System.err.println(thisDir+" is missing metadata.json");
            isValid1 = false;
        }

        if (new File(thisDir, "leaderboard.json").exists())
        {
            if (!parseLeaderboard())
            {
                isValid1 = false;
            }
        }
        else
        {
            System.err.println(thisDir+" is missing leaderboard.json");
            isValid1 = false;
        }

        if (new File(thisDir, "notes.txt").exists())
        {
            notes = new File(thisDir, "notes.txt");
        }
        else
        {
            System.err.println(thisDir+" is missing notes.txt");
            isValid1 = false;
        }

        isValid = isValid1;
    }

    /**
     * Reads in json metadata and assigns values to variables
     */
    public boolean parseMetadata()
    {
        boolean isValid = true;
        File file = new File(thisDir, "metadata.json");
        JSONParser jsonParser = new JSONParser(); //parser to read the file
		
		try(FileReader reader = new FileReader(file))
		{
			Object obj = jsonParser.parse(reader); 
			JSONObject diffStuff = (JSONObject)(obj); //converts read object to a JSONObject
            
            title = (String) diffStuff.get("title");
            bpm = Double.parseDouble(diffStuff.get("bpm")+"");
            numBeats = Integer.parseInt(diffStuff.get("numBeats")+"");
		}
		catch (Exception e)
        {
            e.printStackTrace();
            isValid = false;
        }
        return isValid;
    }

    /**
     * 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();
        }
    }

    /**
     * Reads in json leaderboard and assigns populates list with leaderboardEntries
     */
    public boolean parseLeaderboard()
    {
        boolean isValid = true;
        File file = new File(thisDir, "leaderboard.json");
        JSONParser jsonParser = new JSONParser(); //parser to read the file

		try(FileReader reader = new FileReader(file))
		{
            Object obj = jsonParser.parse(reader); 
			
			JSONArray leaderboardStuff = (JSONArray)(obj); //converts read object to a JSONArray

            for (Object cur: leaderboardStuff)
            {
                JSONObject cur2 = (JSONObject) cur;

                String name = (String) cur2.get("name");
                int score = Integer.parseInt(""+cur2.get("score"));
                String date = (String) cur2.get("date");
                leaderboard.add(new LeaderboardEntry(name, score, date));
            }
		}
		catch (Exception e)
        {
            isValid = false;
            e.printStackTrace();
        }
        return isValid;
    }

    /**
     * Writes leaderboard to json file
     */
    public void writeLeaderboard()
    {
        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)
            {
                JSONObject obj = new JSONObject();
                obj.put("name", cur.getName());
                obj.put("score", cur.getScore());
                obj.put("date",cur.getDate());
                jsonArray.add(obj);
            }
            jsonArray.writeJSONString(fileWriter);
            fileWriter.flush();
 
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 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;
    }

    public String toString()
    {
        return title;
    }

    public boolean isValid() {
        return isValid;
    }

    public String getTitle() {
        return title;
    }
}