Skip to content

Commit

Permalink
Move server ping cache storage to the AdaptedServer instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Aug 15, 2022
1 parent 6a99349 commit 478f2cf
Show file tree
Hide file tree
Showing 23 changed files with 549 additions and 409 deletions.
Expand Up @@ -73,7 +73,7 @@ public interface PlatformMethods {
*/
AdaptedServer getServer(String name);

List<AdaptedServer> getServers();
List<? extends AdaptedServer> getServers();

String getProtocolName(int protocol);
}
13 changes: 8 additions & 5 deletions api/src/main/java/us/ajg0702/queue/api/premium/Logic.java
Expand Up @@ -4,6 +4,7 @@
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"})
Expand All @@ -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
Expand All @@ -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;

Expand All @@ -50,7 +53,7 @@ static int getUnJoinablePriorities(QueueServer server, AdaptedPlayer player) {
}

if(bypassPausedPriotity > 0) {
if(server.isPaused() && (player.hasPermission("ajqueue.bypasspaused"))) {
if(queueServer.isPaused() && (player.hasPermission("ajqueue.bypasspaused"))) {
highest = Math.max(highest, bypassPausedPriotity);
}
}
Expand Down
89 changes: 10 additions & 79 deletions api/src/main/java/us/ajg0702/queue/api/queues/QueueServer.java
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
109 changes: 107 additions & 2 deletions api/src/main/java/us/ajg0702/queue/api/server/AdaptedServer.java
Expand Up @@ -2,8 +2,9 @@

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")
Expand All @@ -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
Expand All @@ -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;
}

}
Expand Up @@ -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();
Expand All @@ -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();
}
6 changes: 6 additions & 0 deletions api/src/main/java/us/ajg0702/queue/api/util/QueueLogger.java
Expand Up @@ -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);
}
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
import us.ajg0702.queue.api.commands.ICommandSender;
import us.ajg0702.queue.api.queues.QueueServer;
import us.ajg0702.queue.api.server.AdaptedServer;
import us.ajg0702.queue.commands.SubCommand;
import us.ajg0702.queue.common.QueueMain;
import us.ajg0702.utils.common.Messages;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void execute(ICommandSender sender, String[] args) {
sender.sendMessage(main.getMessages().toComponent("<red>Not enough args!"));
return;
}
QueueServer server = main.getQueueManager().findServer(args[0]);
AdaptedServer server = main.getPlatformMethods().getServer(args[0]);
if(server == null) {
sender.sendMessage(main.getMessages().toComponent("<red>Server not found"));
return;
Expand Down

0 comments on commit 478f2cf

Please sign in to comment.