2015-03-25 23:10:59 +01:00
|
|
|
package mineplex.chestConverter;
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
2015-05-13 09:00:37 +02:00
|
|
|
import java.sql.Statement;
|
|
|
|
import java.util.ArrayList;
|
2015-03-25 23:10:59 +01:00
|
|
|
import java.util.HashMap;
|
2015-05-13 09:00:37 +02:00
|
|
|
import java.util.List;
|
2015-03-25 23:10:59 +01:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
public class ChestConverterRepository
|
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Account?allowMultiQueries=true";
|
2015-03-25 23:10:59 +01:00
|
|
|
private String _userName = "root";
|
|
|
|
private String _password = "tAbechAk3wR7tuTh";
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
private static String ADD_ACCOUNT_TASK = "INSERT INTO accountTasks (accountId, taskId) VALUES (?, ?);";
|
|
|
|
private static String RETRIEVE_TASKS = "SELECT id, name FROM tasks;";
|
2015-03-25 23:10:59 +01:00
|
|
|
|
|
|
|
private static Connection _connection;
|
|
|
|
|
|
|
|
public ChestConverterRepository()
|
|
|
|
{
|
|
|
|
PreparedStatement preparedStatement = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
|
|
|
|
if (_connection == null || _connection.isClosed())
|
|
|
|
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (preparedStatement != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
preparedStatement.close();
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
public HashMap<String, Integer> getTaskList()
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
HashMap<String, Integer> tasks = new HashMap<String, Integer>();
|
2015-03-25 23:10:59 +01:00
|
|
|
PreparedStatement preparedStatement = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_connection == null || _connection.isClosed())
|
|
|
|
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
2015-05-13 09:00:37 +02:00
|
|
|
|
|
|
|
preparedStatement = _connection.prepareStatement(RETRIEVE_TASKS);
|
|
|
|
preparedStatement.execute();
|
2015-03-25 23:10:59 +01:00
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
ResultSet resultSet = preparedStatement.getResultSet();
|
2015-03-25 23:10:59 +01:00
|
|
|
|
|
|
|
while (resultSet.next())
|
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
tasks.put(resultSet.getString(2), resultSet.getInt(1));
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (preparedStatement != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
preparedStatement.close();
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
return tasks;
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
public List<AccountTask> getTasks(int skip, int count)
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
return new JsonWebCall("http://accounts.mineplex.com/PlayerAccount/GetTasksByCount").Execute(new com.google.gson.reflect.TypeToken<List<AccountTask>>(){}.getType(), new SearchConf(skip, count));
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
public void incrementClients(HashMap<String, List<Integer>> playerList)
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
|
|
|
PreparedStatement preparedStatement = null;
|
2015-05-13 09:00:37 +02:00
|
|
|
Statement statement = null;
|
2015-03-25 23:10:59 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_connection == null || _connection.isClosed())
|
|
|
|
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
statement = _connection.createStatement();
|
|
|
|
HashMap<Integer, List<Integer>> playerIdList = new HashMap<Integer, List<Integer>>();
|
|
|
|
String queryString = "";
|
|
|
|
for (Entry<String, List<Integer>> entry : playerList.entrySet())
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
queryString += "SELECT id FROM accounts WHERE accounts.uuid = '" + entry.getKey() + "' LIMIT 1;";
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
statement.execute(queryString);
|
|
|
|
statement.getUpdateCount();
|
2015-03-25 23:10:59 +01:00
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
for (Entry<String, List<Integer>> entry : playerList.entrySet())
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
ResultSet resultSet = statement.getResultSet();
|
|
|
|
|
|
|
|
while (resultSet.next())
|
|
|
|
{
|
|
|
|
for (Integer taskId : entry.getValue())
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
if (!playerIdList.containsKey(resultSet.getInt(1)))
|
|
|
|
playerIdList.put(resultSet.getInt(1), new ArrayList<Integer>());
|
2015-03-25 23:10:59 +01:00
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
playerIdList.get(resultSet.getInt(1)).add(taskId);
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 09:00:37 +02:00
|
|
|
statement.getMoreResults();
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
|
2015-05-13 09:00:37 +02:00
|
|
|
preparedStatement = _connection.prepareStatement(ADD_ACCOUNT_TASK);
|
|
|
|
System.out.println("adding to mysql db.");
|
|
|
|
for (Entry<Integer, List<Integer>> entry : playerIdList.entrySet())
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
for (Integer taskId : entry.getValue())
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-05-13 09:00:37 +02:00
|
|
|
preparedStatement.setInt(1, entry.getKey());
|
|
|
|
preparedStatement.setInt(2, taskId);
|
|
|
|
preparedStatement.addBatch();
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 09:00:37 +02:00
|
|
|
|
|
|
|
preparedStatement.executeBatch();
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (preparedStatement != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
preparedStatement.close();
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|