Mineplex2018-withcommit/Plugins/Mineplex.ChestConverter/src/mineplex/chestConverter/ChestConverterRepository.java

175 lines
4.2 KiB
Java
Raw Normal View History

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;
import java.util.HashMap;
2015-05-13 09:00:37 +02:00
import java.util.List;
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";
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;";
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-05-13 09:00:37 +02:00
HashMap<String, Integer> tasks = new HashMap<String, Integer>();
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-05-13 09:00:37 +02:00
ResultSet resultSet = preparedStatement.getResultSet();
while (resultSet.next())
{
2015-05-13 09:00:37 +02:00
tasks.put(resultSet.getString(2), resultSet.getInt(1));
}
}
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;
}
public List<AccountTask> getTasks(int lastId, int count) throws Exception
{
return new JsonWebCall("http://accounts.mineplex.com/PlayerAccount/GetTasksByCount").Execute(new com.google.gson.reflect.TypeToken<List<AccountTask>>(){}.getType(), new SearchConf(lastId, count));
}
2015-05-13 09:00:37 +02:00
public void incrementClients(HashMap<String, List<Integer>> playerList)
{
PreparedStatement preparedStatement = null;
2015-05-13 09:00:37 +02:00
Statement statement = null;
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-05-13 09:00:37 +02:00
queryString += "SELECT id FROM accounts WHERE accounts.uuid = '" + entry.getKey() + "' LIMIT 1;";
}
2015-05-13 09:00:37 +02:00
statement.execute(queryString);
statement.getUpdateCount();
2015-05-13 09:00:37 +02:00
for (Entry<String, List<Integer>> entry : playerList.entrySet())
{
2015-05-13 09:00:37 +02:00
ResultSet resultSet = statement.getResultSet();
while (resultSet.next())
{
for (Integer taskId : entry.getValue())
{
2015-05-13 09:00:37 +02:00
if (!playerIdList.containsKey(resultSet.getInt(1)))
playerIdList.put(resultSet.getInt(1), new ArrayList<Integer>());
2015-05-13 09:00:37 +02:00
playerIdList.get(resultSet.getInt(1)).add(taskId);
}
}
2015-05-13 09:00:37 +02:00
statement.getMoreResults();
}
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-05-13 09:00:37 +02:00
for (Integer taskId : entry.getValue())
{
2015-05-13 09:00:37 +02:00
preparedStatement.setInt(1, entry.getKey());
preparedStatement.setInt(2, taskId);
preparedStatement.addBatch();
}
}
2015-05-13 09:00:37 +02:00
preparedStatement.executeBatch();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}