Guest User

Googulator v2 - James Carson

a guest
Dec 29th, 2010
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.34 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_http>
  3. #include <zcmd>
  4.  
  5. #define     QUERY_CALC      0
  6. #define     QUERY_CONV      1
  7.  
  8. new
  9.     QueryType[ MAX_PLAYERS ]
  10. ;
  11.  
  12. public OnFilterScriptInit( )
  13. {
  14.     print("-----------------------------------");
  15.     print( "Googulator by James has loaded!" );
  16.     print("-----------------------------------");
  17.    
  18.     return 1;
  19. }
  20.  
  21. public OnPlayerConnect( playerid )
  22. {
  23.     QueryType[ playerid ] = -1;
  24.     return 1;
  25. }
  26.  
  27. CMD:calc( playerid, params[ ] )
  28. {
  29.     if( !isnull( params ) )
  30.     {
  31.         new
  32.             URL[ 128 ],
  33.             ErrorLevel
  34.         ;
  35.        
  36.         for( new i; i < strlen( params ); i++ )
  37.         {
  38.             switch( params[ i ] )
  39.             {
  40.                 case 0 .. 9: continue;
  41.                
  42.                 case ' ': {
  43.                     strdel( params, i, i + 1 );
  44.                     i--;
  45.                 }
  46.                
  47.                 case 'a' .. 'z': {
  48.                
  49.                     SendClientMessage( playerid, 0xFFFFFFFF, "Error: Equation does not support variables yet." );
  50.                     ErrorLevel = 1;
  51.                     break;
  52.                    
  53.                 }
  54.                
  55.                 case 'A' .. 'Z': {
  56.                
  57.                     SendClientMessage( playerid, 0xFFFFFFFF, "Error: Equation does not support variables yet." );
  58.                     ErrorLevel = 1;
  59.                     break;
  60.                
  61.                 }
  62.             }
  63.         }
  64.        
  65.         if( ErrorLevel == 0 )
  66.         {
  67.             format( URL, sizeof( URL ), "www.google.com/ig/calculator?q=%s", str_replace( "+", "\%2B", params ) );
  68.            
  69.             QueryType[ playerid ] = QUERY_CALC;
  70.             HTTP( playerid, HTTP_GET, URL, "", "QueryResponse");
  71.         }
  72.     }
  73.     else
  74.     {
  75.         SendClientMessage( playerid, 0xFFFFFFFF, "Syntax: /calc [Equation]" );
  76.     }
  77.  
  78.     return 1;
  79. }
  80.  
  81. CMD:convert( playerid, params[ ] )
  82. {
  83.     new
  84.         URL[ 128 ],
  85.         From[ 32 ],
  86.         To[ 32 ],
  87.         Amount[ 32 ],
  88.         ErrorLevel
  89.     ;
  90.    
  91.     if( !sscanf( params, "sss", Amount, From, To ) )
  92.     {
  93.         for( new i; i < strlen( Amount ); i++ )
  94.         {
  95.             switch( Amount[ i ] )
  96.             {
  97.                 case '0' .. '9': continue;
  98.                
  99.                 case '.': continue;
  100.                
  101.                 default: {
  102.                    
  103.                     SendClientMessage( playerid, 0xFFFFFFFF, "Error: Amount is not a valid number." );
  104.                     ErrorLevel = 1;
  105.                     break;
  106.                 }
  107.             }
  108.         }
  109.        
  110.         if( ErrorLevel == 0 )
  111.         {
  112.             format( URL, sizeof( URL ), "www.google.com/ig/calculator?q=%s+%s+in+%s", Amount, From, To );
  113.            
  114.             QueryType[ playerid ] = QUERY_CONV;
  115.             HTTP( playerid, HTTP_GET, URL, "", "QueryResponse");
  116.         }
  117.     }
  118.     else
  119.     {
  120.         SendClientMessage( playerid, 0xFFFFFFFF, "Syntax: /convert [Amount] [From] [To]" );
  121.     }
  122.    
  123.     return 1;
  124. }
  125.  
  126. forward QueryResponse( playerid, response_code, rawData[ ] );
  127. public QueryResponse( playerid, response_code, rawData[ ] )
  128. {
  129.     if( response_code == 200 )
  130.     {  
  131.         new
  132.             Data[ 6 ][ 64 ],
  133.             Output[ 128 ]
  134.         ;
  135.        
  136.         explode( Data, rawData, "\"" );
  137.        
  138.         if( isnull( Data[ 5 ] ) )
  139.         {
  140.             switch( QueryType[ playerid ] )
  141.             {
  142.                 case QUERY_CALC: {
  143.                    
  144.                     format( Output, sizeof( Output ), "Googulator: %s = %s", Data[ 1 ], Data[ 3 ] );
  145.                    
  146.                 }
  147.                
  148.                 case QUERY_CONV: {
  149.                
  150.                     format( Output, sizeof( Output ), "Googulator: %s = %s", Data[ 1 ], Data[ 3 ] );
  151.                    
  152.                 }
  153.             }
  154.            
  155.             SendClientMessage( playerid, 0xFFFFFFFF, Output );
  156.         }
  157.         else
  158.         {
  159.             if( strval( Data[ 5 ] ) == 4 )
  160.             {
  161.                 format( Output, sizeof( Output ), "Googulator: An error occurred, Specifications: Unknown unit." );
  162.             }
  163.             else
  164.             {
  165.                 format( Output, sizeof( Output ), "Googulator: An error occurred, Specifications: %s", Data[ 5 ] );
  166.             }
  167.             SendClientMessage( playerid, 0xFFFFFFFF, Output );
  168.         }
  169.        
  170.         QueryType[ playerid ] = -1;
  171.        
  172.     }
  173.     else
  174.     {
  175.         SendClientMessage( playerid, 0xFFFFFFFF, "Error: Unable to query google, please contact an admin." );
  176.         print( "ERR" );
  177.     }
  178.  
  179.     return 1;
  180. }
  181.  
  182. stock explode(aExplode[][], const sSource[], const sDelimiter[] = " ", iVertices = sizeof aExplode, iLength = sizeof aExplode[])
  183. {
  184.     new
  185.         iNode,
  186.         iPointer,
  187.         iPrevious = -1,
  188.         iDelimiter = strlen(sDelimiter);
  189.  
  190.     while(iNode < iVertices)
  191.     {
  192.         iPointer = strfind(sSource, sDelimiter, false, iPointer);
  193.  
  194.         if(iPointer == -1)
  195.         {
  196.             strmid(aExplode[iNode], sSource, iPrevious, strlen(sSource), iLength);
  197.             break;
  198.         }
  199.         else
  200.         {
  201.             strmid(aExplode[iNode], sSource, iPrevious, iPointer, iLength);
  202.         }
  203.  
  204.         iPrevious = (iPointer += iDelimiter);
  205.         ++iNode;
  206.     }
  207.  
  208.     return iPrevious;
  209. }
  210.  
  211. stock str_replace(sSearch[], sReplace[], const sSubject[], &iCount = 0)
  212. {
  213.     new
  214.         iLengthTarget = strlen(sSearch),
  215.         iLengthReplace = strlen(sReplace),
  216.         iLengthSource = strlen(sSubject),
  217.         iItterations = (iLengthSource - iLengthTarget) + 1;
  218.  
  219.     new
  220.         sTemp[128],
  221.         sReturn[256];
  222.  
  223.     strcat(sReturn, sSubject, 256);
  224.     iCount = 0;
  225.  
  226.     for(new iIndex; iIndex < iItterations; ++iIndex)
  227.     {
  228.         strmid(sTemp, sReturn, iIndex, (iIndex + iLengthTarget), (iLengthTarget + 1));
  229.  
  230.         if(!strcmp(sTemp, sSearch, false))
  231.         {
  232.             strdel(sReturn, iIndex, (iIndex + iLengthTarget));
  233.             strins(sReturn, sReplace, iIndex, iLengthReplace);
  234.  
  235.             iIndex += iLengthTarget;
  236.             iCount++;
  237.         }
  238.     }
  239.  
  240.     return sReturn;
  241. }
  242.  
  243. stock sscanf(string[], format[], {Float,_}:...)
  244. {
  245.     #if defined isnull
  246.         if (isnull(string))
  247.     #else
  248.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  249.     #endif
  250.         {
  251.             return format[0];
  252.         }
  253.     #pragma tabsize 4
  254.     new
  255.         formatPos = 0,
  256.         stringPos = 0,
  257.         paramPos = 2,
  258.         paramCount = numargs(),
  259.         delim = ' ';
  260.     while (string[stringPos] && string[stringPos] <= ' ')
  261.     {
  262.         stringPos++;
  263.     }
  264.     while (paramPos < paramCount && string[stringPos])
  265.     {
  266.         switch (format[formatPos++])
  267.         {
  268.             case '\0':
  269.             {
  270.                 return 0;
  271.             }
  272.             case 'i', 'd':
  273.             {
  274.                 new
  275.                     neg = 1,
  276.                     num = 0,
  277.                     ch = string[stringPos];
  278.                 if (ch == '-')
  279.                 {
  280.                     neg = -1;
  281.                     ch = string[++stringPos];
  282.                 }
  283.                 do
  284.                 {
  285.                     stringPos++;
  286.                     if ('0' <= ch <= '9')
  287.                     {
  288.                         num = (num * 10) + (ch - '0');
  289.                     }
  290.                     else
  291.                     {
  292.                         return -1;
  293.                     }
  294.                 }
  295.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  296.                 setarg(paramPos, 0, num * neg);
  297.             }
  298.             case 'h', 'x':
  299.             {
  300.                 new
  301.                     num = 0,
  302.                     ch = string[stringPos];
  303.                 do
  304.                 {
  305.                     stringPos++;
  306.                     switch (ch)
  307.                     {
  308.                         case 'x', 'X':
  309.                         {
  310.                             num = 0;
  311.                             continue;
  312.                         }
  313.                         case '0' .. '9':
  314.                         {
  315.                             num = (num << 4) | (ch - '0');
  316.                         }
  317.                         case 'a' .. 'f':
  318.                         {
  319.                             num = (num << 4) | (ch - ('a' - 10));
  320.                         }
  321.                         case 'A' .. 'F':
  322.                         {
  323.                             num = (num << 4) | (ch - ('A' - 10));
  324.                         }
  325.                         default:
  326.                         {
  327.                             return -1;
  328.                         }
  329.                     }
  330.                 }
  331.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  332.                 setarg(paramPos, 0, num);
  333.             }
  334.             case 'c':
  335.             {
  336.                 setarg(paramPos, 0, string[stringPos++]);
  337.             }
  338.             case 'f':
  339.             {
  340.  
  341.                 new changestr[16], changepos = 0, strpos = stringPos;
  342.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  343.                 {
  344.                     changestr[changepos++] = string[strpos++];
  345.                     }
  346.                 changestr[changepos] = '\0';
  347.                 setarg(paramPos,0,_:floatstr(changestr));
  348.             }
  349.             case 'p':
  350.             {
  351.                 delim = format[formatPos++];
  352.                 continue;
  353.             }
  354.             case '\'':
  355.             {
  356.                 new
  357.                     end = formatPos - 1,
  358.                     ch;
  359.                 while ((ch = format[++end]) && ch != '\'') {}
  360.                 if (!ch)
  361.                 {
  362.                     return -1;
  363.                 }
  364.                 format[end] = '\0';
  365.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  366.                 {
  367.                     if (format[end + 1])
  368.                     {
  369.                         return -1;
  370.                     }
  371.                     return 0;
  372.                 }
  373.                 format[end] = '\'';
  374.                 stringPos = ch + (end - formatPos);
  375.                 formatPos = end + 1;
  376.             }
  377.             case 'u':
  378.             {
  379.                 new
  380.                     end = stringPos - 1,
  381.                     id = 0,
  382.                     bool:num = true,
  383.                     ch;
  384.                 while ((ch = string[++end]) && ch != delim)
  385.                 {
  386.                     if (num)
  387.                     {
  388.                         if ('0' <= ch <= '9')
  389.                         {
  390.                             id = (id * 10) + (ch - '0');
  391.                         }
  392.                         else
  393.                         {
  394.                             num = false;
  395.                         }
  396.                     }
  397.                 }
  398.                 if (num && IsPlayerConnected(id))
  399.                 {
  400.                     setarg(paramPos, 0, id);
  401.                 }
  402.                 else
  403.                 {
  404.                     #if !defined foreach
  405.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  406.                         #define __SSCANF_FOREACH__
  407.                     #endif
  408.                     string[end] = '\0';
  409.                     num = false;
  410.                     new
  411.                         name[MAX_PLAYER_NAME];
  412.                     id = end - stringPos;
  413.                     foreach (Player, playerid)
  414.                     {
  415.                         GetPlayerName(playerid, name, sizeof (name));
  416.                         if (!strcmp(name, string[stringPos], true, id))
  417.                         {
  418.                             setarg(paramPos, 0, playerid);
  419.                             num = true;
  420.                             break;
  421.                         }
  422.                     }
  423.                     if (!num)
  424.                     {
  425.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  426.                     }
  427.                     string[end] = ch;
  428.                     #if defined __SSCANF_FOREACH__
  429.                         #undef foreach
  430.                         #undef __SSCANF_FOREACH__
  431.                     #endif
  432.                 }
  433.                 stringPos = end;
  434.             }
  435.             case 's', 'z':
  436.             {
  437.                 new
  438.                     i = 0,
  439.                     ch;
  440.                 if (format[formatPos])
  441.                 {
  442.                     while ((ch = string[stringPos++]) && ch != delim)
  443.                     {
  444.                         setarg(paramPos, i++, ch);
  445.                     }
  446.                     if (!i)
  447.                     {
  448.                         return -1;
  449.                     }
  450.                 }
  451.                 else
  452.                 {
  453.                     while ((ch = string[stringPos++]))
  454.                     {
  455.                         setarg(paramPos, i++, ch);
  456.                     }
  457.                 }
  458.                 stringPos--;
  459.                 setarg(paramPos, i, '\0');
  460.             }
  461.             default:
  462.             {
  463.                 continue;
  464.             }
  465.         }
  466.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  467.         {
  468.             stringPos++;
  469.         }
  470.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  471.         {
  472.             stringPos++;
  473.         }
  474.         paramPos++;
  475.     }
  476.     do
  477.     {
  478.         if ((delim = format[formatPos++]) > ' ')
  479.         {
  480.             if (delim == '\'')
  481.             {
  482.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  483.             }
  484.             else if (delim != 'z')
  485.             {
  486.                 return delim;
  487.             }
  488.         }
  489.     }
  490.     while (delim > ' ');
  491.     return 0;
  492. }
Advertisement
Add Comment
Please, Sign In to add comment