Make ForumIntegration request username, and thus use this in listwinners to make the whole thing async.

Rework listwinners (reward) plugin so now it's async and simple.

go back to ded
This commit is contained in:
Rawrington 2021-08-21 20:54:00 +01:00
parent 8405bc52f2
commit 72f2d3168e
3 changed files with 96 additions and 251 deletions

View File

@ -14,6 +14,7 @@
/* STRINGS */ /* STRINGS */
char G_sGroup[MAXPLAYERS+1][64]; char G_sGroup[MAXPLAYERS+1][64];
char G_sName[MAXPLAYERS+1][32];
/* BOOLS */ /* BOOLS */
bool G_bPreAdminChecked[MAXPLAYERS+1]; bool G_bPreAdminChecked[MAXPLAYERS+1];
@ -25,10 +26,10 @@ bool G_bResponsePassed[MAXPLAYERS+1];
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
public Plugin myinfo = public Plugin myinfo =
{ {
name = "UNLOZE Forum Integration", name = "UNLOZE Forum Integration",
author = ".George & zaCade (Original by Botox)", author = ".George & zaCade (Original by Botox)",
description = "Handles forum access ingame", description = "Handles forum access ingame",
version = "1.2.1" version = "1.2.1"
}; };
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -58,6 +59,7 @@ public void OnPluginStart()
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{ {
CreateNative("AsyncHasSteamIDReservedSlot", Native_AsyncHasSteamIDReservedSlot); CreateNative("AsyncHasSteamIDReservedSlot", Native_AsyncHasSteamIDReservedSlot);
CreateNative("GetClientForumName", Native_GetClientForumName);
RegPluginLibrary("UNLOZE_ForumIntegration"); RegPluginLibrary("UNLOZE_ForumIntegration");
@ -109,6 +111,7 @@ public void OnClientDisconnect(int client)
G_bResponsePassed[client] = false; G_bResponsePassed[client] = false;
G_sGroup[client][0] = 0; G_sGroup[client][0] = 0;
G_sName[client][0] = 0;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -118,21 +121,21 @@ public void OnClientAuthorized(int client, const char[] sSteamID32)
{ {
if (IsFakeClient(client)) if (IsFakeClient(client))
return; return;
char sSteamID64[32]; char sSteamID64[32];
SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64)); SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64));
char sRequest[256]; char sRequest[256];
FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", UNLOZE_APIKEY, sSteamID64); FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", UNLOZE_APIKEY, sSteamID64);
int iSerial = GetClientSerial(client); int iSerial = GetClientSerial(client);
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest);
if (!hRequest || if (!hRequest ||
!SteamWorks_SetHTTPCallbacks(hRequest, OnClientAuthorized_OnTransferComplete) || !SteamWorks_SetHTTPCallbacks(hRequest, OnClientAuthorized_OnTransferComplete) ||
!SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) || !SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) ||
!SteamWorks_SendHTTPRequest(hRequest)) !SteamWorks_SendHTTPRequest(hRequest))
{ {
delete hRequest; delete hRequest;
} }
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -147,7 +150,7 @@ public int OnClientAuthorized_OnTransferComplete(Handle hRequest, bool bFailure,
delete hRequest; delete hRequest;
return; return;
} }
if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK) if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK)
{ {
G_bResponseFailed[client] = true; G_bResponseFailed[client] = true;
@ -158,7 +161,7 @@ public int OnClientAuthorized_OnTransferComplete(Handle hRequest, bool bFailure,
delete hRequest; delete hRequest;
return; return;
} }
SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnClientAuthorized_OnTransferResponse, iSerial); SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnClientAuthorized_OnTransferResponse, iSerial);
} }
@ -167,6 +170,8 @@ public int OnClientAuthorized_OnTransferComplete(Handle hRequest, bool bFailure,
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
public int OnClientAuthorized_OnTransferResponse(char[] sData, int iSerial) public int OnClientAuthorized_OnTransferResponse(char[] sData, int iSerial)
{ {
char splitData[2][32];
int client = GetClientFromSerial(iSerial); int client = GetClientFromSerial(iSerial);
if (!client) //Player disconnected. if (!client) //Player disconnected.
@ -175,13 +180,23 @@ public int OnClientAuthorized_OnTransferResponse(char[] sData, int iSerial)
TrimString(sData); TrimString(sData);
StripQuotes(sData); StripQuotes(sData);
LogMessage("reached sData with status 200: %s", sData); LogMessage("reached sData with status 200: %s", sData);
strcopy(G_sGroup[client], sizeof(G_sGroup[]), sData); ExplodeString(sData, "\r\n", splitData, 2, sizeof(splitData[]));
G_bResponsePassed[client] = true; if(strlen(splitData[1]) > 0)
strcopy(G_sName[client], sizeof(G_sName[]), splitData[1]);
if (G_bPreAdminChecked[client]) if(!StrEqual(splitData[0], "NOGROUP"))
NotifyPostAdminCheck(client); {
strcopy(G_sGroup[client], sizeof(G_sGroup[]), splitData[0]);
G_bResponsePassed[client] = true;
if (G_bPreAdminChecked[client])
NotifyPostAdminCheck(client);
}
else
G_bResponseFailed[client] = true; //users with just a forum name did not pass the VIP check! so the response "failed" (but we store their forum name for later!)
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -238,6 +253,15 @@ stock void ApplyGroupFlags(int client)
} }
} }
public int Native_GetClientForumName(Handle plugin, int numParams)
{
int len = GetNativeCell(2);
int client = GetNativeCell(1);
SetNativeString(2, G_sName[client], len+1);
}
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
// Purpose: // Purpose:
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -310,11 +334,15 @@ public int Native_AsyncHasSteamIDReservedSlot_OnTransferResponse(char[] sData, D
any data; any data;
data = hDataPack.ReadCell(); data = hDataPack.ReadCell();
static char splitData[32];
TrimString(sData); TrimString(sData);
StripQuotes(sData); StripQuotes(sData);
SplitString(sData, "\r\n", splitData, sizeof(splitData));
int result; int result;
if (StrEqual(sData, "Game-Donator", false)) if (StrEqual(splitData, "Game-Donator", false))
result = 1; result = 1;
else else
result = 0; result = 0;

View File

@ -1,5 +1,5 @@
#include <sourcemod> #include <sourcemod>
#include <zombiereloaded> #include <unloze>
#include <cstrike> #include <cstrike>
#include <sdktools> #include <sdktools>
@ -12,7 +12,7 @@ char g_directory_path[128];
public Plugin myinfo = public Plugin myinfo =
{ {
name = "Unloze_VIP_Reward", name = "Unloze_VIP_Reward",
author = "Neon + WASD", author = "Neon + WASD + ??????????",
description = "", description = "",
version = "1.1", version = "1.1",
url = "https://steamcommunity.com/id/n3ontm" url = "https://steamcommunity.com/id/n3ontm"
@ -39,8 +39,19 @@ public void OnPluginStart()
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
public Action Command_Winners(int iClient, int iArgs) public Action Command_Winners(int iClient, int iArgs)
{ {
static char sSID[64];
static char sName[64];
for (int client = 1; client <= MaxClients; client++)
{
if(IsValidWinner(client))
{
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
GetForumNameFormat(client, sName, sizeof(sName));
PrintToConsole(iClient, "%s --- %N --- %s", sSID, client, sName);
}
}
CheckMYSQL(iClient);
return Plugin_Handled; return Plugin_Handled;
} }
@ -65,240 +76,44 @@ public Action Event_Round_End(Handle event, const char[] name, bool dontBroadcas
Handle file_handle = OpenFile(file_path, "a", false); Handle file_handle = OpenFile(file_path, "a", false);
WriteFileLine(file_handle, "Current score: (CT) : %d - (T): %d", ct_score, t_score); WriteFileLine(file_handle, "Current score: (CT) : %d - (T): %d", ct_score, t_score);
CheckMYSQL_2(file_handle);
static char sSID[64];
static char sName[64];
for (int client = 1; client <= MaxClients; client++)
{
if(IsValidWinner(client))
{
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
GetForumNameFormat(client, sName, sizeof(sName));
WriteFileLine(file_handle, "%s --- %N --- %s", sSID, client, sName);
}
}
WriteFileLine(file_handle, "\n"); WriteFileLine(file_handle, "\n");
CloseHandle(file_handle); CloseHandle(file_handle);
} }
} }
//---------------------------------------------------------------------------------------------------- GetForumNameFormat(int client, char[] sName, int sNameLen)
// Purpose: Saves all winners into a textfile located in sourcemod/data/events/.
//----------------------------------------------------------------------------------------------------
CheckMYSQL_2(Handle file_handle)
{ {
char error[255]; char sForumName[64];
Database db; GetClientForumName(client, sForumName, sizeof(sForumName));
if (SQL_CheckConfig("xenforo")) if(strlen(sForumName) > 0)
{ Format(sName, sNameLen, "FORUM NAME: %s", sForumName);
db = SQL_Connect("xenforo", true, error, sizeof(error)); else
} Format(sName, sNameLen, "NO FORUM ACCOUNT");
if (db == null)
{
LogError("Could not connect to database: %s", error);
delete db;
return;
}
for (int client = 1; client <= MaxClients; client++)
{
if (IsValidClient(client))
{
if (IsPlayerAlive(client))
{
if (GetClientTeam(client) == CS_TEAM_CT)
{
if (!IsFakeClient(client) && !IsClientSourceTV(client))
{
char sSID[64];
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
char sQuery[255];
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user_field_value WHERE field_value = '%s'", sSID);
DBResultSet rs;
int iUserID;
if ((rs = SQL_Query(db, sQuery)) == null)
{
delete rs;
delete db;
LogError("Database Error: %s", error);
return;
}
if (!(rs.RowCount > 0))
{
char sSID64[64];
GetClientAuthId(client, AuthId_SteamID64, sSID64, sizeof(sSID64));
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user_external_auth WHERE provider_key = '%s'", sSID64);
rs = SQL_Query(db, sQuery);
if (!(rs.RowCount > 0))
{
WriteFileLine(file_handle, "%s --- %N --- NO FORUM ACCOUNT", sSID, client);
delete rs;
continue;
}
else
{
int iField;
rs.FetchRow();
rs.FieldNameToNum("user_id", iField);
iUserID = rs.FetchInt(iField);
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user WHERE user_id = '%d'", iUserID);
rs = SQL_Query(db, sQuery);
rs.FetchRow();
rs.FieldNameToNum("username", iField);
char sUsername[255];
rs.FetchString(iField, sUsername, sizeof(sUsername));
WriteFileLine(file_handle, "%s --- %N --- FORUM NAME: %s", sSID, client, sUsername);
delete rs;
continue;
}
}
int iField;
rs.FetchRow();
rs.FieldNameToNum("user_id", iField);
iUserID = rs.FetchInt(iField);
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user WHERE user_id = '%d'", iUserID);
rs = SQL_Query(db, sQuery);
rs.FetchRow();
rs.FieldNameToNum("username", iField);
char sUsername[255];
rs.FetchString(iField, sUsername, sizeof(sUsername));
WriteFileLine(file_handle, "%s --- %N --- FORUM NAME: %s", sSID, client, sUsername);
delete rs;
}
}
}
}
}
delete db;
} }
//---------------------------------------------------------------------------------------------------- stock bool IsValidWinner(int client)
// Purpose: Prints all winners to the caller of sm_listwinners.
//----------------------------------------------------------------------------------------------------
CheckMYSQL(int iCaller)
{ {
if (iCaller == 0) if (IsValidClient(client, true) && IsPlayerAlive(client) && GetClientTeam(client) == CS_TEAM_CT && !IsClientSourceTV(client))
return; return true;
char error[255]; return false;
Database db;
if (SQL_CheckConfig("xenforo"))
{
db = SQL_Connect("xenforo", true, error, sizeof(error));
}
if (db == null)
{
LogError("Could not connect to database: %s", error);
delete db;
return;
}
for (int client = 1; client <= MaxClients; client++)
{
if (IsValidClient(client))
{
if (IsPlayerAlive(client))
{
if (GetClientTeam(client) == CS_TEAM_CT)
{
if (!IsFakeClient(client) && !IsClientSourceTV(client))
{
char sSID[64];
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
char sQuery[255];
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user_field_value WHERE field_value = '%s'", sSID);
DBResultSet rs;
int iUserID;
if ((rs = SQL_Query(db, sQuery)) == null)
{
delete rs;
delete db;
LogError("Database Error: %s", error);
return;
}
if (!(rs.RowCount > 0))
{
char sSID64[64];
GetClientAuthId(client, AuthId_SteamID64, sSID64, sizeof(sSID64));
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user_external_auth WHERE provider_key = '%s'", sSID64);
rs = SQL_Query(db, sQuery);
if (!(rs.RowCount > 0))
{
PrintToConsole(iCaller, "%s --- %N --- NO FORUM ACCOUNT", sSID, client);
delete rs;
continue;
}
else
{
int iField;
rs.FetchRow();
rs.FieldNameToNum("user_id", iField);
iUserID = rs.FetchInt(iField);
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user WHERE user_id = '%d'", iUserID);
rs = SQL_Query(db, sQuery);
rs.FetchRow();
rs.FieldNameToNum("username", iField);
char sUsername[255];
rs.FetchString(iField, sUsername, sizeof(sUsername));
PrintToConsole(iCaller, "%s --- %N --- FORUM NAME: %s", sSID, client, sUsername);
delete rs;
continue;
}
}
int iField;
rs.FetchRow();
rs.FieldNameToNum("user_id", iField);
iUserID = rs.FetchInt(iField);
Format(sQuery, sizeof(sQuery), "SELECT * FROM xf_user WHERE user_id = '%d'", iUserID);
rs = SQL_Query(db, sQuery);
rs.FetchRow();
rs.FieldNameToNum("username", iField);
char sUsername[255];
rs.FetchString(iField, sUsername, sizeof(sUsername));
PrintToConsole(iCaller, "%s --- %N --- FORUM NAME: %s", sSID, client, sUsername);
delete rs;
}
}
}
}
}
delete db;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
// Purpose: // Purpose:
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View File

@ -10,6 +10,7 @@ typeset AsyncHasSteamIDReservedSlotCallbackFunc
}; };
native void AsyncHasSteamIDReservedSlot(const char[] sSteam32ID, AsyncHasSteamIDReservedSlotCallbackFunc Callback, any Data = 0); native void AsyncHasSteamIDReservedSlot(const char[] sSteam32ID, AsyncHasSteamIDReservedSlotCallbackFunc Callback, any Data = 0);
native void GetClientForumName(int client, char[] sName, int len);
public SharedPlugin __pl_UNLOZE_ForumIntegration = public SharedPlugin __pl_UNLOZE_ForumIntegration =
{ {
@ -26,5 +27,6 @@ public SharedPlugin __pl_UNLOZE_ForumIntegration =
public __pl_UNLOZE_ForumIntegration_SetNTVOptional() public __pl_UNLOZE_ForumIntegration_SetNTVOptional()
{ {
MarkNativeAsOptional("AsyncHasSteamIDReservedSlot"); MarkNativeAsOptional("AsyncHasSteamIDReservedSlot");
MarkNativeAsOptional("GetClientForumName");
} }
#endif #endif