Fix ELO query

This commit is contained in:
TadahTech 2016-04-28 21:09:07 -05:00
commit 80ea7b7a4c
6 changed files with 253 additions and 228 deletions

View File

@ -1,17 +0,0 @@
package mineplex.core.communities;
import mineplex.core.MiniPlugin;
import org.bukkit.plugin.java.JavaPlugin;
/**
* The main class for handling all things Communities
*/
public class CommunitiesManager extends MiniPlugin
{
public CommunitiesManager(JavaPlugin plugin)
{
super("Communities", plugin);
}
}

View File

@ -1,41 +0,0 @@
package mineplex.core.communities;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.RepositoryBase;
/**
* Repository manager for Communities
*/
public class CommuntiesRepository extends RepositoryBase
{
private static final String[] TABLES = {
"CREATE TABLE IF NOT EXISTS `communities` ()",
"CREATE TABLE IF NOT EXISTS `communityMembers` ()"
};
private static String[] UPDATES = {
};
private static String[] INSERTS = {
};
public CommuntiesRepository()
{
super(DBPool.getMineplex());
}
@Override
protected void initialize()
{
}
@Override
protected void update()
{
}
}

View File

@ -138,6 +138,9 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
_eloTeams.get(displayName).Winner = true;
}
/**
* Method for quickly checking if a player is banned from ranked games
*/
public boolean isRankBanned(int accountId)
{
boolean banExpired = System.currentTimeMillis() >= getRankBanExpiry(accountId);
@ -152,16 +155,25 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
return !banExpired;
}
/**
* Method for getting a player's remaining ranked game ban duration if applicable
*/
public long getRankBanExpiry(int accountId)
{
return _repository.getBanExpiry(accountId);
}
/**
* Method for banning a player from joining ranked games temporarily
*/
public void banFromRanked(int accountId)
{
_repository.addRankedBan(accountId);
}
/**
* Method called when game ends to calculate new Elo and award it
*/
public void endMatch(int gameId)
{
EloTeam teamWinner = null;
@ -211,6 +223,9 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
_eloTeams.clear();
}
/**
* Manages and stores all Elo divisions and required data for those divisions for proper operation of Elo division display in-game.
*/
public enum EloDivision
{
DIAMOND("Diamond", -1, 3700, Material.DIAMOND_BLOCK),
@ -242,6 +257,9 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
_visual = visual;
}
/*
* Method for fetching the proper division for a given Elo value
*/
public static EloDivision getDivision(int elo)
{
for (EloDivision ed : EloDivision.values())
@ -276,6 +294,9 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
return _disp;
}
/**
* Method for fetching the Itemstack that represents a player's division and their progress in it
*/
public ItemStack getVisual(int elo)
{
ItemBuilder build = new ItemBuilder(_visual);

View File

@ -1,22 +1,27 @@
package mineplex.core.elo;
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.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnLong;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
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 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;
public class EloRepository extends MinecraftRepository
{
@ -29,6 +34,16 @@ 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)
@ -38,6 +53,192 @@ public class EloRepository extends MinecraftRepository
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;
// 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 (ret.isEmpty())
ret.add(false);
return ret.get(0);
}
public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException
{
EloClientData clientData = new EloClientData();
while (resultSet.next())
{
clientData.Elos.put(resultSet.getInt(1), resultSet.getInt(2));
}
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));
}
});
if (expire.isEmpty())
expire.add(System.currentTimeMillis() - 5555);
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));
}
});
if (expire.isEmpty())
expire.add(System.currentTimeMillis() - 5555);
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));
}
});
if (strike.isEmpty())
strike.add(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);
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)
{
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
executeUpdate(DELETE_STRIKES, new ColumnInt("accountId", accountId));
}
});
}
@Override
protected void update()
{
}
public void getTopElo(int limit, Callback<List<TopEloData>> callback)
{
int gameId = 62;
@ -57,7 +258,7 @@ public class EloRepository extends MinecraftRepository
ResultSet resultSet = statement.executeQuery();
while (resultSet.next())
{
int elo = resultSet.getInt("elo");
int elo = resultSet.getInt(1);
TopEloData data;
nameStatement = connection.prepareStatement(GET_NAME_FROM_ID);
nameStatement.setInt(1, resultSet.getInt("accountId"));
@ -96,146 +297,4 @@ public class EloRepository extends MinecraftRepository
}
}.runTaskAsynchronously(_plugin);
}
public void initialize()
{
}
public boolean saveElo(int accountId, int gameType, int oldElo, int elo) throws SQLException
{
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 (!updateSucceeded && executeUpdate(INSERT_ELO, new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType), new ColumnInt("elo", elo)) > 0)
{
updateSucceeded = true;
}
}
return updateSucceeded;
}
public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException
{
EloClientData clientData = new EloClientData();
while (resultSet.next())
{
clientData.Elos.put(resultSet.getInt(1), resultSet.getInt(2));
}
return clientData;
}
public long getStrikeExpiry(int accountId)
{
final List<Long> expire = Lists.newArrayList();
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);
}
return expire.get(0);
}
public long getBanExpiry(int accountId)
{
final List<Long> expire = Lists.newArrayList();
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);
}
return expire.get(0);
}
public int getStrikes(int accountId)
{
final List<Integer> strike = Lists.newArrayList();
executeQuery(GRAB_STRIKES, resultSet -> {
while (resultSet.next())
{
strike.add(resultSet.getInt(1));
}
}, new ColumnInt("accountId", accountId));
if (strike.isEmpty())
{
strike.add(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;
}
long banEnd = System.currentTimeMillis() + UtilTime.convert(minutes, TimeUnit.MINUTES, TimeUnit.MILLISECONDS);
long strikesExpire = System.currentTimeMillis() + UtilTime.convert(1, TimeUnit.DAYS, TimeUnit.MILLISECONDS);
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));
}
public void resetStrikes(int accountId)
{
executeUpdate(DELETE_STRIKES, new ColumnInt("accountId", accountId));
}
@Override
protected void update()
{
}
}

View File

@ -32,7 +32,7 @@ public class TopEloCommand extends CommandBase<EloManager>
{
return;
}
String limitRaw = args[1];
String limitRaw = args[0];
int limit = 10;
try
{
@ -47,7 +47,7 @@ public class TopEloCommand extends CommandBase<EloManager>
caller.sendMessage(F.main("Top Elo", "Incorrect number: " + limitRaw + "."));
return;
}
caller.sendMessage(C.cAquaB + C.Strike + "================================================");
caller.sendMessage(C.cAquaB + C.Strike + "=============================================");
caller.sendMessage(C.cWhite + "Top Elo Data");
caller.sendMessage(" ");
Plugin.getRepo().getTopElo(limit, data -> {
@ -62,7 +62,7 @@ public class TopEloCommand extends CommandBase<EloManager>
}
});
caller.sendMessage(" ");
caller.sendMessage(C.cAquaB + C.Strike + "================================================");
caller.sendMessage(C.cAquaB + C.Strike + "=============================================");
}
}

View File

@ -21,6 +21,9 @@ import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent;
/**
* Wrapper for team games to implement for simple universal use of Elo and Ranked methods.
*/
public abstract class RankedTeamGame extends TeamGame
{
public int MaxPlayers = -1;