Skip to content

Commit

Permalink
Added top 1, 2, 3 placeholders for elo
Browse files Browse the repository at this point in the history
  • Loading branch information
Devlrxxh committed Feb 6, 2024
1 parent 64d6d3e commit fc0cfb5
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 3 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<id>refine-public</id>
<url>https://maven.refinedev.xyz/repository/public-repo/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -87,6 +91,12 @@
<version>1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/me/funky/praxi/Placeholder.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
7 changes: 7 additions & 0 deletions src/main/java/me/funky/praxi/Praxi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/me/funky/praxi/leaderboards/Leaderboard.java
Original file line number Diff line number Diff line change
@@ -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<Positions> topPositions = init();

public static List<Positions> init() {
FindIterable<Document> profileDocuments = Profile.collection.find();
List<PlayerElo> 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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/me/funky/praxi/leaderboards/PlayerElo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.funky.praxi.leaderboards;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PlayerElo implements Comparable<PlayerElo> {
private final String playerName;
private final int elo;


@Override
public int compareTo(PlayerElo other) {
return Integer.compare(this.elo, other.elo);
}
}
16 changes: 16 additions & 0 deletions src/main/java/me/funky/praxi/leaderboards/Positions.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
9 changes: 7 additions & 2 deletions src/main/java/me/funky/praxi/profile/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ public class Profile {

@Getter
private static final Map<UUID, Profile> profiles = new HashMap<>();
private static MongoCollection<Document> collection;
public static MongoCollection<Document> collection;
private final ProfileOptions options;
private final ProfileKitEditorData kitEditorData;
private final Map<Kit, ProfileKitData> kitData;
private final List<DuelRequest> duelRequests;
private final UUID uuid;
private final String username;
private ProfileState state;
private DuelProcedure duelProcedure;
private ProfileRematchData rematchData;
Expand All @@ -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();
Expand Down Expand Up @@ -188,14 +190,16 @@ 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) {
this.save();
return;
}

document.getString("username");

Document options = (Document) document.get("options");

this.options.showScoreboard(options.getBoolean("showScoreboard"));
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
main: me.funky.praxi.Praxi
name: Praxi
version: '1.0.0'
author: funkyranveer, joeleoli, lugami, lrxh
author: funkyranveer, joeleoli, lugami, lrxh
depend:
- ProtocolLib
softdepend:
- PlaceholderAPI

0 comments on commit fc0cfb5

Please sign in to comment.