Advertisement
Whitetigerswt

Untitled

Nov 10th, 2011
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 10.89 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3.  
  4.  
  5. #if defined _geoip_included
  6.   #endinput
  7. #endif
  8.  
  9.  
  10.  
  11. #define _geoip_included
  12.  
  13.  
  14. AntiDeAMX()
  15. {
  16.     new a[][] =
  17.     {
  18.         "Unarmed (Fist)",
  19.         "Brass K"
  20.     };
  21.     #pragma unused a
  22. }
  23.  
  24.  
  25. enum info {
  26.     ISP[170],
  27.     City[170],
  28.     Country[170]
  29. };
  30.  
  31. new handler;
  32.  
  33. new CountryThread[MAX_PLAYERS];
  34. new CityThread[MAX_PLAYERS];
  35. new CityThread2[MAX_PLAYERS];
  36. new ISPThread[MAX_PLAYERS];
  37.  
  38. new GeoIP[MAX_PLAYERS][info];
  39.  
  40. #define host "localhost"
  41. #define user "root"
  42. #define db "geo_ip"
  43. #define pw ""
  44.  
  45.  
  46. public OnFilterScriptInit() {
  47.     AntiDeAMX();
  48.     handler = mysql_connect(host, user, db, pw);
  49.     mysql_debug(1);
  50.    
  51.     for(new i=0; i < MAX_PLAYERS; ++i) {
  52.         if(IsPlayerConnected(i)) {
  53.             GeoIP_OnPlayerConnect(i);
  54.         }
  55.     }
  56.  
  57. }
  58. new ticks;
  59. forward GeoIP_OnPlayerConnect(playerid);
  60. public GeoIP_OnPlayerConnect(playerid) {
  61.  
  62.     strdel(GeoIP[playerid][Country], 0, strlen(GeoIP[playerid][Country]));
  63.     strdel(GeoIP[playerid][City], 0, strlen(GeoIP[playerid][City]));
  64.     strdel(GeoIP[playerid][ISP], 0, strlen(GeoIP[playerid][ISP]));
  65.    
  66.     ticks = tickcount();
  67.     GetPlayerCountry(playerid);
  68.     GetPlayerISP(playerid);
  69.     GetPlayerCity(playerid);
  70.  
  71.     return 1;
  72. }
  73.  
  74. stock GetPlayerCountry(playerid) {
  75.     if(strlen(GeoIP[playerid][Country]) <= 0) {
  76.         new string[170];
  77.         new ip[24];
  78.         GetPlayerIp(playerid, ip, sizeof(ip));
  79.         format(string, sizeof(string), "SELECT cn FROM ip_country WHERE ip_to >= INET_ATON(\"%s\") ORDER BY ip_to ASC LIMIT 1", ip);
  80.         MySQLFix();
  81.         mysql_query(string, playerid+(MAX_PLAYERS * 2), playerid, handler);
  82.         CountryThread[playerid] = playerid+(MAX_PLAYERS * 2);
  83.        
  84.         return string;
  85.     }
  86.     else
  87.     {
  88.         new str[170];
  89.         strcat(str, GeoIP[playerid][Country]);
  90.         return str;
  91.     }
  92. }
  93.  
  94. stock GetPlayerISP(playerid) {
  95.     if(strlen(GeoIP[playerid][ISP]) <= 0) {
  96.         new string[170];
  97.         new ip[24];
  98.         GetPlayerIp(playerid, ip, sizeof(ip));
  99.        
  100.         format(string, sizeof(string), "SELECT internet_service_provider FROM geo_isp WHERE ip_to >= INET_ATON(\"%s\") ORDER BY ip_to ASC LIMIT 1", ip);
  101.  
  102.        
  103.         //MySQLFix();
  104.         // for some reason checking the mysql connection in this function crashes it.
  105.         mysql_query(string, playerid+(MAX_PLAYERS * 3), playerid, handler);
  106.         ISPThread[playerid] = playerid+(MAX_PLAYERS * 3);
  107.        
  108.         return string;
  109.     }
  110.     else
  111.     {
  112.         new str[170], placeholder[1];
  113.         sscanf_(GeoIP[playerid][ISP], "ss", placeholder, str);
  114.         return str;
  115.     }
  116. }
  117.  
  118. stock GetPlayerCity(playerid) {
  119.     if(strlen(GeoIP[playerid][City]) <= 0) {
  120.         new string[170];
  121.         new ip[24];
  122.         GetPlayerIp(playerid, ip, sizeof(ip));
  123.         format(string, sizeof(string), "SELECT locid FROM ip_city WHERE ip_to >= INET_ATON(\"%s\") ORDER BY ip_to ASC LIMIT 1", ip);
  124.         MySQLFix();
  125.         mysql_query(string, playerid+(MAX_PLAYERS * 4), playerid, handler);
  126.         CityThread[playerid] = playerid+(MAX_PLAYERS * 4);
  127.  
  128.         return string;
  129.     }
  130.     else
  131.     {
  132.         new str[170];
  133.         strcat(str, GeoIP[playerid][City]);
  134.         return str;
  135.     }
  136. }
  137. /*
  138. stock GetPlayerCountry2(ip[]) {
  139.     new playerid = 0;
  140.     if(strlen(GeoIP[playerid][Country]) <= 0) {
  141.         new string[170];
  142.         format(string, sizeof(string), "SELECT cn FROM ip_country WHERE ip_to >= INET_ATON(\"%s\") ORDER BY ip_to ASC LIMIT 1", ip);
  143.         MySQLFix();
  144.         mysql_query(string, playerid+(MAX_PLAYERS * 2), playerid, handler);
  145.         CountryThread[playerid] = playerid+(MAX_PLAYERS * 2);
  146.  
  147.         return string;
  148.     }
  149.     else
  150.     {
  151.         new str[170];
  152.         strcat(str, GeoIP[playerid][Country]);
  153.         return str;
  154.     }
  155. }
  156.  
  157. stock GetPlayerISP2(ip[]) {
  158.     new playerid = 0;
  159.     if(strlen(GeoIP[playerid][ISP]) <= 0) {
  160.         new string[170];
  161.        
  162.         format(string, sizeof(string), "SELECT internet_service_provider FROM geo_isp WHERE ip_to >= INET_ATON(\"%s\") ORDER BY ip_to ASC LIMIT 1", ip);
  163.  
  164.         //MySQLFix();
  165.         // for some reason checking the mysql connection in this function crashes it.
  166.         mysql_query(string, playerid+(MAX_PLAYERS * 3), playerid, handler);
  167.         ISPThread[playerid] = playerid+(MAX_PLAYERS * 3);
  168.  
  169.         return string;
  170.     }
  171.     else
  172.     {
  173.         new str[170], placeholder[1];
  174.         sscanf_(GeoIP[playerid][ISP], "ss", placeholder, str);
  175.         return str;
  176.     }
  177. }
  178.  
  179. stock GetPlayerCity2(ip[]) {
  180.     new playerid = 0;
  181.     if(strlen(GeoIP[playerid][City]) <= 0) {
  182.         new string[170];
  183.         format(string, sizeof(string), "SELECT locid FROM ip_city WHERE ip_to >= INET_ATON(\"%s\") ORDER BY ip_to ASC LIMIT 1", ip);
  184.         MySQLFix();
  185.         mysql_query(string, playerid+(MAX_PLAYERS * 4), playerid, handler);
  186.         CityThread[playerid] = playerid+(MAX_PLAYERS * 4);
  187.  
  188.         return string;
  189.     }
  190.     else
  191.     {
  192.         new str[170];
  193.         strcat(str, GeoIP[playerid][City]);
  194.         return str;
  195.     }
  196. }*/
  197.  
  198. public OnQueryFinish(query[], resultid, extraid, connectionHandle) {
  199.     if(connectionHandle != handler) return 1;
  200.     for(new i=0; i < MAX_PLAYERS; ++i) /*if(IsPlayerConnected(i))*/ {
  201.         if(resultid == CountryThread[i]) {
  202.             mysql_store_result(handler);
  203.             new templine[170];
  204.             mysql_fetch_row_format(templine, "|", handler);
  205.             mysql_free_result(handler);
  206.             GeoIP[i][Country] = templine;
  207.             break;
  208.         }
  209.         else if(resultid == CityThread[i]) {
  210.        
  211.             new templine[170];
  212.             mysql_store_result(handler);
  213.             mysql_fetch_row_format(templine, "|", handler);
  214.             mysql_free_result(handler);
  215.  
  216.             GeoIP[i][City] = templine;
  217.             new string[150];
  218.            
  219.             format(string, sizeof(string), "SELECT city FROM ip_city_locations WHERE locid = %d", strval(templine));
  220.             MySQLFix();
  221.             mysql_query(string, CityThread[i] * 2, extraid, handler);
  222.             CityThread2[i] = CityThread[i] * 2;
  223.             break;
  224.            
  225.         }
  226.         else if(resultid == ISPThread[i]) {
  227.             mysql_store_result(handler);
  228.             new templine[170];
  229.             mysql_fetch_row_format(templine, "|", handler);
  230.             mysql_free_result(handler);
  231.             GeoIP[i][ISP] = templine;
  232.             break;
  233.         }
  234.         else if(resultid == CityThread2[i]) {
  235.             new templine[170];
  236.             mysql_store_result(handler);
  237.             mysql_fetch_row_format(templine, "|", handler);
  238.             mysql_free_result(handler);
  239.  
  240.             GeoIP[i][City] = templine;
  241.             CallLocalFunction("OnGeoIPCityFinish", "d", extraid);
  242.            
  243.             printf("MySQL took %d ms", tickcount()-ticks);
  244.             break;
  245.         }
  246.     }
  247.     return 1;
  248. }
  249.  
  250.  
  251. stock sscanf_(string[], format[], {Float,_}:...)
  252. {
  253.     #if defined isnull
  254.         if (isnull(string))
  255.     #else
  256.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  257.     #endif
  258.         {
  259.             return format[0];
  260.         }
  261.     #pragma tabsize 4
  262.     new
  263.         formatPos = 0,
  264.         stringPos = 0,
  265.         paramPos = 2,
  266.         paramCount = numargs(),
  267.         delim = ' ';
  268.     while (string[stringPos] && string[stringPos] <= ' ')
  269.     {
  270.         stringPos++;
  271.     }
  272.     while (paramPos < paramCount && string[stringPos])
  273.     {
  274.         switch (format[formatPos++])
  275.         {
  276.             case '\0':
  277.             {
  278.                 return 0;
  279.             }
  280.             case 'i', 'd':
  281.             {
  282.                 new
  283.                     neg = 1,
  284.                     num = 0,
  285.                     ch = string[stringPos];
  286.                 if (ch == '-')
  287.                 {
  288.                     neg = -1;
  289.                     ch = string[++stringPos];
  290.                 }
  291.                 do
  292.                 {
  293.                     stringPos++;
  294.                     if ('0' <= ch <= '9')
  295.                     {
  296.                         num = (num * 10) + (ch - '0');
  297.                     }
  298.                     else
  299.                     {
  300.                         return -1;
  301.                     }
  302.                 }
  303.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  304.                 setarg(paramPos, 0, num * neg);
  305.             }
  306.             case 'h', 'x':
  307.             {
  308.                 new
  309.                     num = 0,
  310.                     ch = string[stringPos];
  311.                 do
  312.                 {
  313.                     stringPos++;
  314.                     switch (ch)
  315.                     {
  316.                         case 'x', 'X':
  317.                         {
  318.                             num = 0;
  319.                             continue;
  320.                         }
  321.                         case '0' .. '9':
  322.                         {
  323.                             num = (num << 4) | (ch - '0');
  324.                         }
  325.                         case 'a' .. 'f':
  326.                         {
  327.                             num = (num << 4) | (ch - ('a' - 10));
  328.                         }
  329.                         case 'A' .. 'F':
  330.                         {
  331.                             num = (num << 4) | (ch - ('A' - 10));
  332.                         }
  333.                         default:
  334.                         {
  335.                             return -1;
  336.                         }
  337.                     }
  338.                 }
  339.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  340.                 setarg(paramPos, 0, num);
  341.             }
  342.             case 'c':
  343.             {
  344.                 setarg(paramPos, 0, string[stringPos++]);
  345.             }
  346.             case 'f':
  347.             {
  348.  
  349.                 new changestr[16], changepos = 0, strpos = stringPos;
  350.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  351.                 {
  352.                     changestr[changepos++] = string[strpos++];
  353.                     }
  354.                 changestr[changepos] = '\0';
  355.                 setarg(paramPos,0,_:floatstr(changestr));
  356.             }
  357.             case 'p':
  358.             {
  359.                 delim = format[formatPos++];
  360.                 continue;
  361.             }
  362.             case '\'':
  363.             {
  364.                 new
  365.                     end = formatPos - 1,
  366.                     ch;
  367.                 while ((ch = format[++end]) && ch != '\'') {}
  368.                 if (!ch)
  369.                 {
  370.                     return -1;
  371.                 }
  372.                 format[end] = '\0';
  373.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  374.                 {
  375.                     if (format[end + 1])
  376.                     {
  377.                         return -1;
  378.                     }
  379.                     return 0;
  380.                 }
  381.                 format[end] = '\'';
  382.                 stringPos = ch + (end - formatPos);
  383.                 formatPos = end + 1;
  384.             }
  385.             case 'u':
  386.             {
  387.                 new
  388.                     end = stringPos - 1,
  389.                     id = 0,
  390.                     bool:num = true,
  391.                     ch;
  392.                 while ((ch = string[++end]) && ch != delim)
  393.                 {
  394.                     if (num)
  395.                     {
  396.                         if ('0' <= ch <= '9')
  397.                         {
  398.                             id = (id * 10) + (ch - '0');
  399.                         }
  400.                         else
  401.                         {
  402.                             num = false;
  403.                         }
  404.                     }
  405.                 }
  406.                 if (num && IsPlayerConnected(id))
  407.                 {
  408.                     setarg(paramPos, 0, id);
  409.                 }
  410.                 else
  411.                 {
  412.                     #if !defined foreach
  413.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  414.                         #define __SSCANF_FOREACH__
  415.                     #endif
  416.                     string[end] = '\0';
  417.                     num = false;
  418.                     new
  419.                         name[MAX_PLAYER_NAME];
  420.                     id = end - stringPos;
  421.                     foreach (Player, playerid)
  422.                     {
  423.                         GetPlayerName(playerid, name, sizeof (name));
  424.                         if (!strcmp(name, string[stringPos], true, id))
  425.                         {
  426.                             setarg(paramPos, 0, playerid);
  427.                             num = true;
  428.                             break;
  429.                         }
  430.                     }
  431.                     if (!num)
  432.                     {
  433.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  434.                     }
  435.                     string[end] = ch;
  436.                     #if defined __SSCANF_FOREACH__
  437.                         #undef foreach
  438.                         #undef __SSCANF_FOREACH__
  439.                     #endif
  440.                 }
  441.                 stringPos = end;
  442.             }
  443.             case 's', 'z':
  444.             {
  445.                 new
  446.                     i = 0,
  447.                     ch;
  448.                 if (format[formatPos])
  449.                 {
  450.                     while ((ch = string[stringPos++]) && ch != delim)
  451.                     {
  452.                         setarg(paramPos, i++, ch);
  453.                     }
  454.                     if (!i)
  455.                     {
  456.                         return -1;
  457.                     }
  458.                 }
  459.                 else
  460.                 {
  461.                     while ((ch = string[stringPos++]))
  462.                     {
  463.                         setarg(paramPos, i++, ch);
  464.                     }
  465.                 }
  466.                 stringPos--;
  467.                 setarg(paramPos, i, '\0');
  468.             }
  469.             default:
  470.             {
  471.                 continue;
  472.             }
  473.         }
  474.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  475.         {
  476.             stringPos++;
  477.         }
  478.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  479.         {
  480.             stringPos++;
  481.         }
  482.         paramPos++;
  483.     }
  484.     do
  485.     {
  486.         if ((delim = format[formatPos++]) > ' ')
  487.         {
  488.             if (delim == '\'')
  489.             {
  490.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  491.             }
  492.             else if (delim != 'z')
  493.             {
  494.                 return delim;
  495.             }
  496.         }
  497.     }
  498.     while (delim > ' ');
  499.     return 0;
  500. }
  501.  
  502. MySQLFix()
  503. {
  504.     if(mysql_ping(handler) == -1)
  505.     {
  506.         handler = mysql_connect(host, user, db, pw);
  507.     }
  508.     return 1;
  509. }
  510.  
  511.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement