Fix ELO query

This commit is contained in:
TadahTech 2016-04-28 21:09:28 -05:00
parent 80ea7b7a4c
commit 84ca13c0d3

View File

@ -1,26 +1,22 @@
package mineplex.core.elo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.Lists;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilTime;
import mineplex.core.common.util.UtilTime.TimeUnit;
import mineplex.core.database.MinecraftRepository;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.ResultSetCallable;
import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnLong;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import com.google.common.collect.Lists;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class EloRepository extends MinecraftRepository
{
@ -34,210 +30,164 @@ public class EloRepository extends MinecraftRepository
private static String UPDATE_BAN = "INSERT INTO rankedBans (accountId, strikes, strikesExpire, banEnd) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE strikes=VALUES(strikes), strikesExpire=VALUES(strikesExpire), banEnd=VALUES(banEnd);";
private static String DELETE_STRIKES = "UPDATE rankedBans SET strikes = 1 WHERE accountId = ?;";
private static String GET_ELO = "SELECT `elo`,`accountId` FROM `eloRating` ORDER BY `elo` DESC LIMIT $limit;";
{
private static String INSERT_ELO = "INSERT INTO eloRating (accountId, gameType, elo) VALUES (?, ?, ?);";
private static String UPDATE_ELO = "UPDATE eloRating SET elo = elo + ? WHERE accountId = ? AND gameType = ?;";
private static String UPDATE_ELO_ONLY_IF_MATCH = "UPDATE eloRating SET elo = elo + ? WHERE accountId = ? AND gameType = ? AND elo = ?;";
private static String GRAB_STRIKES = "SELECT strikes FROM rankedBans WHERE accountId = ?;";
private static String GRAB_STRIKE_EXPIRY = "SELECT strikesExpire FROM rankedBans WHERE accountId = ?;";
private static String GRAB_BAN_EXPIRY = "SELECT banEnd FROM rankedBans WHERE accountId = ?;";
private static String UPDATE_BAN = "INSERT INTO rankedBans (accountId, strikes, strikesExpire, banEnd) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE strikes=VALUES(strikes), strikesExpire=VALUES(strikesExpire), banEnd=VALUES(banEnd);";
private static String DELETE_STRIKES = "UPDATE rankedBans SET strikes = 1 WHERE accountId = ?;";
private static String GET_ELO = "SELECT 'elo','accountId' FROM eloRating WHERE gameType=? ORDER BY 'elo' DESC LIMIT $limit;";
private static String GET_NAME_FROM_ID = "SELECT `name` FROM `accounts` WHERE `id`=?;";
public EloRepository(JavaPlugin plugin)
{
super(plugin, DBPool.getAccount());
public EloRepository(JavaPlugin plugin)
{
super(plugin, DBPool.getAccount());
initialize();
}
initialize();
}
public void initialize() { }
public void initialize()
{
}
public boolean saveElo(int accountId, int gameType, int oldElo, int elo) throws SQLException
{
final List<Boolean> ret = new ArrayList<Boolean>();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
boolean updateSucceeded = false;
public boolean saveElo(int accountId, int gameType, int oldElo, int elo) throws SQLException
{
final List<Boolean> ret = Lists.newArrayList();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
boolean updateSucceeded = false;
// 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 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;
}
}
});
if (!updateSucceeded && executeUpdate(INSERT_ELO, new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType), new ColumnInt("elo", elo)) > 0)
{
updateSucceeded = true;
}
}
});
if (ret.isEmpty())
ret.add(false);
if (ret.isEmpty())
{
ret.add(false);
}
return ret.get(0);
}
return ret.get(0);
}
public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException
{
EloClientData clientData = new EloClientData();
public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException
{
EloClientData clientData = new EloClientData();
while (resultSet.next())
{
clientData.Elos.put(resultSet.getInt(1), resultSet.getInt(2));
}
while (resultSet.next())
{
clientData.Elos.put(resultSet.getInt(1), resultSet.getInt(2));
}
return clientData;
}
return clientData;
}
public long getStrikeExpiry(int accountId)
{
final List<Long> expire = new ArrayList<Long>();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
executeQuery(GRAB_STRIKE_EXPIRY, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while(resultSet.next())
{
expire.add(resultSet.getLong(1));
}
}
}, new ColumnInt("accountId", accountId));
}
});
public long getStrikeExpiry(int accountId)
{
final List<Long> expire = Lists.newArrayList();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> executeQuery(GRAB_STRIKE_EXPIRY, resultSet -> {
while (resultSet.next())
{
expire.add(resultSet.getLong(1));
}
}, new ColumnInt("accountId", accountId)));
if (expire.isEmpty())
expire.add(System.currentTimeMillis() - 5555);
if (expire.isEmpty())
{
expire.add(System.currentTimeMillis() - 5555);
}
return expire.get(0);
}
return expire.get(0);
}
public long getBanExpiry(int accountId)
{
final List<Long> expire = new ArrayList<Long>();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
executeQuery(GRAB_BAN_EXPIRY, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while(resultSet.next())
{
expire.add(resultSet.getLong(1));
}
}
}, new ColumnInt("accountId", accountId));
}
});
public long getBanExpiry(int accountId)
{
final List<Long> expire = Lists.newArrayList();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> executeQuery(GRAB_BAN_EXPIRY, resultSet -> {
while (resultSet.next())
{
expire.add(resultSet.getLong(1));
}
}, new ColumnInt("accountId", accountId)));
if (expire.isEmpty())
expire.add(System.currentTimeMillis() - 5555);
if (expire.isEmpty())
{
expire.add(System.currentTimeMillis() - 5555);
}
return expire.get(0);
}
return expire.get(0);
}
public int getStrikes(int accountId)
{
final List<Integer> strike = new ArrayList<Integer>();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
executeQuery(GRAB_STRIKES, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while(resultSet.next())
{
strike.add(resultSet.getInt(1));
}
}
}, new ColumnInt("accountId", accountId));
}
});
public int getStrikes(int accountId)
{
final List<Integer> strike = Lists.newArrayList();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> executeQuery(GRAB_STRIKES, resultSet -> {
while (resultSet.next())
{
strike.add(resultSet.getInt(1));
}
}, new ColumnInt("accountId", accountId)));
if (strike.isEmpty())
strike.add(0);
if (strike.isEmpty())
{
strike.add(0);
}
return strike.get(0);
}
return strike.get(0);
}
public void addRankedBan(int accountId)
{
int minutes = 1;
switch (getStrikes(accountId))
{
case 0:
minutes = 1;
break;
case 1:
minutes = 5;
break;
case 2:
minutes = 10;
break;
case 3:
minutes = 20;
break;
case 4:
minutes = 30;
break;
case 5:
minutes = 60;
break;
case 6:
minutes = 120;
break;
case 7:
minutes = 180;
break;
case 8:
minutes = 240;
break;
}
final long banEnd = System.currentTimeMillis() + UtilTime.convert(minutes, TimeUnit.MINUTES, TimeUnit.MILLISECONDS);
final long strikesExpire = System.currentTimeMillis() + UtilTime.convert(1, TimeUnit.DAYS, TimeUnit.MILLISECONDS);
final int newStrikes = Math.min(getStrikes(accountId) + 1, 8);
public void addRankedBan(int accountId)
{
int minutes = 1;
switch (getStrikes(accountId))
{
case 0:
minutes = 1;
break;
case 1:
minutes = 5;
break;
case 2:
minutes = 10;
break;
case 3:
minutes = 20;
break;
case 4:
minutes = 30;
break;
case 5:
minutes = 60;
break;
case 6:
minutes = 120;
break;
case 7:
minutes = 180;
break;
case 8:
minutes = 240;
break;
}
final long banEnd = System.currentTimeMillis() + UtilTime.convert(minutes, TimeUnit.MINUTES, TimeUnit.MILLISECONDS);
final long strikesExpire = System.currentTimeMillis() + UtilTime.convert(1, TimeUnit.DAYS, TimeUnit.MILLISECONDS);
final int newStrikes = Math.min(getStrikes(accountId) + 1, 8);
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));
}
});
}
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> executeUpdate(UPDATE_BAN, new ColumnInt("accountId", accountId), new ColumnInt("strikes", newStrikes), new ColumnLong("strikesExpire", strikesExpire), new ColumnLong("banEnd", banEnd)));
}
public void resetStrikes(int accountId)
{
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
executeUpdate(DELETE_STRIKES, new ColumnInt("accountId", accountId));
}
});
}
public void resetStrikes(int accountId)
{
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> executeUpdate(DELETE_STRIKES, new ColumnInt("accountId", accountId)));
}
@Override
protected void update()
{
}
@Override
protected void update()
{
}
public void getTopElo(int limit, Callback<List<TopEloData>> callback)
{
@ -261,7 +211,7 @@ public class EloRepository extends MinecraftRepository
int elo = resultSet.getInt(1);
TopEloData data;
nameStatement = connection.prepareStatement(GET_NAME_FROM_ID);
nameStatement.setInt(1, resultSet.getInt("accountId"));
nameStatement.setInt(1, resultSet.getInt(2));
ResultSet nameSet = nameStatement.executeQuery();
if (nameSet.next())
{