Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes */
- #include < amxmodx >
- #include < jbGamesAPI >
- /* Defines */
- #define TASKID_COUNTDOWN 12375 // Do not touch
- #define TASKID_TIMEOVER 15676 // Do not touch
- #define START_TIMER 5 // Countdown timer before starting a contest
- #define DEFAULT_LOWEST 1 // The lowest number possible on 'Closest Number'
- #define CLOSEST_NUMBER_MAX_MAX 150 // The maximum number for 'Closest Number'
- #define CLOSEST_NUMBER_MIN_MAX 20 // The minimum number for the maximum number for 'Closest Number'
- #define FIRSTWRITE_MIN_LETTERS 1 // Minimum amount of numbers on 'Firstwrites'
- #define FIRSTWRITE_MAX_LETTERS 6 // Maximum amount numbers on 'Firstwrites'
- #define TYPERACE_WORDS 8 // Amount of the (If changed need to be changed in line 210 as well)
- /* Enums */
- enum _: GAMES_VARS
- {
- GAME,
- WIN,
- END,
- TIMER,
- CN_NUMBERS,
- CN_WINNER
- };
- enum _: WORDS_DATA
- {
- g_mEnglish[ 30 ],
- g_mHebrew[ 30 ]
- };
- /* Arrays */
- new Array: g_aWords;
- /* Variables */
- new g_iVars[ GAMES_VARS ];
- new g_iWords[ WORDS_DATA ];
- new g_mNumber[ 33 ];
- new szSolution[ 128 ];
- /* Forwards */
- new g_iForwardClientWin;
- new g_iForwardGameEnd;
- new g_iForwardReturn;
- /* Booleans */
- new bool: bAllow;
- /* Plugin Start */
- public plugin_init()
- {
- register_plugin( "Jailbreak: Games API", "1.0", "Rejack" );
- register_clcmd( "say", "cmdSay" );
- g_iForwardClientWin = CreateMultiForward( "fwGamesWinner", ET_IGNORE, FP_CELL );
- g_iForwardGameEnd = CreateMultiForward( "fwGamesEnd", ET_IGNORE, FP_CELL );
- /* Fail safes */
- if ( LoadWordsFromFile() == -1 )
- set_fail_state( "ERROR: FILE NOT FOUND!" );
- if ( ArraySize( g_aWords ) < TYPERACE_WORDS )
- set_fail_state( "ERROR: NOT ENOUGH WORDS!" );
- if ( g_iForwardClientWin < 0 )
- set_fail_state( "ERROR: FORWARD NOT CREATED! <fwGamesWinner>" );
- }
- public plugin_end()
- {
- DestroyForward( g_iForwardClientWin );
- DestroyForward( g_iForwardGameEnd );
- }
- public plugin_natives()
- {
- register_native( "start_game", "_start_game" );
- register_native( "end_game", "_end_game" );
- register_native( "get_game_type", "_get_game_type" );
- register_native( "get_game_purpose", "_get_game_purpose" );
- register_native( "get_end_reason", "_get_end_reason" );
- register_native( "is_game_active", "_is_game_active" );
- register_native( "is_game_allowed", "_is_game_allowed" );
- }
- /* Natives */
- public _start_game( plugin, params )
- {
- if ( is_game_active() )
- return 0; // Failure - already started
- g_iVars[ GAME ] = get_param( 1 );
- g_iVars[ WIN ] = get_param( 2 );
- // Reset games
- g_iVars[ TIMER ] = START_TIMER;
- bAllow = false;
- szSolution = "unknown solution";
- if ( get_game_type() == GAME_CLOSEST_NUMBER )
- {
- g_iVars[ CN_NUMBERS ] = 0;
- static i, iNum, Players[ 32 ];
- get_players( Players, iNum, "ch" );
- for ( i = 0; i < iNum; i++ )
- g_mNumber[ Players[ i ] ] = -1;
- g_iVars[ CN_WINNER ] = random_num( CLOSEST_NUMBER_MIN_MAX, CLOSEST_NUMBER_MAX_MAX );
- }
- else
- set_task( 15.0, "taskTimeover", TASKID_TIMEOVER );
- TaskCountdown();
- return 1; // Success
- }
- public _end_game( plugin, params )
- {
- if ( task_exists( TASKID_COUNTDOWN ) )
- remove_task( TASKID_COUNTDOWN );
- if ( task_exists( TASKID_TIMEOVER ) )
- remove_task( TASKID_TIMEOVER );
- g_iVars[ END ] = get_param( 1 );
- bAllow = false;
- g_iVars[ GAME ] = 0;
- g_iVars[ WIN ] = 0;
- g_iVars[ TIMER ] = 0;
- szSolution = "unknown solution";
- if ( !ExecuteForward( g_iForwardGameEnd, g_iForwardReturn, get_end_reason() ) )
- set_fail_state( "ERROR: FORWARD NOT EXECUTED! <fwGamesEnd>:" );
- }
- public _get_game_type( plugin, params )
- {
- return g_iVars[ GAME ];
- }
- public _get_game_purpose( plugin, params )
- {
- return g_iVars[ WIN ];
- }
- public _get_end_reason( plugin, params )
- {
- return g_iVars[ END ];
- }
- public bool: _is_game_active( plugin, params )
- {
- return ( get_game_type() == 0 ) ? false : true;
- }
- public bool: _is_game_allowed( plugin, params )
- {
- return bAllow;
- }
- /* Client side */
- public client_putinserver( client )
- {
- g_mNumber[ client ] = -1;
- }
- public client_disconnect( client )
- {
- if ( get_game_type() == GAME_CLOSEST_NUMBER && g_mNumber[ client ] != -1 )
- g_iVars[ CN_NUMBERS ]--;
- }
- public cmdSay( const client )
- {
- if ( !is_user_connected( client ) )
- return 1;
- if ( !is_game_active() )
- return 0;
- if ( get_game_purpose() == GAME_PLAYERS )
- {
- if ( get_user_team( client ) != 1 )
- return 0;
- }
- if ( get_game_purpose() == GAME_LR )
- {
- }
- if ( get_game_purpose() == GAME_CT || get_game_purpose() == GAME_EVENT )
- {
- if ( get_user_team( client ) != 1 || !is_user_alive( client ) )
- return 0;
- }
- new szMsg[ 192 ]
- read_argv( 1, szMsg, charsmax( szMsg ) );
- if ( get_game_type() == GAME_CLOSEST_NUMBER )
- {
- if ( !is_str_num( szMsg ) )
- return 0;
- if ( g_mNumber[ client ] != -1 )
- return 0;
- static gNumber;
- gNumber = str_to_num( szMsg );
- if ( gNumber < 1 )
- return ColorPrint( client, "You need to pick a number higher than^4 1^1." );
- if ( gNumber > g_iVars[ CN_WINNER ] )
- return ColorPrint( client, "You need to pick a number lower than^4 %i^1.", g_iVars[ CN_WINNER ] );
- static Players[ 32 ]
- static index, i, iNum;
- get_players( Players, iNum, "ch" );
- for ( i = 0; i < iNum; i++ )
- {
- index = Players[ i ];
- if ( gNumber == g_mNumber[ index ] )
- {
- ColorPrint( client, "Someone has already picked that number." );
- return 1;
- }
- }
- g_mNumber[ client ] = gNumber;
- g_iVars[ CN_NUMBERS ]++;
- return ColorPrint( 0, "^3%s^1 has picked the number^4 #%i^1.", szName( client ), gNumber );
- }
- if ( !is_game_allowed() )
- return 0;
- if ( get_game_type() == GAME_TYPE_RACE )
- {
- new szArgs[ 2 ][ TYPERACE_WORDS ][ 32 ];
- static i;
- parse( szMsg,
- szArgs[ 0 ][ 0 ], 31, szArgs[ 0 ][ 1 ], 31, szArgs[ 0 ][ 2 ], 31, szArgs[ 0 ][ 3 ], 31,
- szArgs[ 0 ][ 4 ], 31, szArgs[ 0 ][ 5 ], 31, szArgs[ 0 ][ 6 ], 31, szArgs[ 0 ][ 7 ], 31 );
- parse( szSolution,
- szArgs[ 1 ][ 0 ], 31, szArgs[ 1 ][ 1 ], 31, szArgs[ 1 ][ 2 ], 31, szArgs[ 1 ][ 3 ], 31,
- szArgs[ 1 ][ 4 ], 31, szArgs[ 1 ][ 5 ], 31, szArgs[ 1 ][ 6 ], 31, szArgs[ 1 ][ 7 ], 31 );
- for ( i = 0; i < TYPERACE_WORDS; i++ )
- {
- if ( equali( szArgs[ 0 ][ i ], szArgs[ 1 ][ i ] ) )
- continue;
- else
- return 0;
- }
- if ( !ExecuteForward( g_iForwardClientWin, g_iForwardReturn, client ) )
- set_fail_state( "ERROR: FORWARD NOT EXECUTED! <fwGamesWinner>" );
- end_game( END_WIN );
- }
- if ( equali( szMsg, szSolution ) )
- {
- if ( !ExecuteForward( g_iForwardClientWin, g_iForwardReturn, client ) )
- set_fail_state( "ERROR: FORWARD NOT EXECUTED! <fwGamesWinner>" );
- end_game( END_WIN );
- }
- return 0;
- }
- /* Tasks */
- public TaskCountdown()
- {
- static iTimer;
- static szMsg[ 512 ];
- szMsg = "";
- iTimer = g_iVars[ TIMER ];
- if ( iTimer <= 0 )
- {
- static i, szBuffer[ 32 ];
- switch ( get_game_type() )
- {
- case GAME_FIRSTWRITES:
- {
- szSolution = "";
- for ( i = 0; i < random_num( FIRSTWRITE_MIN_LETTERS, FIRSTWRITE_MAX_LETTERS ); i++ )
- {
- num_to_str( random( 10 ), szBuffer, charsmax( szBuffer ) );
- add( szSolution, charsmax( szSolution ), szBuffer );
- }
- formatex( szMsg, charsmax( szMsg ), "Winning combination is^n- %s -", szSolution );
- }
- case GAME_TRANSLATE:
- {
- ArrayGetArray( g_aWords, random( ArraySize( g_aWords ) ), g_iWords );
- formatex( szSolution, charsmax( szSolution ), g_iWords[ g_mEnglish ] );
- formatex( szMsg, charsmax( szMsg ), "Translate the word into English^n- %s -", g_iWords[ g_mHebrew ] );
- }
- case GAME_MISSING_LETTER:
- {
- ArrayGetArray( g_aWords, random( ArraySize( g_aWords ) ), g_iWords );
- formatex( szSolution, charsmax( szSolution ), g_iWords[ g_mEnglish ] );
- formatex( szBuffer, charsmax( szBuffer ), get_missing_letter( szSolution ) );
- formatex( szMsg, charsmax( szMsg ), "Complete the missing letter^n- %s -", szBuffer );
- }
- case GAME_CLOSEST_NUMBER:
- {
- if ( g_iVars[ CN_NUMBERS ] < 1 )
- {
- end_game( END_DEFAULT );
- return ColorPrint( 0, "No one has participated in the^4 Closest number^1 contest." );
- }
- static iNum, index;
- g_iVars[ CN_WINNER ] = random_num( DEFAULT_LOWEST, g_iVars[ CN_WINNER ] );
- iNum = g_iVars[ CN_WINNER ];
- index = findClosest( iNum );
- if ( !ExecuteForward( g_iForwardClientWin, g_iForwardReturn, index ) )
- log_amx( "Error: Couldn't execute forward 'fwGamesWinner'." );
- formatex( szMsg, charsmax( szMsg ), "%s won the Closest number contest with the number #%i^nThe closest number to #%i.", szName( index ), g_mNumber[ index ], iNum );
- ColorPrint( 0, "^3%s^1 won the^4 Closest number^1 contest with the number^4 #%i^1, The closest number to^3 #%i^1.", szName( index ), g_mNumber[ index ], iNum );
- end_game( END_WIN );
- }
- case GAME_TYPE_RACE:
- {
- szSolution = "";
- static iNum[ TYPERACE_WORDS ], z;
- for ( i = 0; i < TYPERACE_WORDS; i++ )
- {
- TryAgain:
- iNum[ i ] = random( ArraySize( g_aWords ) );
- for ( z = 0; z < i; z++ ) // Word repeating itself in a race.
- {
- if ( iNum[ i ] == iNum[ z ] )
- goto TryAgain;
- }
- ArrayGetArray( g_aWords, iNum[ i ], g_iWords );
- formatex( szBuffer, charsmax( szBuffer ), "%s%s", g_iWords[ g_mEnglish ], ( i == TYPERACE_WORDS - 1 ) ? "" : " " );
- add( szSolution, charsmax( szSolution ), szBuffer );
- }
- formatex( szMsg, charsmax( szMsg ), "Fastest person to type the sentence wins^n%s", szSolution );
- }
- case GAME_CONFUSED_WORDS:
- {
- ArrayGetArray( g_aWords, random( ArraySize( g_aWords ) ), g_iWords );
- formatex( szSolution, charsmax( szSolution ), g_iWords[ g_mEnglish ] );
- formatex( szBuffer, charsmax( szBuffer ), g_iWords[ g_mEnglish ] );
- str_shuffle( szBuffer );
- formatex( szMsg, charsmax( szMsg ), "Figure out the scrambled word^n- %s -", szBuffer );
- }
- case GAME_MATH:
- {
- static iNum[ 3 ];
- iNum[ 0 ] = random( 250 );
- iNum[ 1 ] = random( 250 );
- iNum[ 2 ] = random( 2 );
- if ( iNum[ 2 ] == 0 )
- {
- num_to_str( iNum[ 0 ] + iNum[ 1 ], szSolution, charsmax( szSolution ) );
- formatex( szMsg, charsmax( szMsg ), "Solve the equation^n%i + %i = ?", iNum[ 0 ], iNum[ 1 ] );
- }
- else
- {
- num_to_str( iNum[ 0 ] - iNum[ 1 ], szSolution, charsmax( szSolution ) );
- formatex( szMsg, charsmax( szMsg ), "Solve the equation^n%i - %i = ?", iNum[ 0 ], iNum[ 1 ] );
- }
- }
- }
- bAllow = true;
- set_hudmessage( 0, 255, 255, -1.0, 0.35, 0, 0.0, 12.0, 0.1, 0.1, 3 );
- show_hudmessage( 0, szMsg );
- return 1;
- }
- // Build customize message
- static szGameType[ 32 ], szTimer[ 16 ], szWinner[ 64 ];
- switch ( g_iVars[ WIN ] )
- {
- case GAME_LR:
- szWinner = "Winner wins the last request.";
- case GAME_CT:
- szWinner = "Winner win the contest.";
- case GAME_PLAYERS:
- szWinner = "Winner moves to the guards team.";
- }
- add( szMsg, charsmax( szMsg ), "Jailbreak Games^n" );
- add( szMsg, charsmax( szMsg ), "-----------------------^n" );
- formatex( szGameType, charsmax( szGameType ), "%s contest^n", szEvents[ get_game_type() ] );
- add( szMsg, charsmax( szMsg ), szGameType );
- if ( get_game_type() == GAME_CLOSEST_NUMBER )
- {
- static szBuffer[ 64 ];
- formatex( szBuffer, charsmax( szBuffer ), "Guess a number between %i-%i^n", DEFAULT_LOWEST, g_iVars[ CN_WINNER ] );
- add( szMsg, charsmax( szMsg ), szBuffer );
- }
- formatex( szTimer, charsmax( szTimer ), "%i second%s^n", iTimer, ( iTimer < 2 ) ? "" : "s" );
- add( szMsg, charsmax( szMsg ), szTimer );
- add( szMsg, charsmax( szMsg ), "-----------------------^n" );
- add( szMsg, charsmax( szMsg ), szWinner );
- set_hudmessage( 0, 255, 255, -1.0, 0.15, 0, 0.0, 1.0, 0.1, 0.1, 3 );
- show_hudmessage( 0, szMsg );
- g_iVars[ TIMER ]--;
- static Vox[ 32 ];
- num_to_word( iTimer, Vox, charsmax( Vox ) );
- client_cmd( 0, "spk ^"vox/%s^"", Vox );
- set_task( 1.0, "TaskCountdown", TASKID_COUNTDOWN );
- return 1;
- }
- public taskTimeover( )
- {
- ColorPrint( 0, "^3%s^1 contest has stopeed because no one has answered.", szEvents[ get_game_type() ] );
- end_game( END_DEFAULT );
- }
- /* Stocks */
- stock LoadWordsFromFile( )
- {
- static szFile[ 64 ];
- get_configsdir( szFile, charsmax( szFile ) );
- format( szFile, charsmax( szFile ), "%s/jbGames.cfg", szFile );
- if ( !file_exists( szFile ) )
- {
- log_amx( "Jailbreak Games - File '%s' was not found!", szFile );
- return -1;
- }
- g_aWords = ArrayCreate( WORDS_DATA );
- static iFile;
- static szBuffer[ 64 ];
- iFile = fopen( szFile, "rt" );
- while ( !feof( iFile ) )
- {
- fgets( iFile, szBuffer, charsmax( szBuffer ) );
- trim( szBuffer );
- if ( szBuffer[ 0 ] == ';' || equali( szBuffer, "//", 2 ) || strlen( szBuffer ) < 3 )
- continue;
- parse( szBuffer, g_iWords[ g_mEnglish ], charsmax( g_iWords[ g_mEnglish ] ), g_iWords[ g_mHebrew ], charsmax( g_iWords[ g_mHebrew ] ) );
- remove_quotes( g_iWords[ g_mEnglish ] );
- remove_quotes( g_iWords[ g_mHebrew ] );
- ArrayPushArray( g_aWords, g_iWords );
- }
- return 1;
- }
- stock findClosest( num )
- {
- static Players[ 32 ];
- static index, i, lowest, iNum;
- static iAttempt;
- iAttempt = 0;
- TryAgain:
- lowest = DEFAULT_LOWEST;
- get_players( Players, iNum, "ch" );
- for ( i = 0; i < iNum; i++ )
- {
- index = Players[ i ];
- if ( abs( num - ( g_mNumber[ index ] ) ) < abs( num - lowest ) )
- {
- lowest = g_mNumber[ index ];
- }
- }
- if ( iAttempt == 5 )
- {
- ColorPrint( 0, "^3ERROR^1: Closest number failed. Please alert server management." );
- end_game( END_DEFAULT );
- }
- if ( !is_user_connected( index ) )
- {
- iAttempt++;
- goto TryAgain;
- }
- return index;
- }
- stock get_missing_letter( const String[] )
- {
- static szBuffer[ 2 ][ 32 ], szReturn[ 32 ];
- static iNum, Max;
- formatex( szBuffer[ 0 ], charsmax( szBuffer[] ), String );
- formatex( szBuffer[ 1 ], charsmax( szBuffer[] ), String );
- Max = strlen( String );
- iNum = random( Max );
- replace( szBuffer[ 0 ], charsmax( szBuffer[] ), szBuffer[ 0 ][ iNum ], "_" );
- formatex( szReturn, charsmax( szReturn ), "%s%s", szBuffer[ 0 ], szBuffer[ 1 ][ iNum + 1 ] );
- return szReturn;
- }
- stock str_shuffle( String[] )
- {
- static iLen;
- iLen = strlen( String );
- SortCustom1D( String, iLen, "_str_shuffle" );
- return iLen;
- }
- public _str_shuffle( item1, item2, array[] )
- {
- return ( ( random( 2 ) * 2 ) - 1 );
- }
- stock access( const index, const flag )
- {
- return ( get_user_flags( index ) & flag );
- }
- stock get_configsdir( name[ ], len )
- {
- return get_localinfo( "amxx_configsdir", name, len );
- }
Advertisement
Add Comment
Please, Sign In to add comment