From 609c46dfdb509ba04a7190e59e8db2d855705435 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 20 Feb 2021 20:04:09 +0100 Subject: [PATCH] added small delay to de-stress get requests spam for the VM or cloudflare if any suffer --- .../scripting/UNLOZE_ForumIntegration.sp | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp b/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp index de060b19..4e0124f2 100644 --- a/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp +++ b/UNLOZE_ForumIntegration/scripting/UNLOZE_ForumIntegration.sp @@ -118,23 +118,48 @@ public void OnClientAuthorized(int client, const char[] sSteamID32) { if (IsFakeClient(client)) return; + DataPack hDataPack = new DataPack(); + hDataPack.WriteCell(client); + hDataPack.WriteString(sSteamID32); + float duration = GetRandomFloat(1.0, 2.5); + CreateTimer(duration, Timer_do_https_request, hDataPack); +} - char sSteamID64[32]; - SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64)); - - int iSerial = GetClientSerial(client); - - char sRequest[256]; - FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", UNLOZE_APIKEY, sSteamID64); - - Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); - if (!hRequest || - !SteamWorks_SetHTTPCallbacks(hRequest, OnClientAuthorized_OnTransferComplete) || - !SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) || - !SteamWorks_SendHTTPRequest(hRequest)) +stock bool IsValidClient(int client) +{ + if (client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client)) { - delete hRequest; + return true; } + return false; +} + +public Action Timer_do_https_request(Handle timer, DataPack pack) +{ + pack.Reset(); + int client = pack.ReadCell(); + if (IsValidClient(client)) + { + char sSteamID32[32]; + pack.ReadString(sSteamID32, sizeof(sSteamID32)); + char sSteamID64[32]; + SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64)); + + int iSerial = GetClientSerial(client); + + char sRequest[256]; + FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", UNLOZE_APIKEY, sSteamID64); + + Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); + if (!hRequest || + !SteamWorks_SetHTTPCallbacks(hRequest, OnClientAuthorized_OnTransferComplete) || + !SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) || + !SteamWorks_SendHTTPRequest(hRequest)) + { + delete hRequest; + } + } + return Plugin_Handled; } //---------------------------------------------------------------------------------------------------- @@ -177,6 +202,7 @@ 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); G_bResponsePassed[client] = true; @@ -356,4 +382,4 @@ stock bool SteamID32toSteamID64(const char[] sSteamID32, char[] sSteamID64, int sSteamID64[9] = iIdx; return true; -} \ No newline at end of file +}