Raised This Month: $ Target: $400
 0% 

Solved sql save data at disconnect


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lexzor
Veteran Member
Join Date: Nov 2020
Old 08-05-2021 , 14:30   sql save data at disconnect
Reply With Quote #1

hello. i have this code

PHP Code:
#if AMXX_VERSION_NUM < 183
    
public client_disconnect(id)
#else 
    
public client_disconnected(id)
#endif
{
    if(
g_PlayerData[id][ISBOT] == )
        return 
PLUGIN_HANDLED;

    switch(
id)
    {
        case 
1..10set_task(0.1"save_data"id SendDataTask);
        case 
11..22set_task(0.1"save_data"id SendDataTask);   
        case 
23..32set_task(0.3"save_data"id SendDataTask);
    }


    return 
PLUGIN_CONTINUE;
}

public 
save_data(id)
{
    
id -= SendDataTask;

    new 
szQuery[512];

    
g_PlayerData[id][LASTVISIT] = get_systime();

    
formatex(szQuerycharsmax(szQuery), "UPDATE `%s` SET `last_visit` = '%i' , `daily_bonus_time` = '%i' , `points` = '%i'  WHERE `steamid` = '%s'",
    
g_szTableg_PlayerData[id][LASTVISIT], g_PlayerData[id][DAILYBONUS], g_PlayerData[id][CASH], g_PlayerData[id][STEAMID]);

    
SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQuery);



the problem is that when map is changing the data are not saved only when player disconnect.

If i don t use set_task i get this error after map change

Code:
[MySQL] Thread worker was unable to start.
i think the plugin free handle the sql tuple and can t send the query.

what are your solutions?

Last edited by lexzor; 08-08-2021 at 21:13.
lexzor is offline
Dragos
Senior Member
Join Date: Oct 2018
Location: Romania
Old 08-05-2021 , 14:58   Re: sql save data at disconnect
Reply With Quote #2

Try using my method.

Not good to use, but try

PHP Code:
public client_disconnect(id) {
    if(!(
is_user_bot(id)||is_user_hltv(id)))    Save_MySql(id)
}

public 
Save_MySql(id)
{
    new 
szTemp[512]
    
format(szTemp,charsmax(szTemp),"UPDATE `%s` SET `kills` = '%d', `deaths` = '%d', `points` = '%d' WHERE `steamid` = '%s';",TABELszKills[id], szDeaths[id], Points[id], szSteamId[id])
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)

Change with your data.
__________________
sup

Last edited by Dragos; 08-05-2021 at 15:02.
Dragos is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-05-2021 , 16:09   Re: sql save data at disconnect
Reply With Quote #3

Have you tried printing out the query in a server_print() or log to make sure it's formatted correctly?
Are you getting a positive result in your FreeHandle callback?
__________________
Bugsy is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-06-2021 , 00:01   Re: sql save data at disconnect
Reply With Quote #4

And what is this all about with the difference in intervals based on the id (1-10 and 11-22 can be grouped btw). And why use set_task() at all, just call save_data() directly.
PHP Code:
    switch(id)
    {
        case 
1..10set_task(0.1"save_data"id SendDataTask);
        case 
11..22set_task(0.1"save_data"id SendDataTask);   
        case 
23..32set_task(0.3"save_data"id SendDataTask);
    } 
__________________
Bugsy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 08-06-2021 , 00:12   Re: sql save data at disconnect
Reply With Quote #5

Show how are you loading the data from the top to the bottom.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
deprale
Senior Member
Join Date: Oct 2018
Location: Leeds
Old 08-08-2021 , 20:08   Re: sql save data at disconnect
Reply With Quote #6

PHP Code:
#if AMXX_VERSION_NUM < 183
    
public client_disconnect(id)
#else 
    
public client_disconnected(id)
#endif
#include <fakemeta> 
public plugin_init(){
register_forwardFM_ChangeLevel"fwd_FM_ChangeLevel");  // HOOK changelevel forward NOTE: you WILL need to include fakemeta.
}
{
    if(
g_PlayerData[id][ISBOT] == )
        return 
PLUGIN_HANDLED;

    switch(
id)
    {
        case 
1..10set_task(0.1"save_data"id SendDataTask);
        case 
11..22set_task(0.1"save_data"id SendDataTask);   
        case 
23..32set_task(0.3"save_data"id SendDataTask);
    }


    return 
PLUGIN_CONTINUE;
}

public 
save_data(id)
{
    
id -= SendDataTask;

    new 
szQuery[512];

    
g_PlayerData[id][LASTVISIT] = get_systime();

    
formatex(szQuerycharsmax(szQuery), "UPDATE `%s` SET `last_visit` = '%i' , `daily_bonus_time` = '%i' , `points` = '%i'  WHERE `steamid` = '%s'",
    
g_szTableg_PlayerData[id][LASTVISIT], g_PlayerData[id][DAILYBONUS], g_PlayerData[id][CASH], g_PlayerData[id][STEAMID]);

    
SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQuery);




SaveStuff(){
new 
iPlayers[32], iPNumiPlayer;
get_players(iPlayersiPNum"hc"// get all players beside HLTV proxies and bots
for (new 0i<<iPNumi++){
iPlayer iPlayers[i]
    switch(
i)
    {
        case 
1..10set_task(0.1"save_data"id SendDataTask);
        case 
11..22set_task(0.1"save_data"id SendDataTask);   
        case 
23..32set_task(0.3"save_data"id SendDataTask);
    }
}
return 
PLUGIN_HANDLED;
}

public 
fwd_FM_ChangeLevelmap[] ) { //HOOK CHANGELEVEL
SaveStuff(); // loop players save data function
return PLUGIN_CONTINUE;
}

public 
plugin_end(){ // HOOK PLUGIN END
SaveStuff();
return 
PLUGIN_CONTINUE;

plugin_end() should be called when the "server stops", not sure if its called at map change, but just in case you shut off the server or it crashes I think it's worth saving then as well, if not just delete the whole function.
forward changelevel hook => NOT RELIABLE if you change map from other plugins, example you use changelevel from x.amxx it won't always register, in b.amxx not sure why that is, maybe it's related to plugin order in amxx.cfg or some other "too advanced stuff" for me. Pardon my ignorance.

I believe others know better, so don't take my post for granted, but would be sweet if you tried it

EDIT: can somebody also clarify for me whether set_task is a good idea to call on a changelevel hook? wouldn't that task try to execute when already the map is changing ? Or does it respect set_task's delays and THEN changes map?

2nd EDIT (SORRY AHAHAH):
Quote:
Originally Posted by Bugsy View Post
And what is this all about with the difference in intervals based on the id (1-10 and 11-22 can be grouped btw). And why use set_task() at all, just call save_data() directly.
PHP Code:
    switch(id)
    {
        case 
1..10set_task(0.1"save_data"id SendDataTask);
        case 
11..22set_task(0.1"save_data"id SendDataTask);   
        case 
23..32set_task(0.3"save_data"id SendDataTask);
    } 

exactly, why doesn't he just pass arguments to save_data? why delay it? if player has disconnected then save for id only, if map changed then pass no arguments, if arguments empty then loop through all players and save.
I suppose I could see why he would want to delay certain ids from saving, maybe he starts saving the data AS SOON as something happens, but is STILL happening, and the latter ids dont YET have the data to be saved, so he's trying to SAVE before actually WRITING the data that's "ACTUAL", a.k.a the one that's true. But hey, I guess it's worth waiting for his answer instead than trying to think about it.

But then he can just delay the changelevel forward with a boolean and another task, "I suppose" (not actually sure its possible, never tried it).
__________________

Last edited by deprale; 08-08-2021 at 20:18.
deprale is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 08-08-2021 , 20:43   Re: sql save data at disconnect
Reply With Quote #7

actually this is working:
PHP Code:

#if AMXX_VERSION_NUM < 183
    
public client_disconnect(id)
#else 
    
public client_disconnected(id)
#endif
{
    if(
g_PlayerData[id][ISBOT] == )
        return 
PLUGIN_HANDLED;

    
save_data(id);

    return 
PLUGIN_CONTINUE;
}

public 
save_data(id)
{
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error));

    if(
SqlConnection == Empty_Handle)
        
set_fail_state(g_Error);
    
    new 
Handle:Queries;
    new 
szQuery[1024];

    
g_PlayerData[id][LASTVISIT] = get_systime();
    
formatex(szQuerycharsmax(szQuery), "UPDATE `%s` SET `last_visit` = '%i' , `points` = '%i'  WHERE `steamid` = '%s'",
        
g_szTableg_PlayerData[id][LASTVISIT], g_PlayerData[id][CASH], g_PlayerData[id][STEAMID]);
    
    
Queries SQL_PrepareQuery(SqlConnectionszQuery);

    if(!
SQL_Execute(Queries))
    {
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error));
        
set_fail_state(g_Error);
    }        
    
    
SQL_FreeHandle(Queries);
    
SQL_FreeHandle(SqlConnection);

and this is how i get data
PHP Code:
public client_authorized(id)
{
    if(
is_user_bot(id) || is_user_hltv(id))
    {
        
g_PlayerData[id][ISBOT] = 1;

        return 
PLUGIN_HANDLED;
    }

    
get_user_authid(idg_PlayerData[id][STEAMID], charsmax(g_PlayerData[][STEAMID]));
    
get_user_name(idg_PlayerData[id][NAME], charsmax(g_PlayerData[][NAME]));
    
get_user_ip(idg_PlayerData[id][IP], charsmax(g_PlayerData[][IP]), 1);

    
g_PlayerData[id][DONATE] = 0;
    
g_PlayerData[id][ID] = 0;
    
g_PlayerData[id][ISBOT] = 0;

    switch(
id)
    {
        case 
1..10set_task(0.1"get_data"id GetDataTask);
        case 
11..22set_task(0.2"get_data"id GetDataTask);   
        case 
23..32set_task(0.3"get_data"id GetDataTask);
    }

    
set_task(float(HELLO_TIME), "hello_user"id HelloTask);

    return 
PLUGIN_CONTINUE;

I don't know why but i get a lot of errors if i don't use set_task.

In the above code i just make a mistake, that's why i put the same value for 2 cases of switch statement.

In first provided code i made a mistake (i was trying to evoid an error that occurs when there are to many queries sending to dtb):
- using set task to save it and at changelevel the data wasn't saved

Maybe if i would used SQL_ThreadQuery to send data like
PHP Code:
new szData[3];
szData[0] = id
szData
[1] = kills//or whatever

//etc...

SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQueryszData2
maybe it would worked but i think the above code is more practical

Last edited by lexzor; 08-08-2021 at 21:11.
lexzor is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-08-2021 , 20:47   Re: sql save data at disconnect
Reply With Quote #8

Yeah, you should never run into an issue with threaded since they should be queued and processed when the system can process. Like I said, I would use execute only at plugin_cfg() or plugin_end() since there, it will never impact gameplay. All mid-game stuff, use threaded.
__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 08-08-2021 , 21:13   Re: sql save data at disconnect
Reply With Quote #9

Actually i can't use threaded in client_disconnected because i get a lot of errors for every player but i think it s ok rn because it s working with no errors when the server is full.

Thanks you all.
lexzor is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-08-2021 , 21:48   Re: sql save data at disconnect
Reply With Quote #10

Show your disconnect code
__________________
Bugsy 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 15:33.


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