From 2e415e59fe22d8ac216e1b67d6117aa5726ee497 Mon Sep 17 00:00:00 2001 From: cervinakuy <16023430+cervinakuy@users.noreply.github.com> Date: Sun, 3 Mar 2024 03:18:23 -0800 Subject: [PATCH] Fixed duplicate player names in leaderboards --- .../kitpvp/game/Leaderboard.java | 45 ++++++++++++------- .../kitpvp/game/Leaderboards.java | 4 +- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/planetgallium/kitpvp/game/Leaderboard.java b/src/main/java/com/planetgallium/kitpvp/game/Leaderboard.java index b3b9028d..4269b9e2 100644 --- a/src/main/java/com/planetgallium/kitpvp/game/Leaderboard.java +++ b/src/main/java/com/planetgallium/kitpvp/game/Leaderboard.java @@ -19,25 +19,40 @@ public Leaderboard(String name, List 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); diff --git a/src/main/java/com/planetgallium/kitpvp/game/Leaderboards.java b/src/main/java/com/planetgallium/kitpvp/game/Leaderboards.java index 709723bb..fff22a6c 100644 --- a/src/main/java/com/planetgallium/kitpvp/game/Leaderboards.java +++ b/src/main/java/com/planetgallium/kitpvp/game/Leaderboards.java @@ -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) {