Raised This Month: $ Target: $400
 0% 

Trying to understand the antiflood plugin...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hristosax
Junior Member
Join Date: Jul 2008
Old 08-23-2010 , 17:47   Trying to understand the antiflood plugin...
Reply With Quote #1

Greetings everybody,

I honestly don't know where to post this, since there aren't any sections for this type of questions. Also, I've searched about this and couldn't find an answer.

This is a very... uhh.. basic question that you all may find absolutely hilarious, but:

Before I started coding on my own I decided to learn from existing plugins, and started with a basic one, antiflood.amxx, which is included in AMXX already.

Here's the code:
PHP Code:
new Float:g_Flooding[33] = {0.0, ...}
new 
g_Flood[33] = {0, ...}

new 
amx_flood_time;

public 
plugin_init()
{
    
register_plugin("Anti Flood"AMXX_VERSION_STR"AMXX Dev Team")
    
register_dictionary("antiflood.txt")
    
register_clcmd("say""chkFlood")
    
register_clcmd("say_team""chkFlood")
    
amx_flood_time=register_cvar("amx_flood_time""0.75")
}

public 
chkFlood(id)
{
    new 
Float:maxChat get_pcvar_float(amx_flood_time)

    if (
maxChat)
    {
        new 
Float:nexTime get_gametime()
        
        if (
g_Flooding[id] > nexTime)
        {
            if (
g_Flood[id] >= 3)
            {
                
client_print(idprint_notify"** %L **"id"STOP_FLOOD")
                
g_Flooding[id] = nexTime maxChat 3.0
                
return PLUGIN_HANDLED
            
}
            
g_Flood[id]++
        }
        else if (
g_Flood[id])
        {
            
g_Flood[id]--
        }
        
        
g_Flooding[id] = nexTime maxChat
    
}

    return 
PLUGIN_CONTINUE

I came across this:
PHP Code:
if (maxChat)
    {
        new 
Float:nexTime get_gametime()
        
        if (
g_Flooding[id] > nexTime)
        {
            if (
g_Flood[id] >= 3)
            {
                
client_print(idprint_notify"** %L **"id"STOP_FLOOD")
                
g_Flooding[id] = nexTime maxChat 3.0
                
return PLUGIN_HANDLED
            
}
            
g_Flood[id]++ 
And I'm just struggling to understand how "id" (which I believe is the player index) fits into this and how this all works. It just doesn't make sense to me and I've had previous experience with programming. The whole sequence just seems illogical.

Uhh... Don't make fun of me

I've gone through the tutorials and have an understanding of pretty much everything they covered.


Cheers
hristosax is offline
Kreation
Veteran Member
Join Date: Jan 2010
Location: Illinois
Old 08-24-2010 , 01:38   Re: Trying to understand the antiflood plugin...
Reply With Quote #2

For the fact you can talk proper and I see a future here for you, I'll answer this when I get on my computer.

EDIT: Arkshine beat me to it. :sadface:
__________________
Hi.

Last edited by Kreation; 08-24-2010 at 12:07.
Kreation is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-24-2010 , 07:50   Re: Trying to understand the antiflood plugin...
Reply With Quote #3

The player's index is passed into chkFlood() function. It's a callback from register_clcmd("say", "chkFlood"). This one registers a client command ( say/say_team, you are aware about them, right? ). So, when a player will say something chkFlood will be called and the player's index is passed into the callback. (You have just to write something in the function header, then you can use it. In this plugin you see "id", but you can write what you want).

You don't understand also what does the code ?
__________________
Arkshine is offline
hristosax
Junior Member
Join Date: Jul 2008
Old 08-24-2010 , 08:21   Re: Trying to understand the antiflood plugin...
Reply With Quote #4

Quote:
Originally Posted by Arkshine View Post
The player's index is passed into chkFlood() function. It's a callback from register_clcmd("say", "chkFlood"). This one registers a client command ( say/say_team, you are aware about them, right? ). So, when a player will say something chkFlood will be called and the player's index is passed into the callback. (You have just to write something in the function header, then you can use it. In this plugin you see "id", but you can write what you want).

You don't understand also what does the code ?
Thanks for the response, but my doubts are only about the part of the code I later separated, starting from
PHP Code:
if (maxchat
. I don't understand what the "id" in the g_flooding[] float array does there and the logic in the code afterwards just doesn't make any sense to me.

Cheers
hristosax is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-24-2010 , 10:38   Re: Trying to understand the antiflood plugin...
Reply With Quote #5

Sorry. Will try to explain.

First read that, it explains what is an array : http://forums.alliedmods.net/showthr...t=94381#arrays


Now, you are aware what does the plugin, it checks each time what you say and see if you chat too fastly or not.
  • nexTime : contains the current game time, which is actually the number of seconds since the game has started.
  • maxChat : contains the value from the amx_flood_time cvar, it tells you how fast you can chat, so the time between 2 messages.

To know if you flood you need to compare 2 times, the last time you have said something and the next time you have said something else. The game time is here as reference.
  • g_Flooding : contains the next time you are allowed to say something. In others words, it will contain the current game time + the value from amx_flood_time cvar.
  • g_Flood : contains the number of times you flood. The more you flood the more you get the warning message.

So, what does the code ? Forget the part with g_Flood for now.

Code:
if (maxChat) {     //It retrieves the current game time     new Float:nexTime = get_gametime()         // It checks if the last time you have said something is > to the current game time.     // In others words, if the old game time + the flood time allowed is still > to the current game time.     // If so, it means you're going to flood.     // Example : old game time : 65, flood time allowed : 0.75, current game time : 65.2     //  => if ( 65 + 0.75 > 65.2 )       //  => Yes and you have to wait  65.75 - 65.2 = 0.55 second to be able to chat.     if (g_Flooding[id] > nexTime)     {         // If you are here, it means you flood.     }         // We update the last time for the next time the player will say something.     // Example : current game time : 65.2     //  => it means the next time you are allowed to chat will be at : 65.2 + 0.75 = 65.95     g_Flooding[id] = nexTime + maxChat }

About g_Flood, its purpose is just to manage when the warning message should appear :

Code:
if (maxChat) {     new Float:nexTime = get_gametime()         if (g_Flooding[id] > nexTime)     {         // If you are here, it means you flood.                 // If you have flooded 3 times in a row !         // Time to send a warning message.         if (g_Flood[id] >= 3)         {             client_print(id, print_notify, "** %L **", id, "STOP_FLOOD")                         // We update the next time a player is allowed to chat             // An extra of 3 seconds is added to calm down the guy.             g_Flooding[id] = nexTime + maxChat + 3.0                         // We stop there.             return PLUGIN_HANDLED         }                 // The player is flooding but it's not yet the third time in a row.         // We increase the count.         g_Flood[id]++     }         // The player doesn't flood right now. If the check true, it means he did it just a moment ago.     else if (g_Flood[id])     {         // But It's a good guy now, we decrease his number of floods.         g_Flood[id]--     }         g_Flooding[id] = nexTime + maxChat }

Hope you understand better now, sorry I suck when It comes to explain something.
__________________
Arkshine is offline
hristosax
Junior Member
Join Date: Jul 2008
Old 08-24-2010 , 11:37   Re: Trying to understand the antiflood plugin...
Reply With Quote #6

Ahh, thank you very much, I understood everything. Took me a while to realize why "id" was being used, but I got it.

I got confused, since I forgot that the first two times that the chkFlood() function is used it skips the if - else loop.


Thank you and have a nice day
hristosax is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:52.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode