Skip to content

Commit

Permalink
Added Kills Leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Devlrxxh committed Apr 7, 2024
1 parent d8478c7 commit eaf7ea2
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 37 deletions.
20 changes: 13 additions & 7 deletions src/main/java/me/lrxh/practice/Placeholder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.lrxh.practice.leaderboards.Leaderboard;
import me.lrxh.practice.leaderboards.PlayerElo;
import me.lrxh.practice.profile.Profile;
import me.lrxh.practice.util.TimeUtil;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -36,16 +35,23 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
if (!player.isOnline()) return "Offline Player";

String[] parts = identifier.split("_");
if (parts.length == 4 && parts[0].equalsIgnoreCase("lb")) {
if (parts.length >= 4 && parts.length <= 6 && parts[0].equalsIgnoreCase("lb")) {
String queue = parts[1];
int position = Integer.parseInt(parts[2]);
PlayerElo playerElo = Leaderboard.getEloLeaderboards().get(queue).getTopPlayers().get(position - 1);
String valueType = parts[parts.length - 1];
boolean showName = parts[parts.length - 2].equalsIgnoreCase("name");

switch (parts[3]) {
case "name":
return playerElo.getPlayerName();
if (position > 10 || position < 1) return "";

switch (valueType) {
case "elo":
return String.valueOf(playerElo.getElo());
return showName ?
Leaderboard.getEloLeaderboards().get(queue).getTopEloPlayers().get(position - 1).getPlayerName() :
String.valueOf(Leaderboard.getEloLeaderboards().get(queue).getTopEloPlayers().get(position - 1).getElo());
case "kills":
return showName ?
Leaderboard.getEloLeaderboards().get(queue).getTopKillPlayers().get(position - 1).getPlayerName() :
String.valueOf(Leaderboard.getEloLeaderboards().get(queue).getTopKillPlayers().get(position - 1).getKills());
}
} else if (identifier.equalsIgnoreCase("leaderboards_update")) {
return TimeUtil.millisToTimer(Leaderboard.getRefreshTime());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/lrxh/practice/Practice.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import me.lrxh.practice.kit.KitEditorListener;
import me.lrxh.practice.kit.command.KitCommand;
import me.lrxh.practice.leaderboards.LeaderboardThread;
import me.lrxh.practice.leaderboards.LeaderboardsCommand;
import me.lrxh.practice.leaderboards.command.LeaderboardsCommand;
import me.lrxh.practice.match.Match;
import me.lrxh.practice.match.MatchListener;
import me.lrxh.practice.party.Party;
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/me/lrxh/practice/leaderboards/Leaderboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.*;
import java.util.stream.Collectors;

@Getter
@Setter
public class Leaderboard {
@Getter
@Setter
Expand All @@ -28,17 +30,27 @@ public static Map<String, QueueLeaderboard> init() {
}

private static QueueLeaderboard initQueueLeaderboard(String queue) {
List<PlayerElo> topPlayers = Profile.collection.find().into(new ArrayList<>()).stream()
List<PlayerElo> topEloPlayers = Profile.collection.find().into(new ArrayList<>()).stream()
.map(profileDocument -> mapToPlayerElo(profileDocument, queue))
.sorted(Comparator.reverseOrder())
.sorted(Comparator.comparingInt(PlayerElo::getElo).reversed()) // Sort by elo
.limit(10)
.collect(Collectors.toList());

for (int i = topPlayers.size(); i < 10; i++) {
topPlayers.add(new PlayerElo("???", 0, 0, 0));
List<PlayerElo> topKillPlayers = Profile.collection.find().into(new ArrayList<>()).stream()
.map(profileDocument -> mapToPlayerElo(profileDocument, queue))
.sorted(Comparator.comparingInt(PlayerElo::getKills).reversed()) // Sort by kills
.limit(10)
.collect(Collectors.toList());

for (int i = topEloPlayers.size(); i < 10; i++) {
topEloPlayers.add(new PlayerElo("???", 0, 0, 0));
}

for (int i = topKillPlayers.size(); i < 10; i++) {
topKillPlayers.add(new PlayerElo("???", 0, 0, 0));
}

return new QueueLeaderboard(queue, topPlayers);
return new QueueLeaderboard(queue, topEloPlayers, topKillPlayers);
}


Expand All @@ -58,12 +70,12 @@ private static int getElo(Document profileDocument, String queue) {
private static int getKills(Document profileDocument, String queue) {
Document kitStatistics = (Document) profileDocument.get("kitStatistics");
Document queueStats = (Document) kitStatistics.get(queue);
return queueStats != null ? queueStats.getInteger("won") : 1000;
return queueStats != null ? queueStats.getInteger("won") : 0;
}

private static int getLoses(Document profileDocument, String queue) {
Document kitStatistics = (Document) profileDocument.get("kitStatistics");
Document queueStats = (Document) kitStatistics.get(queue);
return queueStats != null ? queueStats.getInteger("lost") : 1000;
return queueStats != null ? queueStats.getInteger("lost") : 0;
}
}
96 changes: 87 additions & 9 deletions src/main/java/me/lrxh/practice/leaderboards/LeaderboardsMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import me.lrxh.practice.util.CC;
import me.lrxh.practice.util.ItemBuilder;
import me.lrxh.practice.util.PlayerUtil;
import me.lrxh.practice.util.TimeUtil;
import me.lrxh.practice.util.menu.Button;
import me.lrxh.practice.util.menu.Menu;
import me.lrxh.practice.util.menu.filters.Filters;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
Expand All @@ -22,9 +23,7 @@
@AllArgsConstructor
public class LeaderboardsMenu extends Menu {

{
setAutoUpdate(true);
}
private boolean kills;

@Override
public int getSize() {
Expand All @@ -36,6 +35,14 @@ public Filters getFilter() {
return Filters.valueOf(Practice.getInstance().getMenusConfig().getString("LEADERBOARD.FILTER"));
}

public boolean getFixedPositions() {
return false;
}

public boolean resetCursor() {
return false;
}

@Override
public String getTitle(Player player) {
return Practice.getInstance().getMenusConfig().getString("LEADERBOARD.TITLE");
Expand All @@ -46,13 +53,18 @@ public Map<Integer, Button> getButtons(Player player) {
Map<Integer, Button> buttons = new HashMap<>();

int i = 10;

for (Queue queue : Practice.getInstance().getCache().getQueues()) {
if (!queue.isRanked()) {
buttons.put(i++, new LeaderboardKitButton(queue));
if (!kills) {
buttons.put(i++, new LeaderboardEloButton(queue));
} else {
buttons.put(i++, new LeaderboardKillsButton(queue));
}
}
}

buttons.put(4, new GlobalStatsButton());
buttons.put(7, new SwitchLeaderBoardButton());

return buttons;
}
Expand Down Expand Up @@ -84,7 +96,40 @@ public ItemStack getButtonItem(Player player) {
}

@AllArgsConstructor
private class LeaderboardKitButton extends Button {
private class SwitchLeaderBoardButton extends Button {
public ItemStack getButtonItem(Player player) {

List<String> lore = new ArrayList<>();

lore.add(CC.translate("&7Select one of the following"));
lore.add(CC.translate("&7type of leaderboards!"));
lore.add(CC.translate(""));
lore.add(CC.translate("&fCurrent view:"));
lore.add(!kills ? " &7&l▶ &aRanked ELO" : " &7&l▶ &7Ranked ELO");
lore.add(kills ? " &7&l▶ &aUnranked Wins" : " &7&l▶ &7Unranked Wins");
lore.add(CC.translate(""));
lore.add(CC.translate("&aClick to switch!"));

return new ItemBuilder(Material.COMPASS)
.name(Practice.getInstance().getMenusConfig().getString("LEADERBOARD.TOGGLE_VIEW"))
.lore(lore)
.clearEnchantments()
.clearFlags()
.build();
}

@Override
public void clicked(Player player, ClickType clickType) {
if (kills) {
player.chat("/leaderboard elo");
} else {
player.chat("/leaderboard kills");
}
}
}

@AllArgsConstructor
private class LeaderboardEloButton extends Button {

private Queue queue;

Expand All @@ -96,11 +141,11 @@ public ItemStack getButtonItem(Player player) {
.replace("<player_elo>", String.valueOf(Profile.getByUuid(player.getUniqueId()).getKitData().get(queue.getKit()).getElo())));

for (int i = 1; i <= 10; i++) {
PlayerElo playerElo = Leaderboard.getEloLeaderboards().get(queue.getKit().getName()).getTopPlayers().get(i - 1);
PlayerElo playerElo = Leaderboard.getEloLeaderboards().get(queue.getKit().getName()).getTopEloPlayers().get(i - 1);

lore.add(Practice.getInstance().getMenusConfig().getString("LEADERBOARD.POSITION")
.replace("<position>", String.valueOf(i))
.replace("<player_elo>", String.valueOf(playerElo.getElo()))
.replace("<value>", String.valueOf(playerElo.getElo()))
.replace("<player>", playerElo.getPlayerName()));
}
return new ItemBuilder(queue.getKit().getDisplayIcon())
Expand All @@ -114,4 +159,37 @@ public ItemStack getButtonItem(Player player) {
}

}

@AllArgsConstructor
private class LeaderboardKillsButton extends Button {

private Queue queue;

@Override
public ItemStack getButtonItem(Player player) {
List<String> lore = new ArrayList<>();
lore.add("");
lore.add(Practice.getInstance().getMenusConfig().getString("LEADERBOARD.PLAYER-KILL")
.replace("<player_kill>", String.valueOf(Profile.getByUuid(player.getUniqueId()).getKitData().get(queue.getKit()).getWon())));

for (int i = 1; i <= 10; i++) {
PlayerElo playerElo = Leaderboard.getEloLeaderboards().get(queue.getKit().getName()).getTopKillPlayers().get(i - 1);

lore.add(Practice.getInstance().getMenusConfig().getString("LEADERBOARD.POSITION")
.replace("<position>", String.valueOf(i))
.replace("<value>", String.valueOf(playerElo.getKills()))
.replace("<player>", playerElo.getPlayerName()));
}
return new ItemBuilder(queue.getKit().getDisplayIcon())
.name(Practice.getInstance().getMenusConfig().getString("LEADERBOARD.KIT-NAME")
.replace("<kit>", queue.getKit().getName()))
.lore(lore)
.clearEnchantments()
.clearFlags()
.build();

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@Setter
public class QueueLeaderboard {
private String queue;
private List<PlayerElo> topPlayers;
private List<PlayerElo> topEloPlayers;
private List<PlayerElo> topKillPlayers;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.lrxh.practice.leaderboards;
package me.lrxh.practice.leaderboards.command;

import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import me.lrxh.practice.leaderboards.Leaderboard;
import me.lrxh.practice.leaderboards.LeaderboardsMenu;
import me.lrxh.practice.util.CC;
import org.bukkit.entity.Player;

Expand All @@ -11,7 +13,7 @@ public class LeaderboardsCommand extends BaseCommand {

@Default
public void open(Player player) {
new LeaderboardsMenu().openMenu(player);
new LeaderboardsMenu(false).openMenu(player);
}

@Subcommand("refresh")
Expand All @@ -21,4 +23,14 @@ public void refresh(Player player) {
Leaderboard.setEloLeaderboards(Leaderboard.init());
player.sendMessage(CC.translate("&aRefreshed Leaderboards!"));
}

@Subcommand("kills")
public void kills(Player player) {
new LeaderboardsMenu(true).openMenu(player);
}

@Subcommand("elo")
public void elo(Player player) {
new LeaderboardsMenu(false).openMenu(player);
}
}
2 changes: 1 addition & 1 deletion src/main/java/me/lrxh/practice/match/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void end() {

for (Player participant : getPlayers()) {
if (participant != null && participant.isOnline()) {
if (Practice.getInstance().isReplay() && !kit.getGameRules().isBuild()){
if (Practice.getInstance().isReplay() && !kit.getGameRules().isBuild()) {
for (String msg : Locale.MATCH_SHOW_REPLAY.formatLines(participant)) {
if (msg.contains("%CLICKABLE%")) {
ChatComponentBuilder builder = new ChatComponentBuilder(Locale.MATCH_SHOW_REPLAY_RECEIVED_CLICKABLE.format(participant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package me.lrxh.practice.profile.visibility;

import me.lrxh.practice.Practice;
import me.lrxh.practice.match.participant.MatchGamePlayer;
import me.lrxh.practice.profile.Profile;
import me.lrxh.practice.profile.ProfileState;
import me.lrxh.practice.util.CC;
import me.lrxh.practice.util.PlayerUtil;
import org.apache.commons.lang.StringEscapeUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/me/lrxh/practice/queue/menu/QueueSelectKitMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,25 @@ private String replaceLeaderboardPlaceholders(String line, Queue queue) {
Matcher matcher = Pattern.compile("<lb_(\\d+)_(\\w+)_>").matcher(line);
while (matcher.find()) {
int position = Integer.parseInt(matcher.group(1));
String placeholder = matcher.group(2);
PlayerElo playerElo = Leaderboard.getEloLeaderboards().get(queue.getKit().getName()).getTopPlayers().get(position - 1);
switch (placeholder) {
String valueType = matcher.group(2);
boolean showName = line.contains("_name_");

if (position > 10 || position < 1) return line;

PlayerElo playerElo = Leaderboard.getEloLeaderboards().get(queue.getKit().getName()).getTopEloPlayers().get(position - 1);
PlayerElo playerKills = Leaderboard.getEloLeaderboards().get(queue.getKit().getName()).getTopKillPlayers().get(position - 1);

switch (valueType) {
case "name":
line = line.replace("<lb_" + position + "_name_>", playerElo.getPlayerName());
line = showName ?
line.replace("<lb_" + position + "_name_>", playerElo.getPlayerName()) :
line.replace("<lb_" + position + "_name_>", playerKills.getPlayerName());
break;
case "elo":
line = line.replace("<lb_" + position + "_elo_>", String.valueOf(playerElo.getElo()));
break;
case "kills":
line = line.replace("<lb_" + position + "_kills_>", String.valueOf(playerElo.getKills()));
line = line.replace("<lb_" + position + "_kills_>", String.valueOf(playerKills.getKills()));
break;
default:
break;
Expand All @@ -144,6 +152,7 @@ private String replaceLeaderboardPlaceholders(String line, Queue queue) {
return line;
}


@Override
public void clicked(Player player, ClickType clickType) {
Profile profile = Profile.getByUuid(player.getUniqueId());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/lrxh/practice/util/menu/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void openMenu(final Player player) {
previousMenu.setClosedByMenu(true);
inventory = player.getOpenInventory().getTopInventory();
update = true;
player.updateInventory();
}
}
}
Expand Down Expand Up @@ -222,4 +223,4 @@ public void onOpen(Player player) {
public void onClose(Player player) {
}

}
}
4 changes: 3 additions & 1 deletion src/main/resources/menus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ LEADERBOARD:
FILTER: FILL # THERE IS FILL AND BRODER
KIT-NAME: "&a<kit> &7| &fTop 10"
PLAYER-ELO: "&bYour Elo: &f<player_elo>"
POSITION: "&b<position>. &f<player> &7- &b <player_elo>"
PLAYER-KILL: "&bYour Kills: &f<player_kill>"
TOGGLE_VIEW: "&bToggle view"
POSITION: "&b<position>. &f<player> &7- &b <value>"
GLOBAL-STATS:
GLOBAL-STATS-NAME: "&aGlobal Stats"
LORE:
Expand Down

0 comments on commit eaf7ea2

Please sign in to comment.