Hello. Long time no see.
I am making a small enhancement of the anitflood plugin.
The modified version shall block a chat message only when it is the n
th
in a certain time frame. The server shall be able to set two such time
frames with limit value. My problem is, that my iteration will only work
for the timeframe with the greater limit. The other one won't do anything.
Now I know this code is a bit cumbersome, but a challenge should be
nice in a while, or not?
:p
I am using Float:g_FloodTime[33][MAX_FLOOD_COUNT] as a ringlist.
That means that when I reach its end, I go back to its start. The variable
contains a sort of log of every players' MAX_FLOOD_COUNT recent
messages' gametime.
The problematic loop is the one using static g down in public chkFlood(id).
Code:
/* Modified version of AMXModX Developer Team's Anti Flood Script by Silencer */
#include <amxmodx>
new const VERSION[] = "1.0";
const MAX_FLOOD_COUNT = 32;
const Float:WARNING_DECAY = 1.0;
new Float:g_FloodTime[33][MAX_FLOOD_COUNT];
new Float:g_PreviousWarning[33] = {0.0, ...};
new g_LastFlood[33] = {0, ...};
new amx_flood_time;
new amx_flood_count;
new amx_flood_time2;
new amx_flood_count2;
public plugin_init() {
register_plugin("Anti Flood", VERSION, "Silencer");
register_clcmd("say", "chkFlood");
register_clcmd("say_team", "chkFlood");
amx_flood_time = register_cvar("amx_flood_time", "2.5");
amx_flood_count = register_cvar("amx_flood_count", "4");
amx_flood_time2 = register_cvar("amx_flood_time2", "30.0");
amx_flood_count2 = register_cvar("amx_flood_count2", "12");
for(new i = 0; i < 33; i++) {
for(new j = 0; j < MAX_FLOOD_COUNT; j++) {
g_FloodTime[i][j] = -1000.0;
}
}
}
public chkFlood(id) {
static maxCountCVAR[2];
maxCountCVAR[0] = min(get_pcvar_num(amx_flood_count), MAX_FLOOD_COUNT);
maxCountCVAR[1] = min(get_pcvar_num(amx_flood_count2), MAX_FLOOD_COUNT);
static Float:maxChatCVAR[2];
maxChatCVAR[0] = get_pcvar_float(amx_flood_time);
maxChatCVAR[1] = get_pcvar_float(amx_flood_time2);
g_LastFlood[id] = (g_LastFlood[id] == 0) ? (MAX_FLOOD_COUNT - 1) : (g_LastFlood[id] - 1);
static Float:currentTime;
currentTime = get_gametime();
g_FloodTime[id][g_LastFlood[id]] = currentTime;
static i;
static f;
static g;
for(g = 1; g >= 0; g--) {
f = 0;
static stopChk;
stopChk = (g_LastFlood[id] + maxCountCVAR[g]) % (MAX_FLOOD_COUNT);
static bool:firstIteration;
firstIteration = true;
static bool:lastIteration;
lastIteration = false;
for(i = g_LastFlood[id]; (firstIteration | lastIteration) ? (firstIteration) : ((i != stopChk) ? (true) : (lastIteration = true == true)); i = ((i + 1) % (MAX_FLOOD_COUNT))) {
if((g_FloodTime[id][i] + maxChatCVAR[g]) > currentTime) {
f++;
} else {
break;
}
firstIteration = false;
}
if(f >= maxCountCVAR[g]) {
g_FloodTime[id][g_LastFlood[id]] = -1000.0;
g_LastFlood[id] = (g_LastFlood[id] + 1) % (MAX_FLOOD_COUNT);
if(g_PreviousWarning[id] + WARNING_DECAY < currentTime) {
g_PreviousWarning[id] = currentTime;
client_print(id, print_chat, "Stop spamming! Wait %.1f seconds.", maxChatCVAR[g] - (currentTime - g_FloodTime[id][(stopChk == 0) ? (stopChk = MAX_FLOOD_COUNT - 1) : (stopChk - 1)]));
}
return PLUGIN_HANDLED;
}
}
return PLUGIN_CONTINUE;
}
__________________