Code to improve stat recording, commented out for now.

This commit is contained in:
Jonathan Williams 2015-03-15 19:28:58 -07:00
parent de780ae2f0
commit 4d557cb392
4 changed files with 205 additions and 3 deletions

View File

@ -1,5 +1,7 @@
package mineplex.bungee.playerStats; package mineplex.bungee.playerStats;
import java.util.HashMap;
import net.md_5.bungee.api.event.PostLoginEvent; import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
@ -7,9 +9,13 @@ import net.md_5.bungee.event.EventHandler;
public class PlayerStats implements Listener public class PlayerStats implements Listener
{ {
private static Object _cacheLock = new Object();
private Plugin _plugin; private Plugin _plugin;
private PlayerStatsRepository _repository; private PlayerStatsRepository _repository;
private HashMap<String, Integer> _ipCache = new HashMap<String, Integer>();
public PlayerStats(Plugin plugin) public PlayerStats(Plugin plugin)
{ {
_plugin = plugin; _plugin = plugin;
@ -29,7 +35,27 @@ public class PlayerStats implements Listener
{ {
_repository.addPlayer(event.getPlayer().getName()); _repository.addPlayer(event.getPlayer().getName());
_repository.addPlayerVersion(event.getPlayer().getName(), event.getPlayer().getPendingConnection().getVersion()); _repository.addPlayerVersion(event.getPlayer().getName(), event.getPlayer().getPendingConnection().getVersion());
_repository.addPlayerIP(event.getPlayer().getName(), event.getPlayer().getPendingConnection().getAddress().getAddress().getHostAddress());
String address = event.getPlayer().getPendingConnection().getAddress().getAddress().getHostAddress();
/*
boolean addNewIp = false;
int addressId = 0;
synchronized (_cacheLock)
{
if (_ipCache.containsKey(address))
addressId = _ipCache.get(address);
else
addNewIp = true;
}
if (addNewIp)
addressId = _repository.addNewIP(address);
_repository.addPlayerIP(event.getPlayer().getName(), addressId);
*/
_repository.addPlayerIP(event.getPlayer().getName(), address);
} }
}); });
} }

View File

@ -1,6 +1,5 @@
package mineplex.bungee.playerStats; package mineplex.bungee.playerStats;
import java.net.InetAddress;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -23,6 +22,8 @@ public class PlayerStatsRepository
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 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(?, ?);"; private static String INSERT_IP_PLAYER = "INSERT IGNORE INTO PlayerIP (playerName, ip) values(?, ?);";
//private static String INSERT_IP_PLAYER = "INSERT IGNORE INTO playerIp (playerName, ip) values(?, ?);";
//private static String INSERT_IP_PLAYER = "INSERT IGNORE INTO playerIp (playerName, ip) values(?, ?);";
public void initialize() public void initialize()
{ {
@ -195,4 +196,41 @@ public class PlayerStatsRepository
} }
} }
} }
public int addNewIP(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);
preparedStatement.setString(1, address);
preparedStatement.executeUpdate();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return -1;
}
} }

View File

@ -0,0 +1,16 @@
package mineplex.bungee;
public class IPGeoData
{
public String ip;
public String country_code;
public String country_name;
public String region_code;
public String region_name;
public String city;
public int zip_code;
public String time_zone;
public double latitude;
public double longitude;
public int metro_code;
}

View File

@ -0,0 +1,122 @@
package mineplex.bungee.api;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import com.google.gson.Gson;
public class HttpCallBase
{
private String _url;
public HttpCallBase(String url)
{
_url = url;
}
public <T> T Execute(Type returnType)
{
HttpGet request = new HttpGet(_url);
String response = execute(request);
System.out.println(response);
return new Gson().fromJson(response, returnType);
}
protected String execute(HttpRequestBase request)
{
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
connectionManager.setMaxTotal(200);
connectionManager.setDefaultMaxPerRoute(20);
HttpClient httpClient = new DefaultHttpClient(connectionManager);
InputStream in = null;
String response = "";
try
{
request.addHeader("Content-Type", "application/json");
HttpResponse httpResponse = httpClient.execute(request);
if (httpResponse != null)
{
in = httpResponse.getEntity().getContent();
response = convertStreamToString(in);
}
}
catch (Exception ex)
{
System.out.println("HttpCall Error:\n" + ex.getMessage());
for (StackTraceElement trace : ex.getStackTrace())
{
System.out.println(trace);
}
}
finally
{
httpClient.getConnectionManager().shutdown();
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return response;
}
protected String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
}