feat: save highscores per difficulty
This commit is contained in:
@@ -17,7 +17,7 @@ import java.util.regex.Pattern;
|
||||
* Concept inspired by GSON
|
||||
*/
|
||||
public class NightJson {
|
||||
private static final String KEY_PATTERN = "\"(-?[A-Za-z-_.]*)\":";
|
||||
private static final String KEY_PATTERN = "\"(.*)\":";
|
||||
Class<?> jsonClass;
|
||||
Field jsonMap;
|
||||
String fileLocation;
|
||||
@@ -175,21 +175,8 @@ public class NightJson {
|
||||
private Object getValue(String s, String key, Function<String, Class<?>> keyToType, Iterator<String> pairIterator) {
|
||||
String val = s.split(KEY_PATTERN, 2)[1];
|
||||
|
||||
if (s.contains("{")) { // Handle maps recursively
|
||||
StringBuilder submapString = new StringBuilder();
|
||||
int level = charAmount(s, '{');
|
||||
submapString.append(val);
|
||||
if (pairIterator.hasNext()) submapString.append(",");
|
||||
while (pairIterator.hasNext()) {
|
||||
String next = pairIterator.next();
|
||||
submapString.append(next);
|
||||
level += charAmount(next, '{');
|
||||
level -= charAmount(next, '}');
|
||||
if (level <= 0) break;
|
||||
if (pairIterator.hasNext()) submapString.append(",");
|
||||
}
|
||||
Optional<Field> field = getField(key);
|
||||
return jsonToMap(String.valueOf(submapString), k -> field.isPresent() ? getTypeArgument(field.get(), 1) : String.class);
|
||||
if (s.contains("{")) {
|
||||
return readJsonMap(key, pairIterator, val);
|
||||
}
|
||||
else {
|
||||
while (val.startsWith(" ")) val = val.substring(1);
|
||||
@@ -200,6 +187,27 @@ public class NightJson {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle maps recursively
|
||||
*/
|
||||
private Map<String, Object> readJsonMap(String key, Iterator<String> pairIterator, String val) {
|
||||
StringBuilder submapString = new StringBuilder();
|
||||
String next = val;
|
||||
int level = 0;
|
||||
while (next != null) {
|
||||
submapString.append(next);
|
||||
level += charAmount(next, '{');
|
||||
level -= charAmount(next, '}');
|
||||
if (level <= 0) break;
|
||||
if (!pairIterator.hasNext()) {
|
||||
submapString.append(",");
|
||||
next = pairIterator.next();
|
||||
} else next = null;
|
||||
}
|
||||
Optional<Field> field = getField(key);
|
||||
return jsonToMap(String.valueOf(submapString), k -> field.isPresent() ? getTypeArgument(field.get(), 1) : String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the amount of appearances of a char in a string.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user