Added some code for Global bungee communication.

Fixed up LobbyBalancer
This commit is contained in:
Jonathan Williams 2013-09-19 00:05:23 -07:00
parent 7329cc0aea
commit d2ab872856
12 changed files with 576 additions and 9 deletions

View File

@ -3,27 +3,21 @@ package mineplex.bungee;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import mineplex.bungee.bungeeSigns.BungeeSigns;
import mineplex.bungee.globalServer.GlobalServer;
import mineplex.bungee.lobbyBalancer.LobbyBalancer;
import mineplex.bungee.playerCount.PlayerCount;
import net.md_5.bungee.api.plugin.Plugin;
public class Mineplexer extends Plugin
{
private GlobalServer _dynamicServers;
private LobbyBalancer _lobbyBalancer;
{
@Override
public void onEnable()
{
new BungeeSigns(this);
//_dynamicServers = new GlobalServer(this);
_lobbyBalancer = new LobbyBalancer(this);
new LobbyBalancer(this);
new PlayerCount(this);
/*

View File

@ -0,0 +1,33 @@
package mineplex.bungee.globalServer;
import java.net.ServerSocket;
import net.md_5.bungee.api.plugin.Plugin;
public class GlobalServer
{
public GlobalServer(final Plugin plugin)
{
new Thread(new Runnable()
{
public void run()
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(4444);
while (true)
{
new GlobalServerMultiThread(plugin, serverSocket.accept()).start();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
}
}

View File

@ -0,0 +1,95 @@
package mineplex.bungee.globalServer;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin;
public class GlobalServerMultiThread extends Thread
{
private Plugin _plugin;
private Socket _socket = null;
public GlobalServerMultiThread(Plugin plugin, Socket socket)
{
super("GlobalServerMultiThread");
_plugin = plugin;
_socket = socket;
}
public void run()
{
DataInputStream socketInputStream = null;
DataOutputStream socketOutputStream = null;
try
{
socketInputStream = new DataInputStream(_socket.getInputStream());
socketOutputStream = new DataOutputStream(new BufferedOutputStream(_socket.getOutputStream(), 5120));
int id = socketInputStream.readShort();
if (id == 71)
{
System.out.println("Received packet 71");
Packet71FindPlayerServer packet = new Packet71FindPlayerServer();
packet.parseStream(socketInputStream);
System.out.println("Looking for player: " + packet.getPlayerName());
ProxiedPlayer player = _plugin.getProxy().getPlayer(packet.getPlayerName());
if (player != null)
{
Packet91PlayerServerResponse responsePacket = new Packet91PlayerServerResponse(player.getName(), player.getServer().getInfo().getName());
socketOutputStream.writeShort(91);
responsePacket.write(socketOutputStream);
}
socketOutputStream.close();
socketOutputStream = null;
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (socketInputStream != null)
socketInputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
if (socketOutputStream != null)
socketOutputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
if (_socket != null)
_socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,47 @@
package mineplex.bungee.globalServer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class GlobalServerRepository
{
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/BungeeServers";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS BungeeServers (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(256), updated LONG, players INT, maxPlayers INT, ram INT, maxRam INT, PRIMARY KEY (id));";
public void initialize()
{
Connection connection = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
PreparedStatement preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}

View File

@ -0,0 +1,27 @@
package mineplex.bungee.globalServer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Packet71FindPlayerServer extends Packet
{
private String _playerName;
@Override
public void parseStream(DataInputStream inputStream) throws IOException
{
_playerName = readString(inputStream, 16);
}
@Override
public void write(DataOutputStream dataOutput) throws IOException
{
writeString(_playerName, dataOutput);
}
public String getPlayerName()
{
return _playerName;
}
}

View File

@ -0,0 +1,31 @@
package mineplex.bungee.globalServer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Packet91PlayerServerResponse extends Packet
{
private String _playerName;
private String _serverName;
public Packet91PlayerServerResponse(String playerName, String serverName)
{
_playerName = playerName;
_serverName = serverName;
}
@Override
public void parseStream(DataInputStream inputStream) throws IOException
{
_playerName = readString(inputStream, 16);
_serverName = readString(inputStream, 24);
}
@Override
public void write(DataOutputStream dataOutput) throws IOException
{
writeString(_playerName, dataOutput);
writeString(_serverName, dataOutput);
}
}

View File

@ -87,7 +87,7 @@ public class LobbyBalancer implements Listener, Runnable
for (ServerStatusData serverStatusData : _repository.retrieveServerStatuses())
{
InetSocketAddress socketAddress = new InetSocketAddress(serverStatusData.Address, serverStatusData.Port);
_plugin.getProxy().getServers().put(serverStatusData.Name, _plugin.getProxy().constructServerInfo(serverStatusData.Name, socketAddress, "DynamicServer", false));
_plugin.getProxy().getServers().put(serverStatusData.Name, _plugin.getProxy().constructServerInfo(serverStatusData.Name, socketAddress, "LobbyBalancer", false));
}
for (String key : _plugin.getProxy().getServers().keySet())

View File

@ -0,0 +1,99 @@
package mineplex.bungee.lobbyBalancer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class LobbyBalancerRepository
{
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/ServerStatus";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ServerStatus (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), serverGroup VARCHAR(256), address VARCHAR(256), updated LONG, motd VARCHAR(256), players INT, maxPlayers INT, tps INT, ram INT, maxRam INT, PRIMARY KEY (id));";
private static String RETRIEVE_SERVER_STATUSES = "SELECT serverName, address, motd, players, maxPlayers FROM ServerStatus WHERE TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) < 10;";
public void initialize()
{
Connection connection = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
PreparedStatement preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public List<ServerStatusData> retrieveServerStatuses()
{
Connection connection = null;
List<ServerStatusData> serverData = new ArrayList<ServerStatusData>();
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
PreparedStatement preparedStatement = connection.prepareStatement(RETRIEVE_SERVER_STATUSES);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
ServerStatusData serverStatusData = new ServerStatusData();
serverStatusData.Name = resultSet.getString(1);
serverStatusData.Address = resultSet.getString(2).split(":")[0];
serverStatusData.Port = Integer.parseInt(resultSet.getString(2).split(":")[1]);
serverStatusData.Motd = resultSet.getString(3);
serverStatusData.Players = resultSet.getInt(4);
serverStatusData.MaxPlayers = resultSet.getInt(5);
serverData.add(serverStatusData);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return serverData;
}
}

View File

@ -0,0 +1,11 @@
package mineplex.bungee.lobbyBalancer;
public class ServerStatusData
{
public String Name;
public String Motd;
public int Players;
public int MaxPlayers;
public String Address;
public int Port;
}

View File

@ -0,0 +1,25 @@
package mineplex.bungee.playerTracker;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Plugin;
public class FindCommand extends Command
{
private Plugin _plugin;
public FindCommand(Plugin plugin)
{
super("mineplex.bungee.playertracker.find", "", "");
_plugin = plugin;
}
@Override
public void execute(CommandSender arg0, String[] arg1)
{
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,19 @@
package mineplex.bungee.playerTracker;
import net.md_5.bungee.api.plugin.Plugin;
public class PlayerTracker
{
private Plugin _plugin;
private PlayerTrackerRepository _repository;
public PlayerTracker(Plugin plugin)
{
_plugin = plugin;
_plugin.getProxy().getPluginManager().registerCommand(_plugin, new FindCommand(_plugin));
_repository = new PlayerTrackerRepository();
_repository.initialize();
}
}

View File

@ -0,0 +1,186 @@
package mineplex.bungee.playerTracker;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PlayerTrackerRepository
{
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/PlayerTracker";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS PlayerTracker (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(16), server VARCHAR(255), PRIMARY KEY (id));";
private static String INSERT_PLAYER_SERVER = "INSERT INTO PlayerTracker values(default, ?, ?);";
private static String UPDATE_PLAYER_SERVER = "UPDATE PlayerTracker SET server = ? WHERE name = ?;";
private static String DELETE_PLAYER = "DELETE FROM PlayerTracker WHERE name = ?;";
private static String RETRIEVE_PLAYER_SERVER = "SELECT server FROM PlayerTracker WHERE name = ?;";
public void initialize()
{
Connection connection = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
PreparedStatement preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public boolean updatePlayerServer(String name, String server)
{
Connection connection = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_PLAYER_SERVER);
preparedStatement.setString(1, server);
preparedStatement.setString(2, name);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0)
{
preparedStatement = connection.prepareStatement(INSERT_PLAYER_SERVER);
preparedStatement.setString(1, name);
preparedStatement.setString(2, server);
affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0)
{
throw new SQLException("Updating player server failed, no rows affected.");
}
}
return true;
}
catch (Exception exception)
{
exception.printStackTrace();
return false;
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public String retrievePlayerServer(String name)
{
Connection connection = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
PreparedStatement preparedStatement = connection.prepareStatement(RETRIEVE_PLAYER_SERVER);
preparedStatement.setString(1, name);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
return resultSet.getString(1);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return "Lobby";
}
public boolean removePlayer(String name)
{
Connection connection = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
PreparedStatement preparedStatement = connection.prepareStatement(DELETE_PLAYER);
preparedStatement.setString(1, name);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0)
{
throw new SQLException("Updating player server failed, no rows affected.");
}
return true;
}
catch (Exception exception)
{
exception.printStackTrace();
return false;
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}