Create tables and clean up code

This commit is contained in:
h31ix committed 2013-10-10 18:20:59 -04:00
1 parent 25fe36d21e
commit 0c6db1fed9
3 files changed
+35 -42

No files matched your search

@@ -36,7 +36,6 @@ public class Enterprise extends ConfigurationFile {
public ConfigValue<Integer> loggingInterval; public ConfigValue<Integer> loggingInterval;
public ConfigValue<Boolean> usersEnabled; public ConfigValue<Boolean> usersEnabled;
public ConfigValue<Integer> usersInterval;
public Database database; public Database database;
@@ -53,7 +52,6 @@ public class Enterprise extends ConfigurationFile {
loggingInterval = new ConfigValue<Integer>("logging.interval"); loggingInterval = new ConfigValue<Integer>("logging.interval");
usersEnabled = new ConfigValue<Boolean>("users.enable"); usersEnabled = new ConfigValue<Boolean>("users.enable");
usersInterval = new ConfigValue<Integer>("users.interval");
ConfigValue<String> databaseType = new ConfigValue<String>("database.type"); ConfigValue<String> databaseType = new ConfigValue<String>("database.type");
ConfigValue<String> databaseHostname = new ConfigValue<String>("database.hostname"); ConfigValue<String> databaseHostname = new ConfigValue<String>("database.hostname");
@@ -74,7 +72,6 @@ public class Enterprise extends ConfigurationFile {
databaseSchema.getValue(), databaseSchema.getValue(),
serverName.getValue(), serverName.getValue(),
loggingInterval.getValue(), loggingInterval.getValue(),
usersInterval.getValue(),
loggingLife.getValue() loggingLife.getValue()
); );
} }
@@ -36,6 +36,9 @@ public class Database {
private String sqlSyncFrom; private String sqlSyncFrom;
private String sqlCleanEvents; private String sqlCleanEvents;
private String sqlCreateEvents;
private String sqlCreateUsers;
public enum DatabaseType { public enum DatabaseType {
MySQL, MySQL,
} }
@@ -50,19 +53,16 @@ public class Database {
private String serverName; private String serverName;
private int eventInterval; private int eventInterval;
private int userInterval;
private int eventLife; private int eventLife;
private Connection connection; private Connection connection;
private PreparedStatement eventBatch; private PreparedStatement eventBatch;
private PreparedStatement syncBatch;
private BukkitTask eventTask; private BukkitTask eventTask;
private BukkitTask userTask;
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, String serverName, int eventInterval, int userInterval, int eventLife) { public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, String serverName, int eventInterval, int eventLife) {
this.type = type; this.type = type;
this.hostname = hostname; this.hostname = hostname;
this.port = port; this.port = port;
@@ -73,14 +73,28 @@ public class Database {
this.serverName = serverName; this.serverName = serverName;
this.eventInterval = eventInterval; this.eventInterval = eventInterval;
this.userInterval = userInterval;
this.eventLife = eventLife; this.eventLife = eventLife;
sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE + " (server, user, check_type) VALUES (?, ?, ?)"; sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE + " (server, user, check_type) VALUES (?, ?, ?)";
sqlSyncTo = "INSERT INTO " + prefix + USERS_TABLE + " (user, level) VALUES (?, ?) ON DUPLICATE KEY UPDATE level = ?, last_update=CURRENT_TIMESTAMP, last_update_server=?"; sqlSyncTo = "INSERT INTO " + prefix + USERS_TABLE + " (user, level, last_update_server) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE level = ?, last_update=CURRENT_TIMESTAMP, last_update_server=?";
sqlSyncFrom = "SELECT level FROM " + prefix + USERS_TABLE + " WHERE user = ?"; sqlSyncFrom = "SELECT level FROM " + prefix + USERS_TABLE + " WHERE user = ?";
sqlCleanEvents = "DELETE FROM " + prefix + EVENTS_TABLE + " WHERE time < (CURRENT_TIMESTAMP - INTERVAL ? DAY)"; sqlCleanEvents = "DELETE FROM " + prefix + EVENTS_TABLE + " WHERE time < (CURRENT_TIMESTAMP - INTERVAL ? DAY)";
sqlCreateUsers = "CREATE TABLE IF NOT EXISTS " + prefix + USERS_TABLE + "(" +
" `user` VARCHAR(45) NOT NULL," +
" `level` INT NOT NULL," +
" `last_update` TIMESTAMP NOT NULL DEFAULT NOW()," +
" `last_update_server` VARCHAR(45) NOT NULL," +
" PRIMARY KEY (`user`));";
sqlCreateEvents = "CREATE TABLE IF NOT EXISTS " + prefix + EVENTS_TABLE + "(" +
" `id` INT NOT NULL AUTO_INCREMENT," +
" `server` VARCHAR(45) NOT NULL," +
" `time` TIMESTAMP NOT NULL DEFAULT NOW()," +
" `user` VARCHAR(45) NOT NULL," +
" `check_type` VARCHAR(45) NOT NULL," +
" PRIMARY KEY (`id`));";
} }
public DatabaseType getType() { public DatabaseType getType() {
@@ -117,7 +131,6 @@ public class Database {
try { try {
connection = DriverManager.getConnection(url, username, password); connection = DriverManager.getConnection(url, username, password);
eventBatch = connection.prepareStatement(sqlLogEvent); eventBatch = connection.prepareStatement(sqlLogEvent);
syncBatch = connection.prepareStatement(sqlSyncTo);
connection.setAutoCommit(false); connection.setAutoCommit(false);
@@ -128,29 +141,24 @@ public class Database {
} }
}, eventInterval* 60 * 20, eventInterval * 60 * 20); }, eventInterval* 60 * 20, eventInterval * 60 * 20);
userTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable() {
@Override
public void run() {
flushSyncs();
}
}, userInterval * 60 * 20, userInterval * 60 * 20);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
//TODO: setup tables try {
connection.prepareStatement(sqlCreateUsers).executeUpdate();
connection.prepareStatement(sqlCreateEvents).executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} }
public void shutdown() { public void shutdown() {
eventTask.cancel(); eventTask.cancel();
userTask.cancel();
flushEvents(); flushEvents();
flushSyncs();
try { try {
eventBatch.close(); eventBatch.close();
syncBatch.close();
connection.close(); connection.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -175,14 +183,16 @@ public class Database {
public void syncTo(User user) { public void syncTo(User user) {
if (!user.isWaitingOnLevelSync()) { if (!user.isWaitingOnLevelSync()) {
System.out.println("Syncing to "+user.getName()+" value "+user.getLevel()); AntiCheat.debugLog("Syncing to "+user.getName()+" value "+user.getLevel());
try { try {
syncBatch.setString(1, user.getName()); PreparedStatement statement = connection.prepareStatement(sqlSyncTo);
syncBatch.setInt(2, user.getLevel()); statement.setString(1, user.getName());
syncBatch.setInt(3, user.getLevel()); statement.setInt(2, user.getLevel());
syncBatch.setString(4, serverName); statement.setString(3, serverName);
statement.setInt(4, user.getLevel());
statement.setString(5, serverName);
syncBatch.addBatch(); statement.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -199,7 +209,7 @@ public class Database {
ResultSet set = statement.executeQuery(); ResultSet set = statement.executeQuery();
while (set.next()) { while (set.next()) {
System.out.println("Added level for " + user.getName()+":" +set.getInt("level")); AntiCheat.debugLog("Syncing from "+user.getName()+" value "+set.getInt("level"));
user.setLevel(set.getInt("level")); user.setLevel(set.getInt("level"));
} }
} catch (SQLException e) { } catch (SQLException e) {
@@ -220,18 +230,6 @@ public class Database {
} }
} }
public void flushSyncs() {
System.out.println("Flushing syncs");
try {
syncBatch.executeBatch();
connection.commit();
syncBatch = connection.prepareStatement(sqlSyncTo);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void cleanEvents() { public void cleanEvents() {
Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable(){ Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable(){
@Override @Override
-2
View File
@@ -30,5 +30,3 @@ logging:
users: users:
# Synchronize user's levels in the database? # Synchronize user's levels in the database?
enable: true enable: true
# How often to query the database for synchronization? (in minutes)
interval: 1