From 611b45a5bd6f2e777b36cc0a2b8e47be63fccf24 Mon Sep 17 00:00:00 2001 From: Francois G Date: Sat, 8 Apr 2023 18:09:58 +0200 Subject: [PATCH] change date system to match UI attends that I didn't understand well at first --- src/be/jeffcheasey88/peeratcode/model/Group.java | 6 ------ .../peeratcode/repository/DatabaseQuery.java | 2 +- .../peeratcode/repository/DatabaseRepository.java | 5 +---- src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java | 9 ++++++++- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/model/Group.java b/src/be/jeffcheasey88/peeratcode/model/Group.java index a73a4bc..e3e1eca 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Group.java +++ b/src/be/jeffcheasey88/peeratcode/model/Group.java @@ -11,7 +11,6 @@ public class Group implements Comparable { private int linkToChapter; private int linkToPuzzle; private List players; - private Timestamp endDate; public Group(JSONObject json){ this.name = (String)json.get("name"); @@ -20,13 +19,9 @@ public class Group implements Comparable { } public Group(String name, int initChap, int initPuzz) { - this(name, initChap, initPuzz, null); - } - public Group(String name, int initChap, int initPuzz, Timestamp initEnd) { this.name = name; this.linkToChapter = initChap; this.linkToPuzzle = initPuzz; - this.endDate = initEnd; } public void addPlayer(Player newPlayer) { @@ -102,7 +97,6 @@ public class Group implements Comparable { if (rank != null) groupJSON.put("rank", rank); else if (linkToChapter > 0) groupJSON.put("chapter", linkToChapter); else if (linkToPuzzle > 0) groupJSON.put("puzzle", linkToPuzzle); - if (endDate != null) groupJSON.put("end_date", endDate.toString()); if (players != null) { JSONArray groupsPlayerJSON = new JSONArray(); for (Player p:players) { diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java index 9e74f10..60ab08b 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java @@ -13,7 +13,7 @@ public enum DatabaseQuery { ALL_GROUPS("SELCT * FROM groups"), ALL_PLAYERS_FOR_LEADERBOARD("select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter, g.fk_puzzle"), - ALL_GROUP_FOR_CHAPTER_LEADERBOARD("SELECT g.*, pl.pseudo, co.score, co.tries, ch.end_date FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player LEFT JOIN chapters ch ON g.fk_chapter = ch.id_chapter WHERE fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);"), + ALL_GROUP_FOR_CHAPTER_LEADERBOARD("SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);"), CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"), CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"), diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 0b1c085..45b840e 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -80,10 +80,7 @@ public class DatabaseRepository { } private Group makeGroup(ResultSet result) throws SQLException { - if (hasColumn(result, "end_date")) - return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"), result.getTimestamp("end_date")); - else - return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle")); + return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle")); } private Player makeGroupPlayer(ResultSet result) throws SQLException { return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries")); diff --git a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java index 4ec7307..a537941 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java @@ -8,6 +8,7 @@ import java.util.regex.Matcher; import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import be.jeffcheasey88.peeratcode.model.Chapter; import be.jeffcheasey88.peeratcode.model.Group; import be.jeffcheasey88.peeratcode.model.Player; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; @@ -38,7 +39,12 @@ public class Leaderboard implements Response { } private void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException { + Chapter chInfo = databaseRepo.getChapter(chapterId); + SortedSet allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId); + JSONObject leaderboardJSON = new JSONObject(); + if (chInfo.getStartDate() != null) leaderboardJSON.put("start_date", chInfo.getStartDate().toString()); + if (chInfo.getEndDate() != null) leaderboardJSON.put("end_date", chInfo.getEndDate().toString()); JSONArray groupsJSON = new JSONArray(); if (allGroupsForChapter != null) { int rank = 1; @@ -57,7 +63,8 @@ public class Leaderboard implements Response { previousGroup = g; } } - writer.write(groupsJSON.toJSONString().replace("\\", "")); + leaderboardJSON.put("groups", groupsJSON); + writer.write(leaderboardJSON.toJSONString().replace("\\", "")); } private void playersLeaderboard(HttpWriter writer) throws IOException {