From d74013a13a36664fa57f0a56c02b49b2799ad38d Mon Sep 17 00:00:00 2001 From: Francois G Date: Fri, 17 Mar 2023 14:33:12 +0100 Subject: [PATCH 1/3] Solve compatibility problem with new DB schema. To test use pac_test instead of pc in config.txt --- .../peeratcode/model/Chapter.java | 16 ++++++++++++- .../repository/DatabaseRepository.java | 24 ++++++++++--------- .../peeratcode/routes/ChapterElement.java | 2 ++ .../peeratcode/routes/Leaderboard.java | 3 ++- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/model/Chapter.java b/src/be/jeffcheasey88/peeratcode/model/Chapter.java index 8d98cad..f286ae3 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Chapter.java +++ b/src/be/jeffcheasey88/peeratcode/model/Chapter.java @@ -1,5 +1,7 @@ package be.jeffcheasey88.peeratcode.model; +import java.sql.Timestamp; +import java.time.LocalDateTime; import java.util.List; public class Chapter { @@ -7,10 +9,14 @@ public class Chapter { private int id; private String name; private List puzzles; + private Timestamp startDate; + private Timestamp endDate; - public Chapter(int id, String name) { + public Chapter(int id, String name, Timestamp startDate, Timestamp endDate) { this.id = id; this.name = name; + this.startDate = startDate; + this.endDate = endDate; } public int getId() { @@ -36,6 +42,14 @@ public class Chapter { public void setPuzzles(List puzzles) { this.puzzles = puzzles; } + + public Timestamp getStartDate() { + return startDate; + } + + public Timestamp getEndDate() { + return endDate; + } @Override public boolean equals(Object object){ diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 0f337c3..6dac7d3 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -33,20 +33,21 @@ public class DatabaseRepository { private static final String CHECK_PASSWORD = "SELECT id_player, passwd FROM players WHERE pseudo=?"; private static final String SCORE = "SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"; private static final String GET_COMPLETION = "SELECT id_completion, tries, fileName, score FROM completions WHERE fk_puzzle = ? AND fk_player = ?"; - private static final String GET_PLAYER = "SELECT * FROM players WHERE "; - private static final String GET_PLAYER_BY_ID = GET_PLAYER + "id_player = ?"; - private static final String GET_PLAYER_BY_PSEUDO = GET_PLAYER + "pseudo = ?"; - private static final String GET_PLAYER_DETAILS = "SELECT p.pseudo, p.email, p.firstname, p.lastname, p.description, p.avatar, p.sgroup,\n" + private static final String PART_GET_PLAYER_GROUP = " LEFT JOIN containsGroups cg ON p.id_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group "; + private static final String GET_PLAYER = "SELECT p.*, GROUP_CONCAT(g.name) FROM players p WHERE "; + private static final String GET_PLAYER_BY_ID = GET_PLAYER + "id_player = ?" + PART_GET_PLAYER_GROUP; + private static final String GET_PLAYER_BY_PSEUDO = GET_PLAYER + "pseudo = ?" + PART_GET_PLAYER_GROUP;; + private static final String GET_PLAYER_DETAILS = "SELECT p.pseudo, p.email, p.firstname, p.lastname, p.description, p.avatar, GROUP_CONCAT(DISTINCT g.name) AS sgroup,\n" + " SUM(c.score) AS playerScore, COUNT(c.id_completion) AS playerCompletions, SUM(c.tries) AS playerTries,\n" - + " GROUP_CONCAT(DISTINCT b.name ORDER BY b.name ASC SEPARATOR ', ') AS badges\n" + "FROM players p\n" + + " GROUP_CONCAT(DISTINCT b.name ORDER BY b.name ASC) AS badges FROM players p\n" + "LEFT JOIN completions c ON p.id_player = c.fk_player\n" + "LEFT JOIN containsBadges cb ON p.id_player = cb.fk_player\n" - + "LEFT JOIN badges b ON cb.fk_badge = b.id_badge\n"; + + "LEFT JOIN badges b ON cb.fk_badge = b.id_badge\n" + PART_GET_PLAYER_GROUP; private static final String GET_PLAYER_DETAILS_BY_ID = GET_PLAYER_DETAILS - + "WHERE p.id_player = ? GROUP BY p.id_player;"; + + " WHERE p.id_player = ? GROUP BY p.id_player;"; private static final String GET_PLAYER_DETAILS_BY_PSEUDO = GET_PLAYER_DETAILS - + "WHERE p.pseudo = ? GROUP BY p.pseudo;"; - private static final String ALL_PLAYERS_FOR_LEADERBOARD = "SELECT p.*, sum(c.score) AS playerScore, count(c.id_completion) AS playerCompletions, sum(c.tries) AS playerTries FROM players p LEFT JOIN completions c ON c.fk_player = p.id_player GROUP BY p.id_player ORDER BY playerScore DESC"; + + " WHERE p.pseudo = ? GROUP BY p.pseudo;"; + private static final String ALL_PLAYERS_FOR_LEADERBOARD = "SELECT p.*, GROUP_CONCAT(DISTINCT g.name) AS sgroup, sum(c.score) AS playerScore, count(c.id_completion) AS playerCompletions, sum(c.tries) AS playerTries FROM players p " + PART_GET_PLAYER_GROUP + "LEFT JOIN completions c ON c.fk_player = p.id_player GROUP BY p.id_player ORDER BY playerScore DESC"; private static final String GET_BADGE = "SELECT * FROM badges WHERE id_badge = ?"; private static final String INSERT_COMPLETION = "INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"; private static final String UPDATE_COMPLETION = "UPDATE completions SET tries = ?, filename = ?, score = ? WHERE fk_puzzle = ? AND fk_player = ?"; @@ -68,11 +69,12 @@ public class DatabaseRepository { private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException { return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"), - puzzleResult.getString("content"), null, "", 0, hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1); + puzzleResult.getString("content"), null, "", 0, + hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1); } private Chapter makeChapter(ResultSet chapterResult) throws SQLException { - return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name")); + return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"), chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date")); } private Completion makeCompletion(int playerId, int puzzleId, ResultSet completionResult) throws SQLException { diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index 8e6ade6..0c5d5e0 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -31,6 +31,8 @@ public class ChapterElement implements Response { JSONObject chapterJSON = new JSONObject(); chapterJSON.put("id", chapter.getId()); chapterJSON.put("name", chapter.getName()); + if (chapter.getStartDate() != null) chapterJSON.put("startDate", chapter.getStartDate()); + if (chapter.getEndDate() != null) chapterJSON.put("endDate", chapter.getEndDate()); JSONArray puzzles = new JSONArray(); for (Puzzle puzzle : chapter.getPuzzles()) { JSONObject puzzleJSON = new JSONObject(); diff --git a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java index 48f3487..3178b7a 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java @@ -11,6 +11,7 @@ import be.jeffcheasey88.peeratcode.webserver.User; import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import java.util.Base64; import java.util.SortedSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -33,7 +34,7 @@ public class Leaderboard implements Response { JSONObject playerJSON = new JSONObject(); playerJSON.put("pseudo", player.getPseudo()); playerJSON.put("group", player.getGroup()); - // chapterJSON.put("avatar", player.); + if(player.getAvatar() != null) playerJSON.put("avatar", new String(Base64.getEncoder().encode(player.getAvatar()))); playerJSON.put("score", player.getTotalScore()); playerJSON.put("completions", player.getTotalCompletion()); playerJSON.put("tries", player.getTotalTries()); From 77283bdf7575dcdc5bae78ebc6ea38e018b643cc Mon Sep 17 00:00:00 2001 From: Francois G Date: Fri, 17 Mar 2023 21:19:34 +0100 Subject: [PATCH 2/3] fix base64 to json send --- .gitignore | 4 +++- src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java | 4 ++-- src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java | 4 ++-- src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java | 6 +++--- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 9d49d56..b3043cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .settings/ bin/ .project -config.txt \ No newline at end of file +config.txt +dist/ +testApi/ diff --git a/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java b/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java index fd9a4b1..92ef735 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java +++ b/src/be/jeffcheasey88/peeratcode/routes/BadgeDetails.java @@ -34,10 +34,10 @@ public class BadgeDetails implements Response { JSONObject badgeJSON = new JSONObject(); if (badge != null) { badgeJSON.put("name", badge.getName()); - if(badge.getLogo() != null) badgeJSON.put("logo", new String(Base64.getEncoder().encode(badge.getLogo()))); + if(badge.getLogo() != null) badgeJSON.put("logo", Base64.getEncoder().encodeToString(badge.getLogo())); badgeJSON.put("level", badge.getLevel()); } - writer.write(badgeJSON.toJSONString()); + writer.write(badgeJSON.toJSONString().replace("\\", "")); } else { HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); diff --git a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java index 3178b7a..5f744bd 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java @@ -34,14 +34,14 @@ public class Leaderboard implements Response { JSONObject playerJSON = new JSONObject(); playerJSON.put("pseudo", player.getPseudo()); playerJSON.put("group", player.getGroup()); - if(player.getAvatar() != null) playerJSON.put("avatar", new String(Base64.getEncoder().encode(player.getAvatar()))); + if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar())); playerJSON.put("score", player.getTotalScore()); playerJSON.put("completions", player.getTotalCompletion()); playerJSON.put("tries", player.getTotalTries()); playersJSON.add(playerJSON); } } - writer.write(playersJSON.toJSONString()); + writer.write(playersJSON.toJSONString().replace("\\", "")); } @Override diff --git a/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java b/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java index 2b6f0f2..8554170 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PlayerDetails.java @@ -46,8 +46,8 @@ public class PlayerDetails implements Response { playerJSON.put("completions", player.getTotalCompletion()); playerJSON.put("tries", player.getTotalTries()); playerJSON.put("badges", player.getBadges()); - if(player.getAvatar() != null) playerJSON.put("avatar", new String(Base64.getEncoder().encode(player.getAvatar()))); - writer.write(playerJSON.toJSONString()); + if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar())); + writer.write(playerJSON.toJSONString().replace("\\", "")); } else { HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); } @@ -55,6 +55,6 @@ public class PlayerDetails implements Response { @Override public Pattern getPattern() { - return Pattern.compile("^\\/player\\/(.+)?$"); + return Pattern.compile("^\\/player\\/?(.+)?$"); } } From 2bb0328206e757e3d9d130efafc6829ce2912cf3 Mon Sep 17 00:00:00 2001 From: Francois G Date: Fri, 17 Mar 2023 22:44:20 +0100 Subject: [PATCH 3/3] add tags support --- .../jeffcheasey88/peeratcode/model/Puzzle.java | 16 +++++++++++++--- .../repository/DatabaseRepository.java | 6 +++--- .../peeratcode/routes/ChapterElement.java | 1 + .../peeratcode/routes/PuzzleElement.java | 4 ++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java index 6c23ede..7495d53 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java +++ b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java @@ -8,18 +8,20 @@ public class Puzzle { private byte[] soluce; private String verify; private int scoreMax; + private String tags; private int depend; - public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax){ - this(id, name, content, soluce, verify, scoreMax, -1); + public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags){ + this(id, name, content, soluce, verify, scoreMax, tags, -1); } - public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, int depend){ + public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags, int depend){ this.id = id; this.name = name; this.content = content; this.soluce = soluce; this.verify = verify; this.scoreMax = scoreMax; + this.tags = tags; this.depend = depend; } @@ -71,6 +73,14 @@ public class Puzzle { this.scoreMax = max; } + public String getTags(){ + return this.tags; + } + + public void setTags(int tags){ + this.scoreMax = tags; + } + public int getDepend(){ return this.depend; } diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 6dac7d3..f385784 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -23,9 +23,9 @@ import be.jeffcheasey88.peeratcode.model.Player; import be.jeffcheasey88.peeratcode.model.Puzzle; public class DatabaseRepository { - private static final String SPECIFIC_PUZZLE_QUERY = "SELECT p.*, np.origin FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next WHERE p.id_puzzle = ?;"; + private static final String SPECIFIC_PUZZLE_QUERY = "SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"; private static final String SPECIFIC_CHAPTER_QUERY = "SELECT * FROM chapters WHERE id_chapter = ?"; - private static final String PUZZLES_IN_CHAPTER_QUERY = "SELECT * FROM puzzles WHERE fk_chapter = ?"; + private static final String PUZZLES_IN_CHAPTER_QUERY = "SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle"; private static final String ALL_CHAPTERS_QUERY = "SELECT * FROM chapters WHERE id_chapter > 0"; private static final String CHECK_PSEUDO_AVAILABLE_QUERY = "SELECT * FROM players WHERE pseudo = ?"; private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?"; @@ -69,7 +69,7 @@ public class DatabaseRepository { private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException { return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"), - puzzleResult.getString("content"), null, "", 0, + puzzleResult.getString("content"), null, "", 0, puzzleResult.getString("tags"), hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1); } diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index 0c5d5e0..69fe8f0 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -38,6 +38,7 @@ public class ChapterElement implements Response { JSONObject puzzleJSON = new JSONObject(); puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("name", puzzle.getName()); + if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getTags()); puzzles.add(puzzleJSON); } chapterJSON.put("puzzles", puzzles); diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java index 2c3f67b..d4224f9 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -33,8 +33,8 @@ public class PuzzleElement implements Response { puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("name", puzzle.getName()); puzzleJSON.put("content", puzzle.getContent()); - if (puzzle.getDepend() > 0) - puzzleJSON.put("depend", puzzle.getDepend()); + if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getTags()); + if (puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend()); writer.write(puzzleJSON.toJSONString()); } }