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

View file

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