From 84c0b1b38938a2ce3ccdf1f5f9be38120b51af43 Mon Sep 17 00:00:00 2001 From: TadahTech Date: Thu, 28 Apr 2016 19:22:45 -0500 Subject: [PATCH] Top Elo data for Webpage --- .../src/mineplex/core/elo/EloManager.java | 23 ++- .../src/mineplex/core/elo/EloRepository.java | 163 +++++++++++++----- .../src/mineplex/core/elo/TopEloCommand.java | 66 +++++++ .../src/mineplex/core/elo/TopEloData.java | 26 +++ 4 files changed, 226 insertions(+), 52 deletions(-) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloCommand.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloData.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java index 10bd385f9..0347a621d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java @@ -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 { - private EloRepository _repository; + + private EloRepository _repository; private EloRatingSystem _ratingSystem; private NautHashMap _eloTeams = new NautHashMap<>(); @@ -32,6 +31,18 @@ public class EloManager extends MiniDbClientPlugin 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) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java index 1c5e749f8..33ec918b2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java @@ -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> callback) + { + int gameId = 62; + new BukkitRunnable() + { + @Override + public void run() + { + Connection connection = getConnection(); + PreparedStatement statement = null; + PreparedStatement nameStatement = null; + List 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() + { + } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloCommand.java new file mode 100644 index 000000000..2237f967d --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloCommand.java @@ -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 +{ + + 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("").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 + "================================================"); + } + +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloData.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloData.java new file mode 100644 index 000000000..2219ef60a --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/TopEloData.java @@ -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; + } + +}