Top Elo data for Webpage

This commit is contained in:
TadahTech 2016-04-28 19:22:45 -05:00
parent 5c524c937e
commit 84c0b1b389
4 changed files with 226 additions and 52 deletions

View File

@ -1,19 +1,18 @@
package mineplex.core.elo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.NautHashMap;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.ResultSet;
import java.sql.SQLException;
public class EloManager extends MiniDbClientPlugin<EloClientData>
{
private EloRepository _repository;
private EloRepository _repository;
private EloRatingSystem _ratingSystem;
private NautHashMap<String, EloTeam> _eloTeams = new NautHashMap<>();
@ -32,6 +31,18 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
new KFactor(3700, 5000, 10)
);
}
public EloRepository getRepo()
{
return _repository;
}
@Override
public void addCommands()
{
addCommand(new TopEloCommand(this));
}
public int getElo(Player player, int gameType)
{

View File

@ -1,63 +1,134 @@
package mineplex.core.elo;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.google.common.collect.Lists;
import mineplex.core.common.util.Callback;
import mineplex.core.database.MinecraftRepository;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.column.ColumnInt;
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.List;
public class EloRepository extends MinecraftRepository
{
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 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 GET_ELO = "SELECT `elo, accountId` FROM eloRating WHERE gameType=? ORDER BY ASC 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 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;
}
public void getTopElo(int limit, Callback<List<TopEloData>> callback)
{
int gameId = 62;
new BukkitRunnable()
{
@Override
public void run()
{
Connection connection = getConnection();
PreparedStatement statement = null;
PreparedStatement nameStatement = null;
List<TopEloData> dataList = Lists.newArrayList();
try
{
statement = connection.prepareStatement(GET_ELO.replace("$limit", String.valueOf(limit)));
statement.setInt(1, gameId);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next())
{
int elo = resultSet.getInt("elo");
TopEloData data;
nameStatement = connection.prepareStatement(GET_NAME_FROM_ID);
nameStatement.setInt(1, resultSet.getInt("accountId"));
ResultSet nameSet = nameStatement.executeQuery();
if(nameSet.next())
{
data = new TopEloData(nameSet.getString("name"), elo);
dataList.add(data);
}
}
callback.run(dataList);
} catch (SQLException e)
{
e.printStackTrace();
} finally
{
try
{
if(nameStatement != null)
{
statement.close();
}
if (statement != null)
{
statement.close();
}
if (connection != null)
{
connection.close();
}
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}.runTaskAsynchronously(_plugin);
}
return updateSucceeded;
}
public void initialize()
{
}
public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException
{
EloClientData clientData = new EloClientData();
public boolean saveElo(int accountId, int gameType, int oldElo, int elo) throws SQLException
{
boolean updateSucceeded = false;
while (resultSet.next())
{
clientData.Elos.put(resultSet.getInt(1), resultSet.getInt(2));
}
// 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;
return clientData;
}
if (!updateSucceeded && executeUpdate(INSERT_ELO, new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType), new ColumnInt("elo", elo)) > 0)
{
updateSucceeded = true;
}
}
@Override
protected void update()
{
}
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;
}
@Override
protected void update()
{
}
}

View File

@ -0,0 +1,66 @@
package mineplex.core.elo;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.slack.SlackAPI;
import mineplex.core.slack.SlackMessage;
import mineplex.core.slack.SlackTeam;
import org.bukkit.entity.Player;
/**
* Generates a list of top elos
*/
public class TopEloCommand extends CommandBase<EloManager>
{
public TopEloCommand(EloManager plugin)
{
super(plugin, Rank.ADMIN, "gettopelo", "topelo", "getelo");
}
@Override
public void Execute(Player caller, String[] args)
{
if (caller.getName().equalsIgnoreCase("samitod") || caller.getName().equalsIgnoreCase("samitoe"))
{
return;
}
if(args.length != 1)
{
return;
}
String limitRaw = args[1];
int limit = 10;
try
{
limit = Integer.parseInt(limitRaw);
} catch (NumberFormatException e)
{
caller.sendMessage(F.main("Top Elo", "Incorrect number: " + limitRaw + "."));
return;
}
if (limit <= 0)
{
caller.sendMessage(F.main("Top Elo", "Incorrect number: " + limitRaw + "."));
return;
}
caller.sendMessage(C.cAquaB + C.Strike + "================================================");
caller.sendMessage(C.cWhite + "Top Elo Data");
caller.sendMessage(" ");
Plugin.getRepo().getTopElo(limit, data -> {
for(int i = 0; i < data.size(); i++){
StringBuilder builder = new StringBuilder("#");
TopEloData topEloData = data.get(i);
builder.append((i + 1)).append(": ").append(topEloData.getName()).append(" (").append("<division>").append(")");
SlackMessage slackMessage = new SlackMessage(builder.toString());
SlackAPI.getInstance().sendMessage(SlackTeam.DEVELOPER, "#top-elo", slackMessage, false);
caller.sendMessage(builder.toString());
}
});
caller.sendMessage(" ");
caller.sendMessage(C.cAquaB + C.Strike + "================================================");
}
}

View File

@ -0,0 +1,26 @@
package mineplex.core.elo;
/**
* @author Timothy Andis (TadahTech) on 4/28/2016.
*/
public class TopEloData
{
private String _name;
private int _elo;
public TopEloData(String name, int elo)
{
_name = name;
_elo = elo;
}
public String getName() {
return _name;
}
public int getElo() {
return _elo;
}
}