Updates for task conversion.

This commit is contained in:
Jonathan Williams 2015-05-13 00:00:37 -07:00
parent aee0952c17
commit 82903fde47
13 changed files with 757 additions and 427 deletions

View File

@ -0,0 +1,7 @@
package mineplex.chestConverter;
public class AccountTask
{
public String Task;
public String UUID;
}

View File

@ -0,0 +1,63 @@
package mineplex.chestConverter;
import mineplex.core.common.util.Callback;
public class AsyncJsonWebCall extends JsonWebCall
{
public AsyncJsonWebCall(String url)
{
super(url);
}
public void Execute()
{
Thread asyncThread = new Thread(new Runnable()
{
public void run()
{
AsyncJsonWebCall.super.Execute();
}
});
asyncThread.start();
}
public void Execute(final Object argument)
{
Thread asyncThread = new Thread(new Runnable()
{
public void run()
{
AsyncJsonWebCall.super.Execute(argument);
}
});
asyncThread.start();
}
public <T> void Execute(final Class<T> callbackClass, final Callback<T> callback)
{
Thread asyncThread = new Thread(new Runnable()
{
public void run()
{
AsyncJsonWebCall.super.Execute(callbackClass, callback);
}
});
asyncThread.start();
}
public <T> void Execute(final Class<T> callbackClass, final Callback<T> callback, final Object argument)
{
Thread asyncThread = new Thread(new Runnable()
{
public void run()
{
AsyncJsonWebCall.super.Execute(callbackClass, callback, argument);
}
});
asyncThread.start();
}
}

View File

@ -1,8 +1,10 @@
package mineplex.chestConverter; package mineplex.chestConverter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
public class ChestConverter public class ChestConverter
@ -13,29 +15,41 @@ public class ChestConverter
public static void main (String args[]) public static void main (String args[])
{ {
_repository = new ChestConverterRepository(); _repository = new ChestConverterRepository();
int count = 5000; int count = 0;
int batchAmount = 5000;
HashMap<String, Integer> tasks = _repository.getTaskList();
while (true) while (true)
{ {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
HashMap<String, Integer> playerMap = _repository.retrieveKeyInventoryBatch(count); HashMap<String, List<Integer>> playerMap = new HashMap<String, List<Integer>>();
if (playerMap.size() == 0)
List<AccountTask> taskList = _repository.getTasks(count, batchAmount);
if (taskList.size() == 0)
return; return;
_repository.incrementClient(playerMap, false); for (AccountTask task : taskList)
_repository.deleteKeys(count); {
if (!playerMap.containsKey(task.UUID))
playerMap.put(task.UUID, new ArrayList<Integer>());
playerMap.get(task.UUID).add(tasks.get(task.Task));
}
_repository.incrementClients(playerMap);
try try
{ {
log("Natural sleep. " + count + " took " + (System.currentTimeMillis() - time) / 1000 + " seconds."); count += batchAmount;
Thread.sleep(250); log("Natural sleep. " + batchAmount + " took " + (System.currentTimeMillis() - time) / 1000 + " seconds. Count = " + + count);
Thread.sleep(100);
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
break;
} }
} }

View File

@ -5,23 +5,20 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
public class ChestConverterRepository public class ChestConverterRepository
{ {
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Account"; private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Account?allowMultiQueries=true";
private String _userName = "root"; private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh"; private String _password = "tAbechAk3wR7tuTh";
private static String RETRIEVE_CHESTS = "SELECT A.uuid, count FROM accountInventory INNER JOIN accounts AS A ON A.id = accountInventory.accountId WHERE itemId = 56 AND count > 0 ORDER BY accountInventory.id LIMIT ?;"; private static String ADD_ACCOUNT_TASK = "INSERT INTO accountTasks (accountId, taskId) VALUES (?, ?);";
private static String RETRIEVE_KEYS = "SELECT A.uuid, count FROM accountInventory INNER JOIN accounts AS A ON A.id = accountInventory.accountId WHERE itemId = 67 AND count > 0 ORDER BY accountInventory.id LIMIT ?;"; private static String RETRIEVE_TASKS = "SELECT id, name FROM tasks;";
private static String DELETE_CHESTS = "DELETE FROM accountInventory WHERE itemId = 56 ORDER BY accountInventory.id LIMIT ?";
private static String DELETE_KEYS = "DELETE FROM accountInventory WHERE itemId = 67 ORDER BY accountInventory.id LIMIT ?";
private static String INSERT_CLIENT_INVENTORY = "INSERT INTO accountInventory (accountId, itemId, count) SELECT accounts.id, ?, ? FROM accounts WHERE accounts.uuid = ? ON DUPLICATE KEY UPDATE count=count + VALUES(count);";
private static String UPDATE_CLIENT_INVENTORY = "UPDATE accountInventory AS AI INNER JOIN accounts ON AI.accountId = accounts.id SET AI.count = AI.count + ? WHERE accounts.uuid = ? AND AI.itemId = ?;";
private static Connection _connection; private static Connection _connection;
@ -56,10 +53,9 @@ public class ChestConverterRepository
} }
} }
public HashMap<String, Integer> retrieveChestInventoryBatch(int count) public HashMap<String, Integer> getTaskList()
{ {
HashMap<String, Integer> playerList = new HashMap<String, Integer>(); HashMap<String, Integer> tasks = new HashMap<String, Integer>();
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try try
@ -67,13 +63,14 @@ public class ChestConverterRepository
if (_connection == null || _connection.isClosed()) if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password); _connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(RETRIEVE_CHESTS); preparedStatement = _connection.prepareStatement(RETRIEVE_TASKS);
preparedStatement.setInt(1, count); preparedStatement.execute();
ResultSet resultSet = preparedStatement.executeQuery();
ResultSet resultSet = preparedStatement.getResultSet();
while (resultSet.next()) while (resultSet.next())
{ {
playerList.put(resultSet.getString(1), resultSet.getInt(2)); tasks.put(resultSet.getString(2), resultSet.getInt(1));
} }
} }
catch (Exception exception) catch (Exception exception)
@ -95,131 +92,62 @@ public class ChestConverterRepository
} }
} }
return playerList; return tasks;
} }
public HashMap<String, Integer> retrieveKeyInventoryBatch(int count) public List<AccountTask> getTasks(int skip, int count)
{ {
HashMap<String, Integer> playerList = new HashMap<String, Integer>(); return new JsonWebCall("http://accounts.mineplex.com/PlayerAccount/GetTasksByCount").Execute(new com.google.gson.reflect.TypeToken<List<AccountTask>>(){}.getType(), new SearchConf(skip, count));
}
public void incrementClients(HashMap<String, List<Integer>> playerList)
{
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
Statement statement = null;
try try
{ {
if (_connection == null || _connection.isClosed()) if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password); _connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(RETRIEVE_KEYS); statement = _connection.createStatement();
preparedStatement.setInt(1, count); HashMap<Integer, List<Integer>> playerIdList = new HashMap<Integer, List<Integer>>();
ResultSet resultSet = preparedStatement.executeQuery(); String queryString = "";
for (Entry<String, List<Integer>> entry : playerList.entrySet())
while (resultSet.next())
{ {
playerList.put(resultSet.getString(1), resultSet.getInt(2)); queryString += "SELECT id FROM accounts WHERE accounts.uuid = '" + entry.getKey() + "' LIMIT 1;";
} }
}
catch (Exception exception) statement.execute(queryString);
{ statement.getUpdateCount();
exception.printStackTrace();
} for (Entry<String, List<Integer>> entry : playerList.entrySet())
finally
{
if (preparedStatement != null)
{ {
try ResultSet resultSet = statement.getResultSet();
while (resultSet.next())
{ {
preparedStatement.close(); for (Integer taskId : entry.getValue())
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return playerList;
}
public void incrementClient(HashMap<String, Integer> playerList, boolean chest)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(UPDATE_CLIENT_INVENTORY);
for (Entry<String, Integer> entry : playerList.entrySet())
{
preparedStatement.setInt(1, entry.getValue());
preparedStatement.setString(2, entry.getKey());
preparedStatement.setInt(3, chest ? 690 : 692);
preparedStatement.addBatch();
if (chest)
{
if (entry.getValue() > 20)
{ {
preparedStatement.setInt(1, 1); if (!playerIdList.containsKey(resultSet.getInt(1)))
preparedStatement.setString(2, entry.getKey()); playerIdList.put(resultSet.getInt(1), new ArrayList<Integer>());
preparedStatement.setInt(3, 692);
preparedStatement.addBatch(); playerIdList.get(resultSet.getInt(1)).add(taskId);
}
if (entry.getValue() > 50)
{
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, entry.getKey());
preparedStatement.setInt(3, 691);
preparedStatement.addBatch();
} }
} }
statement.getMoreResults();
} }
int[] rowsAffected = preparedStatement.executeBatch(); preparedStatement = _connection.prepareStatement(ADD_ACCOUNT_TASK);
int i = 0; System.out.println("adding to mysql db.");
for (Entry<Integer, List<Integer>> entry : playerIdList.entrySet())
preparedStatement.close();
preparedStatement = _connection.prepareStatement(INSERT_CLIENT_INVENTORY);
for (Entry<String, Integer> entry : playerList.entrySet())
{ {
if (rowsAffected[i] < 1) for (Integer taskId : entry.getValue())
{ {
preparedStatement.setInt(1, chest ? 690 : 692); preparedStatement.setInt(1, entry.getKey());
preparedStatement.setInt(2, entry.getValue()); preparedStatement.setInt(2, taskId);
preparedStatement.setString(3, entry.getKey());
preparedStatement.addBatch(); preparedStatement.addBatch();
if (chest)
{
if (entry.getValue() > 20)
{
preparedStatement.setInt(1, 692);
preparedStatement.setInt(2, 1);
preparedStatement.setString(3, entry.getKey());
preparedStatement.addBatch();
}
if (entry.getValue() > 50)
{
preparedStatement.setInt(1, 691);
preparedStatement.setInt(2, 1);
preparedStatement.setString(3, entry.getKey());
preparedStatement.addBatch();
}
}
} }
i++;
} }
preparedStatement.executeBatch(); preparedStatement.executeBatch();
@ -243,74 +171,4 @@ public class ChestConverterRepository
} }
} }
} }
public void deleteChests(int count)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(DELETE_CHESTS);
preparedStatement.setInt(1, count);
preparedStatement.executeUpdate();
System.out.println("Deleting " + count + " inventory records.");
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public void deleteKeys(int count)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(DELETE_KEYS);
preparedStatement.setInt(1, count);
preparedStatement.executeUpdate();
System.out.println("Deleting " + count + " inventory records.");
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
} }

View File

@ -0,0 +1,359 @@
package mineplex.chestConverter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilSystem;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import com.google.gson.Gson;
public class JsonWebCall
{
private String _url;
private PoolingClientConnectionManager _connectionManager;
public JsonWebCall(String url)
{
_url = url;
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
_connectionManager = new PoolingClientConnectionManager(schemeRegistry);
_connectionManager.setMaxTotal(200);
_connectionManager.setDefaultMaxPerRoute(20);
}
public String ExecuteReturnStream(Object argument)
{
HttpClient httpClient = new DefaultHttpClient(_connectionManager);
InputStream in = null;
String result = null;
try
{
HttpResponse response;
Gson gson = new Gson();
HttpPost request = new HttpPost(_url);
if (argument != null)
{
StringEntity params = new StringEntity(gson.toJson(argument));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(params);
}
response = httpClient.execute(request);
if (response != null)
{
in = response.getEntity().getContent();
result = convertStreamToString(in);
}
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
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 result;
}
public void Execute()
{
Execute((Object)null);
}
public void Execute(Object argument)
{
HttpClient httpClient = new DefaultHttpClient(_connectionManager);
InputStream in = null;
try
{
Gson gson = new Gson();
HttpPost request = new HttpPost(_url);
if (argument != null)
{
StringEntity params = new StringEntity(gson.toJson(argument));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(params);
}
httpClient.execute(request);
}
catch (Exception ex)
{
System.out.println("JsonWebCall.Execute() 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();
}
}
}
}
public <T> T Execute(Class<T> returnClass)
{
return Execute(returnClass, (Object)null);
}
public <T> T Execute(Type returnType, Object argument)
{
HttpClient httpClient = new DefaultHttpClient(_connectionManager);
InputStream in = null;
T returnData = null;
String result = null;
try
{
HttpResponse response;
Gson gson = new Gson();
HttpPost request = new HttpPost(_url);
if (argument != null)
{
StringEntity params = new StringEntity(gson.toJson(argument));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(params);
}
response = httpClient.execute(request);
if (response != null)
{
in = response.getEntity().getContent();
result = convertStreamToString(in);
returnData = new Gson().fromJson(result, returnType);
}
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
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 returnData;
}
public <T> T Execute(Class<T> returnClass, Object argument)
{
HttpClient httpClient = new DefaultHttpClient(_connectionManager);
InputStream in = null;
T returnData = null;
String result = null;
try
{
HttpResponse response;
Gson gson = new Gson();
HttpPost request = new HttpPost(_url);
if (argument != null)
{
StringEntity params = new StringEntity(gson.toJson(argument));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(params);
}
response = httpClient.execute(request);
if (response != null)
{
in = response.getEntity().getContent();
result = convertStreamToString(in);
returnData = new Gson().fromJson(result, returnClass);
}
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
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 returnData;
}
public <T> void Execute(Class<T> callbackClass, Callback<T> callback)
{
Execute(callbackClass, callback, (Object)null);
}
public <T> void Execute(Class<T> callbackClass, Callback<T> callback, Object argument)
{
HttpClient httpClient = new DefaultHttpClient(_connectionManager);
InputStream in = null;
String result = null;
try
{
HttpResponse response;
Gson gson = new Gson();
HttpPost request = new HttpPost(_url);
if (argument != null)
{
StringEntity params = new StringEntity(gson.toJson(argument));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(params);
}
response = httpClient.execute(request);
if (response != null && callback != null)
{
in = response.getEntity().getContent();
result = convertStreamToString(in);
callback.run(new Gson().fromJson(result, callbackClass));
}
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
UtilSystem.printStackTrace(ex.getStackTrace());
System.out.println("Result: \n" + result);
}
finally
{
httpClient.getConnectionManager().shutdown();
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
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();
}
}

View File

@ -0,0 +1,13 @@
package mineplex.chestConverter;
public class SearchConf
{
public SearchConf(int skip, int count)
{
Skip = skip;
Count = count;
}
public int Skip;
public int Count;
}

View File

@ -76,6 +76,7 @@
<Compile Include="Model\Server\GameServer\MineKart\MineKart.cs" /> <Compile Include="Model\Server\GameServer\MineKart\MineKart.cs" />
<Compile Include="Model\Server\GameServer\MineKart\MineKartStats.cs" /> <Compile Include="Model\Server\GameServer\MineKart\MineKartStats.cs" />
<Compile Include="Model\Server\PvpServer\Weapon.cs" /> <Compile Include="Model\Server\PvpServer\Weapon.cs" />
<Compile Include="SearchConf.cs" />
<Compile Include="Tokens\AccountBatchToken.cs" /> <Compile Include="Tokens\AccountBatchToken.cs" />
<Compile Include="Tokens\AccountNameToken.cs" /> <Compile Include="Tokens\AccountNameToken.cs" />
<Compile Include="Tokens\AccountTask.cs" /> <Compile Include="Tokens\AccountTask.cs" />

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LOC.Core
{
public class SearchConf
{
public int Skip { get; set; }
public int Count { get; set; }
}
}

View File

@ -72,13 +72,13 @@
return accounts; return accounts;
} }
public List<AccountTask> GetTasksByCount(int count) public List<AccountTask> GetTasksByCount(SearchConf searchConf)
{ {
var tasks = new List<AccountTask>(); var tasks = new List<AccountTask>();
using (var repository = _repositoryFactory.CreateRepository()) using (var repository = _repositoryFactory.CreateRepository())
{ {
var gameTasks = repository.GetAll<GameTask>().OrderBy(x => x.GameTaskId).Take(count).Include(x => x.Account).ToList(); var gameTasks = repository.GetAll<GameTask>().OrderBy(x => x.GameTaskId).Skip(searchConf.Skip).Take(searchConf.Count).Include(x => x.Account).ToList();
foreach (var task in gameTasks) foreach (var task in gameTasks)
{ {

View File

@ -6,6 +6,7 @@
using Core.Model.Sales; using Core.Model.Sales;
using Core.Tokens; using Core.Tokens;
using Core.Tokens.Client; using Core.Tokens.Client;
using LOC.Core;
public interface IAccountAdministrator public interface IAccountAdministrator
{ {
@ -42,6 +43,6 @@
ClientToken GetAccountByUUID(string uuid); ClientToken GetAccountByUUID(string uuid);
List<AccountTask> GetTasksByCount(int count); List<AccountTask> GetTasksByCount(SearchConf searchConf);
} }
} }

View File

@ -9,6 +9,7 @@
using Core.Tokens.Client; using Core.Tokens.Client;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Collections.Generic; using System.Collections.Generic;
using LOC.Core;
public class PlayerAccountController : Controller public class PlayerAccountController : Controller
{ {
@ -40,9 +41,9 @@
} }
[HttpPost] [HttpPost]
public ActionResult GetTasksByCount(int count) public ActionResult GetTasksByCount(SearchConf searchConf)
{ {
var tasks = _accountAdministrator.GetTasksByCount(count); var tasks = _accountAdministrator.GetTasksByCount(searchConf);
var json = JsonConvert.SerializeObject(tasks); var json = JsonConvert.SerializeObject(tasks);
return Content(json, "application/json"); return Content(json, "application/json");

File diff suppressed because it is too large Load Diff

Binary file not shown.