Added leaderboards system, and added some changes to Main.java & ProfileKitData.java to support leaderboards

This commit is contained in:
Matheus 2024-03-12 06:42:31 -04:00
parent eefe4a7584
commit ab37109100
6 changed files with 215 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import dev.lugami.otaku.commands.kit.*;
import dev.lugami.otaku.essentials.Essentials;
import dev.lugami.otaku.hotbar.Hotbar;
import dev.lugami.otaku.kit.Kit;
import dev.lugami.otaku.leaderboards.Leaderboards;
import dev.lugami.otaku.listener.FFAListener;
import dev.lugami.otaku.listener.GeneralListeners;
import dev.lugami.otaku.profile.Profile;
@ -68,8 +69,9 @@ public class Main extends JavaPlugin {
Kit.init();
Hotbar.init();
Profile.init();
Leaderboards.setup();
Arrays.asList(new FFAListener(), new GeneralListeners(), new ProfileListener(), new MenuListener()).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
Arrays.asList(new OtakuCommand(), new FFACommand(), new FFASetSpawnCommand(), new KitSetIconCommand(), new KitCreateCommand(), new KitDeleteCommand(), new KitGetLoadoutCommand(), new KitSetLoadoutCommand(), new KitsCommand(), new KitSetLocationCommand(), new MainMenuCommand(), new FFAJoinCommand(), new FFAQuitCommand(), new KitCommand(), new ReKitCommand()).forEach(command -> honcho.registerCommand(command));
Arrays.asList(new OtakuCommand(), new FFACommand(), new FFASetSpawnCommand(), new KitSetIconCommand(), new KitCreateCommand(), new KitDeleteCommand(), new KitGetLoadoutCommand(), new KitSetLoadoutCommand(), new KitsCommand(), new KitSetLocationCommand(), new MainMenuCommand(), new FFAJoinCommand(), new FFAQuitCommand(), new KitCommand(), new ReKitCommand(), new LeaderboardsCommand()).forEach(command -> honcho.registerCommand(command));
getServer().getWorlds().forEach(essentials::clearEntities);
}

View File

@ -0,0 +1,14 @@
package dev.lugami.otaku.commands;
import com.qrakn.honcho.command.CommandMeta;
import dev.lugami.otaku.leaderboards.menu.LeaderboardsMenu;
import org.bukkit.entity.Player;
@CommandMeta(label = "leaderboards")
public class LeaderboardsCommand {
public void exec(Player player) {
new LeaderboardsMenu().openMenu(player);
}
}

View File

@ -0,0 +1,102 @@
package dev.lugami.otaku.leaderboards;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.lugami.otaku.Main;
import dev.lugami.otaku.kit.Kit;
import dev.lugami.otaku.profile.Profile;
import dev.lugami.otaku.profile.data.ProfileKitData;
import dev.lugami.otaku.utils.SaveMethodType;
import dev.lugami.otaku.utils.TaskUtil;
import lombok.Getter;
import org.bson.Document;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
public class Leaderboards {
@Getter private static final Map<Kit, List<LeaderboardsEntry>> leaderboards = Maps.newHashMap();
public static void setup() {
updateLeaderboards();
TaskUtil.runTimerAsync(new BukkitRunnable() {
@Override
public void run() {
updateLeaderboards();
}
}, 0L, 600L);
}
private static List<Document> getSortedPlayers(Kit ladder) {
try {
Document sort = new Document();
sort.put("kitStatistics." + ladder.getName() + ".kills", -1);
return Main.getInstance().getMongoDatabase().getCollection("profiles").find().sort(sort).limit(10).into(new ArrayList<>());
} catch (Exception ex) {
return null;
}
}
private static Map<String, ProfileKitData> getSortedPlayers_ff(Kit ladder) {
Map<String, ProfileKitData> map = new HashMap<>();
ConfigurationSection main = Main.getInstance().getProfileConfig().getConfiguration().getConfigurationSection("stats");
for (String name : main.getKeys(false)) {
ConfigurationSection playerSection = main.getConfigurationSection(name);
if (playerSection != null && playerSection.contains(ladder.getName())) {
ConfigurationSection kitSection = playerSection.getConfigurationSection(ladder.getName());
int kills = kitSection.getInt("kills");
int killstreak = kitSection.getInt("killstreak");
int deaths = kitSection.getInt("deaths");
ProfileKitData data = new ProfileKitData(kills, killstreak, deaths);
map.put(name, data);
}
}
List<Map.Entry<String, ProfileKitData>> list = new ArrayList<>(map.entrySet());
Comparator<Map.Entry<String, ProfileKitData>> comparator = Comparator.comparing(entry -> entry.getValue().kills());
list.sort(comparator.reversed());
int maxSize = Math.min(10, list.size());
list = list.subList(0, maxSize);
Map<String, ProfileKitData> sortedMap = new LinkedHashMap<>();
for (Map.Entry<String, ProfileKitData> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
private static void updateLeaderboards() {
leaderboards.clear();
Kit.getKits().forEach(kit -> {
List<LeaderboardsEntry> entry = Lists.newArrayList();
if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO && Profile.getCollection() != null) {
List<Document> sortedPlayers = getSortedPlayers(kit);
if (sortedPlayers != null) {
sortedPlayers.forEach(document -> {
UUID uuid = UUID.fromString(document.getString("uuid"));
Document kitStatistics = (Document) document.get("kitStatistics");
Document kitDocument = (Document) kitStatistics.get(kit.getName());
Integer kills = (kitDocument != null) ? kitDocument.getInteger("kills") : null;
Integer killstreak = (kitDocument != null) ? kitDocument.getInteger("killstreak") : null;
Integer deaths = (kitDocument != null) ? kitDocument.getInteger("deaths") : null;
// Use a default value if elo is null
kills = (kills != null) ? kills : 0;
deaths = (deaths != null) ? deaths : 0;
killstreak = (killstreak != null) ? killstreak : 0;
entry.add(new LeaderboardsEntry(Bukkit.getOfflinePlayer(uuid).getName(), kills, killstreak, deaths));
});
leaderboards.put(kit, entry);
}
} else {
Map<String, ProfileKitData> sorted = getSortedPlayers_ff(kit);
sorted.forEach((sort, data) -> entry.add(new LeaderboardsEntry(sort, data.kills(), data.killstreak(), data.deaths())));
leaderboards.put(kit, entry);
}
});
}
}

View File

@ -0,0 +1,15 @@
package dev.lugami.otaku.leaderboards;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;
@Data @Accessors(fluent = true) @AllArgsConstructor
public class LeaderboardsEntry {
private String name;
private int kills;
private int killstreak;
private int deaths;
}

View File

@ -0,0 +1,78 @@
package dev.lugami.otaku.leaderboards.menu;
import dev.lugami.otaku.Constants;
import dev.lugami.otaku.kit.Kit;
import dev.lugami.otaku.leaderboards.Leaderboards;
import dev.lugami.otaku.leaderboards.LeaderboardsEntry;
import dev.lugami.otaku.utils.CC;
import dev.lugami.otaku.utils.ItemBuilder;
import dev.lugami.otaku.utils.menu.Button;
import dev.lugami.otaku.utils.menu.Menu;
import lombok.AllArgsConstructor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LeaderboardsMenu extends Menu {
@Override
public String getTitle(Player player) {
return "&7Leaderboards";
}
@Override
public int getSize(){
int i = 3;
if (Kit.getKits().size() - 1 > 7) {
i = 4;
}
if (Kit.getKits().size() - 1 > 14) {
i = 5;
}
return 9*i;
}
@Override
public Map<Integer, Button> getButtons(Player player) {
Map<Integer, Button> buttons = new HashMap<>();
for (int i = 0; i < getSize(); i++) {
buttons.put(i, Constants.getPlaceholder());
}
final int[] i = {10};
Kit.getKits().forEach(kit -> {
if (!kit.isEnabled()) return;
while (i[0] == 17 || i[0] == 18 || i[0] == 26 || i[0] == 27 || i[0] == 36 || i[0] == 37) i[0]++;
buttons.put(i[0]++, new KitStatsButton(kit));
});
return buttons;
}
@AllArgsConstructor
private class KitStatsButton extends Button {
private final Kit kit;
@Override
public ItemStack getButtonItem(Player player) {
List<String> lore = new ArrayList<>();
lore.add(CC.MENU_BAR);
int pos = 1;
for (LeaderboardsEntry entry : Leaderboards.getLeaderboards().get(kit)) {
lore.add("&b" +pos + ". " + entry.name() + "&7 - " + entry.kills() + "/" + entry.killstreak() + "/" + entry.deaths());
pos++;
}
lore.add(CC.MENU_BAR);
return new ItemBuilder(kit.getDisplayIcon())
.name("&b" + kit.getName() + " &7⏐ &fStats")
.lore(lore)
.build();
}
}
}

View File

@ -1,9 +1,12 @@
package dev.lugami.otaku.profile.data;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data @Accessors(fluent = true)
@AllArgsConstructor @NoArgsConstructor
public class ProfileKitData {
private int kills;