Add Mineplexer support for beta lobbies
This commit is contained in:
parent
b80a41ebe7
commit
3d4e64a208
@ -2,33 +2,34 @@ package mineplex.bungee.lobbyBalancer;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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.plugin.Listener;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
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
|
||||
{
|
||||
private Plugin _plugin;
|
||||
private ServerRepository _repository;
|
||||
|
||||
private List<MinecraftServer> _sortedLobbies = Lists.newArrayList();
|
||||
private List<MinecraftServer> _sortedClans = Lists.newArrayList();
|
||||
private static Object _serverLock = new Object();
|
||||
|
||||
private int _lobbyIndex = 0;
|
||||
private int _clansIndex = 0;
|
||||
|
||||
private final Map<LobbyType, List<MinecraftServer>> _sortedLobbyMap = new EnumMap<>(LobbyType.class);
|
||||
private final Map<LobbyType, Integer> _nextIndexMap = new EnumMap<>(LobbyType.class);
|
||||
private static final LobbySorter LOBBY_SORTER = new LobbySorter();
|
||||
private static final Object _serverLock = new Object();
|
||||
|
||||
public LobbyBalancer(Plugin plugin)
|
||||
{
|
||||
@ -46,45 +47,42 @@ public class LobbyBalancer implements Listener, Runnable
|
||||
@EventHandler
|
||||
public void playerConnect(ServerConnectEvent event)
|
||||
{
|
||||
if (event.getTarget().getName().equalsIgnoreCase("Lobby"))
|
||||
{
|
||||
synchronized (_serverLock)
|
||||
{
|
||||
if (_lobbyIndex >= _sortedLobbies.size() || _sortedLobbies.get(_lobbyIndex).getPlayerCount() >= _sortedLobbies.get(_lobbyIndex).getMaxPlayerCount())
|
||||
_lobbyIndex = 0;
|
||||
Arrays.stream(LobbyType.values())
|
||||
.filter(type -> type.getConnectName().equalsIgnoreCase(event.getTarget().getName()))
|
||||
.findFirst()
|
||||
.ifPresent(lobbyType ->
|
||||
{
|
||||
synchronized (_serverLock)
|
||||
{
|
||||
List<MinecraftServer> lobbies = _sortedLobbyMap.get(lobbyType);
|
||||
|
||||
event.setTarget(_plugin.getProxy().getServerInfo(_sortedLobbies.get(_lobbyIndex).getName()));
|
||||
_sortedLobbies.get(_lobbyIndex).incrementPlayerCount(1);
|
||||
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)
|
||||
{
|
||||
if (_clansIndex >= _sortedClans.size() || _sortedClans.get(_clansIndex).getPlayerCount() >= _sortedClans.get(_clansIndex).getMaxPlayerCount())
|
||||
_clansIndex = 0;
|
||||
int nextIndex = _nextIndexMap.getOrDefault(lobbyType, 0);
|
||||
if (nextIndex >= lobbies.size())
|
||||
{
|
||||
nextIndex = 0;
|
||||
}
|
||||
|
||||
event.setTarget(_plugin.getProxy().getServerInfo(_sortedClans.get(_clansIndex).getName()));
|
||||
_sortedClans.get(_clansIndex).incrementPlayerCount(1);
|
||||
System.out.println("Sending " + event.getPlayer().getName() + " to " + _sortedClans.get(_clansIndex).getName() + "(" + _sortedClans.get(_clansIndex).getPublicAddress() + ")");
|
||||
_clansIndex++;
|
||||
}
|
||||
}
|
||||
MinecraftServer server = lobbies.get(nextIndex);
|
||||
|
||||
event.setTarget(_plugin.getProxy().getServerInfo(server.getName()));
|
||||
server.incrementPlayerCount(1);
|
||||
System.out.println("Sending " + event.getPlayer().getName() + " to " + server.getName() + "(" + server.getPublicAddress() + ")");
|
||||
|
||||
_nextIndexMap.put(lobbyType, ++nextIndex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
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("Lobby"))
|
||||
{
|
||||
_plugin.getProxy().getServers().put("Lobby", _plugin.getProxy().constructServerInfo("Lobby", 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,9 +93,12 @@ public class LobbyBalancer implements Listener, Runnable
|
||||
synchronized (_serverLock)
|
||||
{
|
||||
long startTime = System.currentTimeMillis();
|
||||
_sortedLobbies.clear();
|
||||
_sortedClans.clear();
|
||||
|
||||
_sortedLobbyMap.clear();
|
||||
for (LobbyType type : LobbyType.values())
|
||||
{
|
||||
_sortedLobbyMap.put(type, new ArrayList<>());
|
||||
}
|
||||
|
||||
for (MinecraftServer server : servers)
|
||||
{
|
||||
if (server.getName() == null)
|
||||
@ -105,32 +106,26 @@ public class LobbyBalancer implements Listener, Runnable
|
||||
|
||||
InetSocketAddress socketAddress = new InetSocketAddress(server.getPublicAddress(), server.getPort());
|
||||
_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"))
|
||||
{
|
||||
_sortedLobbies.add(server);
|
||||
}
|
||||
}
|
||||
if (server.getName().toUpperCase().startsWith("CLANSHUB-"))
|
||||
{
|
||||
if (server.getMotd() == null || !server.getMotd().contains("Restarting"))
|
||||
{
|
||||
_sortedClans.add(server);
|
||||
}
|
||||
}
|
||||
|
||||
if (server.getMotd() != null && server.getMotd().contains("Restarting"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Arrays.stream(LobbyType.values())
|
||||
.filter(type -> server.getName().toUpperCase().startsWith(type.getUppercasePrefix()))
|
||||
.findFirst()
|
||||
.ifPresent(type -> _sortedLobbyMap.get(type).add(server));
|
||||
}
|
||||
|
||||
Collections.sort(_sortedLobbies, new LobbySorter());
|
||||
Collections.sort(_sortedClans, new LobbySorter());
|
||||
|
||||
|
||||
_sortedLobbyMap.values().forEach(lobbies -> Collections.sort(lobbies, LOBBY_SORTER));
|
||||
|
||||
long timeSpentInLock = System.currentTimeMillis() - startTime;
|
||||
|
||||
if (timeSpentInLock > 50)
|
||||
System.out.println("[==] TIMING [==] Locked loading servers for " + timeSpentInLock + "ms");
|
||||
|
||||
_lobbyIndex = 0;
|
||||
|
||||
_nextIndexMap.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,138 +1,82 @@
|
||||
package mineplex.bungee.motd;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
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.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.plugin.Listener;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
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
|
||||
{
|
||||
private Plugin _plugin;
|
||||
|
||||
private DataRepository<GlobalMotd> _repository;
|
||||
private DataRepository<GlobalMotd> _secondRepository;
|
||||
private Region _region;
|
||||
|
||||
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;
|
||||
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 final DataRepository<GlobalMotd> _repository;
|
||||
private final Random _random = new Random();
|
||||
private final Map<LobbyType, GlobalMotd> motds = new EnumMap<>(LobbyType.class);
|
||||
|
||||
public MotdManager(Plugin plugin)
|
||||
{
|
||||
_plugin = plugin;
|
||||
_region = !new File("eu.dat").exists() ? Region.US : Region.EU;
|
||||
|
||||
_plugin.getProxy().getScheduler().schedule(_plugin, this, 5L, 30L, TimeUnit.SECONDS);
|
||||
_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),
|
||||
Region.ALL, GlobalMotd.class, "globalMotd");
|
||||
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
|
||||
public void serverPing(ProxyPingEvent event)
|
||||
{
|
||||
|
||||
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;
|
||||
if (_motdCLines != null && _motdCLines.size() > 0)
|
||||
{
|
||||
motd += "\n" + _motdCLines.get(_random.nextInt(_motdCLines.size()));
|
||||
}
|
||||
event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), serverPing.getPlayers(), motd, serverPing.getFaviconObject()));
|
||||
maybeType = Arrays.stream(LobbyType.values())
|
||||
.filter(type -> event.getConnection().getListener().getDefaultServer().equalsIgnoreCase(type.getConnectName()))
|
||||
.findFirst();
|
||||
}
|
||||
else
|
||||
|
||||
LobbyType lobbyType = maybeType.orElse(LobbyType.NORMAL);
|
||||
GlobalMotd globalMotd = motds.get(lobbyType);
|
||||
|
||||
String motd = DEFAULT_HEADLINE;
|
||||
if (globalMotd != null && globalMotd.getHeadline() != null)
|
||||
{
|
||||
String motd = _firstLine;
|
||||
if (_motdLines != null && _motdLines.size() > 0)
|
||||
motd = globalMotd.getHeadline() == null ? DEFAULT_HEADLINE : globalMotd.getHeadline();
|
||||
if (globalMotd.getMotd() != null)
|
||||
{
|
||||
motd += "\n" + _motdLines.get(_random.nextInt(_motdLines.size()));
|
||||
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()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
for (LobbyType type : LobbyType.values())
|
||||
{
|
||||
GlobalMotd motd = _repository.getElement("MainMotd");
|
||||
GlobalMotd motd = _repository.getElement(type.getRedisMotdKey());
|
||||
|
||||
if (motd != null)
|
||||
{
|
||||
_motdLines = motd.getMotd();
|
||||
_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")));
|
||||
motds.put(type, 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user