Manage group as Object and no more String for leaderboard and playerDetails
This commit is contained in:
parent
a4b4ef8beb
commit
6442ebdf19
3 changed files with 97 additions and 70 deletions
|
@ -1,9 +1,11 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package be.jeffcheasey88.peeratcode.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
|
@ -21,46 +23,26 @@ public class Player implements Comparable<Player> {
|
||||||
private String firstname;
|
private String firstname;
|
||||||
private String lastname;
|
private String lastname;
|
||||||
private String description;
|
private String description;
|
||||||
private LinkedHashSet<String> groups;
|
private Set<Group> groups;
|
||||||
private byte[] avatar;
|
private byte[] avatar;
|
||||||
|
|
||||||
private int rank;
|
private int rank;
|
||||||
private int totalScore;
|
private int totalScore;
|
||||||
private int totalCompletion;
|
private int totalCompletion;
|
||||||
private int totalTries;
|
private int totalTries;
|
||||||
|
|
||||||
private Set<Badge> badges;
|
private Set<Badge> badges;
|
||||||
|
|
||||||
public Player(String pseudo, String email, String firstname, String lastname, String description) {
|
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) {
|
|
||||||
this(pseudo, email, firstname, lastname, description, groups, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Player(String pseudo, String email, String firstname, String lastname, String description, String groups,
|
|
||||||
byte[] avatar) {
|
|
||||||
this(pseudo, email, firstname, lastname, description, groups, avatar, null);
|
|
||||||
}
|
|
||||||
public Player(String pseudo, String email, String firstname, String lastname, String description, String groups,
|
|
||||||
byte[] avatar, Set<Badge> badges) {
|
|
||||||
this.pseudo = pseudo;
|
this.pseudo = pseudo;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.firstname = firstname;
|
this.firstname = firstname;
|
||||||
this.lastname = lastname;
|
this.lastname = lastname;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
setGroups(groups);
|
|
||||||
this.avatar = avatar;
|
|
||||||
|
|
||||||
totalScore = 0;
|
totalScore = 0;
|
||||||
totalCompletion = 0;
|
totalCompletion = 0;
|
||||||
totalTries = 0;
|
totalTries = 0;
|
||||||
|
|
||||||
if (badges != null)
|
|
||||||
this.badges = new HashSet<Badge>(badges);
|
|
||||||
else
|
|
||||||
this.badges = new HashSet<Badge>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
|
@ -83,41 +65,42 @@ public class Player implements Comparable<Player> {
|
||||||
return this.description;
|
return this.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getGroups() {
|
public Set<Group> getGroups() {
|
||||||
return groups;
|
return groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SEE SET_TAGS IN PUZZLE
|
* SEE SET_TAGS IN PUZZLE
|
||||||
|
*
|
||||||
* @return DEATH
|
* @return DEATH
|
||||||
*/
|
*/
|
||||||
public JSONArray getJsonGroups() {
|
public JSONArray getJsonGroups() {
|
||||||
if (groups == null)
|
if (groups != null) {
|
||||||
return null;
|
JSONArray groupsJSON = new JSONArray();
|
||||||
JSONArray groupsJSON = new JSONArray();
|
for (Group group : groups) {
|
||||||
for (String group: groups) {
|
groupsJSON.add(group.getJson());
|
||||||
JSONObject groupJSON = new JSONObject();
|
}
|
||||||
groupJSON.put("name", group);
|
return groupsJSON;
|
||||||
groupsJSON.add(groupJSON);
|
|
||||||
}
|
}
|
||||||
return groupsJSON;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGroups(String groups) {
|
public void addGroup(Group newGroup) {
|
||||||
if (groups == null || groups.isEmpty())
|
if (newGroup != null) {
|
||||||
this.groups = null;
|
if (this.groups == null)
|
||||||
else
|
this.groups = new HashSet<Group>();
|
||||||
this.groups = new LinkedHashSet<String>(Arrays.asList(groups.split(",")));
|
this.groups.add(newGroup);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getAvatar() {
|
public byte[] getAvatar() {
|
||||||
return this.avatar;
|
return this.avatar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAvatar(byte[] newAvatar) {
|
public void setAvatar(byte[] newAvatar) {
|
||||||
avatar = newAvatar;
|
avatar = newAvatar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPathToSourceCode() {
|
public String getPathToSourceCode() {
|
||||||
return String.format(PATH_TO_CODE, pseudo);
|
return String.format(PATH_TO_CODE, pseudo);
|
||||||
}
|
}
|
||||||
|
@ -129,7 +112,7 @@ public class Player implements Comparable<Player> {
|
||||||
public void setRank(int newRank) {
|
public void setRank(int newRank) {
|
||||||
rank = newRank;
|
rank = newRank;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTotalScore() {
|
public int getTotalScore() {
|
||||||
return totalScore;
|
return totalScore;
|
||||||
}
|
}
|
||||||
|
@ -153,20 +136,21 @@ public class Player implements Comparable<Player> {
|
||||||
public void setTotalTries(int totalTries) {
|
public void setTotalTries(int totalTries) {
|
||||||
this.totalTries = totalTries;
|
this.totalTries = totalTries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Badge> getBadges() {
|
public Set<Badge> getBadges() {
|
||||||
return badges;
|
return badges;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SEE SET_TAGS IN PUZZLE
|
* SEE SET_TAGS IN PUZZLE
|
||||||
|
*
|
||||||
* @return DEATH
|
* @return DEATH
|
||||||
*/
|
*/
|
||||||
public JSONArray getJsonBadges() {
|
public JSONArray getJsonBadges() {
|
||||||
if (badges == null)
|
if (badges == null)
|
||||||
return null;
|
return null;
|
||||||
JSONArray badgesJSON = new JSONArray();
|
JSONArray badgesJSON = new JSONArray();
|
||||||
for (Badge badge: badges) {
|
for (Badge badge : badges) {
|
||||||
JSONObject badgeJSON = new JSONObject();
|
JSONObject badgeJSON = new JSONObject();
|
||||||
badgeJSON.put("name", badge.getName());
|
badgeJSON.put("name", badge.getName());
|
||||||
byte[] logo = badge.getLogo();
|
byte[] logo = badge.getLogo();
|
||||||
|
@ -178,6 +162,14 @@ public class Player implements Comparable<Player> {
|
||||||
return badgesJSON;
|
return badgesJSON;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addBadge(Badge newBadge) {
|
||||||
|
if (newBadge != null) {
|
||||||
|
if (badges == null)
|
||||||
|
badges = new HashSet<Badge>();
|
||||||
|
badges.add(newBadge);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Player other) {
|
public int compareTo(Player other) {
|
||||||
if (this == other)
|
if (this == other)
|
||||||
|
@ -186,17 +178,32 @@ public class Player implements Comparable<Player> {
|
||||||
return -1;
|
return -1;
|
||||||
int compare = Integer.compare(other.getTotalScore(), totalScore);
|
int compare = Integer.compare(other.getTotalScore(), totalScore);
|
||||||
if (compare == 0) {
|
if (compare == 0) {
|
||||||
compare = Integer.compare(other.getTotalCompletion(), totalCompletion);
|
compare = Integer.compare(other.getTotalCompletion(), totalCompletion);
|
||||||
if (compare == 0) {
|
if (compare == 0) {
|
||||||
compare = Integer.compare(totalTries, other.getTotalTries());
|
compare = Integer.compare(totalTries, other.getTotalTries());
|
||||||
if(compare == 0) compare = other.getPseudo().compareTo(pseudo);
|
if (compare == 0)
|
||||||
|
compare = other.getPseudo().compareTo(pseudo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return compare;
|
return compare;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addBadge(Badge newBadge) {
|
@Override
|
||||||
badges.add(newBadge);
|
public int hashCode() {
|
||||||
|
return Objects.hash(email, pseudo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Player other = (Player) obj;
|
||||||
|
return Objects.equals(email, other.email) && Objects.equals(pseudo, other.pseudo);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import be.jeffcheasey88.peeratcode.Configuration;
|
||||||
import be.jeffcheasey88.peeratcode.model.Badge;
|
import be.jeffcheasey88.peeratcode.model.Badge;
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
import be.jeffcheasey88.peeratcode.model.Chapter;
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
import be.jeffcheasey88.peeratcode.model.Completion;
|
||||||
|
import be.jeffcheasey88.peeratcode.model.Group;
|
||||||
import be.jeffcheasey88.peeratcode.model.Player;
|
import be.jeffcheasey88.peeratcode.model.Player;
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||||
|
|
||||||
|
@ -34,12 +35,12 @@ public class DatabaseRepository {
|
||||||
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_SIMPLE = "SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?";
|
private static final String GET_PLAYER_SIMPLE = "SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?";
|
||||||
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_DETAILS = "SELECT p.*, scores.score, scores.completions, scores.tries, scores.rank, 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 AND ";
|
||||||
private static final String GET_PLAYER_DETAILS_BY_ID = GET_PLAYER_DETAILS
|
private static final String GET_PLAYER_DETAILS_BY_ID = GET_PLAYER_DETAILS
|
||||||
+ " p.id_player = ? GROUP BY p.id_player;";
|
+ " p.id_player = ? ORDER BY g.fk_chapter, g.fk_puzzle;";
|
||||||
private static final String GET_PLAYER_DETAILS_BY_PSEUDO = GET_PLAYER_DETAILS
|
private static final String GET_PLAYER_DETAILS_BY_PSEUDO = GET_PLAYER_DETAILS
|
||||||
+ "p.pseudo = ? GROUP BY p.pseudo;";
|
+ "p.pseudo = ? ORDER BY g.fk_chapter, g.fk_puzzle;";
|
||||||
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 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";
|
||||||
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 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 (?, ?, ?, ?, ?, ?)";
|
||||||
|
@ -67,7 +68,8 @@ public class DatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
private Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
||||||
return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"), chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date"));
|
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 {
|
||||||
|
@ -83,18 +85,27 @@ public class DatabaseRepository {
|
||||||
if (hasColumn(playerResult, "avatar")) {
|
if (hasColumn(playerResult, "avatar")) {
|
||||||
p.setAvatar(playerResult.getBytes("avatar"));
|
p.setAvatar(playerResult.getBytes("avatar"));
|
||||||
}
|
}
|
||||||
if (hasColumn(playerResult, "sgroup")) {
|
|
||||||
p.setGroups(playerResult.getString("sgroup"));
|
|
||||||
}
|
|
||||||
if (hasColumn(playerResult, "score")) {
|
if (hasColumn(playerResult, "score")) {
|
||||||
p.setRank(playerResult.getInt("rank"));
|
p.setRank(playerResult.getInt("rank"));
|
||||||
p.setTotalScore(playerResult.getInt("score"));
|
p.setTotalScore(playerResult.getInt("score"));
|
||||||
p.setTotalCompletion(playerResult.getInt("completions"));
|
p.setTotalCompletion(playerResult.getInt("completions"));
|
||||||
p.setTotalTries(playerResult.getInt("tries"));
|
p.setTotalTries(playerResult.getInt("tries"));
|
||||||
}
|
}
|
||||||
|
// Manage groups
|
||||||
|
String groupName = playerResult.getString("name");
|
||||||
|
if (groupName != null) {
|
||||||
|
p.addGroup(makeGroup(playerResult));
|
||||||
|
}
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Group makeGroup(ResultSet result) throws SQLException {
|
||||||
|
Group gr = new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"));
|
||||||
|
|
||||||
|
return gr;
|
||||||
|
}
|
||||||
|
|
||||||
private Badge makeBadge(ResultSet rs) throws SQLException {
|
private Badge makeBadge(ResultSet rs) throws SQLException {
|
||||||
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
||||||
}
|
}
|
||||||
|
@ -196,7 +207,7 @@ public class DatabaseRepository {
|
||||||
public Player getPlayerDetails(String pseudoPlayer) {
|
public Player getPlayerDetails(String pseudoPlayer) {
|
||||||
return getPlayerDetails(-1, pseudoPlayer);
|
return getPlayerDetails(-1, pseudoPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Player getPlayerDetails(int id, String pseudo) {
|
private Player getPlayerDetails(int id, String pseudo) {
|
||||||
try {
|
try {
|
||||||
ensureConnection();
|
ensureConnection();
|
||||||
|
@ -209,16 +220,21 @@ public class DatabaseRepository {
|
||||||
completionsStmt.setInt(1, id);
|
completionsStmt.setInt(1, id);
|
||||||
}
|
}
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
if (result.next()) {
|
Player player = null;
|
||||||
Player player = makePlayer(result);
|
while (result.next()) {
|
||||||
completionsStmt = con.prepareStatement(GET_BADGES_OF_PLAYER);
|
if (player == null) {
|
||||||
completionsStmt.setInt(1, result.getInt("id_player"));
|
player = makePlayer(result);
|
||||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
completionsStmt = con.prepareStatement(GET_BADGES_OF_PLAYER);
|
||||||
while (resultBadges.next()) {
|
completionsStmt.setInt(1, result.getInt("id_player"));
|
||||||
player.addBadge(makeBadge(resultBadges));
|
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||||
|
while (resultBadges.next()) {
|
||||||
|
player.addBadge(makeBadge(resultBadges));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
player.addGroup(makeGroup(result));
|
||||||
}
|
}
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
return player;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -230,13 +246,17 @@ public class DatabaseRepository {
|
||||||
ensureConnection();
|
ensureConnection();
|
||||||
PreparedStatement playersStmt = con.prepareStatement(ALL_PLAYERS_FOR_LEADERBOARD);
|
PreparedStatement playersStmt = con.prepareStatement(ALL_PLAYERS_FOR_LEADERBOARD);
|
||||||
ResultSet result = playersStmt.executeQuery();
|
ResultSet result = playersStmt.executeQuery();
|
||||||
SortedSet<Player> players = new TreeSet<Player>();
|
ArrayList<Player> players = new ArrayList<Player>();
|
||||||
Player tmpPlayer;
|
Player tmpPlayer;
|
||||||
while (result.next()) {
|
while (result.next()) {
|
||||||
tmpPlayer = makePlayer(result);
|
tmpPlayer = makePlayer(result);
|
||||||
players.add(tmpPlayer);
|
if (!players.contains(tmpPlayer)) {
|
||||||
|
players.add(tmpPlayer);
|
||||||
|
} else {
|
||||||
|
players.get(players.indexOf(tmpPlayer)).addGroup(makeGroup(result));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return players;
|
return new TreeSet<Player>(players);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class PlayerDetails implements Response {
|
||||||
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());
|
||||||
if (player.getBadges().size() > 0) playerJSON.put("badges", player.getJsonBadges());
|
if (player.getBadges() != null) 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 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue