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

SQL saving/loading causing data loss and high ping


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
georgik57
Veteran Member
Join Date: Oct 2008
Location: 🎧Music World
Old 09-25-2016 , 01:35   SQL saving/loading causing data loss and high ping
Reply With Quote #1

So I have the following little plugin which is supposed to save information and retrieve it when necessary, but every time it saves or loads for 2 or more players, it corrupts the data.

Also creates server lag spikes every time it does so.

Please either help me fix it or give me an example plugin which would have some sort of stocks to create a database, save data to it and load data from it efficiently without corruption or server performance impact and which would work with both sqlite(to save locally inside the hlds) or mysql(to save to a remote database).

The following plug-in is intended for use with Zombie Plague Nightmare Mode.

bitsums.inc

PHP Code:
#include <amxmodx>
#include <bitsums>
#include <sqlx>
#include <zombieplaguenightmare>

#pragma reqlib sqlite
#if !defined AMXMODX_NOAUTOLOAD
  #pragma loadlib sqlite
#endif

#pragma defclasslib sqlx sqlite

new Handleg_hTupleHandle:g_hConnection;
new 
g_szSqlError[512];
new 
g_iSqlError;

enum (+= 32)
{
    
g_iIDTaskUpdateAmmo 99999,
    
g_iIDTaskLoadAccount
}

InitializeDatabase() {
    
SQL_SetAffinity("sqlite");
    
    
g_hTuple SQL_MakeDbTuple("""""""ZPNM_Stats");//127.0.0.1
    
new HandlehConnection UTIL_ConnectToDB();
    
    new 
HandlehQuery SQL_PrepareQuery(hConnection"CREATE TABLE IF NOT EXISTS Stats (SteamID varchar (36) NOT NULL default '', HumanClass int (32) NOT NULL default -1, ZombieClass int (32) NOT NULL default -1, AmmoPacks int (32) NOT NULL default -1)");
    if(!
SQL_Execute(hQuery)) {
        
SQL_QueryError(hQueryg_szSqlErrorcharsmax(g_szSqlError));
        
set_fail_state(g_szSqlError);
    }
    
    
SQL_FreeHandle(hQuery);
    
//SQL_FreeHandle(hConnection);
    
    
g_hConnection hConnection;
}

HandleUTIL_ConnectToDB() {
    new 
HandlehConnection SQL_Connect(g_hTupleg_iSqlErrorg_szSqlErrorcharsmax(g_szSqlError));
    if(
hConnection == Empty_Handle) {
        
// We are using SQLite, every connect has to be made! Let's just be strict
        
set_fail_state(g_szSqlError);
    }
    
    return 
hConnection;
}

//new Handle: g_iSQLTuple, Handle: g_iSQLConnection;
new g_szAuthID[33][36], g_iAmmoPacks[33], g_iBsAuthorizedg_iBsConnected;

public 
plugin_init()
{
/*    new iError, cError[2];
    g_iSQLTuple = SQL_MakeDbTuple("127.0.0.1", "", "", "ZPNM_Stats");
    g_iSQLConnection = SQL_Connect(g_iSQLTuple, iError, cError, 1);
    
    SQL_SetAffinity( "sqlite" ) // now.test? ye.okay ^^
    if( g_iSQLConnection == Empty_Handle) server_print( "ERROR" )
    new Handle: iQuery = SQL_PrepareQuery(g_iSQLConnection, "CREATE TABLE IF NOT EXISTS Stats (SteamID varchar (36) NOT NULL default '', HumanClass int (1) NOT NULL default -1, ZombieClass int (1) NOT NULL default -1, AmmoPacks int (3) NOT NULL default -1)");
    if ( !SQL_Execute(iQuery) )
        pause("ad")
    SQL_FreeHandle(iQuery);*/
    
    
InitializeDatabase()
    
    
//register_clcmd("say /save", "fwClCmdSaySave")
    //register_clcmd("say /load", "fwClCmdSayLoad")
}
/*
public fwClCmdSaySave(const iID)
{
    SaveAccount(iID)
}

public fwClCmdSayLoad(const iID)
{
    LoadAccount(iID + g_iIDTaskLoadAccount)
}*/

public client_authorized(iID)
{
    if (!
is_user_bot(iID))
        
get_user_authid(iIDg_szAuthID[iID], charsmax(g_szAuthID[]));
    else
    {
        
get_user_name(iIDg_szAuthID[iID], charsmax(g_szAuthID[]))
        
format(g_szAuthID[iID], charsmax(g_szAuthID[]), "BOT%s"g_szAuthID[iID])
    }
    
    
bitsum_add(g_iBsAuthorizediID)
    
    if (
bitsum_get(g_iBsConnectediID))
        
set_task(1.0"LoadAccount"iID g_iIDTaskLoadAccount)
}

public 
client_putinserver(iID)
{
    
bitsum_add(g_iBsConnectediID)
    
    if (
bitsum_get(g_iBsAuthorizediID))
        
set_task(1.0"LoadAccount"iID g_iIDTaskLoadAccount)
}

public 
client_disconnect(iID)
{
    
bitsum_del(g_iBsAuthorizediID)
    
    
remove_task(iID g_iIDTaskUpdateAmmo)
    
remove_task(iID g_iIDTaskLoadAccount)
}

public 
zpnm_user_get_ammo_packs(iIDiAmountiSource)
{
    if (
iSource == AMMO_MAIN)
        return;
    
    
g_iAmmoPacks[iID] = zp_get_user_ammo_packs(iID);
    
    
SaveAmmoPacks(iID)
}

public 
zp_extra_item_selected(iIDiItemIDbIgnoreCostiCost)
{
    if (
bIgnoreCost || !iCost)
        return;
    
    
remove_task(iID g_iIDTaskUpdateAmmo)
    
set_task(0.1"fwTaskUpdateAmmoPacks"iID g_iIDTaskUpdateAmmo)
}

public 
fwTaskUpdateAmmoPacks(iID)
{
    
iID -= g_iIDTaskUpdateAmmo;
    
    
g_iAmmoPacks[iID] = zp_get_user_ammo_packs(iID);
    
    
SaveAmmoPacks(iID)
}

public 
zp_round_ended()
{
    static 
iMaxPlayers;
    
    if (!
iMaxPlayers)
        
iMaxPlayers get_maxplayers();
    
    for (new 
1<= iMaxPlayersi++)
        if (
is_user_connected(i))
            
SaveAccount(i);
}

public 
LoadAccount(iID)
{
    
iID -= g_iIDTaskLoadAccount;
    
    static 
cQuery[256], cData[32];
    
formatex(cQuery255"SELECT * FROM Stats WHERE SteamID = '%s'"g_szAuthID[iID]);
    
num_to_str(iIDcData31);

    
SQL_ThreadQuery(/*g_iSQLTuple*/g_hTuple"_LoadAccount"cQuerycData31);
}

//#include <D7Debug>

SaveAmmoPacks(const iID)
{
    
//ftD7Log(_, _, "[SaveAmmoPacks] iID: %d. g_iAmmoPacks: %d. g_szAuthID: ^"%s^".", iID, g_iAmmoPacks[iID], g_szAuthID[iID])
    
    
new HandleiQuery SQL_PrepareQuery(/*g_iSQLConnection*/g_hConnection"UPDATE Stats SET AmmoPacks = %d WHERE SteamID = '%s'"g_iAmmoPacks[iID], g_szAuthID[iID]);
    
SQL_Execute(iQuery);
    
SQL_FreeHandle(iQuery);
}

SaveAccount(iID)
{
    
//ftD7Log(_, _, "[SaveAccount] iID: %d. g_HumanClass: %d. g_ZombieClass: %d. g_iAmmoPacks: %d. g_szAuthID: ^"%s^".", iID, zpnm_get_user_next_hclass(iID), zp_get_user_next_class(iID), g_iAmmoPacks[iID], g_szAuthID[iID])
    
    
new HandleiQuery SQL_PrepareQuery(/*g_iSQLConnection*/g_hConnection"UPDATE Stats SET HumanClass = %d, ZombieClass = %d, AmmoPacks = %d WHERE SteamID = '%s'"zpnm_get_user_next_hclass(iID), zp_get_user_next_class(iID), g_iAmmoPacks[iID], g_szAuthID[iID]);
    
SQL_Execute(iQuery);
    
SQL_FreeHandle(iQuery);
}

public 
_LoadAccount(iFailstateHandleiQuerycError[], iErrorcData[], iDatasizeFloatfQueuetime)
{
    new 
iID str_to_num(cData);

    if (
SQL_NumResults(iQuery) && SQL_MoreResults(iQuery))
    {
        
//ftD7Log(_, _, "[_LoadAccount] iID: %d. Data found. g_HumanClass: %d. g_ZombieClass: %d. g_iAmmoPacks: %d. g_szAuthID: ^"%s^".", iID, SQL_ReadResult(iQuery, 1), SQL_ReadResult(iQuery, 2), SQL_ReadResult(iQuery, 3), g_szAuthID[iID])
        
        
new iTemp SQL_ReadResult(iQuery1);
        
        if (
iTemp > -1)
            
zpnm_set_user_human_class(iIDiTemp)
        
        
iTemp SQL_ReadResult(iQuery2);
        
        if (
iTemp > -1)
            
zp_set_user_zombie_class(iIDiTemp)
        
        
iTemp SQL_ReadResult(iQuery3);
        
        if (
iTemp > -1)
            
zp_set_user_ammo_packs(iIDiTemp)
    }
    else
    {
        
//ftD7Log(_, _, "[_LoadAccount] iID: %d. Data not found. Inserting default values. g_HumanClass: -1. g_ZombieClass: -1. g_iAmmoPacks: -1. g_szAuthID: ^"%s^".", iID, g_szAuthID[iID])
        
        
new HandleiQuery SQL_PrepareQuery(/*g_iSQLConnection*/g_hConnection"INSERT INTO Stats (SteamID, HumanClass, ZombieClass, AmmoPacks) VALUES ('%s', '-1', '-1', '-1')"g_szAuthID[iID]);
        
SQL_Execute(iQuery);
        
SQL_FreeHandle(iQuery);
    }

Attached Files
File Type: sma Get Plugin or Get Source (zpnm_gp_stats_save_sql.sma - 555 views - 6.3 KB)
__________________

Last edited by georgik57; 09-25-2016 at 01:42.
georgik57 is offline
Send a message via MSN to georgik57 Send a message via Yahoo to georgik57 Send a message via Skype™ to georgik57
wickedd
Veteran Member
Join Date: Nov 2009
Old 09-25-2016 , 13:18   Re: SQL saving/loading causing data loss and high ping
Reply With Quote #2

Why are you using a task to retrieve the player info?
__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.

Last edited by wickedd; 09-25-2016 at 13:24.
wickedd is offline
georgik57
Veteran Member
Join Date: Oct 2008
Location: 🎧Music World
Old 09-25-2016 , 13:25   Re: SQL saving/loading causing data loss and high ping
Reply With Quote #3

Quote:
Originally Posted by wickedd View Post
Why are you retrieving the player info twice and why are you using a task to do it?
I've made a separate save ammo packs function because they are more important.
The task is there because it's a pre event of when the ammo packs amount change.
__________________
georgik57 is offline
Send a message via MSN to georgik57 Send a message via Yahoo to georgik57 Send a message via Skype™ to georgik57
Old 09-25-2016, 14:01
wickedd
This message has been deleted by wickedd. Reason: NVM
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 07:11.


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