Raised This Month: $32 Target: $400
 8% 

Check if a trie key contains a string? Best method to save a file in memory?


Post New Thread Reply   
 
Thread Tools Display Modes
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 07-03-2013 , 17:00   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #11

It depends on how big that file it will be but if you're planning to use big amount of data then use sql it's better than any saving method, otherwise use nvault or keep your file and
Quote:
Originally Posted by Arkshine View Post
Then using Trie is not what you want. Just use an Array, loop and use contain().

Last edited by Clauu; 07-03-2013 at 17:03.
Clauu is offline
Desikac
Senior Member
Join Date: Apr 2010
Location: Serbia
Old 07-03-2013 , 20:03   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #12

PHP Code:
#include <amxmodx>
#include <sqlvault>

new SQLVault:banlist;
new const 
banfile[] = "addons/amxmodx/configs/bans.cfg";

public 
plugin_init() {
    
register_plugin("sqlvault test""1""Desikac");
    
loadbans()
    
    
register_clcmd("listbans""listbans");
    
register_clcmd("addban""addban");
    
register_clcmd("removeban""remove");
}

public 
loadbans() {
    
    if(!
file_exists(banfile))
        return
    
    
banlist sqlv_open_local("banlist");
    
sqlv_connect(banlist);
        
    new 
sadrzaj[100];
    new 
fopenbanfile "rt" );
    
    while( !
feof) )
    {
        
fgetssadrzaj charsmaxsadrzaj ) );
        
trimsadrzaj );
        if(!
sadrzaj[0])
            continue;
            
        
sqlv_set_data(banlistsadrzaj"/");
    }
    
fclose);
    
sqlv_disconnect(banlist);
}
    
public 
listbans(id) {
    
sqlv_connect(banlist);
    
    new 
size sqlv_size(banlist);
    new 
key[64];
    
    for(new 
0<sizei++)
    {
        
sqlv_read(banlistikeycharsmax(key)); //prints only the first line and then gives errors
        
console_print(idkey);
    }
    
    
sqlv_disconnect(banlist);
    
    
console_print(id"Success.");
}

public 
addban(id) {
    new 
arg[100]
    
read_args(arg99)
    
sqlv_connect(banlist);
    
    
sqlv_set_data(banlistarg"/");
    
write_file(banfilearg);
    
    
sqlv_disconnect(banlist);
    
    
console_print(id"Success.");
}

public 
remove(id) {
    new 
arg[100]
    
read_args(arg99)
    
sqlv_connect(banlist);
    
    new 
size sqlv_size(banlist);
    new 
key[64], data[64];
    
    for(new 
0<sizei++)
    {
        
sqlv_read(banlistikeycharsmax(key), datacharsmax(data));
        if(
contain(keyarg)) {
            
sqlv_remove(banlistkey);
//            remove_ban(arg) - Deletes from file
            
console_print(id"Success.");
        }
    }
    
    
sqlv_disconnect(banlist);
    
}

public 
plugin_end() {
    
sqlv_clear(banlist);
    
sqlv_close(banlist);

When I use "listbans" it prints the first line and gives a blank string and the error Error in sqlv_read(): unrecognized token: "" for every other line.
Desikac is offline
Send a message via MSN to Desikac Send a message via Skype™ to Desikac
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-03-2013 , 21:17   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #13

Anything related to a ban list will never be called 1000 times during a map. Since you are recreating the wheel, go look at all the other wheels for how they do it.
__________________

Last edited by fysiks; 07-03-2013 at 21:21.
fysiks is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 07-03-2013 , 21:59   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #14

Don't use SQLVault. Use SQL and do it with threaded queries (SQL_ThreadQuery()).
__________________
Impossible is Nothing
Sylwester is offline
Desikac
Senior Member
Join Date: Apr 2010
Location: Serbia
Old 07-04-2013 , 11:50   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #15

Quote:
Originally Posted by fysiks View Post
Anything related to a ban list will never be called 1000 times during a map. Since you are recreating the wheel, go look at all the other wheels for how they do it.
Well 1000 is probably the worst case scenario. Imagine a very popular server which gets about 300 connects per map. The ban list is checked twice per player connect plus every X seconds when the server checks if a ban has expired and another X times when admins add or remove bans. So it can get to 1000 but usually its 200-400 per map on an average server.

Why is sql better that just saving all the data in an array? The list can get huge but in 90% cases it won't because the maximum duration of bans is 1 month.

I tried again using an array and it works:
PHP Code:
#include <amxmodx>

new Array:banlist
new const banfile[] = "addons/amxmodx/configs/bans.cfg";

public 
plugin_init() {
    
register_plugin("array test""1""Desikac");
    
loadbans()
    
    
register_clcmd("listbans""listbans");
    
register_clcmd("addban""addban");
    
register_clcmd("removeban""remove");
}

public 
loadbans() {
    
    if(!
file_exists(banfile))
        return
    
    
banlist ArrayCreate(50);

    new 
sadrzaj[100];
    new 
fopenbanfile "rt" );
    
    while( !
feof) )
    {
        
fgetssadrzaj charsmaxsadrzaj ) );
        
trimsadrzaj );
        if(!
sadrzaj[0])
            continue;
            
        
ArrayPushString(banlistsadrzaj);
        
console_print(0"Line added: %s"sadrzaj);
    }
    
fclose);
}
    
public 
listbans(id) {
    new 
key[64];
    new 
size ArraySize(banlist);
    for(new 
0sizei++)
    {
        
ArrayGetString(banlistikeycharsmax(key));
        
console_print(idkey);
    }
    
    
console_print(id"Success.");
    return 
PLUGIN_HANDLED
}

public 
addban(id) {
    new 
arg[100]
    
read_args(arg99)

    
ArrayPushString(banlistarg);
    
write_file(banfilearg);
    
    
console_print(id"Success.");
    return 
PLUGIN_HANDLED
}

public 
remove(id) {
    new 
arg[100];
    
read_args(arg99);
    
    new 
key[64], pos[22];
    new 
timesfound = -1
    
new size ArraySize(banlist);
    
    for(new 
0<sizei++)
    {
        
ArrayGetString(banlistikeycharsmax(key));
        if(
containi(keyarg) != -1) {
            
timesfound++;
            
pos[timesfound] = i;
        }
    }
    
    for(new 
0<= timesfoundj++)
        
ArrayDeleteItem(banlistpos[j] - j)
    
    
console_print(id"Deleted %s lines."timesfound);
        
    return 
PLUGIN_HANDLED
    

Desikac is offline
Send a message via MSN to Desikac Send a message via Skype™ to Desikac
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 07-04-2013 , 16:13   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #16

Quote:
Originally Posted by Desikac View Post
Why is sql better that just saving all the data in an array?
Assuming that your plugin is designed properly:
- you can have the same banlist on multiple servers
- operations on banlist do not interrupt gameplay
- player connect, add ban, remove ban, search bans require only 1 operation and you don't need any loops
- you don't need to remove expired bans

Quote:
Originally Posted by Desikac View Post
The list can get huge but in 90% cases it won't because the maximum duration of bans is 1 month.
You don't ban cheaters permanently?
__________________
Impossible is Nothing
Sylwester is offline
Desikac
Senior Member
Join Date: Apr 2010
Location: Serbia
Old 07-04-2013 , 17:38   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #17

Quote:
Originally Posted by Sylwester View Post
- you can have the same banlist on multiple servers
I have a different method for that. Don't need it atm. That is probably the only advantage SQL has over other saving methods.
Quote:
Originally Posted by Sylwester View Post
- operations on banlist do not interrupt gameplay
How does any other method interrupt gameplay?
Quote:
Originally Posted by Sylwester View Post
- player connect, add ban, remove ban, search bans require only 1 operation and you don't need any loops
You don't need to use loops with other methods either. I am using them because some options in my plugin require them. I would still have to use them with SQL.
Quote:
Originally Posted by Sylwester View Post
- you don't need to remove expired bans
How come?

Quote:
Originally Posted by Sylwester View Post
You don't ban cheaters permanently?
What are the chances of the same cheater coming back to the same server after one month? Even if he does, just ban him again. Better than to have 300 more bans per month in the banlist. Anyway, there is an option to ban permanently by putting -1 in the time parameter.

Last edited by Desikac; 07-04-2013 at 17:39.
Desikac is offline
Send a message via MSN to Desikac Send a message via Skype™ to Desikac
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 07-04-2013 , 18:57   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #18

Quote:
Originally Posted by Desikac View Post
How does any other method interrupt gameplay?
If you loop through all entries on your ban list (and those tend to get large), at some point it may start causing lags. Although it will not happen if you keep your ban list small.

Quote:
Originally Posted by Desikac View Post
You don't need to use loops with other methods either. I am using them because some options in my plugin require them. I would still have to use them with SQL.
How can you remove bans from an array or search for (partially) matching ids without looping through all entries? Where would you have to do that with SQL?

Quote:
Originally Posted by Desikac View Post
How come?
You can simply exclude expired bans when checking connecting players, or you can delete those bans at that time. There is no need to check ban list every X seconds.

Quote:
Originally Posted by Desikac View Post
What are the chances of the same cheater coming back to the same server after one month? Even if he does, just ban him again.
Well, if you have active admins that can take care of it then temp ban is fine too.
__________________
Impossible is Nothing

Last edited by Sylwester; 07-04-2013 at 18:58.
Sylwester is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 07-05-2013 , 03:13   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #19

So much trouble for a ban list, read it once save into memory and then just check your saved data inserting/writing new data when is necessary.

Last edited by Clauu; 07-05-2013 at 03:16.
Clauu is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 07-05-2013 , 06:17   Re: Check if a trie key contains a string? Best method to save a file in memory?
Reply With Quote #20

If you have access to a SQL server then use SQL. It was DESIGNED to be fast and efficient, both with large and small amount of data plus it has a bunch more tools than other saving methods.

When a player connects, you can either check if they have a record in the database directly (which can cause high load) or just check the first time they connect to the map, then save the key to a trie with its value being the timestamp when the ban gets lifted.

Last edited by Backstabnoob; 07-05-2013 at 06:19.
Backstabnoob is offline
Reply


Thread Tools
Display Modes

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 23:55.


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