From 44263beb87bc3669e58777efc5939787b868cd7b Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Sun, 17 Apr 2016 17:34:18 -0400 Subject: [PATCH] - Make elo store/recover properly --- .../src/mineplex/core/elo/EloManager.java | 6 +- .../src/mineplex/core/elo/EloRepository.java | 70 ++++++++----------- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java index 1d472c9ed..06e2647f7 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java @@ -66,8 +66,8 @@ public class EloManager extends MiniDbClientPlugin //If this works, we will never hit the return at the bottom try { - elo = _repository.getRepoElo(uuid.toString(), gameType); - Bukkit.broadcastMessage("Fetching Elo..."); + elo = _repository.getRepoElo(uuid.toString(), /*gameType*/"Testing"); + //Bukkit.broadcastMessage("Fetching Elo..."); return elo; } catch (SQLException e) @@ -121,7 +121,7 @@ public class EloManager extends MiniDbClientPlugin { //Bukkit.broadcastMessage("SAVE_ELO - EloManager (uuid type) called"); //String playerDiv = getPlayerDivision(uuid, gameType); - saveElo(uuid.toString(), gameType, elo); + saveElo(uuid.toString(), /*gameType*/"Testing", elo); //saveDivision(uuid.toString(), gameType, playerDiv); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java index ea46403ca..cb54cae6c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java @@ -7,28 +7,23 @@ import java.util.ArrayList; import java.util.List; import mineplex.core.database.MinecraftRepository; - -import org.bukkit.Bukkit; -import org.bukkit.plugin.java.JavaPlugin; - -import com.mysql.jdbc.PreparedStatement; -import com.mysql.jdbc.Statement; - import mineplex.serverdata.database.DBPool; -import mineplex.serverdata.database.RepositoryBase; +import mineplex.serverdata.database.ResultSetCallable; import mineplex.serverdata.database.column.ColumnInt; import mineplex.serverdata.database.column.ColumnVarChar; +import org.bukkit.plugin.java.JavaPlugin; + public class EloRepository extends MinecraftRepository { - private static String CREATE_ELO_TABLE = "CREATE TABLE IF NOT EXISTS eloRating (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(256), gameType VARCHAR(256), elo INT, division VARCHAR(256), PRIMARY KEY (id), UNIQUE INDEX uuid_gameType_index (uuid, gameType));"; - private static String INSERT_ELO = "INSERT INTO eloRating (uuid, gameType, elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE elo=VALUES(elo);"; - private static String INSERT_DIVISION = "INSERT INTO eloRating (uuid, gameType, division) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE division=VALUES(division);"; - private static String UPDATE_ELO = "UPDATE eloRating SET elo=? WHERE uuid=?;"; - private static String SELECT_ELO_BY_UUID = "SELECT elo FROM eloRating WHERE uuid = ? LIMIT 1"; - private static String SELECT_DIVISION_BY_UUID = "SELECT division FROM eloRating WHERE uuid = ? LIMIT 1"; - private static String UPDATE_DIVISION = "UPDATE eloRating SET division=? WHERE uuid=?;"; - private static String ELO_NEW = "INSERT INTO eloRating (uuid, gameType, elo), values(?, ?, ?);"; + private static String CREATE_ELO_TABLE = "CREATE TABLE IF NOT EXISTS eloRating (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(256), Testing INT, division VARCHAR(256), PRIMARY KEY (id), UNIQUE INDEX uuid (uuid));"; + private static String INSERT_ELO = "INSERT INTO eloRatingTest (uuid, $gameType$) VALUES (?, ?) ON DUPLICATE KEY UPDATE $gameType$=VALUES($gameType$);"; + private static String INSERT_DIVISION = "INSERT INTO eloRatingTest (uuid, gameType, division) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE division=VALUES(division);"; + //private static String UPDATE_ELO = "UPDATE eloRating SET $gameType$=? WHERE uuid=?;"; + private static String SELECT_ELO_BY_UUID = "SELECT $gameType$ FROM eloRatingTest WHERE uuid = ? LIMIT 1"; + private static String SELECT_DIVISION_BY_UUID = "SELECT division FROM eloRatingTest WHERE uuid = ? LIMIT 1"; + private static String UPDATE_DIVISION = "UPDATE eloRatingTest SET division=? WHERE uuid=?;"; + private static String ELO_NEW = "INSERT INTO eloRatingTest (uuid, gameType, elo), values(?, ?, ?);"; public EloRepository(JavaPlugin plugin) { @@ -46,34 +41,31 @@ public class EloRepository extends MinecraftRepository public int getRepoElo(String uuid, String gameType) throws SQLException { //elo set to 1000 by default - int elo = 1000; - - try (Connection con = getConnection(); java.sql.PreparedStatement pStatement = con.prepareStatement(SELECT_ELO_BY_UUID);) - { - pStatement.execute(SELECT_ELO_BY_UUID); - pStatement.setString(1, uuid.toString()); - ResultSet resultSet = pStatement.executeQuery(); - - //if something was found, return the elo - if(resultSet.next()) + final List elo = new ArrayList(); + String query = SELECT_ELO_BY_UUID.replace("$gameType$", gameType); + executeQuery(query, new ResultSetCallable() + { + @Override + public void processResultSet(ResultSet resultSet) throws SQLException { - elo = resultSet.getInt("elo"); - Bukkit.broadcastMessage("getRepoElo pulled " + elo + "from database for " + uuid); - return elo; + while (resultSet.next()) + { + elo.add(Integer.parseInt(resultSet.getString(1))); + } } - else - { - saveElo(uuid, gameType, elo); - } - - } + }, new ColumnVarChar("uuid", 100, uuid)); - return elo; + if (elo.isEmpty()) + elo.add(1000); + + return elo.get(0); } public void saveElo(String uuid, String gameType, int elo) throws SQLException - { - Connection con = getConnection(); + { + String query = INSERT_ELO.replace("$gameType$", gameType); + executeUpdate(query, new ColumnVarChar("uuid", 256, uuid), new ColumnInt(gameType, elo)); + /*Connection con = getConnection(); java.sql.PreparedStatement stmt = con.prepareStatement(SELECT_ELO_BY_UUID); stmt.setString(1, uuid.toString()); ResultSet rs = stmt.executeQuery(); @@ -107,7 +99,7 @@ public class EloRepository extends MinecraftRepository Bukkit.broadcastMessage("SAVE_ELO - EloRepository: Adding " + elo + " to " + gameType +" elo database..."); } - Bukkit.broadcastMessage("SAVE_ELO - EloRepository: Adding " + elo + " to " + gameType +" elo database..."); + Bukkit.broadcastMessage("SAVE_ELO - EloRepository: Adding " + elo + " to " + gameType +" elo database...");*/ } public void saveDivision(String uuid, String gameType, String division) throws SQLException