Skip to content

Commit

Permalink
Fixed duplicate player names in leaderboards
Browse files Browse the repository at this point in the history
  • Loading branch information
cervinakuy committed Mar 3, 2024
1 parent d167226 commit 2e415e5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
45 changes: 30 additions & 15 deletions src/main/java/com/planetgallium/kitpvp/game/Leaderboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@ public Leaderboard(String name, List<TopEntry> initialRankings, int maxSize) {
}

public void updateRankings(TopEntry playerEntry) {
if (rankings.size() < maxSize) {
rankings.add(playerEntry);
sortRankings();
} else if (rankings.size() == maxSize) {
TopEntry lowestRankingPlayer = rankings.get(rankings.size() - 1);
if (playerEntry.getValue() > lowestRankingPlayer.getValue()) {
if (!rankingsContainPlayer(playerEntry.getIdentifier())) {
// remove lowest ranking player and add new better player
rankings.remove(rankings.size() - 1);
rankings.add(playerEntry);
} else {
// update ranking for player already in leaderboard
updatePlayerEntryInRanking(playerEntry);
}
sortRankings(); // resort in both above cases
if (rankingsContainPlayer(playerEntry.getIdentifier())) {
// Update the player's score if they are already in the leaderboard
updatePlayerEntryInRanking(playerEntry);
} else if (rankings.size() < maxSize || playerEntry.getValue() > rankings.get(rankings.size() - 1).getValue()) {
// Add the new player if there's space, or they have a higher score than the lowest in the leaderboard
if (rankings.size() == maxSize) {
// Remove the lowest ranking player to make space for the new entry
rankings.remove(rankings.size() - 1);
}
rankings.add(playerEntry);
}
sortRankings();
}

// public void updateRankings(TopEntry playerEntry) {
// if (rankings.size() < maxSize) {
// rankings.add(playerEntry);
// sortRankings();
// } else if (rankings.size() == maxSize) {
// TopEntry lowestRankingPlayer = rankings.get(rankings.size() - 1);
// if (playerEntry.getValue() > lowestRankingPlayer.getValue()) {
// if (!rankingsContainPlayer(playerEntry.getIdentifier())) {
// // remove lowest ranking player and add new better player
// rankings.remove(rankings.size() - 1);
// rankings.add(playerEntry);
// } else {
// // update ranking for player already in leaderboard
// updatePlayerEntryInRanking(playerEntry);
// }
// sortRankings(); // resort in both above cases
// }
// }
// }

public TopEntry getNRanking(int n) { // n = 1 is top player
if (n <= rankings.size() && n >= 1) {
return rankings.get(n - 1);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/planetgallium/kitpvp/game/Leaderboards.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public Leaderboards(Game plugin) {
leaderboards.put("kills",
new Leaderboard("kills", database.getTopNStats("kills", 25), 25));
leaderboards.put("deaths",
new Leaderboard("kills", database.getTopNStats("deaths", 25), 25));
new Leaderboard("deaths", database.getTopNStats("deaths", 25), 25));
leaderboards.put("level",
new Leaderboard("kills", database.getTopNStats("level", 25), 25));
new Leaderboard("level", database.getTopNStats("level", 25), 25));
}

public void updateRankings(String leaderboardName, TopEntry newEntry) {
Expand Down

0 comments on commit 2e415e5

Please sign in to comment.