Clean up Account Repository, attempt to load account ID from cache

This commit is contained in:
Shaun Bennett 2016-03-23 10:21:02 +11:00
parent a107c38f28
commit 309b5464f2
3 changed files with 71 additions and 111 deletions

View File

@ -48,13 +48,6 @@ public class PlayerCache
try
{
PlayerInfo playerInfo = _repository.getElement(uuid.toString());
System.out.println("Got playerInfo: " + playerInfo);
if (playerInfo != null)
{
System.out.println("account id: " + playerInfo.getAccountId());
System.out.println("name: " + playerInfo.getName());
}
return playerInfo;
}
catch (Exception exception)
@ -65,6 +58,17 @@ public class PlayerCache
return null;
}
/**
* Attempts to grab a player's account ID from the cache
* @param uuid Minecraft Account UUID
* @return The account id of the player, or -1 if the player is not in the cache
*/
public int getAccountId(UUID uuid)
{
PlayerInfo info = getPlayer(uuid);
return info == null ? -1 : info.getAccountId();
}
public void clean()
{

View File

@ -1,8 +1,8 @@
package mineplex.core.account;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
@ -49,9 +49,8 @@ public class CoreClientManager extends MiniPlugin
private NautHashMap<String, CoreClient> _clientList;
private HashSet<String> _duplicateLoginGlitchPreventionList;
private NautHashMap<String, ILoginProcessor> _loginProcessors = new NautHashMap<String, ILoginProcessor>();
private LinkedList<IQuerylessLoginProcessor> _querylessLoginProcessors = new LinkedList<IQuerylessLoginProcessor>();
private List<ILoginProcessor> _loginProcessors = new ArrayList<>();
private Object _clientLock = new Object();
private static AtomicInteger _clientsConnecting = new AtomicInteger(0);
@ -260,7 +259,7 @@ public class CoreClientManager extends MiniPlugin
CoreClient client = Add(playerName);
client.SetRank(Rank.valueOf(token.Rank), false);
client.setAccountId(_repository.login(_loginProcessors, _querylessLoginProcessors, uuid.toString(), client.GetPlayerName()));
client.setAccountId(_repository.login(_loginProcessors, uuid, client.GetPlayerName()));
// JSON sql response
Bukkit.getServer().getPluginManager().callEvent(new ClientWebResponseEvent(response, uuid));
@ -332,7 +331,7 @@ public class CoreClientManager extends MiniPlugin
CoreClient client = Add(playerName);
client.SetRank(Rank.valueOf(token.Rank), false);
client.setAccountId(_repository.login(_loginProcessors, _querylessLoginProcessors, uuid.toString(), client.GetPlayerName()));
client.setAccountId(_repository.login(_loginProcessors, uuid, client.GetPlayerName()));
// JSON sql response
Bukkit.getServer().getPluginManager().callEvent(new ClientWebResponseEvent(response, uuid));
@ -373,12 +372,12 @@ public class CoreClientManager extends MiniPlugin
_clientLoginLock.put(client.GetPlayerName(), new Object());
ClientToken token = null;
Gson gson = new Gson();
runAsync(new Runnable()
{
public void run()
{
client.setAccountId(_repository.login(_loginProcessors, _querylessLoginProcessors, uuid.toString(), client.GetPlayerName()));
client.setAccountId(_repository.login(_loginProcessors, uuid, client.GetPlayerName()));
_clientLoginLock.remove(client.GetPlayerName());
}
});
@ -386,7 +385,8 @@ public class CoreClientManager extends MiniPlugin
TimingManager.start(client.GetPlayerName() + " GetClient.");
String response = _repository.GetClient(client.GetPlayerName(), uuid, ipAddress);
TimingManager.stop(client.GetPlayerName() + " GetClient.");
TimingManager.start(client.GetPlayerName() + " Event.");
token = gson.fromJson(response, ClientToken.class);
client.SetRank(Rank.valueOf(token.Rank), false);
@ -395,7 +395,9 @@ public class CoreClientManager extends MiniPlugin
// JSON sql response
Bukkit.getServer().getPluginManager().callEvent(new ClientWebResponseEvent(response, uuid));
TimingManager.stop(client.GetPlayerName() + " Event.");
TimingManager.start(client.GetPlayerName() + " While Loop.");
while (_clientLoginLock.containsKey(client.GetPlayerName()) && System.currentTimeMillis() - timeStart < 15000)
{
try
@ -407,6 +409,7 @@ public class CoreClientManager extends MiniPlugin
e.printStackTrace();
}
}
TimingManager.stop(client.GetPlayerName() + " While Loop.");
if (_clientLoginLock.containsKey(client.GetPlayerName()))
{
@ -646,14 +649,9 @@ public class CoreClientManager extends MiniPlugin
public void addStoredProcedureLoginProcessor(ILoginProcessor processor)
{
_loginProcessors.put(processor.getName(), processor);
_loginProcessors.add(processor);
}
public void addStoredProcedureLoginProcessor(IQuerylessLoginProcessor processor)
{
_querylessLoginProcessors.add(processor);
}
public boolean hasRank(Player player, Rank rank)
{
CoreClient client = Get(player);
@ -662,10 +660,4 @@ public class CoreClientManager extends MiniPlugin
return client.GetRank().has(rank);
}
public int getCachedClientAccountId(UUID uuid)
{
PlayerInfo playerInfo = PlayerCache.getInstance().getPlayer(uuid);
return playerInfo == null ? -1 : playerInfo.getAccountId();
}
}

View File

@ -6,30 +6,28 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import mineplex.core.database.MinecraftRepository;
import org.bukkit.Bukkit;
import com.google.gson.reflect.TypeToken;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.cache.player.PlayerCache;
import mineplex.core.account.ILoginProcessor;
import mineplex.core.account.IQuerylessLoginProcessor;
import mineplex.core.account.repository.token.LoginToken;
import mineplex.core.account.repository.token.RankUpdateToken;
import mineplex.core.common.Rank;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.MinecraftRepository;
import mineplex.core.server.remotecall.JsonWebCall;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.DatabaseRunnable;
import mineplex.serverdata.database.RepositoryBase;
import mineplex.serverdata.database.ResultSetCallable;
import mineplex.serverdata.database.column.ColumnBoolean;
import mineplex.serverdata.database.column.ColumnTimestamp;
import mineplex.serverdata.database.column.ColumnVarChar;
import mineplex.core.server.remotecall.JsonWebCall;
public class AccountRepository extends MinecraftRepository
{
@ -59,100 +57,66 @@ public class AccountRepository extends MinecraftRepository
//executeUpdate(CREATE_ACCOUNT_TABLE);
}
public int login(NautHashMap<String, ILoginProcessor> loginProcessors, LinkedList<IQuerylessLoginProcessor> querylessLoginProcessors, String uuid, String name)
public int login(final List<ILoginProcessor> loginProcessors, final UUID uuid, final String name)
{
// First we try to grab the account id from cache - this saves an extra trip to database
int accountId = -1;
try (
Connection connection = getConnection();
Statement statement = connection.createStatement()
)
try (Connection connection = getConnection(); Statement statement = connection.createStatement())
{
statement.execute("SELECT id FROM accounts WHERE accounts.uuid = '" + uuid + "' LIMIT 1;");
ResultSet resultSet = statement.getResultSet();
while (resultSet.next())
int cachedId = PlayerCache.getInstance().getAccountId(uuid);
if (cachedId > 0)
{
accountId = resultSet.getInt(1);
accountId = cachedId;
System.out.println("Loaded Account ID From Cache [" + name + " - " + accountId + "]");
}
if (accountId == -1)
else
{
final List<Integer> tempList = new ArrayList<Integer>(1);
executeInsert(ACCOUNT_LOGIN_NEW, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while (resultSet.next())
{
tempList.add(resultSet.getInt(1));
}
}
},new ColumnVarChar("uuid", 100, uuid), new ColumnVarChar("name", 100, name));
accountId = tempList.get(0);
}
/*
boolean statementStatus = statement.execute(
"UPDATE accounts SET name='" + name + "', lastLogin=now() WHERE accounts.uuid = '" + uuid + "';"
+ "SELECT games, visibility, showChat, friendChat, privateMessaging, partyRequests, invisibility, forcefield, showMacReports, ignoreVelocity, pendingFriendRequests FROM accountPreferences WHERE accountPreferences.uuid = '" + uuid + "' LIMIT 1;"
+ "SELECT items.name, ic.name as category, count FROM accountInventory AS ai INNER JOIN items ON items.id = ai.itemId INNER JOIN itemCategories AS ic ON ic.id = items.categoryId INNER JOIN accounts ON accounts.id = ai.accountId WHERE accounts.uuid = '" + uuid + "';"
+ "SELECT benefit FROM rankBenefits WHERE rankBenefits.uuid = '" + uuid + "';"
+ "SELECT stats.name, value FROM accountStats INNER JOIN stats ON stats.id = accountStats.statId INNER JOIN accounts ON accountStats.accountId = accounts.id WHERE accounts.uuid = '" + uuid + "';"
+ "SELECT tA.Name, status, serverName, tA.lastLogin, now() FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget LEFT JOIN playerMap ON tA.name = playerName WHERE uuidSource = '" + uuid + "';"
+ "SELECT gameType, elo FROM eloRating WHERE uuid = '" + uuid + "';"
);
*/
// Player was not found in cache, we need to grab the account id from database
statement.execute("SELECT id FROM accounts WHERE accounts.uuid = '" + uuid + "' LIMIT 1;");
ResultSet resultSet = statement.getResultSet();
String loginString = "UPDATE accounts SET name='" + name + "', lastLogin=now() WHERE id = '" + accountId + "';";
for (ILoginProcessor loginProcessor : loginProcessors.values())
{
loginString += loginProcessor.getQuery(accountId, uuid, name);
}
statement.execute(loginString);
/*
while (true)
{
if (statementStatus)
if (resultSet.next())
{
System.out.println("ResultSet : " + statement.getResultSet().getMetaData().getColumnCount() + " columns:");
for (int i = 0; i < statement.getResultSet().getMetaData().getColumnCount(); i++)
{
System.out.println(statement.getResultSet().getMetaData().getColumnName(i + 1));
}
accountId = resultSet.getInt(1);
}
else
{
if (statement.getUpdateCount() == -1)
break;
// Player doesn't exist in our database, add them to the accounts table
final List<Integer> tempList = new ArrayList<Integer>(1);
System.out.println("Update statement : " + statement.getUpdateCount() + " rows affected.");
executeInsert(ACCOUNT_LOGIN_NEW, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while (resultSet.next())
{
tempList.add(resultSet.getInt(1));
}
}
}, new ColumnVarChar("uuid", 100, uuid.toString()), new ColumnVarChar("name", 100, name));
accountId = tempList.get(0);
}
statementStatus = statement.getMoreResults();
}
System.out.println("Done");
*/
final int finalId = accountId;
final String uuidString = uuid.toString();
String loginString = "UPDATE accounts SET name='" + name + "', lastLogin=now() WHERE id = '" + accountId + "';";
// We can use a parallel stream because they will be in the correct order when we collect
loginString += loginProcessors.parallelStream().map(processor -> processor.getQuery(finalId, uuidString, name)).collect(Collectors.joining());
statement.execute(loginString);
statement.getUpdateCount();
statement.getMoreResults();
for (ILoginProcessor loginProcessor : loginProcessors.values())
{
loginProcessor.processLoginResultSet(name, accountId, statement.getResultSet());
statement.getMoreResults();
}
for (IQuerylessLoginProcessor loginProcessor : querylessLoginProcessors)
for (ILoginProcessor loginProcessor : loginProcessors)
{
loginProcessor.processLogin(name, accountId);
loginProcessor.processLoginResultSet(name, finalId, statement.getResultSet());
statement.getMoreResults();
}
}
catch (Exception exception)