Modify playerDetails route to give the completions details and correct rank of player
This commit is contained in:
parent
1059864aa5
commit
0249a7641f
6 changed files with 86 additions and 54 deletions
|
@ -9,17 +9,26 @@ public class Completion{
|
|||
private String fileName;
|
||||
private byte[] code;
|
||||
private int score;
|
||||
private String puzzleName;
|
||||
|
||||
public Completion(int playerId, int puzzleId, int tries, String fileName, int score) {
|
||||
this(playerId, puzzleId, tries, score, fileName, null, null, null);
|
||||
this(playerId, puzzleId, tries, score, fileName, null, null, null, null);
|
||||
}
|
||||
public Completion(int playerId, int puzzleId, int tries, String fileName, int score, String puzzleName) {
|
||||
this(playerId, puzzleId, tries, score, fileName, null, null, null, puzzleName);
|
||||
}
|
||||
|
||||
public Completion(int playerId, int puzzleId, String fileName, byte[] file, byte[] response, Puzzle currentPuzzle) {
|
||||
this(playerId, puzzleId, 0, 0, fileName, file, response, currentPuzzle);
|
||||
this(playerId, puzzleId, 0, 0, fileName, file, response, currentPuzzle, null);
|
||||
}
|
||||
|
||||
public Completion(int initTries, int initScore) {
|
||||
// For group leaderboard
|
||||
this(-1, -1, initTries, initScore, null, null, null, null, null);
|
||||
}
|
||||
|
||||
public Completion(int playerId, int puzzleId, int tries, int score, String fileName, byte[] file, byte[] response,
|
||||
Puzzle currentPuzzle) {
|
||||
Puzzle currentPuzzle, String initPuzzleName) {
|
||||
this.playerId = playerId;
|
||||
this.puzzleId = puzzleId;
|
||||
this.fileName = fileName;
|
||||
|
@ -32,6 +41,8 @@ public class Completion{
|
|||
|
||||
if (currentPuzzle != null)
|
||||
addTry(currentPuzzle, response);
|
||||
|
||||
puzzleName = initPuzzleName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -73,6 +84,10 @@ public class Completion{
|
|||
return fileName;
|
||||
}
|
||||
|
||||
public String getPuzzleName() {
|
||||
return puzzleName;
|
||||
}
|
||||
|
||||
public byte[] getCode() {
|
||||
return code;
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ public class Group implements Comparable<Group> {
|
|||
if (pPosition < 0) {
|
||||
players.add(newPlayer);
|
||||
} else {
|
||||
players.get(pPosition).addScore(newPlayer.getTotalScore(), newPlayer.getTotalTries());
|
||||
players.get(pPosition).addCompletion(new Completion(newPlayer.getTotalTries(), newPlayer.getTotalScore()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,12 +15,10 @@ public class Player implements Comparable<Player> {
|
|||
private String lastname;
|
||||
private String description;
|
||||
private Set<Group> groups;
|
||||
private Set<Completion> completions = new HashSet<Completion>();
|
||||
private byte[] avatar;
|
||||
|
||||
private int rank;
|
||||
private int totalScore;
|
||||
private int totalCompletion;
|
||||
private int totalTries;
|
||||
|
||||
private Set<Badge> badges;
|
||||
|
||||
|
@ -30,21 +28,12 @@ public class Player implements Comparable<Player> {
|
|||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.description = description;
|
||||
|
||||
totalScore = 0;
|
||||
totalCompletion = 0;
|
||||
totalTries = 0;
|
||||
}
|
||||
|
||||
public Player(String pseudo, int score, int tries) {
|
||||
// For groups leaderboard
|
||||
this.pseudo = pseudo;
|
||||
totalScore = score;
|
||||
totalTries = tries;
|
||||
if (totalTries > 0)
|
||||
totalCompletion = totalTries;
|
||||
else
|
||||
totalCompletion = 0;
|
||||
this.completions.add(new Completion(tries, score));
|
||||
email = ""; // TO make compareTo and equals works as usual
|
||||
}
|
||||
|
||||
|
@ -107,34 +96,40 @@ public class Player implements Comparable<Player> {
|
|||
rank = newRank;
|
||||
}
|
||||
|
||||
public void addCompletion(Completion makeCompletion) {
|
||||
completions.add(makeCompletion);
|
||||
}
|
||||
|
||||
public int getTotalScore() {
|
||||
int totalScore = 0;
|
||||
for (Completion c: completions) {
|
||||
totalScore = totalScore + c.getScore();
|
||||
}
|
||||
return totalScore;
|
||||
}
|
||||
|
||||
public void setTotalScore(int totalScore) {
|
||||
this.totalScore = totalScore;
|
||||
}
|
||||
|
||||
public void addScore(int addScore, int tries) {
|
||||
totalScore = totalScore + addScore;
|
||||
totalTries = totalTries + tries;
|
||||
totalCompletion++;
|
||||
}
|
||||
|
||||
public int getTotalCompletion() {
|
||||
return totalCompletion;
|
||||
return completions.size();
|
||||
}
|
||||
|
||||
public void setTotalCompletion(int totalCompletion) {
|
||||
this.totalCompletion = totalCompletion;
|
||||
public JSONArray getJsonCompletions() {
|
||||
JSONArray completionsJSON = new JSONArray();
|
||||
for (Completion completion : completions) {
|
||||
JSONObject completionJSON = new JSONObject();
|
||||
completionJSON.put("puzzleName", completion.getPuzzleName());
|
||||
completionJSON.put("tries", completion.getTries());
|
||||
completionJSON.put("score", completion.getScore());
|
||||
completionsJSON.add(completionJSON);
|
||||
}
|
||||
return completionsJSON;
|
||||
}
|
||||
|
||||
public int getTotalTries() {
|
||||
return totalTries;
|
||||
int totalTries = 0;
|
||||
for (Completion c: completions) {
|
||||
totalTries = totalTries + c.getTries();
|
||||
}
|
||||
|
||||
public void setTotalTries(int totalTries) {
|
||||
this.totalTries = totalTries;
|
||||
return totalTries;
|
||||
}
|
||||
|
||||
public Set<Badge> getBadges() {
|
||||
|
@ -171,11 +166,11 @@ public class Player implements Comparable<Player> {
|
|||
return 0;
|
||||
if (other == null)
|
||||
return -1;
|
||||
int compare = Integer.compare(other.getTotalScore(), totalScore);
|
||||
int compare = Integer.compare(other.getTotalScore(), getTotalScore());
|
||||
if (compare == 0) {
|
||||
compare = Integer.compare(other.getTotalCompletion(), totalCompletion);
|
||||
compare = Integer.compare(other.getTotalCompletion(), getTotalCompletion());
|
||||
if (compare == 0) {
|
||||
compare = Integer.compare(totalTries, other.getTotalTries());
|
||||
compare = Integer.compare(getTotalTries(), other.getTotalTries());
|
||||
if (compare == 0)
|
||||
compare = other.getPseudo().compareTo(pseudo);
|
||||
}
|
||||
|
@ -200,5 +195,4 @@ public class Player implements Comparable<Player> {
|
|||
Player other = (Player) obj;
|
||||
return Objects.equals(email, other.email) && Objects.equals(pseudo, other.pseudo);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ public enum DatabaseQuery {
|
|||
|
||||
// PLAYERS
|
||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||
GET_PLAYER_DETAILS("SELECT p.*, g.*, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank\r\n"
|
||||
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
||||
+ "FROM players p\r\n"
|
||||
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
||||
+ "LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group\r\n"
|
||||
|
@ -77,6 +77,8 @@ public enum DatabaseQuery {
|
|||
+ "WHERE "),
|
||||
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
|
||||
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN players p ON p.id_player = c.fk_player GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
|
||||
|
||||
// BADGES
|
||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
||||
|
|
|
@ -116,15 +116,18 @@ public class DatabaseRepository {
|
|||
}
|
||||
|
||||
private Completion makeCompletion(ResultSet completionResult) throws SQLException {
|
||||
String name = null;
|
||||
String fileName = null;
|
||||
if (hasColumn(completionResult, "fileName"))
|
||||
name = completionResult.getString("fileName");
|
||||
fileName = completionResult.getString("fileName");
|
||||
String puzzleName = null;
|
||||
if (hasColumn(completionResult, "name"))
|
||||
puzzleName = completionResult.getString("name");
|
||||
|
||||
return new Completion(completionResult.getInt("fk_player"), completionResult.getInt("fk_puzzle"), completionResult.getInt("tries"),
|
||||
name, completionResult.getInt("score"));
|
||||
fileName, completionResult.getInt("score"), puzzleName);
|
||||
}
|
||||
|
||||
private Player makePlayer(ResultSet playerResult) throws SQLException {
|
||||
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||
playerResult.getString("firstName"), playerResult.getString("lastName"),
|
||||
playerResult.getString("description"));
|
||||
|
@ -132,10 +135,11 @@ public class DatabaseRepository {
|
|||
p.setAvatar(playerResult.getBytes("avatar"));
|
||||
}
|
||||
if (hasColumn(playerResult, "score")) {
|
||||
p.setRank(playerResult.getInt("rank"));
|
||||
p.setTotalScore(playerResult.getInt("score"));
|
||||
p.setTotalCompletion(playerResult.getInt("completions"));
|
||||
p.setTotalTries(playerResult.getInt("tries"));
|
||||
p.addCompletion(new Completion(playerResult.getInt("tries"), playerResult.getInt("score")));
|
||||
for (int ct = 1; ct < playerResult.getInt("completions"); ct++)
|
||||
{ // TODO refactor for V3
|
||||
p.addCompletion(new Completion(0, 0));
|
||||
}
|
||||
}
|
||||
// Manage groups
|
||||
String groupName = playerResult.getString("name");
|
||||
|
@ -143,6 +147,13 @@ public class DatabaseRepository {
|
|||
p.addGroup(makeGroup(playerResult));
|
||||
}
|
||||
|
||||
// ADD rank
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_RANK.prepare(con);
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
p.setRank(result.getInt("rank"));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -260,7 +271,7 @@ public class DatabaseRepository {
|
|||
completionsStmt.setInt(1, idPlayer);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makePlayer(result);
|
||||
return makePlayer(result, idPlayer);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -291,9 +302,10 @@ public class DatabaseRepository {
|
|||
Player player = null;
|
||||
while (result.next()) {
|
||||
if (player == null) {
|
||||
player = makePlayer(result);
|
||||
id = result.getInt("id_player");
|
||||
player = makePlayer(result, id);
|
||||
completionsStmt = DatabaseQuery.GET_BADGES_OF_PLAYER.prepare(this.con);
|
||||
completionsStmt.setInt(1, result.getInt("id_player"));
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||
while (resultBadges.next()) {
|
||||
player.addBadge(makeBadge(resultBadges));
|
||||
|
@ -302,6 +314,14 @@ public class DatabaseRepository {
|
|||
player.addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
// ADD completions
|
||||
completionsStmt = DatabaseQuery.GET_PLAYER_COMPLETIONS.prepare(con);
|
||||
completionsStmt.setInt(1, id);
|
||||
result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
player.addCompletion(makeCompletion(result));
|
||||
}
|
||||
|
||||
return player;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -317,7 +337,7 @@ public class DatabaseRepository {
|
|||
ArrayList<Player> players = new ArrayList<Player>();
|
||||
Player tmpPlayer;
|
||||
while (result.next()) {
|
||||
tmpPlayer = makePlayer(result);
|
||||
tmpPlayer = makePlayer(result, result.getInt("id_player"));
|
||||
if (!players.contains(tmpPlayer)) {
|
||||
players.add(tmpPlayer);
|
||||
} else {
|
||||
|
|
|
@ -46,6 +46,7 @@ public class PlayerDetails implements Response {
|
|||
playerJSON.put("rank", player.getRank());
|
||||
playerJSON.put("score", player.getTotalScore());
|
||||
playerJSON.put("completions", player.getTotalCompletion());
|
||||
playerJSON.put("completionsList", player.getJsonCompletions());
|
||||
playerJSON.put("tries", player.getTotalTries());
|
||||
if (player.getBadges() != null)
|
||||
playerJSON.put("badges", player.getJsonBadges());
|
||||
|
|
Loading…
Add table
Reference in a new issue