Add ability for auto-joining of gametypes to bypass manual server selection by player.

This commit is contained in:
Ty Sayers 2015-08-02 13:24:59 -04:00
parent be910cced9
commit 612d967aa0
7 changed files with 114 additions and 3 deletions

View File

@ -10,4 +10,9 @@ public class ServerInfo
public String ServerType; public String ServerType;
public String Game; public String Game;
public boolean HostedByStaff; public boolean HostedByStaff;
public int getAvailableSlots()
{
return MaxPlayers - CurrentPlayers;
}
} }

View File

@ -10,6 +10,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.Set; import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -56,6 +57,7 @@ import mineplex.hub.queue.QueueManager;
import mineplex.hub.queue.ui.QueueShop; import mineplex.hub.queue.ui.QueueShop;
import mineplex.hub.server.ui.LobbyShop; import mineplex.hub.server.ui.LobbyShop;
import mineplex.hub.server.ui.QuickShop; import mineplex.hub.server.ui.QuickShop;
import mineplex.hub.server.ui.ServerCountSorter;
import mineplex.hub.server.ui.ServerNpcShop; import mineplex.hub.server.ui.ServerNpcShop;
import mineplex.serverdata.data.MinecraftServer; import mineplex.serverdata.data.MinecraftServer;
import mineplex.serverdata.data.ServerGroup; import mineplex.serverdata.data.ServerGroup;
@ -64,6 +66,10 @@ public class ServerManager extends MiniPlugin
{ {
private static final Long FREE_PORTAL_TIMER = 20000L; private static final Long FREE_PORTAL_TIMER = 20000L;
private static final Long BETA_PORTAL_TIMER = 120000L; private static final Long BETA_PORTAL_TIMER = 120000L;
private static final Random random = new Random();
public final int TOP_SERVERS = 3; // The number of top contending servers for auto-joining games
public final int MIN_SLOTS_REQUIRED = 12; // The number of slots the max server must have for auto-join
private CoreClientManager _clientManager; private CoreClientManager _clientManager;
private DonationManager _donationManager; private DonationManager _donationManager;
@ -521,6 +527,68 @@ public class ServerManager extends MiniPlugin
_portal.sendPlayerToServer(player, serverInfo.Name); _portal.sendPlayerToServer(player, serverInfo.Name);
} }
} }
/**
* Select a {@code serverType} for a {@code player} that wishes to automatically join the best server
* available for that server type.
* @param player - the player hoping to select a server
* @param serverType - the name of the type of server to be joined
*/
public void selectServer(Player player, String serverType)
{
ServerInfo bestServer = getBestServer(player, serverType);
if (bestServer != null)
{
SelectServer(player, bestServer);
}
}
/**
* @param serverType - the type of server that should be fetched
* @return the best server that a new player should join according to a {@code serverType} constraint.
*/
public ServerInfo getBestServer(Player player, String serverType)
{
List<ServerInfo> servers = new ArrayList<ServerInfo>(GetServerList(serverType));
Collections.sort(servers, new ServerCountSorter());
servers = fetchOpenServers(player, servers, servers.size()); // Removes all full servers from list
int count = Math.min(servers.size(), TOP_SERVERS);
if (count > 0)
{
ServerInfo largestServer = servers.get(0);
if (largestServer.getAvailableSlots() >= MIN_SLOTS_REQUIRED)
{
return largestServer;
}
else
{
return servers.get(random.nextInt(count));
}
}
return null;
}
public List<ServerInfo> fetchOpenServers(Player player, List<ServerInfo> servers, int count)
{
List<ServerInfo> results = new ArrayList<ServerInfo>();
int requiredSlots = (servers.size() > 0) ? GetRequiredSlots(player, servers.get(0).ServerType) : 0;
for (ServerInfo server : servers)
{
if (results.size() >= count) break;
if (server.getAvailableSlots() > requiredSlots)
{
results.add(server);
}
}
return results;
}
public void ListServerNpcs(Player caller) public void ListServerNpcs(Player caller)
{ {

View File

@ -0,0 +1,13 @@
package mineplex.hub.server.ui;
import java.util.Comparator;
import mineplex.hub.server.ServerInfo;
public class ServerCountSorter implements Comparator<ServerInfo>
{
public int compare(ServerInfo a, ServerInfo b)
{
return a.CurrentPlayers - b.CurrentPlayers;
}
}

View File

@ -72,7 +72,7 @@ public class ServerGameMenu extends ShopPageBase<ServerManager, QuickShop>
ChatColor.RESET + "Stay away from the borders!", ChatColor.RESET + "Stay away from the borders!",
ChatColor.RESET + "", ChatColor.RESET + "",
ChatColor.RESET + "Join " + ChatColor.GREEN + getPlugin().getGroupTagPlayerCount("HG") + ChatColor.RESET + " other players!", ChatColor.RESET + "Join " + ChatColor.GREEN + getPlugin().getGroupTagPlayerCount("HG") + ChatColor.RESET + " other players!",
})); }));
setItem(4, ItemStackFactory.Instance.CreateStack(Material.FEATHER.getId(), (byte) 0, 1, ChatColor.RESET + C.Bold + ChatColor.YELLOW + "Skywars " + C.cGray + "Solo Survival", new String[] setItem(4, ItemStackFactory.Instance.CreateStack(Material.FEATHER.getId(), (byte) 0, 1, ChatColor.RESET + C.Bold + ChatColor.YELLOW + "Skywars " + C.cGray + "Solo Survival", new String[]
{ {

View File

@ -401,7 +401,7 @@ public class ServerNpcPage extends ShopPageBase<ServerManager, ServerNpcShop> im
{ {
int slots = getPlugin().GetRequiredSlots(player, serverInfo.ServerType); int slots = getPlugin().GetRequiredSlots(player, serverInfo.ServerType);
if (serverInfo.MaxPlayers - serverInfo.CurrentPlayers < slots && !(getDonationManager().Get(getPlayer().getName()).OwnsUnknownPackage(serverInfo.ServerType + " ULTRA") || getClient().GetRank().Has(Rank.ULTRA))) if (serverInfo.getAvailableSlots() < slots && !(getDonationManager().Get(getPlayer().getName()).OwnsUnknownPackage(serverInfo.ServerType + " ULTRA") || getClient().GetRank().Has(Rank.ULTRA)))
{ {
playDenySound(player); playDenySound(player);
return; return;

View File

@ -65,4 +65,29 @@ public class ServerNpcShop extends ShopBase<ServerManager>
{ {
getPlugin().getHubManager().GetVisibility().removeHiddenPlayer(player); getPlugin().getHubManager().GetVisibility().removeHiddenPlayer(player);
} }
@Override
public boolean attemptShopOpen(Player player)
{
return super.attemptShopOpen(player);
/*if (!_openedShop.contains(player.getName()))
{
if (!canOpenShop(player))
return false;
_openedShop.add(player.getName());
openShopForPlayer(player);
if (!_playerPageMap.containsKey(player.getName()))
{
_playerPageMap.put(player.getName(), buildPagesFor(player));
}
openPageForPlayer(player, getOpeningPageForPlayer(player));
return true;
}
return false;*/
}
} }

View File

@ -28,7 +28,7 @@ public class ServerManager
// The cached repository instances // The cached repository instances
private static Map<Region, ServerRepository> repositories = new HashMap<Region, ServerRepository>(); private static Map<Region, ServerRepository> repositories = new HashMap<Region, ServerRepository>();
/** /**
* @param host - the host url used to connect to the database * @param host - the host url used to connect to the database
* @param port - the port to connect to the repository * @param port - the port to connect to the repository