Hey there.
I am updating my vipmenu plugin by using an in-build database system instead of using store by zephyrus plugin.
My credits value is stored on the database correctly but when I use the command to get the information from the database and print it to chat it does not match.
PHP Code:
#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "SpirT"
#define PLUGIN_VERSION "1.0"
Database DB = null;
char Error[256];
ConVar g_credits;
int GlobalClient[MAXPLAYERS + 1];
ConVar g_akprice;
ConVar g_m4price;
ConVar g_m1price;
ConVar g_awpprice;
ConVar g_timer;
ConVar g_usage;
int akprice;
int m4price;
int m1price;
int awpprice;
int usage[MAXPLAYERS + 1];
int max;
char cvarCredits[20];
int TimerFloat;
int balance[MAXPLAYERS + 1];
Handle CreditTimer[MAXPLAYERS + 1];
#include <sourcemod>
#include <sdktools>
#pragma newdecls required
public Plugin myinfo =
{
name = "[SpirT] VipMenu Credits System DATABASE",
author = PLUGIN_AUTHOR,
description = "",
version = PLUGIN_VERSION,
url = ""
};
public void OnPluginStart()
{
DB = SQL_Connect("testeconfig", true, Error, sizeof(Error));
if(DB == null)
{
PrintToServer("[SpirT - VIPMENU] Error conecting to MySQL Database: %s", Error);
CloseHandle(DB);
}
else
{
PrintToServer("[SpirT - VIPMENU] Connection to MySQL Database succeed.");
CreateDatabaseTables();
}
g_credits = CreateConVar("spirt_vipmenu_credits", "5", "Amount of credits to receive at the end of the interval.");
GetConVarString(g_credits, cvarCredits, sizeof(cvarCredits));
g_timer = CreateConVar("spirt_vipmenu_timer_interval", "10", "Interval in seconds to give credits to all Players.");
TimerFloat = GetConVarInt(g_timer);
g_akprice = CreateConVar("spirt_vipmenu_akprice", "1000", "Credit amount needed to buy this pack");
g_m4price = CreateConVar("spirt_vipmenu_m4price", "1000", "Credit amount needed to buy this pack");
g_m1price = CreateConVar("spirt_vipmenu_m1price", "1000", "Credit amount needed to buy this pack");
g_awpprice = CreateConVar("spirt_vipmenu_awpprice", "2000", "Credit amount needed to buy this pack");
akprice = GetConVarInt(g_akprice);
m4price = GetConVarInt(g_m4price);
m1price = GetConVarInt(g_m1price);
awpprice = GetConVarInt(g_awpprice);
g_usage = CreateConVar("spirt_vipmenu_maxusage", "3", "Max amount of uses of the VIPMENU each round.");
max = GetConVarInt(g_usage);
RegAdminCmd("sm_scredits", Command_SCredits, ADMFLAG_RESERVATION);
RegAdminCmd("sm_vipmenu", Command_VipMenu, ADMFLAG_RESERVATION);
AutoExecConfig(true, "spirt.credits.vipmenu");
}
public Action Command_SCredits(int client, int args)
{
if(DB == null)
{
PrintToServer("[SpirT - VIPMENU] Connection do MySQL Database failed. This command is temporately disabled.");
return Plugin_Handled;
}
char steamid[32];
GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
char squery[256];
Format(squery, sizeof(squery), "SELECT `credits` FROM spirt_credits WHERE steamid = '%s'", steamid);
DBResultSet result = SQL_Query(DB, squery);
if(result == null)
{
PrintToServer("[SpirT - VIPMENU] An error occured while giving you your credits.");
return Plugin_Handled;
}
if(result.FetchRow())
{
PrintToChat(client, " \x0B[SpirT - VIPMENU]\x07 You have \x03%d \x04credits", balance[client]);
return Plugin_Handled;
}
return Plugin_Handled;
}
void CreateDatabaseTables()
{
char squery[256];
Format(squery, sizeof(squery), "CREATE TABLE IF NOT EXISTS spirt_credits (steamid varchar(32) NOT NULL, credits int(11) NOT NULL)");
DBResultSet query = SQL_Query(DB, squery);
if(query == null)
{
PrintToServer("[SpirT - VIPMENU] Failed to create spirt_credits table.");
return;
}
PrintToServer("[SpirT - VIPMENU] Table spirt_credits created with success.");
return;
}
public void OnClientPutInServer(int client)
{
if(DB == null)
{
PrintToServer("[SpirT - VIPMENU] Could not connect to database. Player credits are not being fetched.");
return;
}
GlobalClient[client] = client;
CreditTimer[client] = CreateTimer(TimerFloat * 1.0, Timer_InsertDatabase, client, TIMER_REPEAT);
char steamid[32];
GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
char selQuery[256];
Format(selQuery, sizeof(selQuery), "SELECT steamid FROM spirt_credits");
DBResultSet select = SQL_Query(DB, selQuery);
if(select == null)
{
PrintToServer("[SpirT - VIPMENU] Could not select player from database.");
return;
}
if(select.FetchRow())
{
balance[client] = SQL_FetchInt(select, 0);
return;
}
char squery[256];
Format(squery, sizeof(squery), "INSERT INTO spirt_credits (steamid, credits) VALUES ('%s', 0)", steamid);
DBResultSet insert = SQL_Query(DB, squery);
if(insert == null)
{
PrintToServer("[SpirT - VIPMENU] Could not add a new player to database.");
return;
}
PrintToServer("[SpirT - VIPMENU] A new player called %N was added to the Database.", client);
return;
}
public void OnClientDisconnect(int client)
{
if(CreditTimer[client] != INVALID_HANDLE)
{
KillTimer(CreditTimer[client]);
return;
}
}
public Action Timer_InsertDatabase(Handle timer, any client)
{
if(!IsClientInGame(client))
{
return Plugin_Continue;
}
char squery[256];
Format(squery, sizeof(squery), "UPDATE spirt_credits SET credits = credits + %s", cvarCredits);
DBResultSet query = SQL_Query(DB, squery);
if(query == null)
{
PrintToServer("[SpirT - VIPMENU] Could not give more %s credits to the client", cvarCredits);
return Plugin_Handled;
}
PrintToChat(client, " \x0B[SpirT - VIPMENU]\x07 You have received \x03%s \x04credits", cvarCredits);
return Plugin_Handled;
}
public Action Command_VipMenu(int client, int args)
{
if(usage[client] < max)
{
Main().Display(client, MENU_TIME_FOREVER);
return Plugin_Handled;
}
PrintToChat(client, " \x0B[SpirT - VIPMENU]\x07 You have reached the \x02max amount of \x04uses per round! \x03Please wait to the next round.");
return Plugin_Handled;
}
Menu Main()
{
Menu menu = new Menu(MainHandle, MENU_ACTIONS_ALL);
menu.SetTitle("Choose a type of Items:");
menu.AddItem("1", "Guns");
menu.AddItem("2", "Buffs");
menu.ExitButton = true;
return menu;
}
public int MainHandle(Menu menu, MenuAction action, int client, int item)
{
char choice[32];
menu.GetItem(item, choice, sizeof(choice));
if(action == MenuAction_Select)
{
if(StrEqual(choice, "1"))
{
delete menu;
Guns(client).Display(client, MENU_TIME_FOREVER);
}
else if(StrEqual(choice, "2"))
{
delete menu;
//Buffs(client).Display(client, MENU_TIME_FOREVER);
}
}
else if(action == MenuAction_End)
{
delete menu;
}
}
Menu Guns(int client)
{
Menu menu = new Menu(GunsHandle, MENU_ACTIONS_ALL);
menu.SetTitle("Choose a Gun Pack:");
char text[64];
Format(text, sizeof(text), "Credits: %d", balance[client]);
menu.AddItem(text, text, ITEMDRAW_RAWLINE);
char aktext[256];
char m4text[256];
char m1text[256];
char awptext[256];
Format(aktext, sizeof(aktext), "AK47 + DEAGLE [%d]", akprice);
Format(m4text, sizeof(m4text), "M4A4 + DEAGLE [%d]", m4price);
Format(m1text, sizeof(m1text), "M4A1-S + DEAGLE [%d]", m1price);
Format(awptext, sizeof(awptext), "AWP + DEAGLE [%d]", awpprice);
if(balance[client] < akprice)
{
menu.AddItem(aktext, aktext, ITEMDRAW_RAWLINE);
}
if(balance[client] < m4price)
{
menu.AddItem(m4text, m4text, ITEMDRAW_RAWLINE);
}
if(balance[client] < m1price)
{
menu.AddItem(m1text, m1text, ITEMDRAW_RAWLINE);
}
if(balance[client] < awpprice)
{
menu.AddItem(awptext, awptext, ITEMDRAW_RAWLINE);
}
menu.AddItem("1", aktext);
menu.AddItem("2", m4text);
menu.AddItem("3", m1text);
menu.AddItem("4", awptext);
menu.ExitBackButton = true;
menu.ExitButton = true;
return menu;
}
public int GunsHandle(Menu menu, MenuAction action, int client, int item)
{
char steamid[32];
GetClientAuthId(GlobalClient[client], AuthId_Steam2, steamid, sizeof(steamid));
char choice[32];
menu.GetItem(item, choice, sizeof(choice));
if(action == MenuAction_Select)
{
if(StrEqual(choice, "1"))
{
delete menu;
usage[client]++;
GivePlayerItem(client, "weapon_ak47");
GivePlayerItem(client, "weapon_deagle");
UpdateClientCredits(client, akprice);
}
else if(StrEqual(choice, "2"))
{
delete menu;
//Buffs(client).Display(client, MENU_TIME_FOREVER);
}
}
else if(action == MenuAction_End)
{
delete menu;
}
}
void UpdateClientCredits(int client, int credits)
{
char steamid[64];
GetClientAuthId(client, AuthId_Engine, steamid, sizeof(steamid));
char QueryRemoveCredits[256];
Format(QueryRemoveCredits, sizeof(QueryRemoveCredits), "UPDATE spirt_credits SET credits = credits + %d WHERE steamid = '%s'", credits, steamid);
DBResultSet remove = SQL_Query(DB, QueryRemoveCredits);
if(DB == null || remove == null)
{
PrintToServer("[SpirT - VIPMENU] Failed to update player credits.");
}
else
{
PrintToChat(client, " \x0B[SpirT - VIPMENU]\x07 You received: \x03AK47 + DEAGLE");
}
}
Is there any issue? As well I need another thing. I need to get the steamID of the client when he clicks in any item of the Menu to remove a credit amount from that client but it prints to console that client index is invalid (0).