globalchat: switch to add(weight) using floats for ActionFrequency.

This commit is contained in:
asofold 2012-09-01 13:43:53 +02:00
parent 520add8440
commit 9c8ad6ac1b
3 changed files with 35 additions and 18 deletions

View File

@ -71,8 +71,8 @@ public class ChatConfig implements CheckConfig {
public final ActionList colorActions; public final ActionList colorActions;
public final boolean globalChatCheck; public final boolean globalChatCheck;
public double globalChatFrequencyFactor; public final float globalChatFrequencyFactor;
public final double globalChatFrequencyWeight; public final float globalChatFrequencyWeight;
public final double globalChatLevel; public final double globalChatLevel;
public final ActionList globalChatActions; public final ActionList globalChatActions;
@ -143,8 +143,8 @@ public class ChatConfig implements CheckConfig {
colorActions = data.getActionList(ConfPaths.CHAT_COLOR_ACTIONS, Permissions.CHAT_COLOR); colorActions = data.getActionList(ConfPaths.CHAT_COLOR_ACTIONS, Permissions.CHAT_COLOR);
globalChatCheck = data.getBoolean(ConfPaths.CHAT_GLOBALCHAT_CHECK); globalChatCheck = data.getBoolean(ConfPaths.CHAT_GLOBALCHAT_CHECK);
globalChatFrequencyFactor = data.getDouble(ConfPaths.CHAT_GLOBALCHAT_FREQUENCY_FACTOR); globalChatFrequencyFactor = (float) data.getDouble(ConfPaths.CHAT_GLOBALCHAT_FREQUENCY_FACTOR);
globalChatFrequencyWeight = data.getDouble(ConfPaths.CHAT_GLOBALCHAT_FREQUENCY_WEIGHT); globalChatFrequencyWeight = (float) data.getDouble(ConfPaths.CHAT_GLOBALCHAT_FREQUENCY_WEIGHT);
globalChatLevel = data.getDouble(ConfPaths.CHAT_GLOBALCHAT_LEVEL); globalChatLevel = data.getDouble(ConfPaths.CHAT_GLOBALCHAT_LEVEL);
globalChatActions = data.getActionList(ConfPaths.CHAT_GLOBALCHAT_ACTIONS, Permissions.CHAT_GLOBALCHAT); globalChatActions = data.getActionList(ConfPaths.CHAT_GLOBALCHAT_ACTIONS, Permissions.CHAT_GLOBALCHAT);

View File

@ -19,10 +19,13 @@ public class GlobalChat extends Check{
} }
/** /**
* * Start analysis.
* @param player * @param player
* The player who issued the message.
* @param message * @param message
* @param captcha Used for starting captcha on failure. * The message to check.
* @param captcha
* Used for starting captcha on failure, if configured so.
* @return * @return
*/ */
public boolean check(final Player player, final String message, final ICaptcha captcha) { public boolean check(final Player player, final String message, final ICaptcha captcha) {
@ -40,6 +43,15 @@ public class GlobalChat extends Check{
} }
} }
/**
* Check without further synchronization.
* @param player
* @param message
* @param captcha
* @param cc
* @param data
* @return
*/
private boolean unsafeCheck(final Player player, final String message, final ICaptcha captcha, private boolean unsafeCheck(final Player player, final String message, final ICaptcha captcha,
final ChatConfig cc, final ChatData data) { final ChatConfig cc, final ChatData data) {
// Take time once: // Take time once:
@ -47,14 +59,21 @@ public class GlobalChat extends Check{
boolean cancel = false; boolean cancel = false;
data.globalChatFrequency.add(time); // Update the frequency interval weights.
double score = cc.globalChatFrequencyWeight * data.globalChatFrequency.getScore(cc.globalChatFrequencyFactor); data.globalChatFrequency.update(time);
if (score < 2.0 * cc.globalChatFrequencyWeight) double score = 0;
if (score < cc.globalChatFrequencyWeight)
// Reset the VL. // Reset the VL.
data.globalChatVL = 0.0; data.globalChatVL = 0.0;
// Weight of this chat message.
float weight = 1.0f;
// TODO Core checks.... // TODO Core checks....
// Add weight to frequency counts.
data.globalChatFrequency.add(time, weight);
score += cc.globalChatFrequencyWeight * data.globalChatFrequency.getScore(cc.globalChatFrequencyFactor);
if (score > cc.globalChatLevel){ if (score > cc.globalChatLevel){
if (captcha.shouldStartCaptcha(cc, data)){ if (captcha.shouldStartCaptcha(cc, data)){
captcha.sendNewCaptcha(player, cc, data); captcha.sendNewCaptcha(player, cc, data);

View File

@ -12,12 +12,12 @@ public class ActionFrequency {
/** Reference time for filling in. */ /** Reference time for filling in. */
long time = 0; long time = 0;
final int[] buckets; final float[] buckets;
final long durBucket; final long durBucket;
public ActionFrequency(final int nBuckets, final long durBucket){ public ActionFrequency(final int nBuckets, final long durBucket){
this.buckets = new int[nBuckets]; this.buckets = new float[nBuckets];
this.durBucket = durBucket; this.durBucket = durBucket;
} }
@ -25,8 +25,9 @@ public class ActionFrequency {
* Update and add. * Update and add.
* @param ts * @param ts
*/ */
public void add(final long now){ public void add(final long now, final float amount){
update(now); update(now);
buckets[0] += amount;
} }
/** /**
@ -38,13 +39,11 @@ public class ActionFrequency {
final int shift = (int) ((float) diff / (float) durBucket); final int shift = (int) ((float) diff / (float) durBucket);
if (shift == 0){ if (shift == 0){
// No update, just fill in. // No update, just fill in.
buckets[0] ++;
return; return;
} }
else if (shift >= buckets.length){ else if (shift >= buckets.length){
// Clear and fill in (beyond range). // Clear and fill in (beyond range).
clear(now); clear(now);
buckets[0] ++;
return; return;
} }
// Update buckets. // Update buckets.
@ -54,7 +53,6 @@ public class ActionFrequency {
for (int i = 0; i < shift; i++){ for (int i = 0; i < shift; i++){
buckets[i] = 0; buckets[i] = 0;
} }
buckets[0] ++;
// Set time according to bucket duration (!). // Set time according to bucket duration (!).
time += durBucket * shift; time += durBucket * shift;
} }
@ -72,9 +70,9 @@ public class ActionFrequency {
* @param factor * @param factor
* @return * @return
*/ */
public double getScore(final double factor){ public float getScore(final float factor){
double res = buckets[0]; float res = buckets[0];
double cf = factor; float cf = factor;
for (int i = 1; i < buckets.length; i++){ for (int i = 1; i < buckets.length; i++){
res += cf * buckets[i]; res += cf * buckets[i];
cf *= factor; cf *= factor;