Allow database levels to be set manually and updated across servers

This commit is contained in:
gravitylow committed 2013-10-29 19:02:53 -04:00
1 parent d49e44b0ba
commit d06361b3c9
10 files changed
+116 -10

No files matched your search

@@ -34,8 +34,9 @@ public class Enterprise extends ConfigurationFile {
public ConfigValue<String> loggingInterval;
public ConfigValue<Boolean> syncLevels;
public ConfigValue<String> syncInterval;
public ConfigValue<Boolean> configGroups ;
public ConfigValue<Boolean> configGroups;
public ConfigValue<Boolean> configRules;
public Database database;
@@ -53,6 +54,7 @@ public class Enterprise extends ConfigurationFile {
loggingInterval = new ConfigValue<String>("log.interval");
syncLevels = new ConfigValue<Boolean>("sync.levels");
syncInterval = new ConfigValue<String>("sync.interval");
configGroups = new ConfigValue<Boolean>("config.groups");
configRules = new ConfigValue<Boolean>("config.rules");
@@ -77,7 +79,9 @@ public class Enterprise extends ConfigurationFile {
databaseSchema.getValue(),
serverName.getValue(),
loggingInterval.getValue(),
loggingLife.getValue()
loggingLife.getValue(),
syncLevels.getValue(),
syncInterval.getValue()
);
database.connect();
@@ -33,9 +33,11 @@ import java.util.List;
public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
private static final String TABLE = "levels";
private static final String MANUAL_EDIT_TAG = "MANUAL";
private String sqlSave;
private String sqlLoad;
private String sqlUpdate;
public MySQLLevelsHolder(Configuration config) {
super(config, TABLE);
@@ -54,6 +56,9 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
sqlLoad = "SELECT level, last_update FROM " + getFullTable() + " " +
"WHERE user = ?";
sqlUpdate = "SELECT level, last_update FROM " + getFullTable() + " " +
"WHERE user = ? AND last_update <> ? AND last_update_server = ?";
String sqlCreate = "CREATE TABLE IF NOT EXISTS " + getFullTable() + "(" +
" `user` VARCHAR(45) NOT NULL," +
" `level` INT NOT NULL," +
@@ -75,7 +80,7 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
@Override
public void run() {
if (!user.isWaitingOnLevelSync()) {
AntiCheat.debugLog("Syncing to " + user.getName() + " value " + user.getLevel());
AntiCheat.debugLog("Saving level from " + user.getName() + ". Value: " + user.getLevel());
try {
PreparedStatement statement = getConnection().prepareStatement(sqlSave);
statement.setString(1, user.getName());
@@ -110,7 +115,7 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
boolean has = false;
while (set.next()) {
has = true;
AntiCheat.debugLog("Syncing from " + user.getName() + " value " + set.getInt("level"));
AntiCheat.debugLog("Syncing level to " + user.getName() + ". Value: " + set.getInt("level"));
user.setLevel(set.getInt("level"));
user.setLevelSyncTimestamp(set.getTimestamp("last_update"));
}
@@ -149,4 +154,29 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
e.printStackTrace();
}
}
@Override
public void updateLevelToUser(final User user) {
Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable() {
@Override
public void run() {
try {
PreparedStatement statement = getConnection().prepareStatement(sqlUpdate);
statement.setString(1, user.getName());
statement.setTimestamp(2, user.getLevelSyncTimestamp());
statement.setString(3, MANUAL_EDIT_TAG);
ResultSet set = statement.executeQuery();
while (set.next()) {
AntiCheat.debugLog("Syncing level to " + user.getName() + ". Value: " + set.getInt("level"));
user.setLevel(set.getInt("level"));
user.setLevelSyncTimestamp(set.getTimestamp("last_update"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
@@ -56,6 +56,12 @@ public class YamlLevelsHolder extends ConfigurationFile implements Levels {
}
}
@Override
public void updateLevelToUser(User user) {
// This method intentionally left blank.
return;
}
private int getLevel(String name) {
ConfigValue<Integer> level = new ConfigValue<Integer>(name, false);
if (level.hasValue()) {
@@ -24,7 +24,18 @@ import java.util.List;
public interface Groups {
/**
* Get all groups.
*
* @return List of groups.
*/
public List<Group> getGroups();
/**
* Get the highest level value assigned to a group.
* Used as a maximum cap to rising hack levels.
*
* @return Highest group level.
*/
public int getHighestLevel();
}
@@ -24,9 +24,32 @@ import java.util.List;
public interface Levels {
/**
* Checks for a stored level for this user, and loads it to their object if found.
*
* @param user User to load.
*/
public void loadLevelToUser(User user);
/**
* Saves the user's level to storage.
*
* @param user User to save.
*/
public void saveLevelFromUser(User user);
/**
* Saves a list of user's levels to storage.
* Used on server shutdown / reload.
*
* @param users List of users to save.
*/
public void saveLevelsFromUsers(List<User> users);
/**
* Check to see if the user's level has been manually changed since it was loaded, and update it if needed
*
* @param user User to update.
*/
public void updateLevelToUser(User user);
}
@@ -25,5 +25,10 @@ import java.util.List;
public interface Rules {
/**
* Get all rules.
*
* @return List of rules.
*/
public List<Rule> getRules();
}