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

Solved SQL Save


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Suntox
Member
Join Date: Nov 2016
Old 05-30-2018 , 11:19   SQL Save
Reply With Quote #1

hi, im using this hat shop with SQL, i've added hat2 and hat3, but it's saves hat1 instead of hat2, hat3.
any help ?
PHP Code:
#include <amxmodx>
#include <sqlx>
#include <cstrike>
#include <engine>
#include <fakemeta>

#define PLUGIN  "New Plug-In"
#define VERSION "1.0"
#define AUTHOR  "Sugisaki"

#define DB_HOST ""
#define DB_USER ""
#define DB_PASS ""
#define DB_DB    ""

#if AMXX_VERSION_NUM > 182
    #define client_disconnect client_disconnected
#endif

enum _:HATS_ENUM
{
    
H_NAME[32],
    
H_MDL[60],
    
H_COST
}

new 
g_hats[][HATS_ENUM] = 
{
    {
"hat1""models/hats/hat1.mdl"5000},
    {
"hat2""models/hats/hat2.mdl"5000},
    {
"hat3""models/hats/hat3.mdl"5000}
}

new 
table1[] = "CREATE TABLE IF NOT EXISTS `hats_users` ( `user` VARCHAR(33) NOT NULL , `money` INT NOT NULL , `hats` VARCHAR(60) NOT NULL , PRIMARY KEY (`user`)) ENGINE = InnoDB;"

new Handle:g_con

new bool:hats_buyed[33][sizeof g_hats]
new 
ent_hat[33]
new 
g_menu

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
g_con SQL_MakeDbTuple(DB_HOSTDB_USERDB_PASSDB_DB)
    
db_con()
    
g_menu menu_create("\yHats""mh_buy_hat")
    new 
temp[100]
    new 
cb menu_makecallback("mc_hats")
    for(new 
sizeof(g_hats) ; i++)
    {
        
formatex(tempcharsmax(temp), "%s \y[$%i]"g_hats[i][H_NAME], g_hats[i][H_COST])
        
menu_additem(g_menutemp""0cb)
    }
    
menu_setprop(g_menuMPROP_EXITMEXIT_ALL)
    
register_clcmd("say /buyhat""cmds_buyhat")
    
register_clcmd("say /hat""cmd_hats")
}
db_con()
{
    new 
error[60], errno
    SQL_Connect
(g_conerrnoerrorcharsmax(error))
    if(
errno)
    {
        
set_fail_state(error);
        return
    }
    
SQL_ThreadQuery(g_con"_table1"table1)
}
public 
_table1(failstateHandle:queryerror[], errnumdata[], sizeFloat:queuetime)
{
    if(
failstate != TQUERY_SUCCESS)
    {
        
log_amx("MYSQL ERROR: %i %s"errnumerror)
        return
    }
}
public 
plugin_precache()
{
    for(new 
sizeof(g_hats) ; i++)
    {
        
precache_model(g_hats[i][H_MDL])
    }
}
set_hat(idhat_id)
{
    if(
ent_hat[id] <= 0)
    {
        new 
ent create_entity("info_target")
        
ent_hat[id] = ent
        entity_set_string
(entEV_SZ_classname"player_hat")
        
entity_set_int(entEV_INT_movetypeMOVETYPE_FOLLOW)
        
entity_set_edict(entEV_ENT_aimentid)
        
entity_set_edict(entEV_ENT_ownerid)
    }
    if(
<= hat_id sizeof(g_hats))
    {
        
engfunc(EngFunc_SetModelent_hat[id], g_hats[hat_id][H_MDL])
    }
    else if(
is_valid_ent(ent_hat[id]) && hat_id 0)
    {
        
remove_entity(ent_hat[id])
        
ent_hat[id] = 0
    
}
}
public 
mc_hats(idmenuitem)
{

    if(
cs_get_user_money(id) < g_hats[item][H_COST] || hats_buyed[id][item])
    {
        return 
ITEM_DISABLED
    
}
    return 
ITEM_ENABLED
}
public 
mh_buy_hat(idmenuitem)
{
    if(
item == MENU_EXIT)
    {
        return
    }
    
set_hat(iditem)
    
hats_buyed[id][item] = true
}
public 
cmds_buyhat(id)
{
    
menu_display(idg_menu)
}
public 
cmd_hats(id)
{
    new 
menu menu_create("\rYour hats""mh_hats")
    
menu_additem(menu"No hat")
    new 
num[3]
    for(new 
sizeof(g_hats) ; i++)
    {
        if(!
hats_buyed[id][i])
        {
            continue
        }
        
num_to_str(inum2)
        
menu_additem(menug_hats[i][H_NAME], num)
    }
    
menu_setprop(menuMPROP_EXITMEXIT_ALL)
    
menu_display(idmenu)
}
public 
mh_hats(idmenuitem)
{
    if(
item == MENU_EXIT)
    {
        
menu_destroy(menu)
        return
    }
    if(
item == 0)
    {
        
set_hat(id, -1)
        
menu_destroy(menu)
        return
    }
    new 
info[3], a
    menu_item_getinfo
(menuitemainfo2""0a)
    
menu_destroy(menu)
    
set_hat(idstr_to_num(info))
}
public 
client_putinserver(id)
{
    
ent_hat[id] = 0
    
new authid[32]
    
get_user_authid(idauthidcharsmax(authid))
    new 
Query[128]
    new 
data[3]
    
data[0] = id
    formatex
(Querycharsmax(Query), "SELECT * FROM hats_users WHERE user='%s'"authid)
    
SQL_ThreadQuery(g_con"_get_data"Querydatacharsmax(data))
}
public 
_get_data(failstateHandle:queryerror[], errnumdata[], sizeFloat:queuetime)
{
    new 
id data[0]
    if(
failstate != TQUERY_SUCCESS)
    {
        
log_amx(error)
        return
    }
    if(
SQL_NumResults(query) > 0)
    {
        new 
hat_row[60], hat_id[6]
        
SQL_ReadResult(querySQL_FieldNameToNum(query"hats"), hat_rowcharsmax(hat_row))
        
cs_set_user_money(idSQL_ReadResult(querySQL_FieldNameToNum(query"money")))
        
trim(hat_row)
        if(!
hat_row[0])
        {
            return
        }
       
        new 
count 0
        
new len strlen(hat_row)
        new 
i
        
for(len i++)
        {
            if(
hat_row[i] == ',')
            {
                
count += 1
            
}
        }
        for(
<= count i++)
        {
            
strtok(hat_rowhat_idcharsmax(hat_id), hat_rowcharsmax(hat_row), ','1)
            
hats_buyed[id][str_to_num(hat_id)] = true
        
}
    }
    else
    {
        new 
authid[32]
        
get_user_authid(idauthidcharsmax(authid))
        new 
Query[128]
        
formatex(Querycharsmax(Query), "INSERT INTO `hats_users` (`user`) VALUES ('%s')"authid)
        
SQL_ThreadQuery(g_con"_insert_player"Query)
    }
}
public 
_insert_player(failstateHandle:queryerror[], errnumdata[], sizeFloat:queuetime)
{
    if(
failstate != TQUERY_SUCCESS)
    {
        
log_amx(error)
        return
    }
}
public 
client_disconnect(id)
{
    if(
is_valid_ent(ent_hat[id]))
    {
        
remove_entity(ent_hat[id])
        
ent_hat[id] = 
    
}
    new 
authid[32]
    
get_user_authid(idauthidcharsmax(authid))
    new 
Query[128]
    new 
hats[20]
    for(new 
sizeof(g_hats) ; i++)
    {
        if(
hats_buyed[id][i])
        {
            
format(hatscharsmax(hats), "%s,"i)
        }
    }
    
formatex(Querycharsmax(Query), "UPDATE `hats_users` SET `money` = '%i', `hats` = '%s' WHERE `user` = '%s';"cs_get_user_money(id), hatsauthid)
    
SQL_ThreadQuery(g_con"_update_player"Query)
}
public 
_update_player(failstateHandle:queryerror[], errnumdata[], sizeFloat:queuetime)
{
    if(
failstate != TQUERY_SUCCESS)
    {
        
log_amx(error)
        return
    }

Attached Files
File Type: sma Get Plugin or Get Source (hats.sma - 491 views - 6.5 KB)

Last edited by Suntox; 06-04-2018 at 13:36.
Suntox is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 06-02-2018 , 12:10   Re: SQL Save
Reply With Quote #2

PHP Code:
public client_disconnect(id

    if(
is_valid_ent(ent_hat[id])) 
    { 
        
remove_entity(ent_hat[id]) 
        
ent_hat[id] = 0  
    

    new 
authid[32
    
get_user_authid(idauthidcharsmax(authid)) 
    new 
Query[128
    new 
hats[20
    for(new 
sizeof(g_hats) ; i++) 
    { 
        if(
hats_buyed[id][i]) 
        { 
            
format(hatscharsmax(hats), "%s%d,"hatsi
        } 
    }
    if(
strlen(hats) > 1hats[strlen(hats)-1] = 0;
    
formatex(Querycharsmax(Query), "UPDATE `hats_users` SET `money` = '%i', `hats` = '%s' WHERE `user` = '%s';"cs_get_user_money(id), hatsauthid
    
SQL_ThreadQuery(g_con"_update_player"Query


I'd rather to check hats by using bitsums.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 06-04-2018 at 12:17.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Suntox
Member
Join Date: Nov 2016
Old 06-03-2018 , 20:38   Re: SQL Save
Reply With Quote #3

thanks for reply, it's works but, it's doesn't saves hat2

Last edited by Suntox; 06-03-2018 at 20:38.
Suntox is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 06-03-2018 , 21:43   Re: SQL Save
Reply With Quote #4

PHP Code:
public _get_data(failstateHandle:queryerror[], errnumdata[], sizeFloat:queuetime

    new 
id data[0
    if(
failstate != TQUERY_SUCCESS
    { 
        
log_amx(error
        return 
    } 
    if(
SQL_NumResults(query) > 0
    { 
        new 
hat_row[60];
        
SQL_ReadResult(querySQL_FieldNameToNum(query"hats"), hat_rowcharsmax(hat_row)) 
        
cs_set_user_money(idSQL_ReadResult(querySQL_FieldNameToNum(query"money")))
        if(!
hat_row[0]) 
        { 
            return 
        } 
        
        for(new 
ivbFlags read_flags(hat_row); <  sizeof g_hats i++) 
        { 
            if(
vbFlags & (1<<i)) hats_buyed[id][i] = true;
        } 
    } 
    else 
    { 
        new 
authid[32
        
get_user_authid(idauthidcharsmax(authid)) 
        new 
Query[128
        
formatex(Querycharsmax(Query), "INSERT INTO `hats_users` (`user`) VALUES ('%s')"authid
        
SQL_ThreadQuery(g_con"_insert_player"Query
    } 
}

public 
client_disconnect(id

    if(
is_valid_ent(ent_hat[id])) 
    { 
        
remove_entity(ent_hat[id]) 
        
ent_hat[id] = 0  
    

    new 
authid[32
    
get_user_authid(idauthidcharsmax(authid)) 
    new 
Query[128
    new 
hats[20], vbFlags;
    for(new 
sizeof(g_hats) ; i++) 
    {
        if(
hats_buyed[id][i]) 
        {
            
vbFlags |= (1<<i);
        } 
    }
    
get_flags(vbFlagshatscharsmax(hats));
    
formatex(Querycharsmax(Query), "UPDATE `hats_users` SET `money` = '%i', `hats` = '%s' WHERE `user` = '%s';"cs_get_user_money(id), hatsauthid
    
SQL_ThreadQuery(g_con"_update_player"Query

Here you go.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 06-04-2018 at 11:53.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Suntox
Member
Join Date: Nov 2016
Old 06-04-2018 , 13:36   Re: SQL Save
Reply With Quote #5

Thanks Natsheh! It works fine!
Suntox 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:36.


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