package dev.peerat.backend.model; import java.util.Base64; import java.util.HashSet; import java.util.Objects; import java.util.Set; import dev.peerat.framework.utils.json.JsonArray; import dev.peerat.framework.utils.json.JsonMap; public class Player implements Comparable { private String pseudo; private String email; private String firstname; private String lastname; private String description; private Set groups; private Set completions = new HashSet(); private byte[] avatar; private int rank; private Set badges; public Player(String pseudo, String email, String firstname, String lastname) { this.pseudo = pseudo; this.email = email; this.firstname = firstname; this.lastname = lastname; this.description = ""; } public Player(String pseudo, int score, int tries) { // For groups leaderboard this.pseudo = pseudo; this.completions.add(new Completion(tries, score)); email = ""; // TO make compareTo and equals works as usual } public Player(String email) { // For player find in Map during register process this.email = email; } public void setPseudo(String pseudo){ this.pseudo = pseudo; } public String getPseudo() { return this.pseudo; } public String getEmail() { return this.email; } public String getFirstname() { return this.firstname; } public String getLastname() { return this.lastname; } public String getDescription() { return this.description; } public Set getGroups() { return groups; } public JsonArray getJsonGroups() { if (groups != null) { JsonArray groupsJSON = new JsonArray(); for (Group group : groups) { groupsJSON.add(group.toJson()); } return groupsJSON; } return null; } public void addGroup(Group newGroup) { if (newGroup != null) { if (this.groups == null) this.groups = new HashSet(); this.groups.add(newGroup); } } public byte[] getAvatar() { return this.avatar; } public void setAvatar(byte[] newAvatar) { avatar = newAvatar; } public int getRank() { return rank; } public void setRank(int newRank) { 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 int getTotalCompletion() { return completions.size(); } public JsonArray getJsonCompletions() { JsonArray completionsJSON = new JsonArray(); for (Completion completion : completions) { JsonMap completionJSON = new JsonMap(); completionJSON.set("puzzleName", completion.getPuzzleName()); completionJSON.set("tries", completion.getTries()); completionJSON.set("score", completion.getScore()); completionsJSON.add(completionJSON); } return completionsJSON; } public int getTotalTries() { int totalTries = 0; for (Completion c: completions) { totalTries = totalTries + c.getTries(); } return totalTries; } public Set getBadges() { return badges; } public JsonArray getJsonBadges() { if (badges == null) return null; JsonArray badgesJSON = new JsonArray(); for (Badge badge : badges) { JsonMap badgeJSON = new JsonMap(); badgeJSON.set("name", badge.getName()); byte[] logo = badge.getLogo(); if (logo != null) badgeJSON.set("logo", Base64.getEncoder().encodeToString(logo)); badgeJSON.set("level", badge.getLevel()); badgesJSON.add(badgeJSON); } return badgesJSON; } public void addBadge(Badge newBadge) { if (newBadge != null) { if (badges == null) badges = new HashSet(); badges.add(newBadge); } } @Override public int compareTo(Player other) { if (this == other) return 0; if (other == null) return -1; int compare = Integer.compare(other.getTotalScore(), getTotalScore()); if (compare == 0) { compare = Integer.compare(other.getTotalCompletion(), getTotalCompletion()); if (compare == 0) { compare = Integer.compare(getTotalTries(), other.getTotalTries()); if (compare == 0) compare = other.getPseudo().compareTo(pseudo); } } return compare; } @Override public int hashCode(){ return Objects.hash(email); } @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); } }