Raised This Month: $51 Target: $400
 12% 

[Tut] MySql - Save/Load/Usefull Things (Xp Mod)


Post New Thread Reply   
 
Thread Tools Display Modes
Lycode
Junior Member
Join Date: Aug 2014
Old 12-22-2014 , 23:26   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #91

Quote:
Originally Posted by guipatinador View Post
Normally the order is,
client_connect -> client_authorized -> client_putinserver

But in some (rare) situations client_authorized is called after,
client_connect -> client_putinserver -> client_authorized

Note that there is no default order and client_connect is always the first.

============

Keep in mind that client_authorized sometimes is called to fast, right after plugin_init. This could be dangerous if you create the global mysql handle in plugin_init or plugin_cfg.

PHP Code:
g_SqlTuple SQL_MakeDbTupleszHostszUserszPassszDB 
Thats why some coders use client_putinserver.
1- Does this mean if we use client_putinserver it wouldn't be necessary to use a task after client authorized like you do?

2- Where else can we create the global handle that would remove the risk of client_authorized being called too fast causing problems?

3- Is a task of 4.0 seconds in client_authorized really needed? or would something like 0.1 work just as good?
__________________
-This too shall pass
Lycode is offline
guipatinador
SourceMod Donner Party
Join Date: Oct 2009
Location: Poortugal
Old 12-23-2014 , 06:06   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #92

Quote:
Originally Posted by Lycode View Post
1- Does this mean if we use client_putinserver it wouldn't be necessary to use a task after client authorized like you do?
client_putinserver is always a risk to get the player authid. Use client_authorized for safety.

Quote:
2- Where else can we create the global handle that would remove the risk of client_authorized being called too fast causing problems?
You can't. Use plugin_init or plugin_cfg.

Quote:
3- Is a task of 4.0 seconds in client_authorized really needed? or would something like 0.1 work just as good?
It is required. I'm not sure the ammount of time. For example amxbans uses 2 seconds,

PHP Code:
public client_authorized(id) {
    
//fix for the invalid tuple error at mapchange, only a fast fix now
    
if(g_SqlX==Empty_Handle) {
        
set_task(2.0,"client_authorized",id)
        return 
PLUGIN_HANDLED
    
}
    
// code...

__________________
PM for help = ignore

Last edited by guipatinador; 12-23-2014 at 06:09.
guipatinador is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 12-23-2014 , 06:22   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #93

Quote:
Originally Posted by guipatinador View Post
PHP Code:
public client_authorized(id) {
    
//fix for the invalid tuple error at mapchange, only a fast fix now
    
if(g_SqlX==Empty_Handle) {
        
set_task(2.0,"client_authorized",id)
        return 
PLUGIN_HANDLED
    
}
    
// code...

Checking the empty handle is definitly better than create unnecessary tasks. Now the server is happy and the data will be secure
__________________
Jhob94 is offline
guipatinador
SourceMod Donner Party
Join Date: Oct 2009
Location: Poortugal
Old 12-23-2014 , 06:38   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #94

Its not "definitly better".

If the plugin cannot connect to database (example: database offline, wrong password, etc), client_authorized is called each 2 seconds because g_SqlX will be always Empty_Handle.
__________________
PM for help = ignore
guipatinador is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 12-23-2014 , 06:46   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #95

Quote:
Originally Posted by guipatinador View Post
Its not "definitly better".

If the plugin cannot connect to database (example: database offline, wrong password, etc), client_authorized is called each 2 seconds because g_SqlX will be always Empty_Handle.
That's because you are noob
Set task time for 5.0 to another function that isn't client_authorized. Didn't even noticed you looped the client authorized lol, thats so wrong
__________________
Jhob94 is offline
guipatinador
SourceMod Donner Party
Join Date: Oct 2009
Location: Poortugal
Old 12-23-2014 , 07:49   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #96

Quote:
Originally Posted by Jhob94 View Post
Didn't even noticed you looped the client authorized lol, thats so wrong
Thats an example of amx bans, https://code.google.com/p/plukom-amx...spec=svn17#239

Obviously looping client_authorized isn't a good idea.
__________________
PM for help = ignore

Last edited by guipatinador; 12-23-2014 at 07:57.
guipatinador is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 12-23-2014 , 17:46   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #97

This should be ok:
PHP Code:
#define TASK_SQL 432531
#define MAX_PLAYERS 32

new g_Steam[MAX_PLAYERS 1][35]

public 
client_authorized(Id)
{
    
get_user_authid(Idg_Steam[Id], charsmax(g_Steam)) // Lets get SteamID just once. Safe for client_disconnect saving
    
    
if(g_SqlX == Empty_Handle)
        
set_task(5.0,"LoadShit",Id TASK_SQL)
    
    else
        
LoadShit(Id TASK_SQL)
}

public 
LoadShit(Id)
{
    
Id-= TASK_SQL
    
    
// Code

__________________
Jhob94 is offline
guipatinador
SourceMod Donner Party
Join Date: Oct 2009
Location: Poortugal
Old 12-23-2014 , 18:53   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #98

Its okay.

But I would do the following only to prevent offline databases,

Spoiler

=========================
PHP Code:
charsmax(g_Steam)) 
If g_Steam it's a bidimensional array it shoud be,
PHP Code:
charsmax(g_Steam[])) 
__________________
PM for help = ignore

Last edited by guipatinador; 12-23-2014 at 19:13.
guipatinador is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 12-23-2014 , 20:25   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #99

Quote:
Originally Posted by guipatinador View Post
Its okay.

But I would do the following only to prevent offline databases,

Spoiler
Or just using a variable and put it positive in plugin_init after know if we got an Empty_Handle and work with the variable. Anyway, that's something i don't need right now. I am very new into sql so i have some doubts.

Should i regist like this:
SQL_ThreadQuery(g_Ranking, "EmptyFunction", "CREATE TABLE IF NOT EXISTS Rankings (Steam TEXT, Level NUMERIC, Kills NUMERIC, Deaths NUMERIC);")

Or should i create one table for each thing?

The reason why i am asking this is because i am currently using this to get data:
PHP Code:
formatex(Querycharsmax(Query), "SELECT Kills FROM Rankings WHERE Steam = '%s';"g_Steam[Id])
SQL_ThreadQuery(g_Ranking"Query_Handler"QueryDatacharsmax(Data))


And 
in Query_HandlerSQL_ReadResult(pQuery0
With this could i get all the data and parse it or something? Or will i have to do 3 queries?

And last question. Should i update things everytime the values change or loop a task to save it from x in x time?
__________________

Last edited by Jhob94; 12-23-2014 at 20:26.
Jhob94 is offline
guipatinador
SourceMod Donner Party
Join Date: Oct 2009
Location: Poortugal
Old 12-23-2014 , 20:47   Re: [Tut] MySql - Save/Load/Usefull Things (Xp Mod)
Reply With Quote #100

Quote:
Originally Posted by Jhob94 View Post
Should i regist like this:
SQL_ThreadQuery(g_Ranking, "EmptyFunction", "CREATE TABLE IF NOT EXISTS Rankings (Steam TEXT, Level NUMERIC, Kills NUMERIC, Deaths NUMERIC);")

Or should i create one table for each thing?
Your "CREATE TABLE..." query is ok but personally I would change two things.

1 - Replace TEXT by VARCHAR.
"CREATE TABLE IF NOT EXISTS Rankings (Steam VARCHAR(35), Level INT, Kills INT, Deaths INT)"

2 - Use a non-threaded query for "CREATE TABLE...", since its called in plugin_init / plugin_cfg once and will not affect the gameplay (for the rest, use always threaded querys).
PHP Code:
new Handle:Queries SQL_PrepareQuery(g_Ranking"CREATE TABLE IF NOT EXISTS Rankings (Steam VARCHAR(35), Level INT, Kills INT, Deaths INT)")

if(!
SQL_ExecuteQueries ))
{
    
// optionally you can turn off the plugin
    
SQL_QueryError(Queriesg_Errorcharsmax(g_Error))
    
set_fail_state(g_Error)
}

SQL_FreeHandle(Queries
Quote:
The reason why i am asking this is because i am currently using this to get data:
PHP Code:
formatex(Querycharsmax(Query), "SELECT Kills FROM Rankings WHERE Steam = '%s';"g_Steam[Id])
SQL_ThreadQuery(g_Ranking"Query_Handler"QueryDatacharsmax(Data))


And 
in Query_HandlerSQL_ReadResult(pQuery0
With this could i get all the data and parse it or something? Or will i have to do 3 queries?
Its simple with a single query. Just change "SELECT Kills" to "SELECT *" to select all the fields.
Optionally you can select the fields you want,

PHP Code:
formatex(Querycharsmax(Query), "SELECT Level, Kills Deaths FROM Rankings WHERE Steam = '%s';"g_Steam[Id])
SQL_ThreadQuery(g_Ranking"Query_Handler"QueryDatacharsmax(Data)) 
And then in Query_Handler,

PHP Code:
iLevel SQL_ReadResult(pQuery0// first field
iKills SQL_ReadResult(pQuery1// second field
iDeaths SQL_ReadResult(pQuery2// third field 
Quote:
And last question. Should i update things everytime the values change or loop a task to save it from x in x time?
Updating values every time they change may generate a lot of traffic between the server and the database. In most situations the most recommendable is saving at client_disconnect.

Unfortunately when the map change client_disconnect is called for everyone so it may cause a lot of traffic too.
__________________
PM for help = ignore

Last edited by guipatinador; 12-23-2014 at 20:52.
guipatinador 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 12:03.


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