Advertisement
DragonZafiro666

Geo IP - Get Players Country

Apr 17th, 2016
1,777
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*==============================================================================
  2. ** SA-MP (0.3.7)  GeoIP v1.0
  3. ** By: DragonZafiro/NeloAngelo
  4. ** Created: 17/04/16
  5. ==============================================================================*/
  6. #include <a_samp>
  7. #include <a_http>
  8.  
  9. #define APIKEY "api"
  10. //#define ALWAYS_RESPONSE
  11.  
  12. public OnPlayerConnect(playerid)
  13. {
  14.     new IP[30], str[300], pName[MAX_PLAYER_NAME];
  15.     GetPlayerName(playerid, pName, sizeof(pName));
  16.     GetPlayerIp(playerid, IP, sizeof(IP));
  17.     if(!strcmp(IP, "127.0.0.1"))
  18.     {
  19.         format(str, 230, "%s {FAFAFA}Has joined the server. {1FC7FF}Country: {FAFAFA}localhost", pName);
  20.         SendClientMessageToAll(GetPlayerColor(playerid), str);
  21.     }
  22.     else
  23.     {
  24.         format(str, sizeof(str),"api.ipinfodb.com/v3/ip-country/?key="APIKEY"&ip=%s", IP);
  25.         HTTP(playerid, HTTP_GET, str, "", "GetPlayerCountry");
  26.     }
  27.     return 1;
  28. }
  29. forward GetPlayerCountry(index, response_code, data[]);
  30. public GetPlayerCountry(index, response_code, data[])
  31. {
  32.     new buffer[358];
  33.     if(response_code == 200)
  34.     {
  35.         new str[230], city[3], country[20], pName[MAX_PLAYER_NAME];
  36.         GetPlayerName(index, pName, sizeof(pName));
  37.         format(buffer, sizeof(buffer), "%s", data);
  38.         strmid(str, buffer, 4, strlen(buffer)); // Cutting the 'OK' response...
  39.         strmid(city, str, strfind(str, ";", true) + 1, strfind(str, ";", true) + 3); // Getting City
  40.         strmid(country, str, strfind(str, ";", true) + 4, strlen(buffer)); // Getting Country
  41.         //printf("city: %s, country: %s", city, country);
  42.         format(str, 230, "%s {FAFAFA}Has joined the server. {1FC7FF}Country: {FAFAFA}%s %s", pName, country, city);
  43.         SendClientMessageToAll(GetPlayerColor(index), str);
  44.     }
  45.     else
  46.     {
  47.         new str[300], pName[MAX_PLAYER_NAME];
  48.         GetPlayerName(index, pName, sizeof(pName));
  49.         #if defined ALWAYS_RESPONSE
  50.         new IP[30];
  51.         GetPlayerIp(index, IP, sizeof(IP));
  52.         format(str, sizeof(str),"api.ipinfodb.com/v3/ip-country/?key="APIKEY"&ip=%s", IP);
  53.         HTTP(index, HTTP_GET, str, "", "GetPlayerCountry");
  54.         #else // Normal message, without country
  55.         format(str, 120, "%s {FAFAFA}Has joined the server.", pName);
  56.         SendClientMessageToAll(GetPlayerColor(index), str);
  57.         #endif
  58.     }
  59. }
Advertisement
RAW Paste Data Copied
Advertisement