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

Loading and saving data using mysql db


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mrshark45
Member
Join Date: Jan 2016
Old 07-18-2020 , 08:37   Loading and saving data using mysql db
Reply With Quote #1

Hello, I wrote a simple credits plugin and sometimes , I don't know when, player's credits reset.
It doesn't show any error in the logs, I logged everytime the plugins load someone's credits from db and what it saves and sometimes it just saves 2 credits like when a player's kills someone its credits increment by 2 and then saves it, so if someone have 24 credits and kills another player the credits reset and it saves 2. I don't know when it happends. It doesn't happen to all players and not all the time.
Loading code:
PHP Code:
public void OnClientPostAdminCheck(client)
{
    if(
IsFakeClient(client))
        return;

    
LoadClientCredits(client);
}

public 
void LoadClientCredits(int client)
{
    if(!
IsClientConnected(client))
        return;
    
char query[256], steamid[32];
    
GetClientAuthId(clientAuthId_Steam2steamidsizeof(steamid));
    
FormatEx(querysizeof(query), "SELECT * FROM store_players WHERE authid = '%s'"steamid);
    
LogToFile(logPathquery);
    
db.Query(T_LoadDataqueryGetClientUserId(client));
}

public 
void T_LoadData(Database db1DBResultSet results, const char[] errorany data)
{
    if(
db1 == null || results == null)
    {
        
LogError("T_LoadData returned error: %s"error);
        return;
    }

    
int client GetClientOfUserId(data); 

    if(!
client || IsFakeClient(client) || !IsClientConnected(client))
        return;

    
int creditsCol;
    
results.FieldNameToNum("credits"creditsCol);


    if(
results.FetchRow())
    {
        
player_credits[client] = results.FetchInt(creditsCol);
        
LogToFile(logPath"%i"player_credits[client]);
    }
    else
    {
        
//not found in db, insert data:
        
char steamid[32], name[64], name_sanitazed[64]; GetClientAuthId(clientAuthId_Steam2steamidsizeof(steamid)); GetClientName(clientnamesizeof(name));
        
db.Escape(namename_sanitazedsizeof(name_sanitazed));
        
char query[256]; Format(querysizeof(query), "INSERT INTO store_players (authid, name, credits) VALUES ('%s', '%s', 0);"steamidname_sanitazed );
        
LogToFile(logPath"NOT IN DATABASE");
        
db1.Query(T_Genericquery);
    }
}
public 
void T_Generic(Database db1DBResultSet results, const char[] errorany data)
{
    if(
db1 == null || results == null)
    {
        
LogError("T_Generic returned error: %s"error);
        return;
    }

Saving code:
PHP Code:
public Action Event_PlayerDeath(Event event, const char[] chNamebool bDontBroadcast)
{
    
int credits c_kill.IntValue;
    
int attacker GetClientOfUserId(GetEventInt(event"attacker"));
    
int victim GetClientOfUserId(GetEventInt(event"userid"));
    if(!
attacker || !victim || IsFakeClient(victim) || IsFakeClient(attacker) || !IsClientConnected(attacker))
        return;

    
kills[attacker]++;
    
bool headshot GetEventBool(event"headshot");
    
bool noscope GetEventBool(event"noscope");
    
bool smoke GetEventBool(event"thrusmoke");
    
int wallbang GetEventInt(event"noscope");
    
bool blind GetEventBool(event"attackerblind");
    if(
headshot)
        
credits += c_headshot.IntValue;
    if(
noscope)
        
credits += c_noscope.IntValue;
    if(
smoke)
        
credits += c_smoke.IntValue;
    if(
wallbang)
        
credits += c_wall.IntValue;
    if(
blind)
        
credits += c_blind.IntValue;
    if(
c_multikill.IntValue)
        
credits += c_multikill.IntValue kills[attacker];
    
    
player_credits[attacker] += credits;
    

    
UpdateCredits(attacker);

    return;
}
public 
void UpdateCredits(int client)
{
    
char steamid[32]; GetClientAuthId(clientAuthId_Steam2steamidsizeof(steamid));
    
LogToFile(logPath"%s - %i SAVED"steamidplayer_credits[client]);

    
char query[256]; Format(querysizeof(query), "UPDATE store_players SET credits = '%i' WHERE authid = '%s';"player_credits[client], steamid);
    
db.Query(T_Genericquery);

I also reset a player's credits when he disconnects

PHP Code:
public void OnClientDisconnect(int client)
{
    
player_credits[client] = 0;

mrshark45 is offline
Rohanlogs
Senior Member
Join Date: Nov 2015
Old 07-18-2020 , 13:01   Re: Loading and saving data using mysql db
Reply With Quote #2

Hey.
Some things I would change here.
Reset player_credits also on disconnect, create a global variable for clients steamid as you should fetch it only once since its used often.
You don't have to fetch it again everytime.
Try this as your load code:
Spoiler

Some of this code is based on headline's 'gangs' plugin IIRC which has been working well for me.
Remove the local string 'steamid' from everywhere and use the global variable g_sSteamID instead.

You could also replace all these:
PHP Code:
if(!client || IsFakeClient(client) || !IsClientConnected(client)) return; 
To this stock which I added in the code:
PHP Code:
if( !IsValidClient(client) || IsFakeClient(client) ) return; 
You can also use extra parameter 'DBPrio_High' in your query but its not necessary.

In Event_PlayerDeath, check if players stats are loaded before doing anything.
I added this boolean in the code above.
If they aren't loaded, return:
Spoiler



But if you don't have to query on every single kill, perhaps you should just update the score once to the database during disconnect:
Spoiler
__________________

Last edited by Rohanlogs; 07-18-2020 at 13:23.
Rohanlogs is offline
mrshark45
Member
Join Date: Jan 2016
Old 07-19-2020 , 08:20   Re: Loading and saving data using mysql db
Reply With Quote #3

Quote:
Originally Posted by Rohanlogs View Post
Hey.
Some things I would change here.
Reset player_credits also on disconnect, create a global variable for clients steamid as you should fetch it only once since its used often.
You don't have to fetch it again everytime.
Try this as your load code:
Spoiler

Some of this code is based on headline's 'gangs' plugin IIRC which has been working well for me.
Remove the local string 'steamid' from everywhere and use the global variable g_sSteamID instead.

You could also replace all these:
PHP Code:
if(!client || IsFakeClient(client) || !IsClientConnected(client)) return; 
To this stock which I added in the code:
PHP Code:
if( !IsValidClient(client) || IsFakeClient(client) ) return; 
You can also use extra parameter 'DBPrio_High' in your query but its not necessary.

In Event_PlayerDeath, check if players stats are loaded before doing anything.
I added this boolean in the code above.
If they aren't loaded, return:
Spoiler



But if you don't have to query on every single kill, perhaps you should just update the score once to the database during disconnect:
Spoiler
Thanks, I did the changes yesterday and it doesn't seem to reset anymore, maybe some players connected before the db connection was made.

Last edited by mrshark45; 07-19-2020 at 08:21.
mrshark45 is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 07-20-2020 , 04:57   Re: Loading and saving data using mysql db
Reply With Quote #4

Consider FormatQuery and incremental syntax

SQL_FormatQuery(db, query, sizeof(query), "UPDATE store_players SET credits = credits + '%i' WHERE authid = '%s';", credits, steamid);
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 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 23:56.


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