From fc0cfb5ab02b61cf37934d32608be42ba9a4e051 Mon Sep 17 00:00:00 2001 From: nodyt Date: Tue, 6 Feb 2024 18:06:32 +0100 Subject: [PATCH] Added top 1, 2, 3 placeholders for elo --- pom.xml | 10 ++++ src/main/java/me/funky/praxi/Placeholder.java | 48 ++++++++++++++++++ src/main/java/me/funky/praxi/Praxi.java | 7 +++ .../funky/praxi/leaderboards/Leaderboard.java | 49 +++++++++++++++++++ .../funky/praxi/leaderboards/PlayerElo.java | 17 +++++++ .../funky/praxi/leaderboards/Positions.java | 16 ++++++ .../java/me/funky/praxi/profile/Profile.java | 9 +++- src/main/resources/plugin.yml | 6 ++- 8 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/main/java/me/funky/praxi/Placeholder.java create mode 100644 src/main/java/me/funky/praxi/leaderboards/Leaderboard.java create mode 100644 src/main/java/me/funky/praxi/leaderboards/PlayerElo.java create mode 100644 src/main/java/me/funky/praxi/leaderboards/Positions.java diff --git a/pom.xml b/pom.xml index 625ef64..7acc069 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,10 @@ refine-public https://maven.refinedev.xyz/repository/public-repo/ + + placeholderapi + https://repo.extendedclip.com/content/repositories/placeholderapi/ + @@ -87,6 +91,12 @@ 1.3 compile + + me.clip + placeholderapi + 2.11.2 + provided + diff --git a/src/main/java/me/funky/praxi/Placeholder.java b/src/main/java/me/funky/praxi/Placeholder.java new file mode 100644 index 0000000..ff0e611 --- /dev/null +++ b/src/main/java/me/funky/praxi/Placeholder.java @@ -0,0 +1,48 @@ +package me.funky.praxi; + + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import me.funky.praxi.leaderboards.Leaderboard; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.jetbrains.annotations.NotNull; + +public class Placeholder extends PlaceholderExpansion { + @Override + public @NotNull String getIdentifier() { + return "praxi"; + } + + @Override + public @NotNull String getAuthor() { + return "lrxh"; + } + + @Override + public @NotNull String getVersion() { + return "1.0"; + } + + @Override + public boolean canRegister() { + return Bukkit.getPluginManager().isPluginEnabled("Praxi"); + } + + @Override + public String onRequest(OfflinePlayer player, @NotNull String identifier) { + if (player == null) return ""; + if (!player.isOnline()) return "Offline Player"; + + switch (identifier) { + case "top_1": + return Leaderboard.getTopPositions().get(0).getPlayerElo().getPlayerName(); + case "top_2": + return Leaderboard.getTopPositions().get(1).getPlayerElo().getPlayerName(); + case "top_3": + return Leaderboard.getTopPositions().get(2).getPlayerElo().getPlayerName(); + } + return null; + } + +} \ No newline at end of file diff --git a/src/main/java/me/funky/praxi/Praxi.java b/src/main/java/me/funky/praxi/Praxi.java index 9925cd3..68da21d 100644 --- a/src/main/java/me/funky/praxi/Praxi.java +++ b/src/main/java/me/funky/praxi/Praxi.java @@ -34,6 +34,7 @@ import me.funky.praxi.kit.KitEditorListener; import me.funky.praxi.kit.KitTypeAdapter; import me.funky.praxi.kit.command.KitCommand; +import me.funky.praxi.leaderboards.Leaderboard; import me.funky.praxi.match.Match; import me.funky.praxi.match.MatchListener; import me.funky.praxi.party.Party; @@ -56,6 +57,7 @@ import org.bukkit.Difficulty; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import xyz.refinedev.api.spigot.SpigotHandler; @@ -184,6 +186,11 @@ public void onEnable() { }); System.gc(); + Leaderboard.init(); + Plugin placeholderAPI = getServer().getPluginManager().getPlugin("PlaceholderAPI"); + if (placeholderAPI != null && placeholderAPI.isEnabled()) { + new Placeholder().register(); + } } private void loadCommandManager() { diff --git a/src/main/java/me/funky/praxi/leaderboards/Leaderboard.java b/src/main/java/me/funky/praxi/leaderboards/Leaderboard.java new file mode 100644 index 0000000..56757ea --- /dev/null +++ b/src/main/java/me/funky/praxi/leaderboards/Leaderboard.java @@ -0,0 +1,49 @@ +package me.funky.praxi.leaderboards; + +import com.mongodb.client.FindIterable; +import lombok.AllArgsConstructor; +import lombok.Getter; +import me.funky.praxi.profile.Profile; +import org.bson.Document; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Leaderboard { + @Getter + private static final List topPositions = init(); + + public static List init() { + FindIterable profileDocuments = Profile.collection.find(); + List topPlayers = profileDocuments.into(new ArrayList<>()).stream() + .map(Leaderboard::mapToPlayerElo) + .sorted(Comparator.reverseOrder()) + .limit(3) + .collect(Collectors.toList()); + + return IntStream.range(0, topPlayers.size()) + .mapToObj(i -> new Positions(i + 1, topPlayers.get(i))) + .collect(Collectors.toList()); + } + + private static PlayerElo mapToPlayerElo(Document profileDocument) { + String playerName = profileDocument.getString("username"); + int elo = getElo(profileDocument); + return new PlayerElo(playerName, elo); + } + + private static int getElo(Document profileDocument) { + Document kitStatistics = (Document) profileDocument.get("kitStatistics"); + int totalQueue = kitStatistics.keySet().size(); + if (totalQueue == 0) { + return 0; + } + + return kitStatistics.values().stream() + .mapToInt(kit -> ((Document) kit).getInteger("elo")) + .sum() / totalQueue; + } +} diff --git a/src/main/java/me/funky/praxi/leaderboards/PlayerElo.java b/src/main/java/me/funky/praxi/leaderboards/PlayerElo.java new file mode 100644 index 0000000..f379bbf --- /dev/null +++ b/src/main/java/me/funky/praxi/leaderboards/PlayerElo.java @@ -0,0 +1,17 @@ +package me.funky.praxi.leaderboards; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class PlayerElo implements Comparable { + private final String playerName; + private final int elo; + + + @Override + public int compareTo(PlayerElo other) { + return Integer.compare(this.elo, other.elo); + } +} diff --git a/src/main/java/me/funky/praxi/leaderboards/Positions.java b/src/main/java/me/funky/praxi/leaderboards/Positions.java new file mode 100644 index 0000000..e33447d --- /dev/null +++ b/src/main/java/me/funky/praxi/leaderboards/Positions.java @@ -0,0 +1,16 @@ +package me.funky.praxi.leaderboards; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class Positions { + private final int rank; + private final PlayerElo playerElo; + + @Override + public String toString() { + return rank + ". " + playerElo.getPlayerName() + ", ELO: " + playerElo.getElo(); + } +} diff --git a/src/main/java/me/funky/praxi/profile/Profile.java b/src/main/java/me/funky/praxi/profile/Profile.java index 57808ef..db69c88 100644 --- a/src/main/java/me/funky/praxi/profile/Profile.java +++ b/src/main/java/me/funky/praxi/profile/Profile.java @@ -37,12 +37,13 @@ public class Profile { @Getter private static final Map profiles = new HashMap<>(); - private static MongoCollection collection; + public static MongoCollection collection; private final ProfileOptions options; private final ProfileKitEditorData kitEditorData; private final Map kitData; private final List duelRequests; private final UUID uuid; + private final String username; private ProfileState state; private DuelProcedure duelProcedure; private ProfileRematchData rematchData; @@ -54,6 +55,7 @@ public class Profile { public Profile(UUID uuid) { this.uuid = uuid; + this.username = Bukkit.getPlayer(uuid).getName(); this.state = ProfileState.LOBBY; this.options = new ProfileOptions(); this.kitEditorData = new ProfileKitEditorData(); @@ -188,7 +190,7 @@ public boolean isBusy() { return state != ProfileState.LOBBY; } - void load() { + public void load() { Document document = collection.find(Filters.eq("uuid", uuid.toString())).first(); if (document == null) { @@ -196,6 +198,8 @@ void load() { return; } + document.getString("username"); + Document options = (Document) document.get("options"); this.options.showScoreboard(options.getBoolean("showScoreboard")); @@ -246,6 +250,7 @@ void load() { public void save() { Document document = new Document(); document.put("uuid", uuid.toString()); + document.put("username", username); Document optionsDocument = new Document(); optionsDocument.put("showScoreboard", options.showScoreboard()); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fc8b5d0..6dc5216 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,8 @@ main: me.funky.praxi.Praxi name: Praxi version: '1.0.0' -author: funkyranveer, joeleoli, lugami, lrxh \ No newline at end of file +author: funkyranveer, joeleoli, lugami, lrxh +depend: + - ProtocolLib +softdepend: + - PlaceholderAPI \ No newline at end of file