package com.lifeknight.relaymcbungeemain.queue; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.lifeknight.relaymcbungeemain.player.Party; import com.lifeknight.relaymcbungeemain.player.SmartPlayer; import com.lifeknight.relaymcbungeemain.utilities.Utilities; import com.lifeknight.relayutils.basic.Miscellaneous; import com.lifeknight.relayutils.game.GameDetails; import com.lifeknight.relayutils.game.GameType; import com.lifeknight.relayutils.network.Recipient; import com.lifeknight.relayutils.utilities.ComponentBuilder; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import java.awt.*; import java.util.List; import java.util.*; public class ServerDetails { private static final Map<Recipient, ServerDetails> RECIPIENT_SERVER_DETAILS_MAP = new HashMap<>(); public List<GameDetails> gameDetails; public ServerDetails(Recipient sender, List<GameDetails> gameDetails) { this.gameDetails = gameDetails; ServerDetails original = RECIPIENT_SERVER_DETAILS_MAP.get(sender); GameServer gameServer; if (original != null) { original.setValue(this); gameServer = GameServer.getGameServer(sender); if (gameServer != null) { gameServer.online = true; gameServer.getServer().setAvailableForPlayers(true); gameServer.onDetailsUpdate(); } } else { gameServer = new GameServer(Utilities.getServerInfo(sender), this); RECIPIENT_SERVER_DETAILS_MAP.put(sender, this); } Queue.onGameDetailsUpdate(); } public String format() { StringBuilder stringBuilder = new StringBuilder(); for (GameDetails gameDetail : this.gameDetails) { stringBuilder.append(ChatColor.AQUA).append("Players & Spectators: ").append(ChatColor.GOLD).append(gameDetail.getPlayerCount()).append(" : ").append(gameDetail.getSpectatorCount()).append("\n"); stringBuilder.append(ChatColor.GREEN).append("Game: ").append(ChatColor.YELLOW).append(gameDetail.getGameType() == null ? "None" : gameDetail.getGameType().getFullPrettyName()).append("\n"); stringBuilder.append(gameDetail.isAvailable() ? ChatColor.GREEN + "Available" : ChatColor.RED + "Unavailable").append("\n"); } return stringBuilder.toString(); } public void setValue(ServerDetails serverDetails) { this.gameDetails = serverDetails.gameDetails; Queue.onGameDetailsUpdate(); } public int getTotalPlayers() { int totalPlayers = 0; for (GameDetails gameDetail : this.gameDetails) { totalPlayers += gameDetail.getPlayerCount(); totalPlayers += gameDetail.getSpectatorCount(); } return totalPlayers; } public int getPlayerCount() { int playerCount = 0; for (GameDetails gameDetail : this.gameDetails) { playerCount += gameDetail.getPlayerCount(); } return playerCount; } public int getSpectatorCount() { int spectatorCount = 0; for (GameDetails gameDetail : this.gameDetails) { spectatorCount += gameDetail.getSpectatorCount(); } return spectatorCount; } @Override public String toString() { JsonObject asJson = new JsonObject(); JsonArray gameDetails = new JsonArray(); for (GameDetails gameDetail : this.gameDetails) { gameDetails.add(gameDetail.toJson()); } asJson.add("gameDetails", gameDetails); return asJson.toString(); } public static void readJson(Recipient sender, String json) { JsonObject asJson = new JsonParser().parse(json).getAsJsonObject(); JsonArray gameDetailArray = asJson.get("gameDetails").getAsJsonArray(); List<GameDetails> gameDetails = new ArrayList<>(); for (JsonElement jsonElement : gameDetailArray) { gameDetails.add(GameDetails.fromJson(jsonElement.getAsJsonObject())); } new ServerDetails(sender, gameDetails); } public int getUsedGames() { int availableGames = 0; for (GameDetails gameDetail : this.gameDetails) { if (gameDetail.isAvailable()) availableGames++; } return availableGames; } public boolean isAvailable(GameType gameType, List<SmartPlayer> smartPlayers, List<UUID> markedUUIDs) { return this.getAvailable(gameType, smartPlayers, markedUUIDs) != null; } public GameDetails getAvailable(GameType gameType, List<SmartPlayer> smartPlayers, List<UUID> markedUUIDs) { List<GameDetails> details = new ArrayList<>(this.gameDetails); details.removeIf(gameDetails -> markedUUIDs.contains(gameDetails.getUUID())); List<GameDetails> viableDetails = new ArrayList<>(); int totalPlayerCount = 0; if (!Party.allPlayersInParty(smartPlayers)) { for (GameDetails gameDetail : details) { if (gameDetail.isAvailable() && gameDetail.getGameType() == gameType/* && Utilities.suitableQueueSize(gameType, smartPlayers.size() + gameDetail.getPlayerCount())*/) { viableDetails.add(gameDetail); totalPlayerCount += gameDetail.getPlayerCount(); } } } for (GameDetails gameDetail : details) { if (gameDetail.isAvailable() && gameDetail.getPlayerCount() == 0) { Miscellaneous.addIfAbsent(viableDetails, gameDetail); } } details.sort(Comparator.comparingInt(GameDetails::getPlayerCount)); if (totalPlayerCount == 0) { return Miscellaneous.getRandomEntry(viableDetails); } return viableDetails.isEmpty() ? null : viableDetails.get(0); } public BaseComponent getComponent() { ComponentBuilder componentBuilder = new ComponentBuilder(); for (GameDetails gameDetail : this.gameDetails) { componentBuilder.color(ComponentBuilder.ORANGE).append(" - ").append(gameDetail.getUUID()).newLine(); componentBuilder.color(ComponentBuilder.AQUA).append(" - ").color(gameDetail.isAvailable() ? Color.GREEN : Color.RED).append(gameDetail.isAvailable() ? "Available" : "Unavailable").newLine(); componentBuilder.color(ComponentBuilder.GREEN).append(" - Players & Spectators: ").color(ComponentBuilder.AQUA_BLUE).append(gameDetail.getPlayerCount()).append(" | ").append(gameDetail.getSpectatorCount()).newLine(); componentBuilder.color(ComponentBuilder.PINK).append(" - Game: ").color(gameDetail.getGameType() == null ? Color.YELLOW : ComponentBuilder.RED_ORANGE).append(gameDetail.getGameType() == null ? "None" : gameDetail.getGameType().getFullPrettyName()); componentBuilder.newLine(); } return componentBuilder.getResult(); } }