Move server ping cache storage to the AdaptedServer instance

This commit is contained in:
ajgeiss0702
2022-08-15 13:41:32 -07:00
parent 6a99349718
commit 478f2cf78d
23 changed files with 549 additions and 409 deletions
@@ -73,7 +73,7 @@ public interface PlatformMethods {
*/
AdaptedServer getServer(String name);
List<AdaptedServer> getServers();
List<? extends AdaptedServer> getServers();
String getProtocolName(int protocol);
}
@@ -4,6 +4,7 @@ import us.ajg0702.queue.api.AjQueueAPI;
import us.ajg0702.queue.api.players.AdaptedPlayer;
import us.ajg0702.queue.api.players.QueuePlayer;
import us.ajg0702.queue.api.queues.QueueServer;
import us.ajg0702.queue.api.server.AdaptedServer;
import us.ajg0702.utils.common.Config;
@SuppressWarnings({"SameReturnValue", "unused"})
@@ -17,10 +18,12 @@ public interface Logic {
/**
* The priority logic that is executed if the plugin is premium.
* @param server The server/group name that is being queued for
* @param player The player that is being queued
*
* @param queueServer The server/group name that is being queued for
* @param player The player that is being queued
* @param server The server/group name that is being queued for
*/
QueuePlayer priorityLogic(QueueServer server, AdaptedPlayer player);
QueuePlayer priorityLogic(QueueServer queueServer, AdaptedPlayer player, AdaptedServer server);
/**
* The logic for checking if a player has been disconnected for too long
@@ -35,7 +38,7 @@ public interface Logic {
*/
PermissionGetter getPermissionGetter();
static int getUnJoinablePriorities(QueueServer server, AdaptedPlayer player) {
static int getUnJoinablePriorities(QueueServer queueServer, AdaptedServer server, AdaptedPlayer player) {
Config config = AjQueueAPI.getInstance().getConfig();
int highest = 0;
@@ -50,7 +53,7 @@ public interface Logic {
}
if(bypassPausedPriotity > 0) {
if(server.isPaused() && (player.hasPermission("ajqueue.bypasspaused"))) {
if(queueServer.isPaused() && (player.hasPermission("ajqueue.bypasspaused"))) {
highest = Math.max(highest, bypassPausedPriotity);
}
}
@@ -50,17 +50,6 @@ public interface QueueServer {
*/
String getStatus();
/**
* Sends a server ping and uses the response to update online status, player count status, and whitelist status
*/
void updatePing();
/**
* Gets the time the server has been offline, in miliseconds
* @return The number of miliseconds the server has been offline for
*/
int getOfflineTime();
/**
* Gets how long since the last person was sent
* @return The number of miliseconds since the last person was sent
@@ -73,30 +62,6 @@ public interface QueueServer {
*/
void setLastSentTime(long lastSentTime);
/**
* Gets if the server is whitelisted or not
* @return True if whitelisted, false if not
*/
boolean isWhitelisted();
/**
* Sets if the server is whitelisted or not
*/
void setWhitelisted(boolean whitelisted);
/**
* Gets the list of players who are whitelisted
* @return The list of player UUIDs who are whitelisted
*/
ImmutableList<UUID> getWhitelistedPlayers();
/**
* Sets the list of UUIDs that are whitelisted
*/
void setWhitelistedPlayers(List<UUID> whitelistedPlayers);
/**
* Checks if the server is joinable by a player
* @param p The player to see if they can join
@@ -116,24 +81,6 @@ public interface QueueServer {
*/
boolean isPaused();
/**
* Checks if the server is online
* @return True if the server is online, false if not
*/
boolean isOnline();
/**
* Checks if the server went online within the time set in the config
* @return If the sevrer just came online
*/
boolean justWentOnline();
/**
* Checks if the server is full
* @return If the server is full
*/
boolean isFull();
/**
* Removes a player from the queue
* @param player The player to remove
@@ -199,6 +146,16 @@ public interface QueueServer {
*/
ImmutableList<String> getServerNames();
/**
* Returns true if at least one server in the group is online
* @return true if the server is online
*/
default boolean isOnline() {
for (AdaptedServer server : getServers()) {
if(server.isOnline()) return true;
}
return false;
}
/**
* Returns if this server is a group
@@ -232,12 +189,6 @@ public interface QueueServer {
*/
AdaptedServer getIdealServer(AdaptedPlayer player);
/**
* Gets the last server pings
* @return The last server pings for this server/group
*/
HashMap<AdaptedServer, AdaptedServerPing> getLastPings();
/**
* Gets the protocol versions this queue supports.
* A blank list means all protocols are supported.
@@ -258,26 +209,6 @@ public interface QueueServer {
*/
Balancer getBalancer();
/**
* Checks if the player can join this server even if its full
* @param player The player
* @return If the player can join this server if its full
*/
boolean canJoinFull(AdaptedPlayer player);
/**
* Adds one to the player count for a server (temporarily until the next server ping)
*/
void addPlayer(AdaptedServer server);
/**
* Sets if this server is online.
* Note that this is overrided by the pinger, so if you set
* this, it will most likely be temporary
* @param online whether the server is online or not
*/
void setOnline(boolean online);
/**
* elliot is bad
@@ -2,8 +2,9 @@ package us.ajg0702.queue.api.server;
import us.ajg0702.queue.api.players.AdaptedPlayer;
import us.ajg0702.queue.api.util.Handle;
import us.ajg0702.queue.api.util.QueueLogger;
import java.util.List;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@SuppressWarnings("unused")
@@ -25,7 +26,13 @@ public interface AdaptedServer extends Handle {
* Pings the server and gets info back
* @return A CompletableFuture with the ServerPing
*/
CompletableFuture<AdaptedServerPing> ping();
default CompletableFuture<AdaptedServerPing> ping() {
return ping(false, null);
}
CompletableFuture<AdaptedServerPing> ping(boolean debug, QueueLogger logger);
Optional<AdaptedServerPing> getLastPing();
/**
* If the player can access the server
@@ -38,4 +45,102 @@ public interface AdaptedServer extends Handle {
boolean canAccess(AdaptedPlayer player);
List<AdaptedPlayer> getPlayers();
/**
* Gets the number of seconds this server has been offline
* @return The number of seconds this server has been offline
*/
int getOfflineTime();
boolean canJoinFull(AdaptedPlayer player);
boolean justWentOnline();
default boolean isJoinable(AdaptedPlayer player) {
if(player != null) {
if (isWhitelisted() && !getWhitelistedPlayers().contains(player.getUniqueId())) {
return false;
}
if (isFull() && !canJoinFull(player)) {
return false;
}
}
return isOnline() &&
canAccess(player);
}
default boolean isFull() {
if(!getLastPing().isPresent()) return false;
return getLastPing().get().getPlayerCount() >= getLastPing().get().getMaxPlayers();
}
/**
* Gets if the last ping was successfull
* (which almost always means the server is online)
* @return If the server is determined to be online or not
*/
default boolean isOnline() {
return getLastPing().isPresent();
}
/**
* Gets the number of players currently online
* @return The number of players online
*/
default int getPlayerCount() {
if(!getLastPing().isPresent()) return 0;
AdaptedServerPing ping = getLastPing().get();
return ping.getPlayerCount();
}
/**
* Gets the maximum number of players that can join this server.
* @return The maximum number of players that can join this server
*/
default int getMaxPlayers() {
if(!getLastPing().isPresent()) return 0;
AdaptedServerPing ping = getLastPing().get();
return ping.getMaxPlayers();
}
/**
* Temporarly adds one player to the player count
*/
default void addPlayer() {
if(!getLastPing().isPresent()) return;
getLastPing().get().addPlayer();
}
/**
* Checks if the spigot-side reports that the server is whitelisted
* @return True if the server is whitelisted
*/
default boolean isWhitelisted() {
if(!getLastPing().isPresent()) return false;
return getLastPing().get().getPlainDescription().contains("ajQueue;whitelisted=");
}
/**
* (if the server is whitelisted) returns the list of players that are whitelisted
* @return The list of players that are whitelisted
*/
default List<UUID> getWhitelistedPlayers() {
if(!getLastPing().isPresent()) return Collections.emptyList();
if(!isWhitelisted()) return Collections.emptyList();
List<UUID> uuids = new ArrayList<>();
for(String uuid : getLastPing().get().getPlainDescription().substring(20).split(",")) {
if(uuid.isEmpty()) continue;
UUID parsedUUID;
try {
parsedUUID = UUID.fromString(uuid);
} catch(IllegalArgumentException ignored) {
continue;
}
uuids.add(parsedUUID);
}
return uuids;
}
}
@@ -12,7 +12,7 @@ public interface AdaptedServerPing extends Handle {
Component getDescriptionComponent();
/**
* Gets the description stripped of any color or styling
* Gets the description (aka MOTD) stripped of any color or styling
* @return The description, but no colors
*/
String getPlainDescription();
@@ -33,4 +33,10 @@ public interface AdaptedServerPing extends Handle {
* Temporarly adds one player to the player count
*/
void addPlayer();
/**
* Returns an epoch timestamp of when this ping was <bold>sent</bold>.
* @return A long of an epoch timestamp
*/
long getFetchedTime();
}
@@ -8,4 +8,10 @@ public interface QueueLogger extends UtilsLogger {
void info(String message);
void error(String message);
void severe(String message);
void warn(String message, Throwable t);
void warning(String message, Throwable t);
void info(String message, Throwable t);
void error(String message, Throwable t);
void severe(String message, Throwable t);
}