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
+115 -9

No files matched your search

@@ -34,6 +34,7 @@ public class Enterprise extends ConfigurationFile {
public ConfigValue<String> loggingInterval; public ConfigValue<String> loggingInterval;
public ConfigValue<Boolean> syncLevels; public ConfigValue<Boolean> syncLevels;
public ConfigValue<String> syncInterval;
public ConfigValue<Boolean> configGroups; public ConfigValue<Boolean> configGroups;
public ConfigValue<Boolean> configRules; public ConfigValue<Boolean> configRules;
@@ -53,6 +54,7 @@ public class Enterprise extends ConfigurationFile {
loggingInterval = new ConfigValue<String>("log.interval"); loggingInterval = new ConfigValue<String>("log.interval");
syncLevels = new ConfigValue<Boolean>("sync.levels"); syncLevels = new ConfigValue<Boolean>("sync.levels");
syncInterval = new ConfigValue<String>("sync.interval");
configGroups = new ConfigValue<Boolean>("config.groups"); configGroups = new ConfigValue<Boolean>("config.groups");
configRules = new ConfigValue<Boolean>("config.rules"); configRules = new ConfigValue<Boolean>("config.rules");
@@ -77,7 +79,9 @@ public class Enterprise extends ConfigurationFile {
databaseSchema.getValue(), databaseSchema.getValue(),
serverName.getValue(), serverName.getValue(),
loggingInterval.getValue(), loggingInterval.getValue(),
loggingLife.getValue() loggingLife.getValue(),
syncLevels.getValue(),
syncInterval.getValue()
); );
database.connect(); database.connect();
@@ -33,9 +33,11 @@ import java.util.List;
public class MySQLLevelsHolder extends ConfigurationTable implements Levels { public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
private static final String TABLE = "levels"; private static final String TABLE = "levels";
private static final String MANUAL_EDIT_TAG = "MANUAL";
private String sqlSave; private String sqlSave;
private String sqlLoad; private String sqlLoad;
private String sqlUpdate;
public MySQLLevelsHolder(Configuration config) { public MySQLLevelsHolder(Configuration config) {
super(config, TABLE); super(config, TABLE);
@@ -54,6 +56,9 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
sqlLoad = "SELECT level, last_update FROM " + getFullTable() + " " + sqlLoad = "SELECT level, last_update FROM " + getFullTable() + " " +
"WHERE user = ?"; "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() + "(" + String sqlCreate = "CREATE TABLE IF NOT EXISTS " + getFullTable() + "(" +
" `user` VARCHAR(45) NOT NULL," + " `user` VARCHAR(45) NOT NULL," +
" `level` INT NOT NULL," + " `level` INT NOT NULL," +
@@ -75,7 +80,7 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
@Override @Override
public void run() { public void run() {
if (!user.isWaitingOnLevelSync()) { if (!user.isWaitingOnLevelSync()) {
AntiCheat.debugLog("Syncing to " + user.getName() + " value " + user.getLevel()); AntiCheat.debugLog("Saving level from " + user.getName() + ". Value: " + user.getLevel());
try { try {
PreparedStatement statement = getConnection().prepareStatement(sqlSave); PreparedStatement statement = getConnection().prepareStatement(sqlSave);
statement.setString(1, user.getName()); statement.setString(1, user.getName());
@@ -110,7 +115,7 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
boolean has = false; boolean has = false;
while (set.next()) { while (set.next()) {
has = true; 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.setLevel(set.getInt("level"));
user.setLevelSyncTimestamp(set.getTimestamp("last_update")); user.setLevelSyncTimestamp(set.getTimestamp("last_update"));
} }
@@ -149,4 +154,29 @@ public class MySQLLevelsHolder extends ConfigurationTable implements Levels {
e.printStackTrace(); 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) { private int getLevel(String name) {
ConfigValue<Integer> level = new ConfigValue<Integer>(name, false); ConfigValue<Integer> level = new ConfigValue<Integer>(name, false);
if (level.hasValue()) { if (level.hasValue()) {
@@ -24,7 +24,18 @@ import java.util.List;
public interface Groups { public interface Groups {
/**
* Get all groups.
*
* @return List of groups.
*/
public List<Group> getGroups(); 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(); public int getHighestLevel();
} }
@@ -24,9 +24,32 @@ import java.util.List;
public interface Levels { 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); public void loadLevelToUser(User user);
/**
* Saves the user's level to storage.
*
* @param user User to save.
*/
public void saveLevelFromUser(User user); 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); 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 { public interface Rules {
/**
* Get all rules.
*
* @return List of rules.
*/
public List<Rule> getRules(); public List<Rule> getRules();
} }
@@ -86,8 +86,7 @@ public class AnticheatManager {
if (i != 1 && getConfiguration().getConfig().logToConsole.getValue()) { if (i != 1 && getConfiguration().getConfig().logToConsole.getValue()) {
Bukkit.getConsoleSender().sendMessage("[AntiCheat] " + ChatColor.RED + message); 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); fileLog(message);
} else if (getConfiguration().getConfig().fileLogLevel.getValue() != 0) { // alert } else if (getConfiguration().getConfig().fileLogLevel.getValue() != 0) { // alert
fileLog(message); fileLog(message);
@@ -400,4 +400,9 @@ public class User {
public Timestamp getLevelSyncTimestamp() { public Timestamp getLevelSyncTimestamp() {
return levelSyncTimestamp; return levelSyncTimestamp;
} }
@Override
public String toString() {
return "User {name = " + name + ", level = " + level + "}";
}
} }
@@ -52,16 +52,19 @@ public class Database {
private String serverName; private String serverName;
private long logInterval; private long logInterval;
private long logLife; private long logLife;
private boolean syncLevels;
private long syncInterval;
private Connection connection; private Connection connection;
private PreparedStatement eventBatch; private PreparedStatement eventBatch;
private BukkitTask eventTask; 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.type = type;
this.hostname = hostname; this.hostname = hostname;
this.port = port; this.port = port;
@@ -72,9 +75,11 @@ public class Database {
this.serverName = serverName; this.serverName = serverName;
this.logInterval = Utilities.lifeToSeconds(logInterval); this.logInterval = Utilities.lifeToSeconds(logInterval);
this.logLife = Utilities.lifeToSeconds(logLife); this.logLife = Utilities.lifeToSeconds(logLife);
this.syncLevels = syncLevels;
this.syncInterval = Utilities.lifeToSeconds(syncInterval);
sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE + sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE +
" (server, user, check_type) " + " (server, user, check_type) " +
"VALUES (?, ?, ?)"; "VALUES (?, ?, ?)";
@@ -139,6 +144,15 @@ public class Database {
}, logInterval * 20, logInterval * 20); }, 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."); AntiCheat.getPlugin().verboseLog("Connected to the database.");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -146,7 +160,8 @@ public class Database {
} }
public void shutdown() { public void shutdown() {
eventTask.cancel(); if (eventTask != null) eventTask.cancel();
if (syncTask != null) syncTask.cancel();
flushEvents(); flushEvents();
@@ -205,4 +220,10 @@ public class Database {
}); });
} }
} }
private void syncUsers() {
for (User user : AntiCheat.getManager().getUserManager().getUsers()) {
AntiCheat.getManager().getConfiguration().getLevels().updateLevelToUser(user);
}
}
} }
+2
View File
@@ -32,6 +32,8 @@ log:
sync: sync:
# Keep user's levels synced in the database? # Keep user's levels synced in the database?
levels: true levels: true
# How often to check for changes to user's levels? (you can use any combination of d,h,m,s)
interval: 1m
# Configuration settings # Configuration settings
config: config:
# Load groups configuration from the database? # Load groups configuration from the database?