Added Version and IP logging for Bungees.

This commit is contained in:
Jonathan Williams 2015-03-11 17:23:23 -07:00
parent 6614f83579
commit 4db745c1e5
2 changed files with 59 additions and 18 deletions

View File

@ -28,6 +28,8 @@ public class PlayerStats implements Listener
public void run()
{
_repository.addPlayer(event.getPlayer().getName());
_repository.addPlayerVersion(event.getPlayer().getName(), event.getPlayer().getPendingConnection().getVersion());
_repository.addPlayerIP(event.getPlayer().getName(), event.getPlayer().getPendingConnection().getAddress().getAddress().getHostAddress());
}
});
}

View File

@ -1,5 +1,6 @@
package mineplex.bungee.playerStats;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@ -16,8 +17,12 @@ public class PlayerStatsRepository
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS DailyUnique (id INT NOT NULL AUTO_INCREMENT, day VARCHAR(100), playerName VARCHAR(20), PRIMARY KEY (id), UNIQUE KEY unique_player_per_day (day, playerName));";
private static String INSERT_PLAYER = "INSERT INTO DailyUnique (day, playerName) values(curdate(), ?) ON DUPLICATE KEY UPDATE playerName=playerName;";
private static String CREATE_VER_TABLE = "CREATE TABLE IF NOT EXISTS PlayerVersion (id INT NOT NULL AUTO_INCREMENT, playerName VARCHAR(20), version VARCHAR(40), PRIMARY KEY (id), UNIQUE KEY unique_player (playerName));";
private static String INSERT_VER_PLAYER = "INSERT INTO PlayerVersion (playerName, version) values(?, ?) ON DUPLICATE KEY UPDATE version=version;";
private static String CREATE_VER_TABLE = "CREATE TABLE IF NOT EXISTS PlayerVersion (id INT NOT NULL AUTO_INCREMENT, playerName VARCHAR(20), version INT, PRIMARY KEY (id), UNIQUE KEY unique_player (playerName));";
private static String INSERT_VER_PLAYER = "INSERT INTO PlayerVersion (playerName, version) values(?, ?);";
private static String UPDATE_VER_PLAYER = "UPDATE PlayerVersion SET version = ? WHERE playerName = ?;";
private static String CREATE_IP_TABLE = "CREATE TABLE IF NOT EXISTS PlayerIP (id INT NOT NULL AUTO_INCREMENT, playerName VARCHAR(20), ip VARCHAR(20), PRIMARY KEY (id), UNIQUE INDEX unique_player_ip (playerName, ip));";
private static String INSERT_IP_PLAYER = "INSERT IGNORE INTO PlayerIP (playerName, ip) values(?, ?);";
public void initialize()
{
@ -36,6 +41,11 @@ public class PlayerStatsRepository
preparedStatement = _connection.prepareStatement(CREATE_VER_TABLE);
preparedStatement.execute();
preparedStatement.close();
preparedStatement = _connection.prepareStatement(CREATE_IP_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
{
@ -103,7 +113,7 @@ public class PlayerStatsRepository
}
}
public boolean addPlayerVersion(String playerName, String version)
public void addPlayerVersion(String playerName, int version)
{
PreparedStatement preparedStatement = null;
@ -112,34 +122,63 @@ public class PlayerStatsRepository
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(INSERT_VER_PLAYER, Statement.RETURN_GENERATED_KEYS);
preparedStatement = _connection.prepareStatement(UPDATE_VER_PLAYER);
preparedStatement.setString(1, playerName);
preparedStatement.setString(2, version);
preparedStatement.setInt(1, version);
preparedStatement.setString(2, playerName);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0)
{
throw new SQLException("Adding player version record failed, no rows affected.");
}
return true;
preparedStatement.close();
preparedStatement = _connection.prepareStatement(INSERT_VER_PLAYER, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, playerName);
preparedStatement.setInt(2, version);
preparedStatement.executeUpdate();
}
}
catch (Exception exception)
{
exception.printStackTrace();
try
}
finally
{
if (preparedStatement != null)
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public void addPlayerIP(String playerName, String address)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(INSERT_IP_PLAYER, Statement.RETURN_GENERATED_KEYS);
return addPlayer(playerName);
preparedStatement.setString(1, playerName);
preparedStatement.setString(2, address);
preparedStatement.executeUpdate();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{