Guest User

xj shortcuts

a guest
Apr 20th, 2011
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. #include <amxmodx>
  2. #include <sockets>
  3.  
  4. #pragma semicolon 1
  5.  
  6. // hardcoding ftw!!!
  7. #define DIRECTORY_DATA "addons/amxmodx/data/xj_shortcuts"
  8. #define FILE_LAST_UPDATE "addons/amxmodx/data/xj_shortcuts/last_update.txt"
  9. #define FILE_AVAILABLE_MAPS "addons/amxmodx/data/xj_shortcuts/available_maps.txt"
  10.  
  11. new g_iAvailableMaps;
  12. new Trie:g_trieAvailableMaps;
  13. new Array:g_arrayAvailableMaps;
  14.  
  15. new g_hSocketId;
  16.  
  17. public plugin_init( )
  18. {
  19. register_plugin( "XJ: Shortcuts", "1.1", "SchlumPF" );
  20. register_clcmd( "say", "hookSay" );
  21. }
  22.  
  23. public plugin_cfg( )
  24. {
  25. g_arrayAvailableMaps = ArrayCreate( 32 );
  26. g_trieAvailableMaps = TrieCreate( );
  27.  
  28. if( !dir_exists( DIRECTORY_DATA ) )
  29. {
  30. mkdir( DIRECTORY_DATA );
  31. }
  32.  
  33. new year, month, day;
  34. date( year, month, day );
  35.  
  36. if( !file_exists( FILE_LAST_UPDATE ) )
  37. {
  38. updateAvailableMaps( year, month, day );
  39. return PLUGIN_CONTINUE;
  40. }
  41.  
  42. new buffer[32];
  43. new f = fopen( FILE_LAST_UPDATE, "rt" );
  44. fgets( f, buffer, 31 );
  45. fclose( f );
  46.  
  47. if( str_to_num( buffer[0] ) < year || str_to_num( buffer[5] ) < month || str_to_num( buffer[8] ) < day )
  48. {
  49. updateAvailableMaps( year, month, day );
  50. }
  51. else
  52. {
  53. storeAvailableMaps( );
  54. }
  55.  
  56. return PLUGIN_CONTINUE;
  57. }
  58.  
  59. public updateAvailableMaps( year, month, day )
  60. {
  61. log_amx( "update" );
  62.  
  63. new curDate[128];
  64. new f = fopen( FILE_LAST_UPDATE, "wt" );
  65. format( curDate, 128, "%04ix%02ix%02i", year, month, day );
  66. fputs( f, curDate );
  67. fclose( f );
  68.  
  69. new error;
  70. g_hSocketId = socket_open( "xtreme-jumps.eu", 80, SOCKET_TCP, error );
  71.  
  72. new message[256];
  73. formatex( message, 255, "GET /shortcuts/sc.php HTTP/1.0^nHost: xtreme-jumps.eu^r^n^r^n" );
  74.  
  75. socket_send( g_hSocketId, message, 255 );
  76.  
  77. if( file_exists( FILE_AVAILABLE_MAPS ) )
  78. {
  79. delete_file( FILE_AVAILABLE_MAPS );
  80. }
  81.  
  82. readWeb( );
  83. }
  84.  
  85. public readWeb( )
  86. {
  87. new buffer[2048];
  88. socket_recv( g_hSocketId, buffer, 2047 );
  89.  
  90. if( buffer[0] )
  91. {
  92. // put all datapackets together to ensure a correct reading of all available maps
  93. replace_all( buffer, 2047, "|", "^n" );
  94.  
  95. new f = fopen( FILE_AVAILABLE_MAPS, "at" );
  96. fputs( f, buffer );
  97. fclose( f );
  98.  
  99. // watch out for new datapackets
  100. set_task( 0.5,"readWeb" );
  101. }
  102. else
  103. {
  104. // if we received a empty datapacket we know that there is no more data we are
  105. // interested in, so start reading our temporary file
  106.  
  107. storeAvailableMaps( );
  108. socket_close( g_hSocketId );
  109. }
  110. }
  111.  
  112. public storeAvailableMaps( )
  113. {
  114. log_amx( "store" );
  115.  
  116. new bool:data;
  117.  
  118. new buffer[256];
  119. new f = fopen( FILE_AVAILABLE_MAPS, "rt" );
  120. while( !feof( f ) )
  121. {
  122. fgets( f, buffer, 255 );
  123. replace_all( buffer, 255, "^n", "" );
  124.  
  125. if( data )
  126. {
  127. // i am using a trie to look whether the map the player wants to
  128. // see shortcuts for exists in the way he typed it. this is much
  129. // faster than looping through all maps in the database which i only
  130. // do if the map does not exist to show a few maps which he culd have
  131. // searched for.
  132.  
  133. ArrayPushString( g_arrayAvailableMaps, buffer );
  134. TrieSetCell( g_trieAvailableMaps, buffer, 1337 );
  135.  
  136. g_iAvailableMaps++;
  137. }
  138. else if( equal( buffer, "i like turtles, bitch!" ) )
  139. {
  140. data = true;
  141. }
  142. }
  143. fclose( f );
  144. }
  145.  
  146. #define PICTURE_DIRECTORY "http://xtreme-jumps.eu/shortcuts/shortcuts"
  147.  
  148. public hookSay( plr )
  149. {
  150. static command[64];
  151. read_args( command, 32 );
  152. remove_quotes( command );
  153.  
  154. if( !( equal( command, "/sc", 3 ) || equal( command, "/scs", 4 ) || equal( command, "/shortcuts", 10 ) ) )
  155. {
  156. return PLUGIN_CONTINUE;
  157. }
  158.  
  159. static pattern[64], pos;
  160. if( ( pos = contain( command, " " ) ) > -1 )
  161. {
  162. // the player wants to know the shortcuts of a specific map
  163. formatex( pattern, 63, "%s", command[pos+1] );
  164. }
  165. else
  166. {
  167. // the player wants to know the shortcuts of the current map
  168. get_mapname( pattern, 63 );
  169. }
  170.  
  171. if( TrieKeyExists( g_trieAvailableMaps, pattern ) )
  172. {
  173. static motd[256];
  174. formatex( motd, 255, "%s/%s.jpg", PICTURE_DIRECTORY, pattern );
  175.  
  176. show_motd( plr, motd, pattern );
  177. }
  178. else
  179. {
  180. static menuitem[64], map[64];
  181. static menu, items;
  182.  
  183. menu = menu_create( "\rXJ: Shortcuts - SchlumPF", "menuMapHandler" );
  184.  
  185. items = 0;
  186.  
  187. for( new i; i < g_iAvailableMaps; i++ )
  188. {
  189. ArrayGetString( g_arrayAvailableMaps, i, map, 63 );
  190.  
  191. if( containi( map, pattern ) > -1 )
  192. {
  193. formatex( menuitem, 63, "\w%s", map );
  194. menu_additem( menu, menuitem, map );
  195.  
  196. items++;
  197. }
  198. }
  199.  
  200. if( items )
  201. {
  202. menu_display( plr, menu, 0 );
  203. }
  204. else
  205. {
  206. chat_print( plr, "Could not find '%s' in the Xtreme-Jumps database!", pattern );
  207. }
  208. }
  209.  
  210. return PLUGIN_HANDLED;
  211. }
  212.  
  213. public menuMapHandler( plr, menu, item )
  214. {
  215. if( item == MENU_EXIT )
  216. {
  217. menu_destroy( menu );
  218. return PLUGIN_HANDLED;
  219. }
  220.  
  221. static data[64], name[64];
  222. static access, callback;
  223. menu_item_getinfo( menu, item, access, data, 63, name, 63, callback );
  224.  
  225. static motd[256];
  226. formatex( motd, 255, "%s/%s.jpg", PICTURE_DIRECTORY, data );
  227.  
  228. show_motd( plr, motd, data );
  229.  
  230. menu_destroy( menu );
  231.  
  232. return PLUGIN_HANDLED;
  233. }
  234.  
  235. stock chat_print( plr, const message[], { Float, Sql, Result, _ }:... )
  236. {
  237. new msg[192];
  238. format( msg, 191, "^x04[XJ]^x01 " );
  239. vformat( msg[7], 185, message, 3 );
  240.  
  241. static saytext, maxplayers;
  242.  
  243. if( !saytext )
  244. {
  245. saytext = get_user_msgid( "SayText" );
  246. }
  247.  
  248. if( !maxplayers )
  249. {
  250. maxplayers = get_maxplayers( );
  251. }
  252.  
  253. if( plr <= maxplayers && plr )
  254. {
  255. message_begin( MSG_ONE_UNRELIABLE, saytext, {0, 0, 0}, plr );
  256. write_byte( plr );
  257. write_string( msg );
  258. message_end( );
  259. }
  260. else if( !plr )
  261. {
  262. for( new i = 1; i <= maxplayers; i++ )
  263. {
  264. if( !is_user_connected( i ) )
  265. {
  266. continue;
  267. }
  268.  
  269. message_begin( MSG_ONE_UNRELIABLE, saytext, { 0, 0, 0 }, i );
  270. write_byte( i );
  271. write_string( msg );
  272. message_end( );
  273. }
  274. }
  275.  
  276. return 1;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment