Wrong section
Hellow!! Chilean user here x) *yeah it may be strange for a CIS forum (CIS i think it is) lol.
I got a request or help.
This is a plugin i have, it seems to works but the save method its not perfect, sometimes it will lose data
What i wants is a fix to the save method and remove authid save to nick save and that you can buy models and hats
I got a request or help.
This is a plugin i have, it seems to works but the save method its not perfect, sometimes it will lose data
What i wants is a fix to the save method and remove authid save to nick save and that you can buy models and hats
PHP:
#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 "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()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
g_con = SQL_MakeDbTuple(DB_HOST, DB_USER, DB_PASS, DB_DB)
db_con()
g_menu = menu_create("\yHats", "mh_buy_hat")
new temp[100]
new cb = menu_makecallback("mc_hats")
for(new i = 0 ; i < sizeof(g_hats) ; i++)
{
formatex(temp, charsmax(temp), "%s \y[$%i]", g_hats[i][H_NAME], g_hats[i][H_COST])
menu_additem(g_menu, temp, "", 0, cb)
}
menu_setprop(g_menu, MPROP_EXIT, MEXIT_ALL)
register_clcmd("say /buyhat", "cmds_buyhat")
register_clcmd("say /hat", "cmd_hats")
}
db_con()
{
new error[60], errno
SQL_Connect(g_con, errno, error, charsmax(error))
if(errno)
{
set_fail_state(error);
return
}
SQL_ThreadQuery(g_con, "_table1", table1)
}
public _table1(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
{
if(failstate != TQUERY_SUCCESS)
{
log_amx("MYSQL ERROR: %i %s", errnum, error)
return
}
}
public plugin_precache()
{
for(new i = 0 ; i < sizeof(g_hats) ; i++)
{
precache_model(g_hats[i][H_MDL])
}
}
set_hat(id, hat_id)
{
if(ent_hat[id] <= 0)
{
new ent = create_entity("info_target")
ent_hat[id] = ent
entity_set_string(ent, EV_SZ_classname, "player_hat")
entity_set_int(ent, EV_INT_movetype, MOVETYPE_FOLLOW)
entity_set_edict(ent, EV_ENT_aiment, id)
entity_set_edict(ent, EV_ENT_owner, id)
}
if(0 <= hat_id < sizeof(g_hats))
{
engfunc(EngFunc_SetModel, ent_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(id, menu, item)
{
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(id, menu, item)
{
if(item == MENU_EXIT)
{
return
}
set_hat(id, item)
hats_buyed[id][item] = true
}
public cmds_buyhat(id)
{
menu_display(id, g_menu)
}
public cmd_hats(id)
{
new menu = menu_create("\rYour hats", "mh_hats")
menu_additem(menu, "No hat")
new num[3]
for(new i = 0 ; i < sizeof(g_hats) ; i++)
{
if(!hats_buyed[id][i])
{
continue
}
num_to_str(i, num, 2)
menu_additem(menu, g_hats[i][H_NAME], num)
}
menu_setprop(menu, MPROP_EXIT, MEXIT_ALL)
menu_display(id, menu)
}
public mh_hats(id, menu, item)
{
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(menu, item, a, info, 2, "", 0, a)
menu_destroy(menu)
set_hat(id, str_to_num(info))
}
public client_putinserver(id)
{
ent_hat[id] = 0
new authid[32]
get_user_authid(id, authid, charsmax(authid))
new Query[128]
new data[3]
data[0] = id
formatex(Query, charsmax(Query), "SELECT * FROM hats_users WHERE user='%s'", authid)
SQL_ThreadQuery(g_con, "_get_data", Query, data, charsmax(data))
}
public _get_data(failstate, Handle:query, error[], errnum, data[], size, Float: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(query, SQL_FieldNameToNum(query, "hats"), hat_row, charsmax(hat_row))
cs_set_user_money(id, SQL_ReadResult(query, SQL_FieldNameToNum(query, "money")))
trim(hat_row)
if(!hat_row[0])
{
return
}
new count = 0
new len = strlen(hat_row)
new i
for(i = 0 ; i < len ; i++)
{
if(hat_row[i] == ',')
{
count += 1
}
}
for(i = 0 ; i <= count ; i++)
{
strtok(hat_row, hat_id, charsmax(hat_id), hat_row, charsmax(hat_row), ',', 1)
hats_buyed[id][str_to_num(hat_id)] = true
}
}
else
{
new authid[32]
get_user_authid(id, authid, charsmax(authid))
new Query[128]
formatex(Query, charsmax(Query), "INSERT INTO `hats_users` (`user`) VALUES ('%s')", authid)
SQL_ThreadQuery(g_con, "_insert_player", Query)
}
}
public _insert_player(failstate, Handle:query, error[], errnum, data[], size, Float: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] = 0
}
new authid[32]
get_user_authid(id, authid, charsmax(authid))
new Query[128]
new hats[20]
for(new i = 0 ; i < sizeof(g_hats) ; i++)
{
if(hats_buyed[id][i])
{
format(hats, charsmax(hats), "%s,", i)
}
}
formatex(Query, charsmax(Query), "UPDATE `hats_users` SET `money` = '%i', `hats` = '%s' WHERE `user` = '%s';", cs_get_user_money(id), hats, authid)
SQL_ThreadQuery(g_con, "_update_player", Query)
}
public _update_player(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
{
if(failstate != TQUERY_SUCCESS)
{
log_amx(error)
return
}
}