EloRepository overhaul started

Tinkering with EloRepository, using AccountRepository as a base
This commit is contained in:
Joseph Prezioso Jr. 2016-04-14 20:09:09 -04:00
parent 742807d80e
commit 4a4e64a3ff

View File

@ -3,6 +3,8 @@ package mineplex.core.elo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import mineplex.core.database.MinecraftRepository;
@ -21,6 +23,9 @@ 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 ELO_NEW = "INSERT INTO eloRating (uuid, gameType, elo), values(?, ?, ?);";
public EloRepository(JavaPlugin plugin)
{
@ -39,25 +44,23 @@ public class EloRepository extends MinecraftRepository
//get an elo from the database
public int getRepoElo(String uuid, String gameType) throws SQLException
{
Connection con = getConnection();
java.sql.Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
String selectQuery = "SELECT elo FROM eloRating WHERE uuid = '" + uuid + "' AND gameType = '" + gameType + "';";
ResultSet rs = stmt.executeQuery(selectQuery);
//elo set to 1000 by default
int elo = 1000;
//if something was found, return the elo
if(rs.next())
{
elo = rs.getInt(1);
Bukkit.broadcastMessage("getRepoElo pulled " + elo + "from database for " + uuid);
return elo;
try (Connection con = getConnection(); java.sql.Statement satement = con.createStatement())
{
satement.execute(SELECT_ELO_BY_UUID);
ResultSet resultSet = satement.getResultSet();
//if something was found, return the elo
if(resultSet.next())
{
elo = resultSet.getInt(1);
Bukkit.broadcastMessage("getRepoElo pulled " + elo + "from database for " + uuid);
return elo;
}
}
//make a new entry into eloRatings, if there is no elo for the player for this gameType
saveElo(uuid, gameType, elo);
//then return the elo (should be 1000)
return elo;
}
@ -65,7 +68,7 @@ public class EloRepository extends MinecraftRepository
{
Connection con = getConnection();
java.sql.Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
String selectQuery = "SELECT gameType, elo FROM eloRating WHERE uuid = '" + uuid + "';";
String selectQuery = SELECT_ELO_BY_UUID;
ResultSet rs = stmt.executeQuery(selectQuery);
boolean contains = false;
@ -74,8 +77,7 @@ public class EloRepository extends MinecraftRepository
{
contains = true;
//update elo
String updateQuery = "UPDATE eloRating SET elo='" + elo + "' WHERE uuid = '" + uuid + "' AND gametype = '" + gameType + "';";
executeQuery(updateQuery, null, new ColumnVarChar("elo", 100, uuid));
executeUpdate(UPDATE_ELO, new ColumnVarChar("elo", 100, uuid));
Bukkit.broadcastMessage("Updating existing elo");
}
if(!contains)
@ -100,7 +102,7 @@ public class EloRepository extends MinecraftRepository
while(rs.next())
{
String updateQuery = "UPDATE eloRating SET division='" + division + "' WHERE uuid = '" + uuid + "' AND gametype = '" + gameType + "';";
executeQuery(updateQuery, null, new ColumnVarChar("elo", 100, uuid));
executeUpdate(updateQuery, new ColumnVarChar("elo", 100, uuid));
}
if(!contains)
{