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

Help with Models/Hats shop with MySQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xeloxded
Member
Join Date: Dec 2016
Old 03-17-2018 , 15:54   Help with Models/Hats shop with MySQL
Reply With Quote #1

Hello i got the following code that is a Hats shop with MySQL save but i want it to be a Models and hats shop with MySQL save but the MySQL save seems to work but not perfect, sometimes it will lose data and i want to remove auth save and use it with Nick save if possible, thanks

PHP Code:
#include <amxmodx>
#include <sqlx>
#include <cstrike>
#include <engine>
#include <fakemeta>

#define DB_HOST "127.0.0.1"
#define DB_USER "sugi"
#define DB_PASS "sugi"
#define DB_DB    "sugi"

#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] = 
{
    {
"Santa""models/hats/santa.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()
{
    
g_con SQL_MakeDbTuple(DB_HOSTDB_USERDB_PASSDB_DB)
    
db_con()
    
g_menu menu_create("\y Hats""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 hats")
    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
    }

xeloxded is offline
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 03-18-2018 , 05:05   Re: Help with Models/Hats shop with MySQL
Reply With Quote #2

You can use this csgo mod which i'm developing as a reference. Because it is private, I will cut out half of the functionality from it, but you should still be able to pick parts of it which do what you want
Spoiler
__________________
retired chump

Last edited by DjSoftero; 03-18-2018 at 05:07.
DjSoftero is offline
xeloxded
Member
Join Date: Dec 2016
Old 03-18-2018 , 11:35   Re: Help with Models/Hats shop with MySQL
Reply With Quote #3

Quote:
Originally Posted by DjSoftero View Post
You can use this csgo mod which i'm developing as a reference. Because it is private, I will cut out half of the functionality from it, but you should still be able to pick parts of it which do what you want
Spoiler
Thanks for your reply, the thing is, what i have to look into your code? because im not familiarized with MySQL so thats why im here, and i need to implement a Models shop along with the hats one, hope some1 can help me with that
xeloxded is offline
Reply


Thread Tools
Display Modes

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 04:40.


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