Raised This Month: $ Target: $400
 0% 

Could someone correct this small plugin?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OldWolf
Member
Join Date: Mar 2009
Old 04-05-2009 , 04:28   Could someone correct this small plugin?
Reply With Quote #1

Howdy,

I wrote this small plugin tonight (or maybe I should say modified it from a much larger one) as an intro for myself to Pawn. Unfortunately I seemed to have made at least one mistake (probably more).

It compiles with no errors, and shows up on my plugin list, but it doesn't seem to actually load up the cvars from my database (though it did correctly create the table).

I added the command so that I could test it while I was in the server, and whenever I run it it also fails to get my cvars from the database. In addition, it seems to freeze up the server for about 10 seconds, where I get a "Warning, connection problem" in the upper right hand corner... then it unfreezes and continues to work.

Here it is:
Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <sqlx>

#define PLUGIN "SQL CVARs"
#define VERSION "0.1"
#define AUTHOR "oldwolf"

#define TABLE "cvars"


public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    
    register_dictionary("sql_cvars.txt")
    
    register_concmd("amx_check_cvars","get_cvars",ADMIN_ADMIN,"Nothing special.") 

    get_cvars()
    
}


public get_cvars()
{
    new error[128], type[12], errno
    
    new Handle:info = SQL_MakeStdTuple()
    new Handle:sql = SQL_Connect(info, errno, error, 127)
    
    SQL_GetAffinity(type, 11)
    
    if (sql == Empty_Handle)
    {
        server_print("[AMXX] %L", LANG_SERVER, "SQL_CANT_CON", error)

        return PLUGIN_HANDLED
    }
    
    
    new Handle:query
    
    SQL_QueryAndIgnore(sql, "CREATE TABLE IF NOT EXISTS `%s` ( `cvar_key` VARCHAR( 32 ) NOT NULL, `cvar_value` VARCHAR( 32 ) NOT NULL, `status` TINYINT( 1 ) NOT NULL ) COMMENT = 'AMX Mod X CVARS'", TABLE)
    query = SQL_PrepareQuery(sql,"SELECT `cvar_key`,`cvar_value` FROM `%s` WHERE `status` = 1", TABLE)


    if (!SQL_Execute(query))
    {
        SQL_QueryError(query, error, 127)
        server_print("[AMXX] %L", LANG_SERVER, "CANT_LOAD_CVARS", error)
    } else if (!SQL_NumResults(query)) {
        server_print("[AMXX] %L", LANG_SERVER, "NO_CVARS")
    } else {
        
        new CVARS = 0

        /** do this incase people change the query order and forget to modify below */
        new k_cvar_key = SQL_FieldNameToNum(query, "cvar_key")
        new k_cvar_value = SQL_FieldNameToNum(query, "cvar_value")

        
        new cvar_key[44];
        new cvar_value[44];


        
        while (SQL_MoreResults(query))
        {
            SQL_ReadResult(query, k_cvar_key, cvar_key, sizeof(cvar_key)-1);
            SQL_ReadResult(query, k_cvar_value, cvar_value, sizeof(cvar_value)-1);

    
            register_cvar(cvar_key,cvar_value);
    
            ++CVARS;
            SQL_NextRow(query)
        }
    
        if (CVARS == 1)
        {
            server_print("[AMXX] %L", LANG_SERVER, "CVAR_LOADED")
        }
        else
        {
            server_print("[AMXX] %L", LANG_SERVER, "CVARS_LOADED", CVARS)
        }
        
        SQL_FreeHandle(query)
        SQL_FreeHandle(sql)
        SQL_FreeHandle(info)
    }
    
    return PLUGIN_HANDLED
}
If you could point out all of my mistakes, I'm sure it would help me learn... plus I'd actually like this plugin to work, lol. I plan to expand it with other features, such as the ability to add/change a cvar in the midst of a map.

Thanks!
OldWolf is offline
TheRadiance
Senior Member
Join Date: Nov 2007
Location: Kazakhstan
Old 04-05-2009 , 05:36   Re: Could someone correct this small plugin?
Reply With Quote #2

You don't need to connect to DB everytime when you type amx_check_cvars.
TheRadiance is offline
Send a message via ICQ to TheRadiance
OldWolf
Member
Join Date: Mar 2009
Old 04-05-2009 , 14:17   Re: Could someone correct this small plugin?
Reply With Quote #3

Ah, so I would leave the connection up and just end it with plugin_end?
OldWolf is offline
OldWolf
Member
Join Date: Mar 2009
Old 04-05-2009 , 14:50   Re: Could someone correct this small plugin?
Reply With Quote #4

I'll be honest, I've been messing with it and it just seems to get worse. I'm guessing that I'm not going the right direction with this.

Does anyone know of perhaps an example script that already exists that I might use to learn from?
OldWolf is offline
TheRadiance
Senior Member
Join Date: Nov 2007
Location: Kazakhstan
Old 04-05-2009 , 21:50   Re: Could someone correct this small plugin?
Reply With Quote #5

Example of connection:
PHP Code:
#include < amxmodx >
#include < sqlx >

new Handle:g_hSqlCon

public plugin_init ( )
{
    
register_plugin ( ..., ..., ..., )
    
mysql_init ( )
}

mysql_init ( )
{
    new 
szHost 32 ]
    new 
szUser 32 ]
    new 
szPass 32 ]
    new 
szDtbe 32 ]
    new 
szError 128 ]
    new 
iError

    get_pcvar_string 
"amx_sql_host"szHostsizeof szHost ) - )
    
get_pcvar_string "amx_sql_user"szUsersizeof szUser ) - )
    
get_pcvar_string "amx_sql_pass"szPasssizeof szPass ) - )
    
get_pcvar_string "amx_sql_db"szDtbesizeof szDtbe ) - )

    new 
Handle:hSqlTpl SQL_MakeDbTuple szHostszUserszPassszDtbe )
    
g_hSqlCon SQL_Connect hSqlTpliErrorszErrorsizeof szError ) - )

    if ( !
g_hSqlCon )
    {
        
// .. connection failed
    
}

    
// .. successfully connected

public plugin_end ( ) // .. this is not so necessary
    
SQL_FreeHandle g_hSqlCon 

Last edited by TheRadiance; 04-06-2009 at 05:01.
TheRadiance is offline
Send a message via ICQ to TheRadiance
OldWolf
Member
Join Date: Mar 2009
Old 04-06-2009 , 02:32   Re: Could someone correct this small plugin?
Reply With Quote #6

Thanks for that example, I appreciate that! How would you apply a query?

In the examples I found, they had set the connection to a variable and used that variable later for queries. Is that just an optional parameter in case of multiple connections perhaps?
OldWolf is offline
TheRadiance
Senior Member
Join Date: Nov 2007
Location: Kazakhstan
Old 04-06-2009 , 04:05   Re: Could someone correct this small plugin?
Reply With Quote #7

Quote:
How would you apply a query?
PHP Code:
new Handle:hQuerySelect SQL_PrepareQuery g_hSqlCon"SELECT `password` FROM `admins` WHERE `auth` = 'Radiance'" )
SQL_Execute hQuerySelect )

new 
szPassword 32 ]

while ( 
SQL_MoreResults hQuerySelect ) )
{
    
SQL_ReadResult hQuerySelect0szPasswordsizeof szPassword ) - )
    
SQL_FreeHandle hQuerySelect )

    break
}

if ( !
strlen szPassword ) )
}
    
// ..no result returned..
}

// ..result returned in variable szPassword.. 

Last edited by TheRadiance; 04-06-2009 at 05:02.
TheRadiance is offline
Send a message via ICQ to TheRadiance
OldWolf
Member
Join Date: Mar 2009
Old 04-06-2009 , 17:15   Re: Could someone correct this small plugin?
Reply With Quote #8

Thanks a ton!

I'll work with it tonight and see if I can correct my mistakes with a modified version of your code and post the result.

Thanks for your help!

Last edited by OldWolf; 04-06-2009 at 18:04. Reason: Didn't sound quite right...
OldWolf is offline
OldWolf
Member
Join Date: Mar 2009
Old 04-07-2009 , 04:28   Re: Could someone correct this small plugin?
Reply With Quote #9

Alright, I spent a lot of time working on it, and it's doing pretty well now. I'd like to add some more features, but then I'll post it for anyone who wants to use it, and I'll credit you for your great help.

The main problem was actually totally unrelated to my script (my sql connection was terrible, I found a new host and it's working great now), but I still much prefer the idea of connecting just once rather than every time I do an update.

Right now my script properly reads from the mysql table, and sets the cvars correctly. If you update a value in the table, you can use a command to have the server check the table and update the values accordingly.

If anyone is interested, I can post my current progress.

I am having a little trouble working this out though:
The plugin (thankfully) properly yields to map settings at the start, but if you use the amx_get_cvars command (which grabs the values off the table and updates the server), it of course overwrites them. My thoughts are that perhaps I should just leave it the way it is, and people should be aware not to do that.. but I'd like a more ellegant solution if anyone has any ideas.

Thanks!
OldWolf is offline
TheRadiance
Senior Member
Join Date: Nov 2007
Location: Kazakhstan
Old 04-07-2009 , 07:00   Re: Could someone correct this small plugin?
Reply With Quote #10

If you will have serious problems just post them here, there are many peoples that can help you here.
TheRadiance is offline
Send a message via ICQ to TheRadiance
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 02:18.


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