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();
}
@@ -86,8 +86,7 @@ public class AnticheatManager {
if (i != 1 && getConfiguration().getConfig().logToConsole.getValue()) {
Bukkit.getConsoleSender().sendMessage("[AntiCheat] " + ChatColor.RED + message);
}
if (i == 0 && getConfiguration().getConfig().fileLogLevel.getValue() == LOG_LEVEL_HIGH) // Not an alert, normal log message
{
if (i == 0 && getConfiguration().getConfig().fileLogLevel.getValue() == LOG_LEVEL_HIGH) { // Not an alert, normal log message
fileLog(message);
} else if (getConfiguration().getConfig().fileLogLevel.getValue() != 0) { // alert
fileLog(message);
@@ -400,4 +400,9 @@ public class User {
public Timestamp getLevelSyncTimestamp() {
return levelSyncTimestamp;
}
@Override
public String toString() {
return "User {name = " + name + ", level = " + level + "}";
}
}
@@ -52,16 +52,19 @@ public class Database {
private String serverName;
private long logInterval;
private long logLife;
private boolean syncLevels;
private long syncInterval;
private Connection connection;
private PreparedStatement eventBatch;
private BukkitTask eventTask;
private BukkitTask syncTask;
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, String serverName, String logInterval, String logLife) {
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, String serverName, String logInterval, String logLife, boolean syncLevels, String syncInterval) {
this.type = type;
this.hostname = hostname;
this.port = port;
@@ -72,9 +75,11 @@ public class Database {
this.serverName = serverName;
this.logInterval = Utilities.lifeToSeconds(logInterval);
this.logLife = Utilities.lifeToSeconds(logLife);
this.syncLevels = syncLevels;
this.syncInterval = Utilities.lifeToSeconds(syncInterval);
sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE +
" (server, user, check_type) " +
"VALUES (?, ?, ?)";
@@ -139,6 +144,15 @@ public class Database {
}, logInterval * 20, logInterval * 20);
}
if(syncLevels && syncInterval != 0) {
syncTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable() {
@Override
public void run() {
syncUsers();
}
}, syncInterval * 20, syncInterval * 20);
}
AntiCheat.getPlugin().verboseLog("Connected to the database.");
} catch (SQLException e) {
e.printStackTrace();
@@ -146,7 +160,8 @@ public class Database {
}
public void shutdown() {
eventTask.cancel();
if (eventTask != null) eventTask.cancel();
if (syncTask != null) syncTask.cancel();
flushEvents();
@@ -205,4 +220,10 @@ public class Database {
});
}
}
private void syncUsers() {
for (User user : AntiCheat.getManager().getUserManager().getUsers()) {
AntiCheat.getManager().getConfiguration().getLevels().updateLevelToUser(user);
}
}
}