This commit is contained in:
jeffcheasey88 2023-03-18 15:04:56 +01:00
commit 3863fb659b
8 changed files with 57 additions and 25 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@
bin/
.project
config.txt
dist/
testApi/

View file

@ -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<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.name = name;
this.startDate = startDate;
this.endDate = endDate;
}
public int getId() {
@ -37,6 +43,14 @@ public class Chapter {
this.puzzles = puzzles;
}
public Timestamp getStartDate() {
return startDate;
}
public Timestamp getEndDate() {
return endDate;
}
@Override
public boolean equals(Object object){
if(this == object) return true;

View file

@ -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;
}

View file

@ -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 = ?";
@ -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, puzzleResult.getString("tags"),
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 {

View file

@ -31,11 +31,14 @@ 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();
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);

View file

@ -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,14 +34,14 @@ 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", 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

View file

@ -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\\/?(.+)?$");
}
}

View file

@ -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());
}
}