Merge remote-tracking branch 'origin/18-03-2023'
This commit is contained in:
commit
288cf33f1c
6 changed files with 104 additions and 52 deletions
|
@ -4,9 +4,12 @@ public class Badge {
|
||||||
private String name;
|
private String name;
|
||||||
private byte[] logo;
|
private byte[] logo;
|
||||||
private int level;
|
private int level;
|
||||||
|
|
||||||
|
public Badge(String name, int level) {
|
||||||
|
this(name, null, level);
|
||||||
|
}
|
||||||
|
|
||||||
public Badge(String name, byte[] logo, int level) {
|
public Badge(String name, byte[] logo, int level) {
|
||||||
super();
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.logo = logo;
|
this.logo = logo;
|
||||||
this.level = level;
|
this.level = level;
|
||||||
|
@ -35,6 +38,5 @@ public class Badge {
|
||||||
public void setLevel(int level) {
|
public void setLevel(int level) {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package be.jeffcheasey88.peeratcode.model;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
@ -21,12 +24,17 @@ public class Player implements Comparable<Player> {
|
||||||
private LinkedHashSet<String> groups;
|
private LinkedHashSet<String> groups;
|
||||||
private byte[] avatar;
|
private byte[] avatar;
|
||||||
|
|
||||||
|
private int rank;
|
||||||
private int totalScore;
|
private int totalScore;
|
||||||
private int totalCompletion;
|
private int totalCompletion;
|
||||||
private int totalTries;
|
private int totalTries;
|
||||||
|
|
||||||
private String badges; // To change to a set of model
|
private Set<Badge> badges;
|
||||||
|
|
||||||
|
public Player(String pseudo, String email, String firstname, String lastname, String description) {
|
||||||
|
this(pseudo, email, firstname, lastname, description, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
public Player(String pseudo, String email, String firstname, String lastname, String description, String groups) {
|
public Player(String pseudo, String email, String firstname, String lastname, String description, String groups) {
|
||||||
this(pseudo, email, firstname, lastname, description, groups, null);
|
this(pseudo, email, firstname, lastname, description, groups, null);
|
||||||
}
|
}
|
||||||
|
@ -36,7 +44,7 @@ public class Player implements Comparable<Player> {
|
||||||
this(pseudo, email, firstname, lastname, description, groups, avatar, null);
|
this(pseudo, email, firstname, lastname, description, groups, avatar, null);
|
||||||
}
|
}
|
||||||
public Player(String pseudo, String email, String firstname, String lastname, String description, String groups,
|
public Player(String pseudo, String email, String firstname, String lastname, String description, String groups,
|
||||||
byte[] avatar, String badges) {
|
byte[] avatar, Set<Badge> badges) {
|
||||||
this.pseudo = pseudo;
|
this.pseudo = pseudo;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.firstname = firstname;
|
this.firstname = firstname;
|
||||||
|
@ -49,7 +57,10 @@ public class Player implements Comparable<Player> {
|
||||||
totalCompletion = 0;
|
totalCompletion = 0;
|
||||||
totalTries = 0;
|
totalTries = 0;
|
||||||
|
|
||||||
this.badges = null;
|
if (badges != null)
|
||||||
|
this.badges = new HashSet<Badge>(badges);
|
||||||
|
else
|
||||||
|
this.badges = new HashSet<Badge>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
|
@ -94,7 +105,7 @@ public class Player implements Comparable<Player> {
|
||||||
|
|
||||||
public void setGroups(String groups) {
|
public void setGroups(String groups) {
|
||||||
if (groups == null || groups.isEmpty())
|
if (groups == null || groups.isEmpty())
|
||||||
groups = null;
|
this.groups = null;
|
||||||
else
|
else
|
||||||
this.groups = new LinkedHashSet<String>(Arrays.asList(groups.split(",")));
|
this.groups = new LinkedHashSet<String>(Arrays.asList(groups.split(",")));
|
||||||
}
|
}
|
||||||
|
@ -103,10 +114,22 @@ public class Player implements Comparable<Player> {
|
||||||
return this.avatar;
|
return this.avatar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setAvatar(byte[] newAvatar) {
|
||||||
|
avatar = newAvatar;
|
||||||
|
}
|
||||||
|
|
||||||
public String getPathToSourceCode() {
|
public String getPathToSourceCode() {
|
||||||
return String.format(PATH_TO_CODE, pseudo);
|
return String.format(PATH_TO_CODE, pseudo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getRank() {
|
||||||
|
return rank;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRank(int newRank) {
|
||||||
|
rank = newRank;
|
||||||
|
}
|
||||||
|
|
||||||
public int getTotalScore() {
|
public int getTotalScore() {
|
||||||
return totalScore;
|
return totalScore;
|
||||||
}
|
}
|
||||||
|
@ -131,11 +154,28 @@ public class Player implements Comparable<Player> {
|
||||||
this.totalTries = totalTries;
|
this.totalTries = totalTries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBadges() {
|
public Set<Badge> getBadges() {
|
||||||
return badges;
|
return badges;
|
||||||
}
|
}
|
||||||
public void setBadges(String initBadges) {
|
|
||||||
badges = initBadges;
|
/**
|
||||||
|
* SEE SET_TAGS IN PUZZLE
|
||||||
|
* @return DEATH
|
||||||
|
*/
|
||||||
|
public JSONArray getJsonBadges() {
|
||||||
|
if (badges == null)
|
||||||
|
return null;
|
||||||
|
JSONArray badgesJSON = new JSONArray();
|
||||||
|
for (Badge badge: badges) {
|
||||||
|
JSONObject badgeJSON = new JSONObject();
|
||||||
|
badgeJSON.put("name", badge.getName());
|
||||||
|
byte[] logo = badge.getLogo();
|
||||||
|
if (logo != null)
|
||||||
|
badgeJSON.put("logo", Base64.getEncoder().encodeToString(logo));
|
||||||
|
badgeJSON.put("level", badge.getLevel());
|
||||||
|
badgesJSON.add(badgeJSON);
|
||||||
|
}
|
||||||
|
return badgesJSON;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -155,4 +195,8 @@ public class Player implements Comparable<Player> {
|
||||||
|
|
||||||
return compare;
|
return compare;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addBadge(Badge newBadge) {
|
||||||
|
badges.add(newBadge);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,22 +33,15 @@ 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 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_SIMPLE = "SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?";
|
||||||
private static final String GET_PLAYER = "SELECT p.*, GROUP_CONCAT(g.name ORDER BY g.fk_chapter, g.fk_puzzle) FROM players p WHERE ";
|
private static final String GET_PLAYER_DETAILS = "SELECT p.*, scores.score, scores.completions, scores.tries, scores.rank, GROUP_CONCAT(DISTINCT g.name ORDER BY g.fk_chapter, g.fk_puzzle) AS sgroup 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 AND ";
|
||||||
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 ORDER BY g.fk_chapter, g.fk_puzzle) 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) 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" + 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;";
|
+ " 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;";
|
+ "p.pseudo = ? GROUP BY p.pseudo;";
|
||||||
private static final String ALL_PLAYERS_FOR_LEADERBOARD = "SELECT p.*, GROUP_CONCAT(DISTINCT g.name ORDER BY g.fk_chapter, g.fk_puzzle) 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 ALL_PLAYERS_FOR_LEADERBOARD = "select DISTINCT p.*, scores.* 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 WHERE p.id_player = scores.fk_player";
|
||||||
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 GET_BADGES_OF_PLAYER = "SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?";
|
||||||
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 = ?";
|
||||||
|
|
||||||
|
@ -85,16 +78,19 @@ public class DatabaseRepository {
|
||||||
|
|
||||||
private Player makePlayer(ResultSet playerResult) throws SQLException {
|
private Player makePlayer(ResultSet playerResult) throws SQLException {
|
||||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||||
playerResult.getString("firstName"), playerResult.getString("LastName"),
|
playerResult.getString("firstName"), playerResult.getString("lastName"),
|
||||||
playerResult.getString("description"), playerResult.getString("sgroup"),
|
playerResult.getString("description"));
|
||||||
playerResult.getBytes("avatar"));
|
if (hasColumn(playerResult, "avatar")) {
|
||||||
if (hasColumn(playerResult, "playerScore")) {
|
p.setAvatar(playerResult.getBytes("avatar"));
|
||||||
p.setTotalScore(playerResult.getInt("playerScore"));
|
|
||||||
p.setTotalCompletion(playerResult.getInt("playerCompletions"));
|
|
||||||
p.setTotalTries(playerResult.getInt("playerTries"));
|
|
||||||
}
|
}
|
||||||
if (hasColumn(playerResult, "badges")) {
|
if (hasColumn(playerResult, "sgroup")) {
|
||||||
p.setBadges(playerResult.getString("badges"));
|
p.setGroups(playerResult.getString("sgroup"));
|
||||||
|
}
|
||||||
|
if (hasColumn(playerResult, "score")) {
|
||||||
|
p.setRank(playerResult.getInt("rank"));
|
||||||
|
p.setTotalScore(playerResult.getInt("score"));
|
||||||
|
p.setTotalCompletion(playerResult.getInt("completions"));
|
||||||
|
p.setTotalTries(playerResult.getInt("tries"));
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -181,7 +177,7 @@ public class DatabaseRepository {
|
||||||
|
|
||||||
public Player getPlayer(int idPlayer) {
|
public Player getPlayer(int idPlayer) {
|
||||||
try {
|
try {
|
||||||
PreparedStatement completionsStmt = con.prepareStatement(GET_PLAYER);
|
PreparedStatement completionsStmt = con.prepareStatement(GET_PLAYER_SIMPLE);
|
||||||
completionsStmt.setInt(1, idPlayer);
|
completionsStmt.setInt(1, idPlayer);
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
if (result.next()) {
|
if (result.next()) {
|
||||||
|
@ -194,28 +190,34 @@ public class DatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getPlayerDetails(int idPlayer) {
|
public Player getPlayerDetails(int idPlayer) {
|
||||||
try {
|
return getPlayerDetails(idPlayer, null);
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement completionsStmt = con.prepareStatement(GET_PLAYER_DETAILS_BY_ID);
|
|
||||||
completionsStmt.setInt(1, idPlayer);
|
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
|
||||||
if (result.next()) {
|
|
||||||
return makePlayer(result);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getPlayerDetails(String pseudoPlayer) {
|
public Player getPlayerDetails(String pseudoPlayer) {
|
||||||
|
return getPlayerDetails(-1, pseudoPlayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Player getPlayerDetails(int id, String pseudo) {
|
||||||
try {
|
try {
|
||||||
ensureConnection();
|
ensureConnection();
|
||||||
PreparedStatement completionsStmt = con.prepareStatement(GET_PLAYER_DETAILS_BY_PSEUDO);
|
PreparedStatement completionsStmt;
|
||||||
completionsStmt.setString(1, pseudoPlayer);
|
if (pseudo != null) {
|
||||||
|
completionsStmt = con.prepareStatement(GET_PLAYER_DETAILS_BY_PSEUDO);
|
||||||
|
completionsStmt.setString(1, pseudo);
|
||||||
|
} else {
|
||||||
|
completionsStmt = con.prepareStatement(GET_PLAYER_DETAILS_BY_ID);
|
||||||
|
completionsStmt.setInt(1, id);
|
||||||
|
}
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
if (result.next()) {
|
if (result.next()) {
|
||||||
return makePlayer(result);
|
Player player = makePlayer(result);
|
||||||
|
completionsStmt = con.prepareStatement(GET_BADGES_OF_PLAYER);
|
||||||
|
completionsStmt.setInt(1, result.getInt("id_player"));
|
||||||
|
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||||
|
while (resultBadges.next()) {
|
||||||
|
player.addBadge(makeBadge(resultBadges));
|
||||||
|
}
|
||||||
|
return player;
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -36,6 +36,7 @@ public class Leaderboard implements Response {
|
||||||
playerJSON.put("pseudo", player.getPseudo());
|
playerJSON.put("pseudo", player.getPseudo());
|
||||||
if (player.getGroups() != null) playerJSON.put("groups", player.getJsonGroups());
|
if (player.getGroups() != null) playerJSON.put("groups", player.getJsonGroups());
|
||||||
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||||
|
playerJSON.put("rank", player.getRank());
|
||||||
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());
|
||||||
|
|
|
@ -40,11 +40,12 @@ public class PlayerDetails implements Response {
|
||||||
playerJSON.put("lastname", player.getLastname());
|
playerJSON.put("lastname", player.getLastname());
|
||||||
playerJSON.put("description", player.getDescription());
|
playerJSON.put("description", player.getDescription());
|
||||||
if (player.getGroups() != null) playerJSON.put("groups", player.getJsonGroups());
|
if (player.getGroups() != null) playerJSON.put("groups", player.getJsonGroups());
|
||||||
|
playerJSON.put("rank", player.getRank());
|
||||||
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());
|
||||||
playerJSON.put("badges", player.getBadges());
|
if (player.getBadges().size() > 0) playerJSON.put("badges", player.getJsonBadges());
|
||||||
if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
//if(player.getAvatar() != null) playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||||
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
||||||
} else {
|
} else {
|
||||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||||
|
|
|
@ -34,7 +34,9 @@ public class Client extends Thread{
|
||||||
router.exec(headers[0], headers[1], isLogin(reader), reader, writer);
|
router.exec(headers[0], headers[1], isLogin(reader), reader, writer);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
} catch (Exception e){}
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private User isLogin(HttpReader reader) throws Exception{
|
private User isLogin(HttpReader reader) throws Exception{
|
||||||
|
|
Loading…
Add table
Reference in a new issue