package mineplex.serverstatifier; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class StatusSnapshotRepository { private String _serverStatusConnectionString = "jdbc:mysql://db.mineplex.com:3306/ServerStatus"; private String _userName = "root"; private String _password = "tAbechAk3wR7tuTh"; private String _bungeeConnectionString = "jdbc:mysql://db.mineplex.com:3306/BungeeServers"; 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), port VARCHAR(11), updated LONG, lastTimeWithPlayers LONG, motd VARCHAR(256), players INT, maxPlayers INT, tps INT, ram INT, maxRam INT, PRIMARY KEY (id));"; private static String CREATE_DYNAMIC_TABLE = "CREATE TABLE IF NOT EXISTS DynamicServers (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), address VARCHAR(256), privateAddress VARCHAR(256), US BOOLEAN NOT NULL DEFAULT 'true', PRIMARY KEY (id));"; private static String RETRIEVE_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, DynamicServers.US, serverGroup, motd, DynamicServers.address, ServerStatus.address, ServerStatus.port, players, maxPlayers, case when TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.lastTimeWithPlayers)) > 300 then 1 else 0 end as empty, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress"; private static String RETRIEVE_BUNGEE_STATUSES = "SELECT address, US, players, maxPlayers FROM BungeeServers WHERE TIME_TO_SEC(TIMEDIFF(now(), updated)) < 15"; public static Connection serverStatusConnection; public static Connection bungeeConnection; public void initialize() { PreparedStatement preparedStatement = null; try { Class.forName("com.mysql.jdbc.Driver"); if (serverStatusConnection == null || serverStatusConnection.isClosed()) serverStatusConnection = DriverManager.getConnection(_serverStatusConnectionString, _userName, _password); // Create table preparedStatement = serverStatusConnection.prepareStatement(CREATE_TABLE); preparedStatement.execute(); } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } } preparedStatement = null; try { Class.forName("com.mysql.jdbc.Driver"); // Create table preparedStatement = serverStatusConnection.prepareStatement(CREATE_DYNAMIC_TABLE); preparedStatement.execute(); } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public List retrieveBungeeStatuses() { ResultSet resultSet = null; PreparedStatement preparedStatement = null; List bungeeData = new ArrayList(); try { if (bungeeConnection == null || bungeeConnection.isClosed()) bungeeConnection = DriverManager.getConnection(_bungeeConnectionString, _userName, _password); preparedStatement = bungeeConnection.prepareStatement(RETRIEVE_BUNGEE_STATUSES); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { BungeeStatusData data = new BungeeStatusData(); data.Address = resultSet.getString(1); data.US = resultSet.getBoolean(2); data.Players = resultSet.getInt(3); data.MaxPlayers = resultSet.getInt(4); bungeeData.add(data); } } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } } return bungeeData; } public HashMap> retrieveGroupStatusData() { ResultSet resultSet = null; PreparedStatement preparedStatement = null; HashMap> groupData = new HashMap>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); groupData.put(true, new HashMap()); groupData.put(false, new HashMap()); try { if (serverStatusConnection == null || serverStatusConnection.isClosed()) serverStatusConnection = DriverManager.getConnection(_serverStatusConnectionString, _userName, _password); preparedStatement = serverStatusConnection.prepareStatement(RETRIEVE_SERVERGROUP_STATUSES); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { ServerStatusData serverStatusData = new ServerStatusData(); serverStatusData.Name = resultSet.getString(1); serverStatusData.US = resultSet.getBoolean(2); String serverGroup = resultSet.getString(3); serverStatusData.Motd = resultSet.getString(4); serverStatusData.Address = resultSet.getString(5); serverStatusData.PrivateAddress = resultSet.getString(6); serverStatusData.Port = Integer.parseInt(resultSet.getString(7)); serverStatusData.Players = resultSet.getInt(8); serverStatusData.MaxPlayers = resultSet.getInt(9); serverStatusData.Empty = resultSet.getBoolean(10); if (!groupData.get(serverStatusData.US).containsKey(serverGroup)) { groupData.get(serverStatusData.US).put(serverGroup, new GroupStatusData()); } long current = dateFormat.parse(resultSet.getString(11)).getTime(); long updated = dateFormat.parse(resultSet.getString(12)).getTime(); if (current - updated < 15000) groupData.get(serverStatusData.US).get(serverGroup).addServer(serverStatusData); } } catch (Exception exception) { exception.printStackTrace(); } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } } return groupData; } }