Advertisement
razvan_xd

Simple Trivia System

Jul 23rd, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.46 KB | None | 0 0
  1. #define USE_SSCANF_PLUGIN                                                       // Daca nu folositi sscanf plugin, stergeti
  2. #define EXACTLY_ANSWER                                                          // Daca aceasta constanta e definita, raspunsul 'abc' nu este acelasi lucru cu 'abc.'
  3. //#define GIVE_MONEY_FUNCTION               ( "GivePlayerCash" )                // Daca aveti vreun money anticheat, definiti aceasta constanta cu numele functiei de dat ban a anticheat-ului
  4. #define CASE_SENSITIVE                      ( false )                           // true / false
  5. #define MAX_QUESTIONS                       ( 50 )                              // Numarul maxim de intrebari ce poate fi incarcat din fisier
  6. #define TRIVIA_PATH                         ( "trivia.cfg" )                    // Fisierul cu intrebarile
  7. #define ASK_INTERVAL                        ( 600 )                             // Intervalul de timp dintre intrebari ( secunde )
  8. #define RESPONSE_INTERVAL                   ( 60 )                              // Peste cat timp sa fie afisat raspunsul corect daca nu s-a raspuns ( secunde )
  9. #define MONEY_WON                           ( 5000 )                            // Banii castigati
  10.  
  11. // Sintaxa:
  12. // INTREBARE|RASPUNS
  13.  
  14. #if ASK_INTERVAL <= RESPONSE_INTERVAL
  15.     #undef ASK_INTERVAL
  16.     #define ASK_INTERVAL                    ( RESPONSE_INTERVAL + 10 )
  17. #endif
  18.  
  19. #include < a_samp >
  20. #if defined USE_SSCANF_PLUGIN
  21.     #include < sscanf2 >
  22. #endif
  23.  
  24. forward rtTimer ( );
  25. forward rtTimerAnswer ( );
  26.  
  27. enum ertTrivia
  28. {
  29.     rtQuestion [ 64 ],
  30.     rtAnswer [ 64 ]
  31. };
  32.  
  33. new rtTrivia [ MAX_QUESTIONS ] [ ertTrivia ];
  34. new rtTimerID;
  35. new rtAnswerTimerID;
  36. new rtQuestionID;
  37. new rtLoadedQuestions;
  38.  
  39. public OnFilterScriptInit ( )
  40. {
  41.     print ( "\n[R-TRIVIA] - Loaded\n\nSimple Trivia System by RaZVaN ^ xD @ gta-mp.ro\n" );
  42.     if ( !fexist ( TRIVIA_PATH ) ) return print ( "[R-TRIVIA]: Trivia config file was not found." );
  43.     new
  44.         File: rtFile = fopen ( TRIVIA_PATH, io_read ),
  45.         string [ 128 + 1 ]
  46.     ;
  47.     rtQuestionID = -1;
  48.     rtLoadedQuestions = 0;
  49.     while ( fread ( rtFile, string ) )
  50.     {
  51.         #if defined USE_SSCANF_PLUGIN
  52.         if ( !sscanf ( string, "p<|>s[64]s[64]", rtTrivia [ rtLoadedQuestions ] [ rtQuestion ], rtTrivia [ rtLoadedQuestions ] [ rtAnswer ] ) )
  53.         #else
  54.         if ( !sscanf ( string, "p|ss", rtTrivia [ rtLoadedQuestions ] [ rtQuestion ], rtTrivia [ rtLoadedQuestions ] [ rtAnswer ] ) )
  55.         #endif
  56.         {
  57.             for ( new i = 0, j = strlen ( rtTrivia [ rtLoadedQuestions ] [ rtAnswer ] ); i != j; ++i )
  58.             {
  59.                 switch ( rtTrivia [ rtLoadedQuestions ] [ rtAnswer ] [ i ] )
  60.                 {
  61.                     case '\r', '\n': strdel ( rtTrivia [ rtLoadedQuestions ] [ rtAnswer ], i, i + 1 );
  62.                     default: continue;
  63.                 }
  64.             }
  65.             ++rtLoadedQuestions;
  66.             if ( rtLoadedQuestions == MAX_QUESTIONS )
  67.             {
  68.                 print ( "[R-TRIVIA]: Limit of questions - reached." );
  69.                 break;
  70.             }
  71.         }
  72.     }
  73.     fclose ( rtFile );
  74.     printf ( "[R-TRIVIA]: %i questions - loaded.", rtLoadedQuestions );
  75.     if ( rtLoadedQuestions != 0 ) rtTimerID = SetTimer ( "rtTimer", ASK_INTERVAL * 1000, true );
  76.     return 1;
  77. }
  78.  
  79. public OnFilterScriptExit ( )
  80. {
  81.     print ( "\n[R-TRIVIA] - Unloaded\n" );
  82.     KillTimer ( rtTimerID );
  83.     if ( rtQuestionID != -1 ) KillTimer ( rtAnswerTimerID );
  84.     return 1;
  85. }
  86.  
  87. public OnPlayerText ( playerid, text [ ] )
  88. {
  89.     if ( rtQuestionID != -1 )
  90.     {
  91.         if ( text [ 0 ] == 0 || ( text [ 0 ] == 1 && text [ 1 ] == 0 ) ) return 1;
  92.         #if defined EXACTLY_ANSWER
  93.         if ( !strcmp ( text, rtTrivia [ rtQuestionID ] [ rtAnswer ], !CASE_SENSITIVE ) )
  94.         #else
  95.         if ( strfind ( text, rtTrivia [ rtQuestionID ] [ rtAnswer ], !CASE_SENSITIVE ) != -1 )
  96.         #endif
  97.         {
  98.             new string [ 160 ];
  99.             GetPlayerName ( playerid, string, MAX_PLAYER_NAME );
  100.             format ( string, sizeof ( string ), "[R-TRIVIA]: %s {FFFFFF}a raspuns corect ( {00FF00}%s {FFFFFF}) .", string, rtTrivia [ rtQuestionID ] [ rtAnswer ] );
  101.             SendClientMessageToAll ( 0x00FF00FF, string );
  102.             #if defined GIVE_MONEY_FUNCTION
  103.             CallRemoteFunction ( GIVE_MONEY_FUNCTION, "ii", playerid, MONEY_WON );
  104.             #else
  105.             GivePlayerMoney ( playerid, MONEY_WON );
  106.             #endif
  107.             KillTimer ( rtAnswerTimerID );
  108.             rtQuestionID = -1;
  109.             return 0;
  110.         }
  111.     }
  112.     return 1;
  113. }
  114.  
  115. public rtTimer ( )
  116. {
  117.     new string [ 128 ];
  118.     rtQuestionID = random ( rtLoadedQuestions );
  119.     format ( string, sizeof ( string ), "[R-TRIVIA]: {FFFFFF}%s {00FF00}?", rtTrivia [ rtQuestionID ] [ rtQuestion ] );
  120.     SendClientMessageToAll ( 0x00FF00FF, string );
  121.     SendClientMessageToAll ( 0x00FF00FF, "[R-TRIVIA]: {FFFFFF}Tastati raspunsul in {00FF00}chat {FFFFFF}." );
  122.     rtAnswerTimerID = SetTimer ( "rtTimerAnswer", RESPONSE_INTERVAL * 1000, false );
  123.     return 1;
  124. }
  125.  
  126. public rtTimerAnswer ( )
  127. {
  128.     new string [ 128 ];
  129.     format ( string, sizeof ( string ), "[R-TRIVIA]: {FFFFFF}Raspunsul era {00FF00}%s {FFFFFF}.", rtTrivia [ rtQuestionID ] [ rtAnswer ] );
  130.     SendClientMessageToAll ( 0x00FF00FF, string );
  131.     rtQuestionID = -1;
  132.     return 1;
  133. }
  134.  
  135. #if !defined USE_SSCANF_PLUGIN
  136. stock sscanf(string[], format[], {Float,_}:...)
  137. {
  138.     #if defined isnull
  139.         if (isnull(string))
  140.     #else
  141.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  142.     #endif
  143.         {
  144.             return format[0];
  145.         }
  146.     #pragma tabsize 4
  147.     new
  148.         formatPos = 0,
  149.         stringPos = 0,
  150.         paramPos = 2,
  151.         paramCount = numargs(),
  152.         delim = ' ';
  153.     while (string[stringPos] && string[stringPos] <= ' ')
  154.     {
  155.         stringPos++;
  156.     }
  157.     while (paramPos < paramCount && string[stringPos])
  158.     {
  159.         switch (format[formatPos++])
  160.         {
  161.             case '\0':
  162.             {
  163.                 return 0;
  164.             }
  165.             case 'i', 'd':
  166.             {
  167.                 new
  168.                     neg = 1,
  169.                     num = 0,
  170.                     ch = string[stringPos];
  171.                 if (ch == '-')
  172.                 {
  173.                     neg = -1;
  174.                     ch = string[++stringPos];
  175.                 }
  176.                 do
  177.                 {
  178.                     stringPos++;
  179.                     if ('0' <= ch <= '9')
  180.                     {
  181.                         num = (num * 10) + (ch - '0');
  182.                     }
  183.                     else
  184.                     {
  185.                         return -1;
  186.                     }
  187.                 }
  188.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  189.                 setarg(paramPos, 0, num * neg);
  190.             }
  191.             case 'h', 'x':
  192.             {
  193.                 new
  194.                     num = 0,
  195.                     ch = string[stringPos];
  196.                 do
  197.                 {
  198.                     stringPos++;
  199.                     switch (ch)
  200.                     {
  201.                         case 'x', 'X':
  202.                         {
  203.                             num = 0;
  204.                             continue;
  205.                         }
  206.                         case '0' .. '9':
  207.                         {
  208.                             num = (num << 4) | (ch - '0');
  209.                         }
  210.                         case 'a' .. 'f':
  211.                         {
  212.                             num = (num << 4) | (ch - ('a' - 10));
  213.                         }
  214.                         case 'A' .. 'F':
  215.                         {
  216.                             num = (num << 4) | (ch - ('A' - 10));
  217.                         }
  218.                         default:
  219.                         {
  220.                             return -1;
  221.                         }
  222.                     }
  223.                 }
  224.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  225.                 setarg(paramPos, 0, num);
  226.             }
  227.             case 'c':
  228.             {
  229.                 setarg(paramPos, 0, string[stringPos++]);
  230.             }
  231.             case 'f':
  232.             {
  233.  
  234.                 new changestr[16], changepos = 0, strpos = stringPos;    
  235.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  236.                 {
  237.                     changestr[changepos++] = string[strpos++];
  238.                 }
  239.                 changestr[changepos] = '\0';
  240.                 setarg(paramPos,0,_:floatstr(changestr));
  241.             }
  242.             case 'p':
  243.             {
  244.                 delim = format[formatPos++];
  245.                 continue;
  246.             }
  247.             case '\'':
  248.             {
  249.                 new
  250.                     end = formatPos - 1,
  251.                     ch;
  252.                 while ((ch = format[++end]) && ch != '\'') {}
  253.                 if (!ch)
  254.                 {
  255.                     return -1;
  256.                 }
  257.                 format[end] = '\0';
  258.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  259.                 {
  260.                     if (format[end + 1])
  261.                     {
  262.                         return -1;
  263.                     }
  264.                     return 0;
  265.                 }
  266.                 format[end] = '\'';
  267.                 stringPos = ch + (end - formatPos);
  268.                 formatPos = end + 1;
  269.             }
  270.             case 'u':
  271.             {
  272.                 new
  273.                     end = stringPos - 1,
  274.                     id = 0,
  275.                     bool:num = true,
  276.                     ch;
  277.                 while ((ch = string[++end]) && ch != delim)
  278.                 {
  279.                     if (num)
  280.                     {
  281.                         if ('0' <= ch <= '9')
  282.                         {
  283.                             id = (id * 10) + (ch - '0');
  284.                         }
  285.                         else
  286.                         {
  287.                             num = false;
  288.                         }
  289.                     }
  290.                 }
  291.                 if (num && IsPlayerConnected(id))
  292.                 {
  293.                     setarg(paramPos, 0, id);
  294.                 }
  295.                 else
  296.                 {
  297.                     #if !defined foreach
  298.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  299.                         #define __SSCANF_FOREACH__
  300.                     #endif
  301.                     string[end] = '\0';
  302.                     num = false;
  303.                     new
  304.                         name[MAX_PLAYER_NAME];
  305.                     id = end - stringPos;
  306.                     foreach (Player, playerid)
  307.                     {
  308.                         GetPlayerName(playerid, name, sizeof (name));
  309.                         if (!strcmp(name, string[stringPos], true, id))
  310.                         {
  311.                             setarg(paramPos, 0, playerid);
  312.                             num = true;
  313.                             break;
  314.                         }
  315.                     }
  316.                     if (!num)
  317.                     {
  318.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  319.                     }
  320.                     string[end] = ch;
  321.                     #if defined __SSCANF_FOREACH__
  322.                         #undef foreach
  323.                         #undef __SSCANF_FOREACH__
  324.                     #endif
  325.                 }
  326.                 stringPos = end;
  327.             }
  328.             case 's', 'z':
  329.             {
  330.                 new
  331.                     i = 0,
  332.                     ch;
  333.                 if (format[formatPos])
  334.                 {
  335.                     while ((ch = string[stringPos++]) && ch != delim)
  336.                     {
  337.                         setarg(paramPos, i++, ch);
  338.                     }
  339.                     if (!i)
  340.                     {
  341.                         return -1;
  342.                     }
  343.                 }
  344.                 else
  345.                 {
  346.                     while ((ch = string[stringPos++]))
  347.                     {
  348.                         setarg(paramPos, i++, ch);
  349.                     }
  350.                 }
  351.                 stringPos--;
  352.                 setarg(paramPos, i, '\0');
  353.             }
  354.             default:
  355.             {
  356.                 continue;
  357.             }
  358.         }
  359.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  360.         {
  361.             stringPos++;
  362.         }
  363.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  364.         {
  365.             stringPos++;
  366.         }
  367.         paramPos++;
  368.     }
  369.     do
  370.     {
  371.         if ((delim = format[formatPos++]) > ' ')
  372.         {
  373.             if (delim == '\'')
  374.             {
  375.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  376.             }
  377.             else if (delim != 'z')
  378.             {
  379.                 return delim;
  380.             }
  381.         }
  382.     }
  383.     while (delim > ' ');
  384.     return 0;
  385. }
  386. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement