Add Mineplexer support for beta lobbies

This commit is contained in:
cnr 2017-02-22 17:41:23 -06:00
parent eb6003a169
commit af8b5f6522
3 changed files with 133 additions and 160 deletions

View File

@ -2,33 +2,34 @@ package mineplex.bungee.lobbyBalancer;
import java.io.File; import java.io.File;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import mineplex.serverdata.Region;
import mineplex.serverdata.data.MinecraftServer;
import mineplex.serverdata.servers.ServerManager;
import mineplex.serverdata.servers.ServerRepository;
import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import com.google.common.collect.Lists; import mineplex.serverdata.Region;
import mineplex.serverdata.data.MinecraftServer;
import mineplex.serverdata.servers.ServerManager;
import mineplex.serverdata.servers.ServerRepository;
public class LobbyBalancer implements Listener, Runnable public class LobbyBalancer implements Listener, Runnable
{ {
private Plugin _plugin; private Plugin _plugin;
private ServerRepository _repository; private ServerRepository _repository;
private List<MinecraftServer> _sortedLobbies = Lists.newArrayList(); private final Map<LobbyType, List<MinecraftServer>> _sortedLobbyMap = new EnumMap<>(LobbyType.class);
private List<MinecraftServer> _sortedClans = Lists.newArrayList(); private final Map<LobbyType, Integer> _nextIndexMap = new EnumMap<>(LobbyType.class);
private static Object _serverLock = new Object(); private static final LobbySorter LOBBY_SORTER = new LobbySorter();
private static final Object _serverLock = new Object();
private int _lobbyIndex = 0;
private int _clansIndex = 0;
public LobbyBalancer(Plugin plugin) public LobbyBalancer(Plugin plugin)
{ {
@ -46,45 +47,42 @@ public class LobbyBalancer implements Listener, Runnable
@EventHandler @EventHandler
public void playerConnect(ServerConnectEvent event) public void playerConnect(ServerConnectEvent event)
{ {
if (event.getTarget().getName().equalsIgnoreCase("Lobby")) Arrays.stream(LobbyType.values())
.filter(type -> type.getConnectName().equalsIgnoreCase(event.getTarget().getName()))
.findFirst()
.ifPresent(lobbyType ->
{ {
synchronized (_serverLock) synchronized (_serverLock)
{ {
if (_lobbyIndex >= _sortedLobbies.size() || _sortedLobbies.get(_lobbyIndex).getPlayerCount() >= _sortedLobbies.get(_lobbyIndex).getMaxPlayerCount()) List<MinecraftServer> lobbies = _sortedLobbyMap.get(lobbyType);
_lobbyIndex = 0;
event.setTarget(_plugin.getProxy().getServerInfo(_sortedLobbies.get(_lobbyIndex).getName())); int nextIndex = _nextIndexMap.getOrDefault(lobbyType, 0);
_sortedLobbies.get(_lobbyIndex).incrementPlayerCount(1); if (nextIndex >= lobbies.size())
System.out.println("Sending " + event.getPlayer().getName() + " to " + _sortedLobbies.get(_lobbyIndex).getName() + "(" + _sortedLobbies.get(_lobbyIndex).getPublicAddress() + ")");
_lobbyIndex++;
}
}
if (event.getTarget().getName().equalsIgnoreCase("ClansHub"))
{ {
synchronized (_serverLock) nextIndex = 0;
{ }
if (_clansIndex >= _sortedClans.size() || _sortedClans.get(_clansIndex).getPlayerCount() >= _sortedClans.get(_clansIndex).getMaxPlayerCount())
_clansIndex = 0;
event.setTarget(_plugin.getProxy().getServerInfo(_sortedClans.get(_clansIndex).getName())); MinecraftServer server = lobbies.get(nextIndex);
_sortedClans.get(_clansIndex).incrementPlayerCount(1);
System.out.println("Sending " + event.getPlayer().getName() + " to " + _sortedClans.get(_clansIndex).getName() + "(" + _sortedClans.get(_clansIndex).getPublicAddress() + ")"); event.setTarget(_plugin.getProxy().getServerInfo(server.getName()));
_clansIndex++; server.incrementPlayerCount(1);
} System.out.println("Sending " + event.getPlayer().getName() + " to " + server.getName() + "(" + server.getPublicAddress() + ")");
_nextIndexMap.put(lobbyType, ++nextIndex);
} }
});
} }
public void run() public void run()
{ {
loadServers(); loadServers();
if (!_plugin.getProxy().getServers().containsKey("ClansHub")) for (LobbyType type : LobbyType.values())
{ {
_plugin.getProxy().getServers().put("ClansHub", _plugin.getProxy().constructServerInfo("ClansHub", new InetSocketAddress("lobby.mineplex.com", 25565), "LobbyBalancer", false)); if (!_plugin.getProxy().getServers().containsKey(type.getConnectName()))
{
_plugin.getProxy().getServers().put(type.getConnectName(), _plugin.getProxy().constructServerInfo(type.getConnectName(), new InetSocketAddress("lobby.mineplex.com", 25565), "LobbyBalancer", false));
} }
if (!_plugin.getProxy().getServers().containsKey("Lobby"))
{
_plugin.getProxy().getServers().put("Lobby", _plugin.getProxy().constructServerInfo("Lobby", new InetSocketAddress("lobby.mineplex.com", 25565), "LobbyBalancer", false));
} }
} }
@ -95,8 +93,11 @@ public class LobbyBalancer implements Listener, Runnable
synchronized (_serverLock) synchronized (_serverLock)
{ {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
_sortedLobbies.clear(); _sortedLobbyMap.clear();
_sortedClans.clear(); for (LobbyType type : LobbyType.values())
{
_sortedLobbyMap.put(type, new ArrayList<>());
}
for (MinecraftServer server : servers) for (MinecraftServer server : servers)
{ {
@ -106,31 +107,25 @@ public class LobbyBalancer implements Listener, Runnable
InetSocketAddress socketAddress = new InetSocketAddress(server.getPublicAddress(), server.getPort()); InetSocketAddress socketAddress = new InetSocketAddress(server.getPublicAddress(), server.getPort());
_plugin.getProxy().getServers().put(server.getName(), _plugin.getProxy().constructServerInfo(server.getName(), socketAddress, "LobbyBalancer", false)); _plugin.getProxy().getServers().put(server.getName(), _plugin.getProxy().constructServerInfo(server.getName(), socketAddress, "LobbyBalancer", false));
if (server.getName().toUpperCase().startsWith("LOBBY-")) if (server.getMotd() != null && server.getMotd().contains("Restarting"))
{ {
if (server.getMotd() == null || !server.getMotd().contains("Restarting")) continue;
{
_sortedLobbies.add(server);
}
}
if (server.getName().toUpperCase().startsWith("CLANSHUB-"))
{
if (server.getMotd() == null || !server.getMotd().contains("Restarting"))
{
_sortedClans.add(server);
}
}
} }
Collections.sort(_sortedLobbies, new LobbySorter()); Arrays.stream(LobbyType.values())
Collections.sort(_sortedClans, new LobbySorter()); .filter(type -> server.getName().toUpperCase().startsWith(type.getUppercasePrefix()))
.findFirst()
.ifPresent(type -> _sortedLobbyMap.get(type).add(server));
}
_sortedLobbyMap.values().forEach(lobbies -> Collections.sort(lobbies, LOBBY_SORTER));
long timeSpentInLock = System.currentTimeMillis() - startTime; long timeSpentInLock = System.currentTimeMillis() - startTime;
if (timeSpentInLock > 50) if (timeSpentInLock > 50)
System.out.println("[==] TIMING [==] Locked loading servers for " + timeSpentInLock + "ms"); System.out.println("[==] TIMING [==] Locked loading servers for " + timeSpentInLock + "ms");
_lobbyIndex = 0; _nextIndexMap.clear();
} }
} }
} }

View File

@ -0,0 +1,34 @@
package mineplex.bungee.lobbyBalancer;
public enum LobbyType
{
NORMAL("Lobby", "LOBBY-", "MainMotd"),
CLANS("ClansHub", "CLANSHUB-", "ClansMotd"),
BETA("BetaHub","BETAHUB-", "BetaMotd"),
;
private final String _connectName; // The name of the server the player is connecting to
private final String _uppercasePrefix; // The (toUpperCase()) prefix given to servers of this lobby type
private final String _redisMotdKey;
LobbyType(String connectName, String uppercasePrefix, String redisMotdKey)
{
_connectName = connectName;
_uppercasePrefix = uppercasePrefix;
_redisMotdKey = redisMotdKey;
}
public String getConnectName()
{
return _connectName;
}
public String getUppercasePrefix()
{
return _uppercasePrefix;
}
public String getRedisMotdKey()
{
return _redisMotdKey;
}
}

View File

@ -1,138 +1,82 @@
package mineplex.bungee.motd; package mineplex.bungee.motd;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import mineplex.serverdata.Region;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ConnectionData;
import mineplex.serverdata.servers.ConnectionData.ConnectionType;
import mineplex.serverdata.servers.ServerManager;
import net.md_5.bungee.api.event.ProxyPingEvent; import net.md_5.bungee.api.event.ProxyPingEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import mineplex.bungee.lobbyBalancer.LobbyType;
import mineplex.serverdata.Region;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ServerManager;
public class MotdManager implements Listener, Runnable public class MotdManager implements Listener, Runnable
{ {
private Plugin _plugin; private static final String DEFAULT_HEADLINE = " §b§l§m §8§l§m[ §r §9§lMineplex§r §f§lGames§r §8§l§m ]§b§l§m §r";
private DataRepository<GlobalMotd> _repository; private final DataRepository<GlobalMotd> _repository;
private DataRepository<GlobalMotd> _secondRepository; private final Random _random = new Random();
private Region _region; private final Map<LobbyType, GlobalMotd> motds = new EnumMap<>(LobbyType.class);
private Random _random = new Random();
private String _firstLine = " §b§l§m §8§l§m[ §r §9§lMineplex§r §f§lGames§r §8§l§m ]§b§l§m §r";
private List<String> _motdLines;
private String _firstCLine = " §b§l§m §8§l§m[ §r §9§lMineplex§r §f§lGames§r §8§l§m ]§b§l§m §r";
private List<String> _motdCLines;
public MotdManager(Plugin plugin) public MotdManager(Plugin plugin)
{ {
_plugin = plugin; plugin.getProxy().getScheduler().schedule(plugin, this, 5L, 30L, TimeUnit.SECONDS);
_region = !new File("eu.dat").exists() ? Region.US : Region.EU; plugin.getProxy().getPluginManager().registerListener(plugin, this);
_plugin.getProxy().getScheduler().schedule(_plugin, this, 5L, 30L, TimeUnit.SECONDS);
_plugin.getProxy().getPluginManager().registerListener(_plugin, this);
_repository = new RedisDataRepository<GlobalMotd>(ServerManager.getConnection(true, ServerManager.SERVER_STATUS_LABEL), ServerManager.getConnection(false, ServerManager.SERVER_STATUS_LABEL), _repository = new RedisDataRepository<GlobalMotd>(ServerManager.getConnection(true, ServerManager.SERVER_STATUS_LABEL), ServerManager.getConnection(false, ServerManager.SERVER_STATUS_LABEL),
Region.ALL, GlobalMotd.class, "globalMotd"); Region.ALL, GlobalMotd.class, "globalMotd");
run(); run();
if (new File("updateMOTD.dat").exists())
{
if (_region == Region.US)
_secondRepository = new RedisDataRepository<GlobalMotd>(new ConnectionData("10.81.1.156", 6379, ConnectionType.MASTER, "ServerStatus"), new ConnectionData("10.81.1.156", 6377, ConnectionType.SLAVE, "ServerStatus"),
Region.ALL, GlobalMotd.class, "globalMotd");
else
_secondRepository = new RedisDataRepository<GlobalMotd>(new ConnectionData("10.33.53.16", 6379, ConnectionType.MASTER, "ServerStatus"), new ConnectionData("10.33.53.16", 6377, ConnectionType.SLAVE, "ServerStatus"),
Region.ALL, GlobalMotd.class, "globalMotd");
//String motdLine = "§f§l◄ §c§lMaintenance§f§l ►";
//String motdLine = "§f§l◄ §a§lCarl the Creeper§f§l ►";
// String motdLine = " §2§l§n M O N S T E R M A Z E B E T A §f";
String motdLine = " §f> §4§lCLANS BETA §f- §c§lOpen to Everyone §f<";
// String motdLine = " §f❄ §2§lServer Maintenance §f❄ §2§lBe Back Soon §f❄";
//String motdLine = " §d§lRank Sale §a§l40% Off");
//String motdLine = " §f§l◄§c§lMAINTENANCE§f§l►");
updateMainMotd(" §f§m §8§l§m[ §r §9§lMineplex§r §f§lGames§r §8§l§m ]§f§m §r", motdLine);
System.out.println("Updated Bungee MOTD");
}
} }
@EventHandler @EventHandler
public void serverPing(ProxyPingEvent event) public void serverPing(ProxyPingEvent event)
{ {
net.md_5.bungee.api.ServerPing serverPing = event.getResponse(); net.md_5.bungee.api.ServerPing serverPing = event.getResponse();
if (event.getConnection().getListener() != null && event.getConnection().getListener().getDefaultServer().equalsIgnoreCase("ClansHub")) Optional<LobbyType> maybeType = Optional.empty();
if (event.getConnection().getListener() != null)
{ {
String motd = _firstCLine; maybeType = Arrays.stream(LobbyType.values())
if (_motdCLines != null && _motdCLines.size() > 0) .filter(type -> event.getConnection().getListener().getDefaultServer().equalsIgnoreCase(type.getConnectName()))
{ .findFirst();
motd += "\n" + _motdCLines.get(_random.nextInt(_motdCLines.size()));
} }
LobbyType lobbyType = maybeType.orElse(LobbyType.NORMAL);
GlobalMotd globalMotd = motds.get(lobbyType);
String motd = DEFAULT_HEADLINE;
if (globalMotd != null && globalMotd.getHeadline() != null)
{
motd = globalMotd.getHeadline() == null ? DEFAULT_HEADLINE : globalMotd.getHeadline();
if (globalMotd.getMotd() != null)
{
motd += "\n" + globalMotd.getMotd().get(_random.nextInt(globalMotd.getMotd().size()));
}
}
event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), serverPing.getPlayers(), motd, serverPing.getFaviconObject())); event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), serverPing.getPlayers(), motd, serverPing.getFaviconObject()));
} }
else
{
String motd = _firstLine;
if (_motdLines != null && _motdLines.size() > 0)
{
motd += "\n" + _motdLines.get(_random.nextInt(_motdLines.size()));
}
event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), serverPing.getPlayers(), motd, serverPing.getFaviconObject()));
}
}
@Override @Override
public void run() public void run()
{ {
for (LobbyType type : LobbyType.values())
{ {
GlobalMotd motd = _repository.getElement("MainMotd"); GlobalMotd motd = _repository.getElement(type.getRedisMotdKey());
if (motd != null) if (motd != null)
{ {
_motdLines = motd.getMotd(); motds.put(type, motd);
_firstLine = motd.getHeadline();
} }
} }
{
GlobalMotd motd = _repository.getElement("ClansMotd");
if (motd != null)
{
_motdCLines = motd.getMotd();
_firstCLine = motd.getHeadline();
}
else
{
_repository.addElement(new GlobalMotd("ClansMotd", "§4§lMineplex Clans§r", Arrays.asList("Default MOTD")));
}
}
}
/**
* Update the main {@link GlobalMotd} determining the MOTD for Bungee instances.
* @param motdLines - the lines to update the MOTD to.
*/
public void updateMainMotd(String headline, String motdLine)
{
List<String> motdLines = new ArrayList<String>();
motdLines.add(motdLine);
_repository.addElement(new GlobalMotd("MainMotd", headline, motdLines));
_secondRepository.addElement(new GlobalMotd("MainMotd", headline, motdLines));
}
public List<String> getMotdLines()
{
return _motdLines;
} }
} }