Allow finer specification of intervals

This commit is contained in:
h31ix committed 2013-10-12 23:59:42 -04:00
1 parent a08a558bc2
commit db77040560
5 files changed
+49 -23

No files matched your search

@@ -30,8 +30,8 @@ public class Enterprise extends ConfigurationFile {
public ConfigValue<String> serverName; public ConfigValue<String> serverName;
public ConfigValue<Boolean> loggingEnabled; public ConfigValue<Boolean> loggingEnabled;
public ConfigValue<Integer> loggingLife; public ConfigValue<String> loggingLife;
public ConfigValue<Integer> loggingInterval; public ConfigValue<String> loggingInterval;
public ConfigValue<Boolean> syncUsers; public ConfigValue<Boolean> syncUsers;
@@ -49,8 +49,8 @@ public class Enterprise extends ConfigurationFile {
serverName = new ConfigValue<String>("server.name"); serverName = new ConfigValue<String>("server.name");
loggingEnabled = new ConfigValue<Boolean>("log.enable"); loggingEnabled = new ConfigValue<Boolean>("log.enable");
loggingLife = new ConfigValue<Integer>("log.life"); loggingLife = new ConfigValue<String>("log.life");
loggingInterval = new ConfigValue<Integer>("log.interval"); loggingInterval = new ConfigValue<String>("log.interval");
syncUsers = new ConfigValue<Boolean>("sync.users"); syncUsers = new ConfigValue<Boolean>("sync.users");
@@ -54,7 +54,7 @@ public class User {
*/ */
public User(String name) { public User(String name) {
this.name = name; this.name = name;
this.id = getPlayer().isOnline() ? getPlayer().getEntityId() : -1; this.id = getPlayer() != null && getPlayer().isOnline() ? getPlayer().getEntityId() : -1;
} }
/** /**
@@ -33,6 +33,8 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class Utilities { public final class Utilities {
private static final List<Material> INSTANT_BREAK = new ArrayList<Material>(); private static final List<Material> INSTANT_BREAK = new ArrayList<Material>();
@@ -475,6 +477,27 @@ public final class Utilities {
return b.toString(); return b.toString();
} }
/**
* Parse a string in the format of "XdXhXmXs" to seconds
* @param string The string to parse
* @return seconds
*/
public static long lifeToSeconds(String string) {
if (string.equals("0") || string.equals("")) return 0;
String[] lifeMatch = new String[]{ "d", "h", "m", "s" };
int[] lifeInterval = new int[]{ 86400, 3600, 60, 1 };
long seconds = 0L;
for (int i=0;i<lifeMatch.length;i++) {
Matcher matcher = Pattern.compile("([0-9]*)" + lifeMatch[i]).matcher(string);
while (matcher.find()) {
seconds += Integer.parseInt(matcher.group(1)) * lifeInterval[i];
}
}
return seconds;
}
static { static {
// START INSTANT BREAK MATERIALS // START INSTANT BREAK MATERIALS
@@ -21,6 +21,7 @@ package net.h31ix.anticheat.util.enterprise;
import net.h31ix.anticheat.AntiCheat; import net.h31ix.anticheat.AntiCheat;
import net.h31ix.anticheat.manage.CheckType; import net.h31ix.anticheat.manage.CheckType;
import net.h31ix.anticheat.manage.User; import net.h31ix.anticheat.manage.User;
import net.h31ix.anticheat.util.Utilities;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@@ -50,9 +51,9 @@ public class Database {
private String schema; private String schema;
private String serverName; private String serverName;
private int eventInterval; private long logInterval;
private int eventLife; private long logLife;
private Connection connection; private Connection connection;
@@ -60,7 +61,7 @@ public class Database {
private BukkitTask eventTask; private BukkitTask eventTask;
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, String serverName, int eventInterval, int eventLife) { public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, String serverName, String logInterval, String logLife) {
this.type = type; this.type = type;
this.hostname = hostname; this.hostname = hostname;
this.port = port; this.port = port;
@@ -70,16 +71,16 @@ public class Database {
this.schema = schema; this.schema = schema;
this.serverName = serverName; this.serverName = serverName;
this.eventInterval = eventInterval; this.logInterval = Utilities.lifeToSeconds(logInterval);
this.eventLife = eventLife; this.logLife = Utilities.lifeToSeconds(logLife);
sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE + sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE +
" (server, user, check_type) " + " (server, user, check_type) " +
"VALUES (?, ?, ?)"; "VALUES (?, ?, ?)";
sqlCleanEvents = "DELETE FROM " + prefix + EVENTS_TABLE + " " + sqlCleanEvents = "DELETE FROM " + prefix + EVENTS_TABLE + " " +
"WHERE time < (CURRENT_TIMESTAMP - INTERVAL ? DAY)"; "WHERE time < (CURRENT_TIMESTAMP - INTERVAL ? SECOND)";
sqlCreateEvents = "CREATE TABLE IF NOT EXISTS " + prefix + EVENTS_TABLE + "(" + sqlCreateEvents = "CREATE TABLE IF NOT EXISTS " + prefix + EVENTS_TABLE + "(" +
" `id` INT NOT NULL AUTO_INCREMENT," + " `id` INT NOT NULL AUTO_INCREMENT," +
@@ -129,12 +130,14 @@ public class Database {
connection.setAutoCommit(false); connection.setAutoCommit(false);
eventTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable() { if (logInterval != 0) {
@Override eventTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable() {
public void run() { @Override
flushEvents(); public void run() {
} flushEvents();
}, eventInterval * 60 * 20, eventInterval * 60 * 20); }
}, logInterval * 20, logInterval * 20);
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -182,13 +185,13 @@ public class Database {
} }
public void cleanEvents() { public void cleanEvents() {
if (eventLife != 0) { if (logLife != 0) {
Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
PreparedStatement statement = connection.prepareStatement(sqlCleanEvents); PreparedStatement statement = connection.prepareStatement(sqlCleanEvents);
statement.setInt(1, eventLife); statement.setLong(1, logLife);
statement.executeUpdate(); statement.executeUpdate();
+4 -4
View File
@@ -24,10 +24,10 @@ database:
log: log:
# Upload cheat logs to the database? # Upload cheat logs to the database?
enable: true enable: true
# Time for logs to stay in the database (in days) # Time for logs to stay in the database (you can use any combination of d,h,m,s). Set to '' to disable pruning.
life: 2 life: 2d
# How often to upload logs to the database (in minutes) # How often to upload logs to the database (you can use any combination of d,h,m,s)
interval: 5 interval: 5m
# Syncing settings # Syncing settings
sync: sync:
# Keep user's levels synced in the database? # Keep user's levels synced in the database?