Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <amxmodx>
- #include <sockets>
- #pragma semicolon 1
- // hardcoding ftw!!!
- #define DIRECTORY_DATA "addons/amxmodx/data/xj_shortcuts"
- #define FILE_LAST_UPDATE "addons/amxmodx/data/xj_shortcuts/last_update.txt"
- #define FILE_AVAILABLE_MAPS "addons/amxmodx/data/xj_shortcuts/available_maps.txt"
- new g_iAvailableMaps;
- new Trie:g_trieAvailableMaps;
- new Array:g_arrayAvailableMaps;
- new g_hSocketId;
- public plugin_init( )
- {
- register_plugin( "XJ: Shortcuts", "1.1", "SchlumPF" );
- register_clcmd( "say", "hookSay" );
- }
- public plugin_cfg( )
- {
- g_arrayAvailableMaps = ArrayCreate( 32 );
- g_trieAvailableMaps = TrieCreate( );
- if( !dir_exists( DIRECTORY_DATA ) )
- {
- mkdir( DIRECTORY_DATA );
- }
- new year, month, day;
- date( year, month, day );
- if( !file_exists( FILE_LAST_UPDATE ) )
- {
- updateAvailableMaps( year, month, day );
- return PLUGIN_CONTINUE;
- }
- new buffer[32];
- new f = fopen( FILE_LAST_UPDATE, "rt" );
- fgets( f, buffer, 31 );
- fclose( f );
- if( str_to_num( buffer[0] ) < year || str_to_num( buffer[5] ) < month || str_to_num( buffer[8] ) < day )
- {
- updateAvailableMaps( year, month, day );
- }
- else
- {
- storeAvailableMaps( );
- }
- return PLUGIN_CONTINUE;
- }
- public updateAvailableMaps( year, month, day )
- {
- log_amx( "update" );
- new curDate[128];
- new f = fopen( FILE_LAST_UPDATE, "wt" );
- format( curDate, 128, "%04ix%02ix%02i", year, month, day );
- fputs( f, curDate );
- fclose( f );
- new error;
- g_hSocketId = socket_open( "xtreme-jumps.eu", 80, SOCKET_TCP, error );
- new message[256];
- formatex( message, 255, "GET /shortcuts/sc.php HTTP/1.0^nHost: xtreme-jumps.eu^r^n^r^n" );
- socket_send( g_hSocketId, message, 255 );
- if( file_exists( FILE_AVAILABLE_MAPS ) )
- {
- delete_file( FILE_AVAILABLE_MAPS );
- }
- readWeb( );
- }
- public readWeb( )
- {
- new buffer[2048];
- socket_recv( g_hSocketId, buffer, 2047 );
- if( buffer[0] )
- {
- // put all datapackets together to ensure a correct reading of all available maps
- replace_all( buffer, 2047, "|", "^n" );
- new f = fopen( FILE_AVAILABLE_MAPS, "at" );
- fputs( f, buffer );
- fclose( f );
- // watch out for new datapackets
- set_task( 0.5,"readWeb" );
- }
- else
- {
- // if we received a empty datapacket we know that there is no more data we are
- // interested in, so start reading our temporary file
- storeAvailableMaps( );
- socket_close( g_hSocketId );
- }
- }
- public storeAvailableMaps( )
- {
- log_amx( "store" );
- new bool:data;
- new buffer[256];
- new f = fopen( FILE_AVAILABLE_MAPS, "rt" );
- while( !feof( f ) )
- {
- fgets( f, buffer, 255 );
- replace_all( buffer, 255, "^n", "" );
- if( data )
- {
- // i am using a trie to look whether the map the player wants to
- // see shortcuts for exists in the way he typed it. this is much
- // faster than looping through all maps in the database which i only
- // do if the map does not exist to show a few maps which he culd have
- // searched for.
- ArrayPushString( g_arrayAvailableMaps, buffer );
- TrieSetCell( g_trieAvailableMaps, buffer, 1337 );
- g_iAvailableMaps++;
- }
- else if( equal( buffer, "i like turtles, bitch!" ) )
- {
- data = true;
- }
- }
- fclose( f );
- }
- #define PICTURE_DIRECTORY "http://xtreme-jumps.eu/shortcuts/shortcuts"
- public hookSay( plr )
- {
- static command[64];
- read_args( command, 32 );
- remove_quotes( command );
- if( !( equal( command, "/sc", 3 ) || equal( command, "/scs", 4 ) || equal( command, "/shortcuts", 10 ) ) )
- {
- return PLUGIN_CONTINUE;
- }
- static pattern[64], pos;
- if( ( pos = contain( command, " " ) ) > -1 )
- {
- // the player wants to know the shortcuts of a specific map
- formatex( pattern, 63, "%s", command[pos+1] );
- }
- else
- {
- // the player wants to know the shortcuts of the current map
- get_mapname( pattern, 63 );
- }
- if( TrieKeyExists( g_trieAvailableMaps, pattern ) )
- {
- static motd[256];
- formatex( motd, 255, "%s/%s.jpg", PICTURE_DIRECTORY, pattern );
- show_motd( plr, motd, pattern );
- }
- else
- {
- static menuitem[64], map[64];
- static menu, items;
- menu = menu_create( "\rXJ: Shortcuts - SchlumPF", "menuMapHandler" );
- items = 0;
- for( new i; i < g_iAvailableMaps; i++ )
- {
- ArrayGetString( g_arrayAvailableMaps, i, map, 63 );
- if( containi( map, pattern ) > -1 )
- {
- formatex( menuitem, 63, "\w%s", map );
- menu_additem( menu, menuitem, map );
- items++;
- }
- }
- if( items )
- {
- menu_display( plr, menu, 0 );
- }
- else
- {
- chat_print( plr, "Could not find '%s' in the Xtreme-Jumps database!", pattern );
- }
- }
- return PLUGIN_HANDLED;
- }
- public menuMapHandler( plr, menu, item )
- {
- if( item == MENU_EXIT )
- {
- menu_destroy( menu );
- return PLUGIN_HANDLED;
- }
- static data[64], name[64];
- static access, callback;
- menu_item_getinfo( menu, item, access, data, 63, name, 63, callback );
- static motd[256];
- formatex( motd, 255, "%s/%s.jpg", PICTURE_DIRECTORY, data );
- show_motd( plr, motd, data );
- menu_destroy( menu );
- return PLUGIN_HANDLED;
- }
- stock chat_print( plr, const message[], { Float, Sql, Result, _ }:... )
- {
- new msg[192];
- format( msg, 191, "^x04[XJ]^x01 " );
- vformat( msg[7], 185, message, 3 );
- static saytext, maxplayers;
- if( !saytext )
- {
- saytext = get_user_msgid( "SayText" );
- }
- if( !maxplayers )
- {
- maxplayers = get_maxplayers( );
- }
- if( plr <= maxplayers && plr )
- {
- message_begin( MSG_ONE_UNRELIABLE, saytext, {0, 0, 0}, plr );
- write_byte( plr );
- write_string( msg );
- message_end( );
- }
- else if( !plr )
- {
- for( new i = 1; i <= maxplayers; i++ )
- {
- if( !is_user_connected( i ) )
- {
- continue;
- }
- message_begin( MSG_ONE_UNRELIABLE, saytext, { 0, 0, 0 }, i );
- write_byte( i );
- write_string( msg );
- message_end( );
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment