NOW() is a numeric representation of the date, not a timestamp....  so thats a nono.

Also,  we need to update UUIDs grabbing by name.
This commit is contained in:
t00thpick1 2014-08-07 13:54:28 -04:00
parent d857bf483e
commit 8766d31943
3 changed files with 39 additions and 33 deletions

View File

@ -11,8 +11,8 @@ import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SkillType;
public interface DatabaseManager {
// One month in milliseconds
public final long PURGE_TIME = 2630000000L * Config.getInstance().getOldUsersCutoff();
// One month in seconds
public final long PURGE_TIME = 2630000L * Config.getInstance().getOldUsersCutoff();
// During convertUsers, how often to output a status
public final int progressInterval = 200;
@ -67,8 +67,9 @@ public interface DatabaseManager {
* Add a new user to the database.
*
* @param playerName The name of the player to be added to the database
* @param uuid The uuid of the player to be added to the database
*/
public void newUser(String playerName, String uuid);
public void newUser(String playerName, UUID uuid);
/**
* Load a player from the database.

View File

@ -378,7 +378,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return skills;
}
public void newUser(String playerName, String uuid) {
public void newUser(String playerName, UUID uuid) {
BufferedWriter out = null;
synchronized (fileWritingLock) {
try {
@ -427,7 +427,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD
out.append("0:"); // Alchemy
out.append("0:"); // AlchemyXp
out.append(uuid).append(":"); // UUID
out.append(uuid != null ? uuid.toString() : "").append(":"); // UUID
// Add more in the same format as the line above
@ -451,18 +451,14 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
@Deprecated
public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
return loadPlayerProfile(playerName, "", false);
return loadPlayerProfile(playerName, null, false);
}
public PlayerProfile loadPlayerProfile(UUID uuid) {
return loadPlayerProfile("", uuid.toString(), false);
return loadPlayerProfile("", uuid, false);
}
public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) {
return loadPlayerProfile(playerName, uuid.toString(), create);
}
private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create) {
BufferedReader in = null;
String usersFilePath = mcMMO.getUsersFilePath();
@ -476,7 +472,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Find if the line contains the player we want.
String[] character = line.split(":");
if (!character[41].equalsIgnoreCase(uuid) && !character[0].equalsIgnoreCase(playerName)) {
if ((uuid == null || !character[41].equalsIgnoreCase(uuid.toString())) && !character[0].equalsIgnoreCase(playerName)) {
continue;
}
@ -491,13 +487,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Didn't find the player, create a new one
if (create) {
if (uuid.isEmpty()) {
if (uuid == null) {
newUser(playerName, uuid);
return new PlayerProfile(playerName, true);
}
newUser(playerName, uuid);
return new PlayerProfile(playerName, UUID.fromString(uuid), true);
return new PlayerProfile(playerName, uuid, true);
}
}
catch (Exception e) {
@ -518,11 +514,11 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
}
// Return unloaded profile
if (uuid.isEmpty()) {
if (uuid == null) {
return new PlayerProfile(playerName);
}
return new PlayerProfile(playerName, UUID.fromString(uuid));
return new PlayerProfile(playerName, uuid);
}
public void convertUsers(DatabaseManager destination) {

View File

@ -127,7 +127,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
public void purgeOldUsers() {
massUpdateLock.lock();
mcMMO.p.getLogger().info("Purging inactive users older than " + (PURGE_TIME / 2630000000L) + " months...");
mcMMO.p.getLogger().info("Purging inactive users older than " + (PURGE_TIME / 2630000L) + " months...");
Connection connection = null;
Statement statement = null;
@ -142,7 +142,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
"JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
"JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
"JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
"WHERE ((NOW() - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")");
"WHERE ((UNIX_TIMESTAMP() - lastlogin) > " + PURGE_TIME + ")");
}
catch (SQLException ex) {
printErrors(ex);
@ -229,7 +229,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
int id = getUserID(connection, profile.getUniqueId());
if (id == -1) {
newUser(profile.getPlayerName(), profile.getUniqueId().toString());
newUser(profile.getPlayerName(), profile.getUniqueId());
id = getUserID(connection, profile.getUniqueId());
if (id == -1) {
return false;
@ -504,7 +504,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
return skills;
}
public void newUser(String playerName, String uuid) {
public void newUser(String playerName, UUID uuid) {
Connection connection = null;
try {
@ -526,14 +526,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
}
private void newUser(Connection connection, String playerName, String uuid) {
private void newUser(Connection connection, String playerName, UUID uuid) {
ResultSet resultSet = null;
PreparedStatement statement = null;
try {
statement = connection.prepareStatement("INSERT INTO " + tablePrefix + "users (user, uuid, lastlogin) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, playerName);
statement.setString(2, uuid);
statement.setString(2, uuid.toString());
statement.setLong(3, System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
statement.executeUpdate();
@ -572,7 +572,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
* This is a fallback method to provide the old way of getting a
* PlayerProfile in case there is no UUID match found
*/
private PlayerProfile loadPlayerNameProfile(String playerName, String uuid, boolean create, boolean retry) {
private PlayerProfile loadPlayerNameProfile(String playerName, UUID uuid, boolean create, boolean retry) {
PreparedStatement statement = null;
Connection connection = null;
ResultSet resultSet = null;
@ -605,7 +605,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) "
+ "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
+ "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
+ "WHERE u.user = ?");
+ "WHERE u.user = ? AND u.uuid = NULL");
statement.setString(1, playerName);
resultSet = statement.executeQuery();
@ -613,6 +613,16 @@ public final class SQLDatabaseManager implements DatabaseManager {
if (resultSet.next()) {
try {
PlayerProfile ret = loadFromResult(playerName, resultSet);
resultSet.close();
statement.close();
statement = connection.prepareStatement(
"UPDATE `" + tablePrefix + "users` "
+ "SET uuid = ? "
+ "WHERE user = ?");
statement.setString(1, uuid.toString());
statement.setString(2, playerName);
statement.executeUpdate();
statement.close();
return ret;
}
catch (SQLException e) {
@ -662,30 +672,29 @@ public final class SQLDatabaseManager implements DatabaseManager {
@Deprecated
public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
return loadPlayerProfile(playerName, "", false, true);
return loadPlayerProfile(playerName, null, false, true);
}
public PlayerProfile loadPlayerProfile(UUID uuid) {
return loadPlayerProfile("", uuid.toString(), false, true);
return loadPlayerProfile("", uuid, false, true);
}
public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) {
return loadPlayerProfile(playerName, uuid.toString(), create, true);
return loadPlayerProfile(playerName, uuid, create, true);
}
private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create, boolean retry) {
private PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create, boolean retry) {
PreparedStatement statement = null;
Connection connection = null;
ResultSet resultSet = null;
try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
int id = getUserID(connection, playerName);
int id = getUserID(connection, uuid);
if (id == -1) {
// There is no such user
if (create) {
newUser(playerName, uuid);
return loadPlayerNameProfile(playerName, uuid, false, false);
}
@ -707,7 +716,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
+ "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
+ "WHERE u.uuid = ?");
statement.setString(1, uuid);
statement.setString(1, uuid.toString());
resultSet = statement.executeQuery();
@ -723,7 +732,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "SET user = ? "
+ "WHERE uuid = ?");
statement.setString(1, playerName);
statement.setString(2, uuid);
statement.setString(2, uuid.toString());
statement.executeUpdate();
statement.close();
}
@ -1278,7 +1287,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
private void printErrors(SQLException ex) {
StackTraceElement element = ex.getStackTrace()[0];
StackTraceElement element = ex.getStackTrace()[ex.getStackTrace().length];
mcMMO.p.getLogger().severe("Location: " + element.getMethodName() + " " + element.getLineNumber());
mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());