change date system to match UI attends that I didn't understand well at first

This commit is contained in:
Francois G 2023-04-08 18:09:58 +02:00
parent 14905a489a
commit 611b45a5bd
4 changed files with 10 additions and 12 deletions

View file

@ -11,7 +11,6 @@ public class Group implements Comparable<Group> {
private int linkToChapter;
private int linkToPuzzle;
private List<Player> players;
private Timestamp endDate;
public Group(JSONObject json){
this.name = (String)json.get("name");
@ -20,13 +19,9 @@ public class Group implements Comparable<Group> {
}
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<Group> {
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) {

View file

@ -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 = ?"),

View file

@ -80,9 +80,6 @@ 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"));
}
private Player makeGroupPlayer(ResultSet result) throws SQLException {

View file

@ -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<Group> 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 {