- Made SQL calls async

This commit is contained in:
AlexTheCoder 2016-04-28 20:59:49 -04:00
parent 156982460f
commit e032b81842

View File

@ -38,22 +38,31 @@ public class EloRepository extends MinecraftRepository
public boolean saveElo(int accountId, int gameType, int oldElo, int elo) throws SQLException public boolean saveElo(int accountId, int gameType, int oldElo, int elo) throws SQLException
{ {
boolean updateSucceeded = false; final List<Boolean> ret = new ArrayList<Boolean>();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
// If we're increasing in elo we verify the server version matches the database version (prevent d/c and double wins with concurrent matches)
// Otherwise we always take their elo down if they lose.
if (elo > oldElo)
updateSucceeded = executeUpdate(UPDATE_ELO_ONLY_IF_MATCH, new ColumnInt("elo", elo - oldElo), new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType), new ColumnInt("elo", oldElo)) > 0;
else
{ {
updateSucceeded = executeUpdate(UPDATE_ELO, new ColumnInt("elo", elo - oldElo), new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType)) > 0; public void run()
{
if (!updateSucceeded && executeUpdate(INSERT_ELO, new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType), new ColumnInt("elo", elo)) > 0) boolean updateSucceeded = false;
updateSucceeded = true;
} // If we're increasing in elo we verify the server version matches the database version (prevent d/c and double wins with concurrent matches)
// Otherwise we always take their elo down if they lose.
if (elo > oldElo)
updateSucceeded = executeUpdate(UPDATE_ELO_ONLY_IF_MATCH, new ColumnInt("elo", elo - oldElo), new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType), new ColumnInt("elo", oldElo)) > 0;
else
{
updateSucceeded = executeUpdate(UPDATE_ELO, new ColumnInt("elo", elo - oldElo), new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType)) > 0;
if (!updateSucceeded && executeUpdate(INSERT_ELO, new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType), new ColumnInt("elo", elo)) > 0)
updateSucceeded = true;
}
}
});
return updateSucceeded; if (ret.isEmpty())
ret.add(false);
return ret.get(0);
} }
public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException
@ -71,17 +80,23 @@ public class EloRepository extends MinecraftRepository
public long getStrikeExpiry(int accountId) public long getStrikeExpiry(int accountId)
{ {
final List<Long> expire = new ArrayList<Long>(); final List<Long> expire = new ArrayList<Long>();
executeQuery(GRAB_STRIKE_EXPIRY, new ResultSetCallable() Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{ {
@Override public void run()
public void processResultSet(ResultSet resultSet) throws SQLException
{ {
while(resultSet.next()) executeQuery(GRAB_STRIKE_EXPIRY, new ResultSetCallable()
{ {
expire.add(resultSet.getLong(1)); @Override
} public void processResultSet(ResultSet resultSet) throws SQLException
{
while(resultSet.next())
{
expire.add(resultSet.getLong(1));
}
}
}, new ColumnInt("accountId", accountId));
} }
}, new ColumnInt("accountId", accountId)); });
if (expire.isEmpty()) if (expire.isEmpty())
expire.add(System.currentTimeMillis() - 5555); expire.add(System.currentTimeMillis() - 5555);
@ -92,17 +107,23 @@ public class EloRepository extends MinecraftRepository
public long getBanExpiry(int accountId) public long getBanExpiry(int accountId)
{ {
final List<Long> expire = new ArrayList<Long>(); final List<Long> expire = new ArrayList<Long>();
executeQuery(GRAB_BAN_EXPIRY, new ResultSetCallable() Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{ {
@Override public void run()
public void processResultSet(ResultSet resultSet) throws SQLException
{ {
while(resultSet.next()) executeQuery(GRAB_BAN_EXPIRY, new ResultSetCallable()
{ {
expire.add(resultSet.getLong(1)); @Override
} public void processResultSet(ResultSet resultSet) throws SQLException
{
while(resultSet.next())
{
expire.add(resultSet.getLong(1));
}
}
}, new ColumnInt("accountId", accountId));
} }
}, new ColumnInt("accountId", accountId)); });
if (expire.isEmpty()) if (expire.isEmpty())
expire.add(System.currentTimeMillis() - 5555); expire.add(System.currentTimeMillis() - 5555);
@ -113,17 +134,23 @@ public class EloRepository extends MinecraftRepository
public int getStrikes(int accountId) public int getStrikes(int accountId)
{ {
final List<Integer> strike = new ArrayList<Integer>(); final List<Integer> strike = new ArrayList<Integer>();
executeQuery(GRAB_STRIKES, new ResultSetCallable() Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{ {
@Override public void run()
public void processResultSet(ResultSet resultSet) throws SQLException
{ {
while(resultSet.next()) executeQuery(GRAB_STRIKES, new ResultSetCallable()
{ {
strike.add(resultSet.getInt(1)); @Override
} public void processResultSet(ResultSet resultSet) throws SQLException
{
while(resultSet.next())
{
strike.add(resultSet.getInt(1));
}
}
}, new ColumnInt("accountId", accountId));
} }
}, new ColumnInt("accountId", accountId)); });
if (strike.isEmpty()) if (strike.isEmpty())
strike.add(0); strike.add(0);
@ -164,15 +191,28 @@ public class EloRepository extends MinecraftRepository
minutes = 240; minutes = 240;
break; break;
} }
long banEnd = System.currentTimeMillis() + UtilTime.convert(minutes, TimeUnit.MINUTES, TimeUnit.MILLISECONDS); final long banEnd = System.currentTimeMillis() + UtilTime.convert(minutes, TimeUnit.MINUTES, TimeUnit.MILLISECONDS);
long strikesExpire = System.currentTimeMillis() + UtilTime.convert(1, TimeUnit.DAYS, TimeUnit.MILLISECONDS); final long strikesExpire = System.currentTimeMillis() + UtilTime.convert(1, TimeUnit.DAYS, TimeUnit.MILLISECONDS);
int newStrikes = Math.min(getStrikes(accountId) + 1, 8); final int newStrikes = Math.min(getStrikes(accountId) + 1, 8);
executeUpdate(UPDATE_BAN, new ColumnInt("accountId", accountId), new ColumnInt("strikes", newStrikes), new ColumnLong("strikesExpire", strikesExpire), new ColumnLong("banEnd", banEnd));
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
executeUpdate(UPDATE_BAN, new ColumnInt("accountId", accountId), new ColumnInt("strikes", newStrikes), new ColumnLong("strikesExpire", strikesExpire), new ColumnLong("banEnd", banEnd));
}
});
} }
public void resetStrikes(int accountId) public void resetStrikes(int accountId)
{ {
executeUpdate(DELETE_STRIKES, new ColumnInt("accountId", accountId)); Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
executeUpdate(DELETE_STRIKES, new ColumnInt("accountId", accountId));
}
});
} }
@Override @Override