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

View File

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

View File

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