#include <adminlib>
#include <plugin>
 
#define MAX_TIME 30
 
new g_Version[] = "0.8";
new g_LastTime[MAX_PLAYERS];
new g_NumSaid[MAX_PLAYERS][MAX_TIME];
new g_GagTime[MAX_PLAYERS];
new g_LIMIT = 3;
new g_TIME = 3;
new g_LIMIT2 = 10;
new g_TIME2 = 15;
new g_war;
 
public plugin_init() {
    plugin_registerinfo("Rinde's Autogag Plugin","Auto gags flooders",g_Version);
    plugin_registercmd("say","HandleSay",ACCESS_ALL,"");
    plugin_registercmd("say_team","HandleSay",ACCESS_ALL,"");
    if(!get_vaultnumdata("AF_LIMIT",g_LIMIT)) {
        set_vaultnumdata("AF_LIMIT",g_LIMIT);
    }
    if(!get_vaultnumdata("AF_TIME",g_TIME)) {
        set_vaultnumdata("AF_TIME",g_TIME);
    }
    if(!get_vaultnumdata("AF_LIMIT2",g_LIMIT2)) {
        set_vaultnumdata("AF_LIMIT2",g_LIMIT2);
    }
    if(!get_vaultnumdata("AF_TIME2",g_TIME2)) {
        set_vaultnumdata("AF_TIME2",g_TIME2);
    }
    g_TIME = min(MAX_TIME,g_TIME);
    g_TIME2 = min(MAX_TIME,g_TIME2);
    return PLUGIN_CONTINUE;
}
 
public plugin_connect(HLUserName,HLIP,UserIndex) {
    updatewar();
    g_LastTime[UserIndex] = 0;
    g_GagTime[UserIndex] = 0;
    return PLUGIN_CONTINUE;
}
 
 
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
    if(g_war) return PLUGIN_CONTINUE;
    if(access(ACCESS_IMMUNITY,"")) return PLUGIN_CONTINUE;
    new CurTime = systemtime();
    if(g_LastTime[UserIndex] + g_GagTime[UserIndex] > CurTime) {
        g_GagTime[UserIndex] += 2;
        new UserName[MAX_NAME_LENGTH];
        new Text[80];
        convert_string(HLUserName,UserName,MAX_NAME_LENGTH);
        snprintf(Text,80,"You are still flooding the server and can't chat the next %i seconds.",g_LastTime[UserIndex] - CurTime + g_GagTime[UserIndex]);
        messageex(UserName,Text,print_chat);
        selfmessage(Text);
        return PLUGIN_HANDLED;
    }
    new SecsAgo = CurTime - g_LastTime[UserIndex];
    new i,j;
    new Total;
    g_LastTime[UserIndex] = CurTime;
    g_GagTime[UserIndex] = 0;
    j = max(SecsAgo,g_TIME);
    for(i=g_TIME2-1;i>=j;i--) {
        g_NumSaid[UserIndex][i] = g_NumSaid[UserIndex][i-SecsAgo];
        Total += g_NumSaid[UserIndex][i];
    }
    i = Total;
    for(j=j-1;j>=SecsAgo;j--) {
        g_NumSaid[UserIndex][j] = g_NumSaid[UserIndex][j-SecsAgo];
        Total += g_NumSaid[UserIndex][j];
    }
    Total += 1;
    if(Total - i >= g_LIMIT) g_GagTime[UserIndex] = g_TIME;
    if(Total >= g_LIMIT2) g_GagTime[UserIndex] += g_TIME2;
    for(i=min(SecsAgo,g_TIME2)-1;i>=0;i--) {
        g_NumSaid[UserIndex][i] = 0;
    }
    g_NumSaid[UserIndex][0] += 1;
    if(!g_GagTime[UserIndex]) return PLUGIN_CONTINUE;
    new UserName[MAX_NAME_LENGTH];
    new Text[80];
    convert_string(HLUserName,UserName,MAX_NAME_LENGTH);
    snprintf(Text,80,"You are flooding the server and can't chat the next %i seconds.",g_GagTime[UserIndex]);
    messageex(UserName,Text,print_chat);
    return PLUGIN_HANDLED;
}
 
updatewar() {
    static value[10];
    get_serverinfo("war",value,10);
    g_war = check_param(value);
}