- Make elo store/recover properly

This commit is contained in:
AlexTheCoder 2016-04-17 17:34:18 -04:00
parent ee30012011
commit 44263beb87
2 changed files with 34 additions and 42 deletions

View File

@ -66,8 +66,8 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
//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<EloClientData>
{
//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);
}

View File

@ -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);)
final List<Integer> elo = new ArrayList<Integer>();
String query = SELECT_ELO_BY_UUID.replace("$gameType$", gameType);
executeQuery(query, new ResultSetCallable()
{
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())
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
elo = resultSet.getInt("elo");
Bukkit.broadcastMessage("getRepoElo pulled " + elo + "from database for " + uuid);
return elo;
}
else
{
saveElo(uuid, gameType, elo);
while (resultSet.next())
{
elo.add(Integer.parseInt(resultSet.getString(1)));
}
}
}, new ColumnVarChar("uuid", 100, uuid));
}
if (elo.isEmpty())
elo.add(1000);
return elo;
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