Merge branch 'main' of https://github.com/Peer-at-Code/peer-at-code-backend
This commit is contained in:
commit
3863fb659b
8 changed files with 57 additions and 25 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,4 +1,6 @@
|
||||||
.settings/
|
.settings/
|
||||||
bin/
|
bin/
|
||||||
.project
|
.project
|
||||||
config.txt
|
config.txt
|
||||||
|
dist/
|
||||||
|
testApi/
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package be.jeffcheasey88.peeratcode.model;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Chapter {
|
public class Chapter {
|
||||||
|
@ -7,10 +9,14 @@ public class Chapter {
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private List<Puzzle> puzzles;
|
private List<Puzzle> 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.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.startDate = startDate;
|
||||||
|
this.endDate = endDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
|
@ -36,6 +42,14 @@ public class Chapter {
|
||||||
public void setPuzzles(List<Puzzle> puzzles) {
|
public void setPuzzles(List<Puzzle> puzzles) {
|
||||||
this.puzzles = puzzles;
|
this.puzzles = puzzles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Timestamp getStartDate() {
|
||||||
|
return startDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Timestamp getEndDate() {
|
||||||
|
return endDate;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object object){
|
public boolean equals(Object object){
|
||||||
|
|
|
@ -8,18 +8,20 @@ public class Puzzle {
|
||||||
private byte[] soluce;
|
private byte[] soluce;
|
||||||
private String verify;
|
private String verify;
|
||||||
private int scoreMax;
|
private int scoreMax;
|
||||||
|
private String tags;
|
||||||
private int depend;
|
private int depend;
|
||||||
|
|
||||||
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax){
|
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags){
|
||||||
this(id, name, content, soluce, verify, scoreMax, -1);
|
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.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.soluce = soluce;
|
this.soluce = soluce;
|
||||||
this.verify = verify;
|
this.verify = verify;
|
||||||
this.scoreMax = scoreMax;
|
this.scoreMax = scoreMax;
|
||||||
|
this.tags = tags;
|
||||||
this.depend = depend;
|
this.depend = depend;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +73,14 @@ public class Puzzle {
|
||||||
this.scoreMax = max;
|
this.scoreMax = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTags(){
|
||||||
|
return this.tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTags(int tags){
|
||||||
|
this.scoreMax = tags;
|
||||||
|
}
|
||||||
|
|
||||||
public int getDepend(){
|
public int getDepend(){
|
||||||
return this.depend;
|
return this.depend;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,9 +23,9 @@ import be.jeffcheasey88.peeratcode.model.Player;
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||||
|
|
||||||
public class DatabaseRepository {
|
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 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 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_PSEUDO_AVAILABLE_QUERY = "SELECT * FROM players WHERE pseudo = ?";
|
||||||
private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?";
|
private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?";
|
||||||
|
@ -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 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 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_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 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_BY_ID = GET_PLAYER + "id_player = ?";
|
private static final String GET_PLAYER = "SELECT p.*, GROUP_CONCAT(g.name) FROM players p WHERE ";
|
||||||
private static final String GET_PLAYER_BY_PSEUDO = GET_PLAYER + "pseudo = ?";
|
private static final String GET_PLAYER_BY_ID = GET_PLAYER + "id_player = ?" + PART_GET_PLAYER_GROUP;
|
||||||
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 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"
|
+ " 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 completions c ON p.id_player = c.fk_player\n"
|
||||||
+ "LEFT JOIN containsBadges cb ON p.id_player = cb.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
|
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
|
private static final String GET_PLAYER_DETAILS_BY_PSEUDO = GET_PLAYER_DETAILS
|
||||||
+ "WHERE p.pseudo = ? GROUP BY p.pseudo;";
|
+ " 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";
|
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 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 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 = ?";
|
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 {
|
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
||||||
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
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, puzzleResult.getString("tags"),
|
||||||
|
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
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 {
|
private Completion makeCompletion(int playerId, int puzzleId, ResultSet completionResult) throws SQLException {
|
||||||
|
|
|
@ -31,11 +31,14 @@ public class ChapterElement implements Response {
|
||||||
JSONObject chapterJSON = new JSONObject();
|
JSONObject chapterJSON = new JSONObject();
|
||||||
chapterJSON.put("id", chapter.getId());
|
chapterJSON.put("id", chapter.getId());
|
||||||
chapterJSON.put("name", chapter.getName());
|
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();
|
JSONArray puzzles = new JSONArray();
|
||||||
for (Puzzle puzzle : chapter.getPuzzles()) {
|
for (Puzzle puzzle : chapter.getPuzzles()) {
|
||||||
JSONObject puzzleJSON = new JSONObject();
|
JSONObject puzzleJSON = new JSONObject();
|
||||||
puzzleJSON.put("id", puzzle.getId());
|
puzzleJSON.put("id", puzzle.getId());
|
||||||
puzzleJSON.put("name", puzzle.getName());
|
puzzleJSON.put("name", puzzle.getName());
|
||||||
|
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getTags());
|
||||||
puzzles.add(puzzleJSON);
|
puzzles.add(puzzleJSON);
|
||||||
}
|
}
|
||||||
chapterJSON.put("puzzles", puzzles);
|
chapterJSON.put("puzzles", puzzles);
|
||||||
|
|
|
@ -11,6 +11,7 @@ import be.jeffcheasey88.peeratcode.webserver.User;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
@ -33,14 +34,14 @@ public class Leaderboard implements Response {
|
||||||
JSONObject playerJSON = new JSONObject();
|
JSONObject playerJSON = new JSONObject();
|
||||||
playerJSON.put("pseudo", player.getPseudo());
|
playerJSON.put("pseudo", player.getPseudo());
|
||||||
playerJSON.put("group", player.getGroup());
|
playerJSON.put("group", player.getGroup());
|
||||||
// chapterJSON.put("avatar", player.);
|
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||||
playerJSON.put("score", player.getTotalScore());
|
playerJSON.put("score", player.getTotalScore());
|
||||||
playerJSON.put("completions", player.getTotalCompletion());
|
playerJSON.put("completions", player.getTotalCompletion());
|
||||||
playerJSON.put("tries", player.getTotalTries());
|
playerJSON.put("tries", player.getTotalTries());
|
||||||
playersJSON.add(playerJSON);
|
playersJSON.add(playerJSON);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.write(playersJSON.toJSONString());
|
writer.write(playersJSON.toJSONString().replace("\\", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -46,8 +46,8 @@ public class PlayerDetails implements Response {
|
||||||
playerJSON.put("completions", player.getTotalCompletion());
|
playerJSON.put("completions", player.getTotalCompletion());
|
||||||
playerJSON.put("tries", player.getTotalTries());
|
playerJSON.put("tries", player.getTotalTries());
|
||||||
playerJSON.put("badges", player.getBadges());
|
playerJSON.put("badges", player.getBadges());
|
||||||
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()));
|
||||||
writer.write(playerJSON.toJSONString());
|
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
||||||
} else {
|
} else {
|
||||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,6 @@ public class PlayerDetails implements Response {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pattern getPattern() {
|
public Pattern getPattern() {
|
||||||
return Pattern.compile("^\\/player\\/(.+)?$");
|
return Pattern.compile("^\\/player\\/?(.+)?$");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,8 @@ public class PuzzleElement implements Response {
|
||||||
puzzleJSON.put("id", puzzle.getId());
|
puzzleJSON.put("id", puzzle.getId());
|
||||||
puzzleJSON.put("name", puzzle.getName());
|
puzzleJSON.put("name", puzzle.getName());
|
||||||
puzzleJSON.put("content", puzzle.getContent());
|
puzzleJSON.put("content", puzzle.getContent());
|
||||||
if (puzzle.getDepend() > 0)
|
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getTags());
|
||||||
puzzleJSON.put("depend", puzzle.getDepend());
|
if (puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend());
|
||||||
writer.write(puzzleJSON.toJSONString());
|
writer.write(puzzleJSON.toJSONString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue