2013-09-18 00:50:23 +02:00
package mineplex.bungee.playerCount ;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.Statement ;
public class PlayerCountRepository
{
2013-11-16 11:59:28 +01:00
private String _connectionString = " jdbc:mysql://sql.mineplex.com:3306/BungeeServers?autoReconnect=true&failOverReadOnly=false&maxReconnects=10 " ;
2013-09-18 00:50:23 +02:00
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)); " ;
2013-11-07 04:48:00 +01:00
private static String INSERT_PLAYER_COUNT = " INSERT INTO BungeeServers(address, updated, players, maxPlayers, ram, maxRam) values(?, now(), ?, ?, ?, ?); " ;
2013-09-18 00:50:23 +02:00
private static String UPDATE_PLAYER_COUNT = " UPDATE BungeeServers SET updated = now(), players = ?, maxPlayers = ?, ram = ?, maxRam = ? WHERE id = ?; " ;
private static String RETRIEVE_ID = " SELECT id FROM BungeeServers WHERE address = ?; " ;
private static String RETRIEVE_PLAYER_COUNT = " SELECT SUM(players) AS playerCount, SUM(maxPlayers) AS maxPlayerCount FROM BungeeServers WHERE TIME_TO_SEC(TIMEDIFF(now(), BungeeServers.updated)) < 10; " ;
private int _id = - 1 ;
private String _address ;
private int _maxPlayers = 0 ;
public PlayerCountRepository ( String address , int maxPlayers )
{
_address = address ;
_maxPlayers = maxPlayers ;
}
public void initialize ( )
{
Connection connection = null ;
2013-10-10 18:01:48 +02:00
ResultSet resultSet = null ;
2013-10-17 09:37:57 +02:00
PreparedStatement preparedStatement = null ;
PreparedStatement preparedStatementRetrieve = null ;
PreparedStatement preparedStatementInsert = null ;
2013-09-18 00:50:23 +02:00
try
{
connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
// Create table
2013-10-17 09:37:57 +02:00
preparedStatement = connection . prepareStatement ( CREATE_TABLE ) ;
2013-09-18 00:50:23 +02:00
preparedStatement . execute ( ) ;
// Retrieve id
2013-10-17 09:37:57 +02:00
preparedStatementRetrieve = connection . prepareStatement ( RETRIEVE_ID ) ;
2013-09-18 00:50:23 +02:00
preparedStatementRetrieve . setString ( 1 , _address ) ;
2013-10-10 18:01:48 +02:00
resultSet = preparedStatementRetrieve . executeQuery ( ) ;
2013-09-18 00:50:23 +02:00
while ( resultSet . next ( ) )
{
_id = resultSet . getInt ( " id " ) ;
}
// Insert if not there
if ( _id = = - 1 )
{
2013-10-17 09:37:57 +02:00
preparedStatementInsert = connection . prepareStatement ( INSERT_PLAYER_COUNT , Statement . RETURN_GENERATED_KEYS ) ;
2013-09-18 00:50:23 +02:00
preparedStatementInsert . setString ( 1 , _address ) ;
preparedStatementInsert . setInt ( 2 , 0 ) ;
preparedStatementInsert . setInt ( 3 , _maxPlayers ) ;
preparedStatementInsert . setInt ( 4 , ( int ) ( ( Runtime . getRuntime ( ) . maxMemory ( ) - Runtime . getRuntime ( ) . freeMemory ( ) ) / 1048576 ) ) ;
preparedStatementInsert . setInt ( 5 , ( int ) ( Runtime . getRuntime ( ) . maxMemory ( ) / 1048576 ) ) ;
int affectedRows = preparedStatementInsert . executeUpdate ( ) ;
if ( affectedRows = = 0 )
{
throw new SQLException ( " Creating bungee server failed, no rows affected. " ) ;
}
2013-11-08 19:52:56 +01:00
resultSet . close ( ) ;
2013-09-18 00:50:23 +02:00
resultSet = preparedStatementInsert . getGeneratedKeys ( ) ;
if ( resultSet . next ( ) )
{
_id = resultSet . getInt ( 1 ) ;
System . out . println ( " id = " + _id ) ;
}
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
2013-10-17 09:37:57 +02:00
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
if ( preparedStatementRetrieve ! = null )
{
try
{
preparedStatementRetrieve . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
if ( preparedStatementInsert ! = null )
{
try
{
preparedStatementInsert . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
2013-10-10 18:01:48 +02:00
if ( resultSet ! = null )
{
try
{
resultSet . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
2013-09-18 00:50:23 +02:00
if ( connection ! = null )
{
try
{
connection . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
}
public boolean updatePlayerCountInDatabase ( int players )
{
Connection connection = null ;
2013-10-17 09:37:57 +02:00
PreparedStatement preparedStatement = null ;
2013-09-18 00:50:23 +02:00
try
{
connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
2013-10-17 09:37:57 +02:00
preparedStatement = connection . prepareStatement ( UPDATE_PLAYER_COUNT , Statement . RETURN_GENERATED_KEYS ) ;
2013-09-18 00:50:23 +02:00
preparedStatement . setInt ( 1 , players ) ;
preparedStatement . setInt ( 2 , _maxPlayers ) ;
preparedStatement . setInt ( 3 , ( int ) ( ( Runtime . getRuntime ( ) . maxMemory ( ) - Runtime . getRuntime ( ) . freeMemory ( ) ) / 1048576 ) ) ;
preparedStatement . setInt ( 4 , ( int ) ( Runtime . getRuntime ( ) . maxMemory ( ) / 1048576 ) ) ;
preparedStatement . setInt ( 5 , _id ) ;
int affectedRows = preparedStatement . executeUpdate ( ) ;
if ( affectedRows = = 0 )
{
throw new SQLException ( " Updating bungee server player count failed, no rows affected. " ) ;
}
return true ;
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
2013-11-18 02:00:12 +01:00
try
{
Thread . sleep ( 10 ) ;
}
catch ( InterruptedException e )
{
e . printStackTrace ( ) ;
}
return updatePlayerCountInDatabase ( players ) ;
2013-09-18 00:50:23 +02:00
}
finally
{
2013-10-17 09:37:57 +02:00
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
2013-09-18 00:50:23 +02:00
if ( connection ! = null )
{
try
{
connection . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
}
public PlayerTotalData retrievePlayerCount ( )
{
Connection connection = null ;
PlayerTotalData playerData = new PlayerTotalData ( ) ;
2013-10-10 18:01:48 +02:00
ResultSet resultSet = null ;
2013-10-17 09:37:57 +02:00
PreparedStatement preparedStatement = null ;
2013-09-18 00:50:23 +02:00
try
{
connection = DriverManager . getConnection ( _connectionString , _userName , _password ) ;
2013-10-17 09:37:57 +02:00
preparedStatement = connection . prepareStatement ( RETRIEVE_PLAYER_COUNT ) ;
2013-10-10 18:01:48 +02:00
resultSet = preparedStatement . executeQuery ( ) ;
2013-09-18 00:50:23 +02:00
while ( resultSet . next ( ) )
{
playerData . CurrentPlayers = resultSet . getInt ( 1 ) ;
playerData . MaxPlayers = resultSet . getInt ( 2 ) ;
return playerData ;
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
2013-10-17 09:37:57 +02:00
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
2013-10-10 18:01:48 +02:00
if ( resultSet ! = null )
{
try
{
resultSet . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
2013-09-18 00:50:23 +02:00
if ( connection ! = null )
{
try
{
connection . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return playerData ;
}
}