8guawong |
08-17-2020 02:07 |
Re: [Help!] Showing Expiration Time of client from Sourcebans Donation Control
Quote:
Originally Posted by Oylsister
(Post 2714324)
Hi, I tried to write a plugin that fetches the data from MySQL. the Donations Control itself is not a plugin and working mostly on the website ( https://forums.alliedmods.net/showthread.php?t=221742)
So the plugin has to fetch the data from another table since the plugin doesn't have a table itself. I did ask my player on the server to try out but it showing a message of that player has no perk.
This is my first time doing the SQL and database stuff.
PHP Code:
#pragma newdecls required
Handle g_hDatabase = INVALID_HANDLE;
char g_sDatabaseName[] = "sourcedonate";
bool g_bhaveperk[MAXPLAYERS];
public void OnPluginStart()
{
RegConsoleCmd("sm_vip", Expiration_Check);
}
public void OnConfigsExecuted()
{
SQL_TConnect(SQL_Connected, g_sDatabaseName);
}
public void SQL_Connected(Handle owner, Handle hndl, const char[] error, any data)
{
if (g_hDatabase != INVALID_HANDLE)
{
CloseHandle(g_hDatabase);
g_hDatabase = INVALID_HANDLE;
}
if (hndl == INVALID_HANDLE)
{
LogError("[Source Donator] could not connect to database.");
return;
}
g_hDatabase = hndl;
LogMessage("[Source Donator] Connected successfully to database.");
}
public void OnClientPostAdminCheck(int client)
{
g_bhaveperk[client] = false;
LoadVip(client);
}
public Action Expiration_Check(int client, int args)
{
if (g_hDatabase == INVALID_HANDLE)
{
CReplyToCommand(client, "\x04[Sourcebans Donator] \x01the Database is now being loaded.");
return Plugin_Handled;
}
if (g_bhaveperk[client])
{
char sSteamID[32];
GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));
char sQuery[1024];
Format(sQuery, sizeof(sQuery), "SELECT tier, sign_up_date, renewal_date, expiration_date FROM donors WHERE steam_id = '%s';", sSteamID);
SQL_TQuery(g_hDatabase, SQL_ProcessResult, sQuery, client);
}
else
{
CReplyToCommand(client, "\x04[Sourcebans Donator] \x01You don't have donator perk right now or the database is not loaded yet.");
return Plugin_Handled;
}
return Plugin_Handled;
}
public void SQL_ProcessResult(Handle db, Handle hQuery, const char[] error, any client)
{
int iDay, iMonth, iYear, iHour, iMinute, iSecond;
int iDay2, iMonth2, iYear2, iHour2, iMinute2, iSecond2;
int iDay3, iMonth3, iYear3, iHour3, iMinute3, iSecond3;
char m_clienttier[64];
if (hQuery == INVALID_HANDLE)
{
LogError("[Source Donator] Query failed: %s", error);
return;
}
while (SQL_FetchRow(hQuery))
{
int ClientTier = SQL_FetchInt(hQuery, 0);
if (ClientTier == 1)
Format(m_clienttier, sizeof(m_clienttier), "Donator");
else if (ClientTier == 3)
Format(m_clienttier, sizeof(m_clienttier), "VIP-Diamond");
int SignUnixTime = SQL_FetchInt(hQuery, 1);
int RenewalUnixTime = SQL_FetchInt(hQuery, 2);
int ExpUnixTime = SQL_FetchInt(hQuery, 3);
UnixToTime(SignUnixTime, iYear, iMonth, iDay, iHour, iMinute, iSecond, UT_TIMEZONE_SERVER);
UnixToTime(RenewalUnixTime, iYear2, iMonth2, iDay3, iHour2, iMinute2, iSecond2, UT_TIMEZONE_SERVER);
UnixToTime(ExpUnixTime, iYear3, iMonth3, iDay3, iHour3, iMinute3, iSecond3, UT_TIMEZONE_SERVER);
}
Menu SourcedonatorMenu = CreateMenu(SourcedonatorMenuHandler);
char m_signdate[512];
char m_renewdate[512];
char m_expdate[512];
char sSteamID[32];
char m_type[512];
GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));
Format(m_signdate, sizeof(m_signdate), "First Signup date: %02d/%02d/%d", iMonth, iDay, iYear);
Format(m_renewdate, sizeof(m_renewdate), "Renewal Date: %02d/%02d/%d", iMonth2, iDay2, iYear2);
Format(m_expdate, sizeof(m_expdate), "Expiry date: %02d/%02d/%d", iMonth3, iDay3, iYear3);
Format(m_type, sizeof(m_type), "Perk Type: %s", m_clienttier);
SetMenuTitle(SourcedonatorMenu, "Sourcebans Donator Status");
AddMenuItem(SourcedonatorMenu, "SteamID: %s", sSteamID, ITEMDRAW_DISABLED);
AddMenuItem(SourcedonatorMenu, "%s\n", m_type, ITEMDRAW_DISABLED);
AddMenuItem(SourcedonatorMenu, "%s", m_signdate, ITEMDRAW_DISABLED);
AddMenuItem(SourcedonatorMenu, "%s\n", m_renewdate, ITEMDRAW_DISABLED);
AddMenuItem(SourcedonatorMenu, "%s", m_expdate, ITEMDRAW_DISABLED);
DisplayMenu(SourcedonatorMenu, client, 60);
}
public int SourcedonatorMenuHandler(Handle menu, MenuAction action, int client, int item)
{
char sString[32];
GetMenuItem(menu, item, sString, sizeof(sString));
if (action == MenuAction_Select)
{
// Meh nothing to do with the option
}
}
public void LoadVip(int client)
{
char sSteamID[32];
GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));
char isDonatorQuery[1024];
Format(isDonatorQuery, sizeof(isDonatorQuery), "SELECT expiration_date FROM Donors WHERE steam_id '%s';", sSteamID);
SQL_TQuery(g_hDatabase, SQLCheckPerkQuery, isDonatorQuery, GetClientUserId(client));
}
public void SQLCheckPerkQuery(Handle owner, Handle hDonatorQuery, const char[] error, any client)
{
if (IsValidClient(client))
{
while (SQL_FetchRow(hDonatorQuery))
{
int ExpUnixTime = SQL_FetchInt(hDonatorQuery, 0);
int CurrentTime = GetTime();
if (ExpUnixTime > CurrentTime)
g_bhaveperk[client] = true;
}
}
}
stock bool IsValidClient(int client)
{
return (1 <= client <= MaxClients && IsClientInGame(client));
}
Plugin successfully connect to the database btw.
|
show a result of your menu and your sql table
|