Respond empty json even if no one in leaderboard

This commit is contained in:
jeffcheasey88 2023-02-25 22:36:50 +01:00
parent b5db4328f2
commit 792005e84d
2 changed files with 6 additions and 4 deletions

View file

@ -27,8 +27,8 @@ public class Leaderboard implements Response {
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
if (allPlayers != null) {
JSONArray playersJSON = new JSONArray();
if (allPlayers != null) {
for (Player player : allPlayers) {
JSONObject playerJSON = new JSONObject();
playerJSON.put("pseudo", player.getPseudo());
@ -39,8 +39,8 @@ public class Leaderboard implements Response {
playerJSON.put("tries", player.getTotalTries());
playersJSON.add(playerJSON);
}
writer.write(playersJSON.toJSONString());
}
writer.write(playersJSON.toJSONString());
writer.flush();
writer.close();
}

View file

@ -58,9 +58,11 @@ public class HttpUtil {
String line;
String key = null;
while(((line = reader.readLine()) != null) && (line.length() > 0)){
if(key != null) continue;
Matcher matcher = AUTORIZATION.matcher(line);
if(matcher.matches()) key = matcher.group(1);
if(matcher.matches()){
key = matcher.group(1);
break;
}
}
return key;
}