Removed bad eventhandler line

Refactored StatsManager
Removed Pvp Table add for StatsManager
Added commented out hooks for HG stats
This commit is contained in:
Jonathan Williams 2014-01-18 09:02:56 -06:00
parent 8e51e4061a
commit 177f4a878b
11 changed files with 638 additions and 99 deletions

View File

@ -245,7 +245,6 @@ public class DisguiseManager extends MiniPlugin implements IPacketRunnable
}
}
@EventHandler
public void clearDisguises()
{
_spawnPacketMap.clear();

View File

@ -0,0 +1,9 @@
package mineplex.core.stats;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.stats.column.Column;
public class Row
{
public NautHashMap<String, Column<?>> Columns = new NautHashMap<String, Column<?>>();
}

View File

@ -1,12 +1,11 @@
package mineplex.core.stats;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.bukkit.entity.Player;
@ -17,23 +16,20 @@ import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.core.account.event.RetrieveClientInformationEvent;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.stats.column.Column;
import mineplex.core.stats.column.ColumnInt;
import mineplex.core.stats.column.ColumnVarChar;
public class StatsManager extends MiniPlugin
{
private static Object _statSync = new Object();
private static Connection _connection;
private NautHashMap<String, NautHashMap<String, PlayerStats>> _statUploadQueue = new NautHashMap<String, NautHashMap<String, PlayerStats>>();
private NautHashMap<String, NautHashMap<String, Row>> _statUploadQueue = new NautHashMap<String, NautHashMap<String, Row>>();
private Runnable _saveRunnable;
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/Stats?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private NautHashMap<String, Table> _tables = new NautHashMap<String, Table>();
private NautHashMap<String, String> _retrieveStatements = new NautHashMap<String, String>();
private NautHashMap<String, String> _updateStatements = new NautHashMap<String, String>();
private NautHashMap<String, NautHashMap<String, PlayerStats>> _playerStatList = new NautHashMap<String, NautHashMap<String, PlayerStats>>();
private NautHashMap<String, NautHashMap<String, Row>> _playerStatList = new NautHashMap<String, NautHashMap<String, Row>>();
public StatsManager(JavaPlugin plugin)
{
@ -53,57 +49,29 @@ public class StatsManager extends MiniPlugin
}
}
public StatsManager addTable(String tableName)
public StatsManager addTable(String tableName, String...columns)
{
if (!_retrieveStatements.containsKey(tableName))
if (!_tables.containsKey(tableName))
{
_retrieveStatements.put(tableName, "Select statName, statValue FROM " + tableName + " WHERE playerName = ?");
}
if (!_updateStatements.containsKey(tableName))
{
_updateStatements.put(tableName, "INSERT INTO " + tableName + " (playerName, statName, statValue) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE statName = VALUES(statName), statValue = VALUES(statValue);");
}
if (!_playerStatList.containsKey(tableName))
{
_playerStatList.put(tableName, new NautHashMap<String, PlayerStats>());
}
if (!_playerStatList.containsKey(tableName))
{
_playerStatList.put(tableName, new NautHashMap<String, PlayerStats>());
}
PreparedStatement createStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
ColumnVarChar playerColumn = new ColumnVarChar("playerName", 16);
List<Column<?>> columnList = new ArrayList<Column<?>>();
columnList.add(playerColumn);
createStatement = _connection.prepareStatement("CREATE TABLE IF NOT EXISTS " + tableName + " (playerName VARCHAR(25), statName VARCHAR(256), statValue INT, PRIMARY KEY (playerName, statName), INDEX (playerName));");
createStatement.execute();
}
catch (Exception exception)
{
System.out.println("Error creating table " + tableName + ".");
exception.printStackTrace();
}
finally
{
if (createStatement != null)
for (String column : columns)
{
try
{
createStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
columnList.add(new ColumnInt(column));
}
Table statTable = new Table(tableName, columnList, columnList, playerColumn);
statTable.initialize();
// Can't Arrays.asList here because of weird java generics
_tables.put(tableName, statTable);
}
if (!_playerStatList.containsKey(tableName))
{
_playerStatList.put(tableName, new NautHashMap<String, Row>());
}
return this;
@ -124,16 +92,21 @@ public class StatsManager extends MiniPlugin
if (!_playerStatList.get(table).containsKey(playerName))
{
_playerStatList.get(table).put(playerName, new PlayerStats());
Row row = _tables.get(table).createRow();
((ColumnVarChar)row.Columns.get("playerName")).Value = playerName;
_playerStatList.get(table).put(playerName, row);
}
_playerStatList.get(table).get(playerName).addStat(statName, value);
_playerStatList.get(table).get(playerName).Columns.put(statName, new ColumnInt(statName, value));
synchronized (_statSync)
{
if (!_statUploadQueue.containsKey(table))
{
_statUploadQueue.put(table, new NautHashMap<String, PlayerStats>());
_statUploadQueue.put(table, new NautHashMap<String, Row>());
}
_statUploadQueue.get(table).put(playerName, _playerStatList.get(table).get(playerName));
@ -153,22 +126,29 @@ public class StatsManager extends MiniPlugin
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
for (String tableName : _retrieveStatements.keySet())
for (Entry<String, Table> tableEntry : _tables.entrySet())
{
Table table = tableEntry.getValue();
String tableName = tableEntry.getKey();
try
{
preparedStatement = event.getConnection().prepareStatement(_retrieveStatements.get(tableName));
preparedStatement.setString(1, event.getClientName());
List<Column<?>> columnList = new ArrayList<Column<?>>();
columnList.add(new ColumnVarChar("playerName", 16, event.getClientName()));
List<Row> rows = table.retrieve(columnList);
resultSet = preparedStatement.executeQuery();
PlayerStats playerStats = new PlayerStats();
Row row = table.createRow();
((ColumnVarChar)row.Columns.get("playerName")).Value = event.getClientName();
while (resultSet.next())
{
playerStats.addStat(resultSet.getString(1), resultSet.getInt(2));
if (rows.size() > 0)
{
for (Column<?> column : rows.get(0).Columns.values())
{
row.Columns.put(column.Name, column);
}
}
_playerStatList.get(tableName).put(event.getClientName(), playerStats);
_playerStatList.get(tableName).put(event.getClientName(), row);
}
catch (Exception exception)
{
@ -209,13 +189,13 @@ public class StatsManager extends MiniPlugin
try
{
NautHashMap<String, NautHashMap<String, PlayerStats>> uploadQueue = new NautHashMap<String, NautHashMap<String, PlayerStats>>();
NautHashMap<String, NautHashMap<String, Row>> uploadQueue = new NautHashMap<String, NautHashMap<String, Row>>();
synchronized (_statSync)
{
for (String key : _statUploadQueue.keySet())
{
uploadQueue.put(key, new NautHashMap<String, PlayerStats>());
uploadQueue.put(key, new NautHashMap<String, Row>());
for (String stat : _statUploadQueue.get(key).keySet())
{
@ -228,23 +208,12 @@ public class StatsManager extends MiniPlugin
for (String tableName : uploadQueue.keySet())
{
for (Iterator<Entry<String, PlayerStats>> iterator = uploadQueue.get(tableName).entrySet().iterator(); iterator.hasNext();)
for (Iterator<Entry<String, Row>> iterator = uploadQueue.get(tableName).entrySet().iterator(); iterator.hasNext();)
{
Entry<String, PlayerStats> entry = iterator.next();
for (String statName : entry.getValue().getStatsNames())
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(_updateStatements.get(tableName), Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, entry.getKey());
preparedStatement.setString(2, statName);
preparedStatement.setInt(3, entry.getValue().getStat(statName));
preparedStatement.executeUpdate();
}
Entry<String, Row> entry = iterator.next();
if (!_tables.get(tableName).update(new ArrayList<Column<?>>(entry.getValue().Columns.values()), new ColumnVarChar("playerName", 16, entry.getKey())))
_tables.get(tableName).insert(new ArrayList<Column<?>>(entry.getValue().Columns.values()));
iterator.remove();
}
@ -289,8 +258,6 @@ public class StatsManager extends MiniPlugin
return 0;
}
PlayerStats stats = _playerStatList.get(table).get(player);
return stats.getStat(stat);
return ((ColumnInt)_playerStatList.get(table).get(player).Columns.get(stat)).Value;
}
}

View File

@ -0,0 +1,417 @@
package mineplex.core.stats;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.stats.column.Column;
public class Table
{
private static Connection _connection;
private String _connectionString = "jdbc:mysql://sql.mineplex.com:3306/Mineplex?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private String _name;
private List<Column<?>> _primaryKeys;
private NautHashMap<String, Column<?>> _columns = new NautHashMap<String, Column<?>>();
private Column<?> _index;
public Table(String name, List<Column<?>> primaryKeys, List<Column<?>> columns, Column<?> index)
{
_name = name;
_primaryKeys = primaryKeys;
for (Column<?> column : columns)
{
_columns.put(column.Name, column);
}
_index = index;
}
public void initialize()
{
if (!doesTableExist())
{
create();
}
}
/*
private void updateSchema()
{
PreparedStatement getTableColumns = null;
ResultSet resultSet = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
getTableColumns = _connection.prepareStatement("SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '`" + _name + "`' AND table_schema = 'Mineplex';");
resultSet = getTableColumns.executeQuery();
HashSet<String> columnExists = new HashSet<String>();
while (resultSet.next())
{
columnExists.add(resultSet.getString("COLUMN_NAME"));
}
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (getTableColumns != null)
{
try
{
getTableColumns.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
*/
private void create()
{
PreparedStatement createStatement = null;
try
{
StringBuilder columnBuilder = new StringBuilder();
for (Iterator<Column<?>> columnIterator = _columns.values().iterator(); columnIterator.hasNext();)
{
Column<?> column = columnIterator.next();
columnBuilder.append(column.getCreateString());
if (columnIterator.hasNext())
{
columnBuilder.append(", ");
}
}
StringBuilder primaryKey = new StringBuilder();
for (Column<?> column : _primaryKeys)
{
primaryKey.append(column.Name);
if (!column.equals(_primaryKeys.get(_primaryKeys.size() - 1)))
{
primaryKey.append(", ");
}
}
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
createStatement = _connection.prepareStatement("CREATE TABLE IF NOT EXISTS `" + _name + "` (" + columnBuilder.toString() + ", PRIMARY KEY (" + primaryKey.toString() + "), INDEX (" + _index.Name + "));");
createStatement.execute();
}
catch (Exception exception)
{
System.out.println("Error creating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (createStatement != null)
{
try
{
createStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
private boolean doesTableExist()
{
PreparedStatement checkIfTableExistsStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
checkIfTableExistsStatement = _connection.prepareStatement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'Mineplex' AND table_name LIKE '`" + _name + "`'");
if (checkIfTableExistsStatement.executeQuery().next())
return true;
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (checkIfTableExistsStatement != null)
{
try
{
checkIfTableExistsStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}
public void insert(List<Column<?>> columns)
{
StringBuilder temporaryBuilder = new StringBuilder();
StringBuilder questionBuilder = new StringBuilder();
StringBuilder updateBuilder = new StringBuilder();
for (Column<?> column : columns)
{
temporaryBuilder.append(column.Name);
questionBuilder.append("'" + column.Value + "'");
updateBuilder.append(column.Name + " = VALUES(" + column.Name + ")");
if (!column.equals(columns.get(columns.size() - 1)))
{
temporaryBuilder.append(", ");
questionBuilder.append(", ");
updateBuilder.append(", ");
}
}
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement("INSERT INTO `" + _name + "` (" + temporaryBuilder.toString() + ") VALUES (" + questionBuilder.toString() + ") ON DUPLICATE KEY UPDATE " + updateBuilder.toString() + ";", Statement.RETURN_GENERATED_KEYS);
preparedStatement.execute();
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public boolean update(List<Column<?>> columns, Column<?> whereColumn)
{
List<Column<?>> whereColumnList = new ArrayList<Column<?>>();
whereColumnList.add(whereColumn);
return update(columns, whereColumnList);
}
public boolean update(List<Column<?>> columns, List<Column<?>> whereColumns)
{
String updateStatement = buildUpdateStatement(columns, whereColumns);
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(updateStatement);
if (preparedStatement.executeUpdate() != 0)
return true;
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}
public List<Row> retrieve(List<Column<?>> columns)
{
StringBuilder temporaryBuilder = new StringBuilder();
for (Iterator<Column<?>> columnIterator = _columns.values().iterator(); columnIterator.hasNext();)
{
Column<?> column = columnIterator.next();
temporaryBuilder.append(column.Name);
if (columnIterator.hasNext())
temporaryBuilder.append(", ");
}
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Row> rows = new ArrayList<Row>();
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement("Select " + temporaryBuilder.toString() + " FROM `" + _name + "` " + buildWhereString(columns) + ";");
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
Row row = new Row();
for (Column<?> column : columns)
{
column.getValue(resultSet);
row.Columns.put(column.Name, column);
}
rows.add(row);
}
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return rows;
}
private String buildUpdateStatement(List<Column<?>> columns, List<Column<?>> whereColumns)
{
StringBuilder setBuilder = new StringBuilder();
if (columns.size() > 0)
setBuilder.append("SET ");
for (Column<?> column : columns)
{
setBuilder.append(column.Name + " = '" + column.Value + "'");
if (!column.equals(columns.get(columns.size() - 1)))
setBuilder.append(", ");
}
return "UPDATE `" + _name + "` " + setBuilder.toString() + " " + buildWhereString(whereColumns) + ";";
}
private String buildWhereString(List<Column<?>> columns)
{
StringBuilder whereBuilder = new StringBuilder();
if (columns.size() > 0)
{
whereBuilder.append("WHERE ");
}
for (Column<?> column : columns)
{
whereBuilder.append(column.Name + " = '" + column.Value + "'");
if (!column.equals(columns.get(columns.size() - 1)))
whereBuilder.append(" AND ");
}
return whereBuilder.toString();
}
public Column<?> getColumn(String columnName)
{
return _columns.get(columnName);
}
public Row createRow()
{
Row row = new Row();
for (Column<?> column : _columns.values())
{
row.Columns.put(column.Name, column);
}
return row;
}
}

View File

@ -0,0 +1,27 @@
package mineplex.core.stats.column;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class Column<Type>
{
public String Name;
public Type Value;
public Column(String name)
{
Name = name;
}
public Column(String name, Type value)
{
Name = name;
Value = value;
}
public abstract String getCreateString();
public abstract Type getValue(ResultSet resultSet) throws SQLException;
public abstract Column<Type> clone();
}

View File

@ -0,0 +1,36 @@
package mineplex.core.stats.column;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ColumnInt extends Column<Integer>
{
public ColumnInt(String name)
{
super(name);
Value = 0;
}
public ColumnInt(String name, int value)
{
super(name, value);
}
@Override
public String getCreateString()
{
return Name + " INT";
}
@Override
public Integer getValue(ResultSet resultSet) throws SQLException
{
return resultSet.getInt(Name);
}
@Override
public ColumnInt clone()
{
return new ColumnInt(Name, Value);
}
}

View File

@ -0,0 +1,36 @@
package mineplex.core.stats.column;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ColumnLong extends Column<Long>
{
public ColumnLong(String name)
{
super(name);
Value = 0L;
}
public ColumnLong(String name, Long value)
{
super(name, value);
}
@Override
public String getCreateString()
{
return Name + " LONG";
}
@Override
public Long getValue(ResultSet resultSet) throws SQLException
{
return resultSet.getLong(Name);
}
@Override
public ColumnLong clone()
{
return new ColumnLong(Name, Value);
}
}

View File

@ -0,0 +1,39 @@
package mineplex.core.stats.column;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ColumnVarChar extends Column<String>
{
public int Length = 25;
public ColumnVarChar(String name, int length)
{
this(name, length, "");
}
public ColumnVarChar(String name, int length, String value)
{
super(name);
Length = length;
Value = value;
}
public String getCreateString()
{
return Name + " VARCHAR(" + Length + ")";
}
@Override
public String getValue(ResultSet resultSet) throws SQLException
{
return resultSet.getString(Name);
}
@Override
public ColumnVarChar clone()
{
return new ColumnVarChar(Name, Length, Value);
}
}

View File

@ -130,7 +130,6 @@ public class HubManager extends MiniClientPlugin<HubClient>
_visibilityManager = new VisibilityManager(this);
_statsManager = new StatsManager(plugin);
_statsManager.addTable("Pvp");
((CraftWorld)Bukkit.getWorlds().get(0)).getHandle().pvpMode = true;
}

View File

@ -220,6 +220,7 @@ public class HungerGames extends SoloGame
System.out.println("===================");
System.out.println("CREEP TYPE: " + _spreadName);
System.out.println("===================");
//Manager.GetStatsManager().addTable(GetName(), "kills", "deaths", "chestsOpened");
}
@Override
@ -364,7 +365,7 @@ public class HungerGames extends SoloGame
if (_supplyChests.contains(event.getClickedBlock()))
{
count = 2;
if (Math.random() > 0.75) count++;
if (Math.random() > 0.75) count++;
if (Math.random() > 0.95) count++;
}
@ -372,6 +373,8 @@ public class HungerGames extends SoloGame
chest.getBlockInventory().setItem(UtilMath.r(27), GetChestItem(_supplyChests.contains(event.getClickedBlock())));
_supplyChests.remove(event.getClickedBlock());
//Manager.GetStatsManager().addStat(event.getPlayer(), GetName(), "chestsOpened", 1);
}
private ItemStack GetChestItem(boolean superChest)
@ -379,10 +382,10 @@ public class HungerGames extends SoloGame
if (_baseChestLoot.isEmpty())
{
//Armor
for (int i = 0; i < 10; i++) _baseChestLoot.add(new ItemStack(Material.IRON_HELMET));
for (int i = 0; i < 10; i++) _baseChestLoot.add(new ItemStack(Material.IRON_HELMET));
for (int i = 0; i < 3; i++) _baseChestLoot.add(new ItemStack(Material.IRON_CHESTPLATE));
for (int i = 0; i < 5; i++) _baseChestLoot.add(new ItemStack(Material.IRON_LEGGINGS));
for (int i = 0; i < 10; i++) _baseChestLoot.add(new ItemStack(Material.IRON_BOOTS));
for (int i = 0; i < 10; i++) _baseChestLoot.add(new ItemStack(Material.IRON_BOOTS));
for (int i = 0; i < 30; i++) _baseChestLoot.add(new ItemStack(Material.CHAINMAIL_HELMET));
for (int i = 0; i < 20; i++) _baseChestLoot.add(new ItemStack(Material.CHAINMAIL_CHESTPLATE));
@ -1517,7 +1520,15 @@ public class HungerGames extends SoloGame
if (killer != null && !killer.equals(killed))
{
Manager.GetStatsManager().addStat(killer, "Pvp", "Kills", 1);
//Manager.GetStatsManager().addStat(killer, GetName(), "kills", 1);
}
}
if (event.GetLog().GetPlayer() != null)
{
if (killed != null)
{
//Manager.GetStatsManager().addStat(killed, GetName(), "deaths", 1);
}
}
}

View File

@ -46,8 +46,7 @@ public class GamePlayerManager implements Listener
{
Manager = manager;
Manager.GetPluginManager().registerEvents(this, Manager.GetPlugin());
Manager.GetStatsManager().addTable("Pvp");
Manager.GetPluginManager().registerEvents(this, Manager.GetPlugin());
}
@EventHandler(priority = EventPriority.HIGH)