Advertisement
razvan_xd

Simple Trivia System v1.1

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