From 72f2d3168e45e815aaddc7cd5b542538493f920e Mon Sep 17 00:00:00 2001 From: Rawrington Date: Sat, 21 Aug 2021 20:54:00 +0100 Subject: [PATCH] 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 --- .../scripting/UNLOZE_ForumIntegration.sp | 76 +++-- VIP_Reward/scripting/VIP_Reward.sp | 269 +++--------------- includes/unloze.inc | 2 + 3 files changed, 96 insertions(+), 251 deletions(-) diff --git a/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp b/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp index 2910bc7c..2895c36a 100644 --- a/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp +++ b/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp @@ -14,6 +14,7 @@ /* STRINGS */ char G_sGroup[MAXPLAYERS+1][64]; +char G_sName[MAXPLAYERS+1][32]; /* BOOLS */ bool G_bPreAdminChecked[MAXPLAYERS+1]; @@ -25,10 +26,10 @@ bool G_bResponsePassed[MAXPLAYERS+1]; //---------------------------------------------------------------------------------------------------- public Plugin myinfo = { - name = "UNLOZE Forum Integration", - author = ".George & zaCade (Original by Botox)", + name = "UNLOZE Forum Integration", + author = ".George & zaCade (Original by Botox)", 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) { CreateNative("AsyncHasSteamIDReservedSlot", Native_AsyncHasSteamIDReservedSlot); + CreateNative("GetClientForumName", Native_GetClientForumName); RegPluginLibrary("UNLOZE_ForumIntegration"); @@ -109,6 +111,7 @@ public void OnClientDisconnect(int client) G_bResponsePassed[client] = false; 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)) return; - char sSteamID64[32]; - SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64)); + char sSteamID64[32]; + SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64)); - char sRequest[256]; - FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", UNLOZE_APIKEY, sSteamID64); + char sRequest[256]; + FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", UNLOZE_APIKEY, sSteamID64); - int iSerial = GetClientSerial(client); - Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); - if (!hRequest || - !SteamWorks_SetHTTPCallbacks(hRequest, OnClientAuthorized_OnTransferComplete) || - !SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) || - !SteamWorks_SendHTTPRequest(hRequest)) - { - delete hRequest; - } + int iSerial = GetClientSerial(client); + Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); + if (!hRequest || + !SteamWorks_SetHTTPCallbacks(hRequest, OnClientAuthorized_OnTransferComplete) || + !SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) || + !SteamWorks_SendHTTPRequest(hRequest)) + { + delete hRequest; + } } //---------------------------------------------------------------------------------------------------- @@ -147,7 +150,7 @@ public int OnClientAuthorized_OnTransferComplete(Handle hRequest, bool bFailure, delete hRequest; return; } - + if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK) { G_bResponseFailed[client] = true; @@ -158,7 +161,7 @@ public int OnClientAuthorized_OnTransferComplete(Handle hRequest, bool bFailure, delete hRequest; return; } - + 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) { + char splitData[2][32]; + int client = GetClientFromSerial(iSerial); if (!client) //Player disconnected. @@ -175,13 +180,23 @@ public int OnClientAuthorized_OnTransferResponse(char[] sData, int iSerial) TrimString(sData); StripQuotes(sData); - LogMessage("reached sData with status 200: %s", sData); - strcopy(G_sGroup[client], sizeof(G_sGroup[]), sData); + LogMessage("reached sData with status 200: %s", 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]) - NotifyPostAdminCheck(client); + if(!StrEqual(splitData[0], "NOGROUP")) + { + 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: //---------------------------------------------------------------------------------------------------- @@ -310,11 +334,15 @@ public int Native_AsyncHasSteamIDReservedSlot_OnTransferResponse(char[] sData, D any data; data = hDataPack.ReadCell(); + static char splitData[32]; + TrimString(sData); StripQuotes(sData); + + SplitString(sData, "\r\n", splitData, sizeof(splitData)); int result; - if (StrEqual(sData, "Game-Donator", false)) + if (StrEqual(splitData, "Game-Donator", false)) result = 1; else result = 0; diff --git a/VIP_Reward/scripting/VIP_Reward.sp b/VIP_Reward/scripting/VIP_Reward.sp index a62ad739..342bd655 100644 --- a/VIP_Reward/scripting/VIP_Reward.sp +++ b/VIP_Reward/scripting/VIP_Reward.sp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -12,7 +12,7 @@ char g_directory_path[128]; public Plugin myinfo = { name = "Unloze_VIP_Reward", - author = "Neon + WASD", + author = "Neon + WASD + ??????????", description = "", version = "1.1", url = "https://steamcommunity.com/id/n3ontm" @@ -39,8 +39,19 @@ public void OnPluginStart() //---------------------------------------------------------------------------------------------------- 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; } @@ -65,240 +76,44 @@ public Action Event_Round_End(Handle event, const char[] name, bool dontBroadcas Handle file_handle = OpenFile(file_path, "a", false); 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"); CloseHandle(file_handle); } } -//---------------------------------------------------------------------------------------------------- -// Purpose: Saves all winners into a textfile located in sourcemod/data/events/. -//---------------------------------------------------------------------------------------------------- -CheckMYSQL_2(Handle file_handle) +GetForumNameFormat(int client, char[] sName, int sNameLen) { - char error[255]; - 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)) - { - 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; + char sForumName[64]; + GetClientForumName(client, sForumName, sizeof(sForumName)); + + if(strlen(sForumName) > 0) + Format(sName, sNameLen, "FORUM NAME: %s", sForumName); + else + Format(sName, sNameLen, "NO FORUM ACCOUNT"); } -//---------------------------------------------------------------------------------------------------- -// Purpose: Prints all winners to the caller of sm_listwinners. -//---------------------------------------------------------------------------------------------------- -CheckMYSQL(int iCaller) +stock bool IsValidWinner(int client) { - if (iCaller == 0) - return; - - char error[255]; - 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; + if (IsValidClient(client, true) && IsPlayerAlive(client) && GetClientTeam(client) == CS_TEAM_CT && !IsClientSourceTV(client)) + return true; + + return false; } + //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- diff --git a/includes/unloze.inc b/includes/unloze.inc index c5ab0178..e518e9a2 100644 --- a/includes/unloze.inc +++ b/includes/unloze.inc @@ -10,6 +10,7 @@ typeset AsyncHasSteamIDReservedSlotCallbackFunc }; 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 = { @@ -26,5 +27,6 @@ public SharedPlugin __pl_UNLOZE_ForumIntegration = public __pl_UNLOZE_ForumIntegration_SetNTVOptional() { MarkNativeAsOptional("AsyncHasSteamIDReservedSlot"); + MarkNativeAsOptional("GetClientForumName"); } #endif