Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. // Player country lookup by Emmet
  2.  
  3. #include <a_http>
  4.  
  5. #if !defined MAX_COUNTRY_NAME
  6.     #define MAX_COUNTRY_NAME    (32)
  7. #endif
  8.  
  9. forward OnLookupComplete(playerid, country[], region[], city[]);
  10.  
  11. enum e_LookupData {
  12.     // Packed string containing the name of the country.
  13.     e_LookupCountry[MAX_COUNTRY_NAME char],
  14.    
  15.     // Packed string containing the name of the region.
  16.     e_LookupRegion[MAX_COUNTRY_NAME char],
  17.  
  18.     // Packed string containing the name of the city.
  19.     e_LookupCity[MAX_COUNTRY_NAME char]
  20. };
  21.  
  22. static
  23.     g_asLookupData[MAX_PLAYERS][e_LookupData]
  24. ;
  25.  
  26. public OnPlayerConnect(playerid)
  27. {
  28.     new
  29.         string[64]
  30.     ;
  31.        
  32.     strpack(g_asLookupData[playerid][e_LookupCountry], "Unknown", MAX_COUNTRY_NAME char);
  33.     strpack(g_asLookupData[playerid][e_LookupRegion], "Unknown", MAX_COUNTRY_NAME char);
  34.     strpack(g_asLookupData[playerid][e_LookupCity], "Unknown", MAX_COUNTRY_NAME char);
  35.  
  36.     GetPlayerIp(playerid, string, sizeof(string));
  37.  
  38.     if (!IsPlayerNPC(playerid) && strcmp(string, "127.0.0.1") != 0)
  39.     {
  40.         // Insert the domain at the beginning of the IP.
  41.         strins(string, "www.realip.info/api/p/geoip.php?ip=", 0);
  42.        
  43.         // Send the lookup request.
  44.         HTTP(playerid, HTTP_GET, string, "", "HTTP_OnLookupResponse");
  45.     }
  46.     #if defined CL_OnPlayerConnect
  47.         return CL_OnPlayerConnect(playerid);
  48.     #else
  49.         return 1;
  50.     #endif
  51. }
  52.  
  53. forward HTTP_OnLookupResponse(index, response, data[]);
  54. public HTTP_OnLookupResponse(index, response, data[])
  55. {
  56.     new
  57.         pos = -1;
  58.  
  59.     if (!IsPlayerConnected(index))
  60.     {
  61.         return 0;
  62.     }
  63.     else if (response == 200)
  64.     {
  65.         if (strfind(data, "Reserved", true) != -1 || strlen(data) < 15)
  66.         {
  67.             return 0;
  68.         }
  69.         else
  70.         {
  71.             new
  72.                 country[MAX_COUNTRY_NAME],
  73.                 region[MAX_COUNTRY_NAME],
  74.                 city[MAX_COUNTRY_NAME]
  75.             ;
  76.  
  77.             if ((pos = strfind(data, "\"country_name\":")) != -1)
  78.             {
  79.                 pos = pos + 16;
  80.  
  81.                 strmid(country, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  82.             }
  83.             if ((pos = strfind(data, "\"region_name\":")) != -1)
  84.             {
  85.                 pos = pos + 15;
  86.  
  87.                 strmid(region, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  88.  
  89.             }
  90.             if ((pos = strfind(data, "\"city_name\":")) != -1)
  91.             {
  92.                 pos = pos + 13;
  93.  
  94.                 strmid(city, data, pos, strfind(data, "\"", true, pos), MAX_COUNTRY_NAME);
  95.             }
  96.  
  97.             if (pos != -1)
  98.             {
  99.                 // Call our callback, which is called when a lookup has completed.
  100.                 CallLocalFunction("OnLookupComplete", "dsss", index, country, region, city);
  101.  
  102.                 // Pack the data for later use.
  103.                 strpack(g_asLookupData[index][e_LookupCountry], country, MAX_COUNTRY_NAME char);
  104.                 strpack(g_asLookupData[index][e_LookupRegion], region, MAX_COUNTRY_NAME char);
  105.                 strpack(g_asLookupData[index][e_LookupCity], city, MAX_COUNTRY_NAME char);
  106.             }
  107.         }
  108.     }
  109.     return 0;
  110. }
  111.  
  112. stock GetPlayerCountry(playerid, country[], size = sizeof(country))
  113. {
  114.     if (IsPlayerConnected(playerid))
  115.         return strunpack(country, g_asLookupData[playerid][e_LookupCountry], size);
  116.  
  117.     else
  118.         strunpack(country, !"Unknown", size);
  119.  
  120.     return 0;
  121. }
  122.  
  123. stock GetPlayerRegion(playerid, region[], size = sizeof(region))
  124. {
  125.     if (IsPlayerConnected(playerid))
  126.         return strunpack(region, g_asLookupData[playerid][e_LookupRegion], size);
  127.  
  128.     else
  129.         strunpack(region, !"Unknown", size);
  130.        
  131.     return 0;
  132. }
  133.  
  134. stock GetPlayerCity(playerid, city[], size = sizeof(city))
  135. {
  136.     if (IsPlayerConnected(playerid))
  137.         return strunpack(city, g_asLookupData[playerid][e_LookupCity], size);
  138.  
  139.     else
  140.         strunpack(city, !"Unknown", size);
  141.        
  142.     return 0;
  143. }
  144.  
  145. #if defined _ALS_OnPlayerConnect
  146.     #undef OnPlayerConnect
  147. #else
  148.     #define _ALS_OnPlayerConnect
  149. #endif
  150.  
  151. #define OnPlayerConnect CL_OnPlayerConnect
  152.  
  153. #if defined CL_OnPlayerConnect
  154.     forward CL_OnPlayerConnect(playerid);
  155. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement