Th3-822

Too lazy for a PR

Jul 13th, 2019
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.31 KB | None | 0 0
  1. #include <amxmodx>
  2. #include <curl>
  3. #include <json>
  4. #include <hl_kreedz_util>
  5.  
  6. #define PLUGIN "HLKZ Discord WR Notifier"
  7. #define VERSION "1.0.0"
  8. #define AUTHOR "Th3-822"
  9.  
  10. new const g_iTopList = 5; // Max entries to show on webhook
  11. new const g_iTopListMinimum = 1; // Minimum entries on top to trigger webhook
  12. new const g_szWRecType[][] = {"Pure", "Pro"};
  13. new const g_szTLPrefix[][] = {"++-", "--+", "**-", "---"};
  14.  
  15. new CURL:g_cURLHandle, curl_slist:g_cURLHeaders;
  16. new bool:g_bIsWorking
  17. new g_cvar_webhook, g_cvar_bot_name, g_cvar_bot_avatar;
  18. new g_szMapName[64];
  19.  
  20. public plugin_init()
  21. {
  22.     register_plugin(PLUGIN, VERSION, AUTHOR);
  23.  
  24.     g_cvar_webhook = register_cvar("kz_discord_webhook", "");
  25.     g_cvar_bot_name = register_cvar("kz_discord_bot_name", "HL KreedZ");
  26.     g_cvar_bot_avatar = register_cvar("kz_discord_bot_avatar", "http://212.71.238.124/hlkz/hlkz.png");
  27.  
  28.     get_mapname(g_szMapName, charsmax(g_szMapName));
  29. }
  30.  
  31. public plugin_end()
  32. {
  33.     if (g_cURLHandle)
  34.     {
  35.         curl_easy_cleanup(g_cURLHandle);
  36.         g_cURLHandle = CURL:0;
  37.     }
  38.     if (g_cURLHeaders)
  39.     {
  40.         curl_slist_free_all(g_cURLHeaders);
  41.         g_cURLHeaders = SList_Empty;
  42.     }
  43. }
  44.  
  45. public hlkz_worldrecord(id, Float:flWRecTime, iWRecType, Array:arTop)
  46. {
  47.     static iTopSize;
  48.     if (g_bIsWorking || !(0 <= iWRecType <= 1) || (iTopSize = ArraySize(arTop)) < g_iTopListMinimum)
  49.     {
  50.         return;
  51.     }
  52.  
  53.     static szURL[128];
  54.     if (get_pcvar_string(g_cvar_webhook, szURL, charsmax(szURL)) < 123)
  55.     {
  56.         log_amx("Invalid Webhook URL?");
  57.         return;
  58.     }
  59.  
  60.     static szBuffer[128], sTopEntry[STATS], szMessage[2001], iMsgLen, szDate[11], iMinutes, Float:flSeconds;
  61.     new JSON:jWebhook = json_init_object();
  62.  
  63.     if (get_pcvar_string(g_cvar_bot_name, szBuffer, charsmax(szBuffer)))
  64.     {
  65.         json_object_set_string(jWebhook, "username", szBuffer);
  66.     }
  67.     if (get_pcvar_string(g_cvar_bot_avatar, szBuffer, charsmax(szBuffer)))
  68.     {
  69.         json_object_set_string(jWebhook, "avatar_url", szBuffer);
  70.     }
  71.  
  72.     for (new i, iTop = min(iTopSize, g_iTopList); i < iTop; i++)
  73.     {
  74.         ArrayGetArray(arTop, i, sTopEntry);
  75.         replace_string(sTopEntry[STATS_NAME], charsmax(sTopEntry[STATS_NAME]), "`", "'");
  76.         floatToMinSec(sTopEntry[STATS_TIME], iMinutes, flSeconds);
  77.  
  78.         if (!i)
  79.         {
  80.             copy(szBuffer, charsmax(szBuffer), sTopEntry[STATS_NAME]);
  81.             replace_string(szBuffer, charsmax(szBuffer), "\", "\\");
  82.             replace_string(szBuffer, charsmax(szBuffer), "~", "\~");
  83.             replace_string(szBuffer, charsmax(szBuffer), "*", "\*");
  84.             replace_string(szBuffer, charsmax(szBuffer), "@", "\@");
  85.  
  86.             iMsgLen = formatex(szMessage, charsmax(szMessage), "[HLKZ] **%s** has now the %s WR for **%s**! Finished in **%02d:%06.3f**^n^n**Top %d %s @ %s**^n```diff", szBuffer, g_szWRecType[iWRecType], g_szMapName, iMinutes, flSeconds, g_iTopList, g_szWRecType[iWRecType], g_szMapName);
  87.         }
  88.  
  89.         format_time(szDate, charsmax(szDate), "%d/%m/%Y", sTopEntry[STATS_TIMESTAMP]);
  90.  
  91.         iMsgLen += formatex(szMessage[iMsgLen], charsmax(szMessage) - iMsgLen, "^n%s %-2d %31s %02d:%06.3f @ %s", g_szTLPrefix[min(i, charsmax(g_szTLPrefix))], i + 1, sTopEntry[STATS_NAME], iMinutes, flSeconds, szDate);
  92.     }
  93.     iMsgLen += formatex(szMessage[iMsgLen], charsmax(szMessage) - iMsgLen, "```");
  94.     json_object_set_string(jWebhook, "content", szMessage);
  95.  
  96.     postJSON(szURL, jWebhook);
  97.     json_free(jWebhook);
  98. }
  99.  
  100. floatToMinSec(const Float:flTime, &iMinutes, &Float:flSeconds)
  101. {
  102.     iMinutes = floatround(flTime, floatround_floor) / 60;
  103.     flSeconds = flTime - (60 * iMinutes);
  104. }
  105.  
  106. postJSON(const szUrl[], JSON:jData)
  107. {
  108.     if (!g_cURLHandle)
  109.     {
  110.         if (!(g_cURLHandle = curl_easy_init()))
  111.         {
  112.             set_fail_state("Cannot init cURL's Handle.");
  113.         }
  114.         if (!g_cURLHeaders)
  115.         {
  116.             if (!(g_cURLHeaders = curl_slist_append(SList_Empty, "Content-Type: application/json")))
  117.             {
  118.                 set_fail_state("Cannot init cURL's Headers.");
  119.             }
  120.             curl_slist_append(g_cURLHeaders, "User-Agent: 822_AMXX_PLUGIN/1.1"); // User-Agent
  121.             curl_slist_append(g_cURLHeaders, "Connection: Keep-Alive"); // Keep-Alive
  122.         }
  123.  
  124.         // Static Options
  125.         curl_easy_setopt(g_cURLHandle, CURLOPT_SSL_VERIFYPEER, 0);
  126.         curl_easy_setopt(g_cURLHandle, CURLOPT_SSL_VERIFYHOST, 0);
  127.         curl_easy_setopt(g_cURLHandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  128.         curl_easy_setopt(g_cURLHandle, CURLOPT_FAILONERROR, 0);
  129.         curl_easy_setopt(g_cURLHandle, CURLOPT_FOLLOWLOCATION, 0);
  130.         curl_easy_setopt(g_cURLHandle, CURLOPT_FORBID_REUSE, 0);
  131.         curl_easy_setopt(g_cURLHandle, CURLOPT_FRESH_CONNECT, 0);
  132.         curl_easy_setopt(g_cURLHandle, CURLOPT_CONNECTTIMEOUT, 10);
  133.         curl_easy_setopt(g_cURLHandle, CURLOPT_TIMEOUT, 10);
  134.         curl_easy_setopt(g_cURLHandle, CURLOPT_HTTPHEADER, g_cURLHeaders);
  135.         curl_easy_setopt(g_cURLHandle, CURLOPT_POST, 1);
  136.     }
  137.  
  138.     static szPostData[4096];
  139.     json_serial_to_string(jData, szPostData, charsmax(szPostData));
  140.  
  141.     curl_easy_setopt(g_cURLHandle, CURLOPT_URL, szUrl);
  142.     curl_easy_setopt(g_cURLHandle, CURLOPT_COPYPOSTFIELDS, szPostData);
  143.  
  144.     g_bIsWorking = true;
  145.     curl_easy_perform(g_cURLHandle, "postJSON_done");
  146. }
  147.  
  148. public postJSON_done(CURL:curl, CURLcode:code)
  149. {
  150.     g_bIsWorking = false;
  151.     if (code == CURLE_OK)
  152.     {
  153.         static iStatusCode;
  154.         curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, iStatusCode);
  155.         if (iStatusCode >= 400)
  156.         {
  157.             log_amx("[Error] HTTP Error: %d", iStatusCode);
  158.         }
  159.     }
  160.     else
  161.     {
  162.         log_amx("[Error] cURL Error: %d", code);
  163.         curl_easy_cleanup(g_cURLHandle);
  164.         g_cURLHandle = CURL:0;
  165.     }
  166. }
Advertisement