/****************************************************************************
 *    $Id:    plugin_ejl_listmaps.sma      v1.0 02/13/02 $ - October 26, 2002 
 *     Originally  $Id: plugin_listmaps.sma,v 1.0 05/04/01 PetitMorte(?) $
 *	 Re-written for easier use by Eric Lidman      ejlmozart@hotmail.com
 *	 Upgrade: http://lidmanmusic.com/cs/plugins.html
 *
 *    Fixed: 10/26/02  Mistake in code that reported the same map twice.
 *
 *    Please note that file_access_read must be changed to 1 in the adminmod.cfg  
 *
 *	This plugin allows users to search thru all the maps on your server.
 *	 You will need to create a file called "tmaps.txt" and place it in your
 *	 Mod directory.  This file should contain a list of all the maps you
 *	 have available to play.  The easiest way to create this file is to copy
 *	 your maps.ini file, rename the copy to tmaps.txt. Add 8 blank lines to
 *	 the end of tmaps.txt and save it in your mod directory.
 *
 *    Commands:
 *     admin_maplist <number>   returns a list of 9 maps starting from <number> 
 *     admin_search <target>   returns up to 9 maps containing the search target
 *     admin_map_help   instructs on the use of ejl_listmaps
 *
 *    Maps are listed in the chat to the player who requests them. If the chat
 *     is moving too fast. The player can still see the list in the console as
 *     the chat moves up.  
 *
 *    All the above commands will work as if working under plugin_dio_lazy 
 *     (with less typing), just remove the "admin_"  from them. Also, the
 *     commands can be issued in that shortcut form from the chat mode.
 *     For example:     say: maplist <number>
 *   
 *    In the original plugin_listmaps, if a search returned too many results,
 *     it flooded and crashed the server. Eric's server had 455 maps on it
 *     available for play. Eric fixed this by limitimg the number of search
 *     results returned to 9. 
 *
 *    Eric Lidman thanks the above original writer of plugin_listmaps
 *     for the essential guts of the code, Luke Sanky for code from
 *     plugin_sank_ai, and dio for code from plugin_dio_lazy.
 *
 ***************************************************************************/
 
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.51";
 
public admin_maplist(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new From;
	convert_string(HLCommand,Command,MAX_NAME_LENGTH);
	convert_string(HLData,Data,MAX_NAME_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	/* how many maps are in list? */
	new TotalMaps = filesize("tmaps.txt", 1);
        new TotalMaps8 = TotalMaps -8;
 
	/* Make sure From is acceptable. If it isn't, make it so. */
	if (strlen(Data)==0) {
		From = 1;
	}
	From = strtonum(Data);
 
	if( From >= TotalMaps ) {
		From = TotalMaps - 8;
	}
	if( From < 1 ) {
		From = 1;
	}
 
	new From9 = (From + 8);
	if (From9 > TotalMaps8) {
		From9 = TotalMaps8;
	}
	// setup a place for each map result to go
        new mapcount1 = 1;
        new mapword1[100];
        new mapword2[100];
        new mapword3[100];
        new mapword4[100];
        new mapword5[100];
        new mapword6[100];
        new mapword7[100];
        new mapword8[100];
        new mapword9[100];
 
	snprintf(Text, MAX_TEXT_LENGTH, "Listing maps %i - %i of %i.    Say: search <part of mapname>   to find a map.", From, From9, TotalMaps8);
        messageex (User, Text, print_chat);
	new i = From;
	new sLine[MAX_NAME_LENGTH];
	while (i <= From9) {
		readfile("tmaps.txt", sLine, i, 100);
               	if ( mapcount1 == 1 ){
                	strcpy(mapword1, sLine, 100);
		}
		else if ( mapcount1 == 2 ){
                	strcpy(mapword2, sLine, 100);
		}
 		else if ( mapcount1 == 3 ){
    			strcpy(mapword3, sLine, 100);
 		}
 		else if ( mapcount1 == 4 ){
    			strcpy(mapword4, sLine, 100);
 		}
 		else if ( mapcount1 == 5 ){
    			strcpy(mapword5, sLine, 100);
 		}
 		else if ( mapcount1 == 6 ){
    			strcpy(mapword6, sLine, 100);
 		}
 		else if ( mapcount1 == 7 ){
    			strcpy(mapword7, sLine, 100);
 		}
 		else if ( mapcount1 == 8 ){
    			strcpy(mapword8, sLine, 100);
 		}
 		else if ( mapcount1 == 9 ){
    			strcpy(mapword9, sLine, 100);
 		}
		mapcount1++;
		i++;
	}
	// print out the results in the chat to the player who requested (User) only
        snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword1, mapword2, mapword3);
        messageex (User, Text, print_chat);
        snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword4, mapword5, mapword6);
        messageex (User, Text, print_chat);
        snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword7, mapword8, mapword9);
        messageex (User, Text, print_chat);
	return PLUGIN_HANDLED;
}
 
public admin_search(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	convert_string(HLCommand,Command,MAX_NAME_LENGTH);
	convert_string(HLData,Data,MAX_NAME_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	/* how many maps are in list? */
	new TotalMaps = filesize("tmaps.txt", 1);
        new TotalMaps8 = TotalMaps -8;
        new mapcount = 1;
        new mapcount1 = 1;
        new mapword1[100];
        new mapword2[100];
        new mapword3[100];
        new mapword4[100];
        new mapword5[100];
        new mapword6[100];
        new mapword7[100];
        new mapword8[100];
        new mapword9[100];
	// if search was said, but nothing after then we say this
	if (strlen(Data)==0){
		snprintf(Text, MAX_TEXT_LENGTH, "Add a search target.  Such as ---  Say: search jeep.  We've got %i maps to play.  ", TotalMaps8);
	        messageex (User, Text, print_chat);
	// i forget why i needed this, but it was in the original listmaps and it doesnt seem to hurt anything
	} else if (strlen(Data)==1){
		new i = 1;
		new sLine[100];
		snprintf(Text, MAX_TEXT_LENGTH, "Add a search target.  Such as ---  Say: search jeep.  No ()");
	        messageex (User, Text, print_chat);
                selfmessage (Text);
		while (i <= TotalMaps8) {
			readfile("tmaps.txt", sLine, i, 100);
			if (strncasecmp(sLine, Data, 1) == 0) {
                  		if ( mapcount > 4 ) {
					return PLUGIN_HANDLED;
                        	}
				mapcount++; 
      				messageex (User, sLine, print_chat);
             	  		selfmessage (sLine);
			}
			i++;
		}
	// Here is where we search for maps and return the search results in the chat
	} else {
	 	new i = 1;
		new sLine[100];
		// header message
		snprintf(Text, MAX_TEXT_LENGTH, "Maximum of 9 search results (first ones only).  Not here?  Try another search target. No ()");
	       	messageex (User, Text, print_chat);
               	selfmessage (Text);
		// loop to find maps (limit 9 finds)
		while (i <= TotalMaps8) {
			readfile("tmaps.txt", sLine, i, 100);
			if (strcasestr(sLine, Data) != -1) {
                       		if ( mapcount1 > 9 ) {
               	        		snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword1, mapword2, mapword3);
	                      		messageex (User, Text, print_chat);
                           		snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword4, mapword5, mapword6);
	                	        messageex (User, Text, print_chat);
                        		snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword7, mapword8, mapword9);
	                       		messageex (User, Text, print_chat);
					return PLUGIN_HANDLED;
					}
                       	 	if ( mapcount1 == 1 ){
                       			strcpy(mapword1, sLine, 100);
				}
				else if ( mapcount1 == 2 ){
                       			strcpy(mapword2, sLine, 100);
				}
 				else if ( mapcount1 == 3 ){
    					strcpy(mapword3, sLine, 100);
 				}
 				else if ( mapcount1 == 4 ){
    					strcpy(mapword4, sLine, 100);
 				}
 				else if ( mapcount1 == 5 ){
    					strcpy(mapword5, sLine, 100);
 				}
 				else if ( mapcount1 == 6 ){
    					strcpy(mapword6, sLine, 100);
 				}
 				else if ( mapcount1 == 7 ){
    					strcpy(mapword7, sLine, 100);
 				}
 				else if ( mapcount1 == 8 ){
    					strcpy(mapword8, sLine, 100);
 				}
 				else if ( mapcount1 == 9 ){
    					strcpy(mapword9, sLine, 100);
 				}
				mapcount1++;
			}
			i++;
		}
               	snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword1, mapword2, mapword3);
                messageex (User, Text, print_chat);
                snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword4, mapword5, mapword6);
                messageex (User, Text, print_chat);
                snprintf(Text, MAX_TEXT_LENGTH, "%s        %s        %s", mapword7, mapword8, mapword9);
                messageex (User, Text, print_chat);
		}
	return PLUGIN_HANDLED;
}
 
public admin_map_help(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	convert_string(HLCommand,Command,MAX_NAME_LENGTH);
	convert_string(HLData,Data,MAX_NAME_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	/* how many maps are in list? */
	new TotalMaps = filesize("tmaps.txt", 1);
     	new TotalMaps8 = TotalMaps -8;
 
	snprintf(Text, MAX_TEXT_LENGTH, "We have %i maps on the server.", TotalMaps8);
        selfmessage (Text);      
        selfmessage (" Say: maplist <number>           (no <>) to get a list of 9 maps starting from <number>.");
        selfmessage (" Say: search <part of mapname>   (no <>) to get a list of 9 maps matching the search.");
        messageex (User, Text, print_chat);      
        messageex (User, " Say: maplist <number>           (no <>) to get a list of 9 maps starting from <number>.", print_chat);
        messageex (User, " Say: search <part of mapname>   (no <>) to get a list of 9 maps matching the search.", print_chat);
	return PLUGIN_HANDLED;
}
 
////////////////////////////////////////////////////////////////////////
//    
//     Auto responds to people incorrectly using the command with
//     help from code from plugin_sank_ai    and handles the lazy 
//     commands from the chat thanks to code from plugin_dio_lazy. 
//
////////////////////////////////////////////////////////////////////////
 
public HandleSay(HLCommand, HLData, HLUserName, UserIndex){
	new Command[MAX_COMMAND_LENGTH];
	new Speech[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
 
	/* Ignore the console */
	if (UserIndex < 1) 
		return PLUGIN_CONTINUE;
 
	convert_string(HLCommand, Command, MAX_COMMAND_LENGTH);
	convert_string(HLData, Speech, MAX_DATA_LENGTH);
	convert_string(HLUserName, User, MAX_NAME_LENGTH);
	strstripquotes(Speech);
 
	if ( (strcasestr(Speech, "search") != -1) && ((strcasestr(Speech, "<") != -1) || (strcasestr(Speech, "mapname") != -1) || (strcasestr(Speech, "part of") != -1)) ){
		snprintf(Text, MAX_TEXT_LENGTH, "No <> around <part of mapname> and not literally <part of mapname>, %s", User);
	        messageex (User, Text, print_chat);
                selfmessage (Text);
		new i;
 		new iMaxPlayers = maxplayercount();
  		new Name[MAX_NAME_LENGTH];
		for (i = 1; i <= iMaxPlayers; i++) {
      			if (playerinfo(i,Name,MAX_NAME_LENGTH) == 1) {
        			execclient(Name, "speak ^"idiot^"");
      				}
			}
               	return PLUGIN_HANDLED;
		}
	else if ( (strcasestr(Speech, "maplist") != -1) && ((strcasestr(Speech, "<") != -1) || (strcasestr(Speech, "n") != -1)) ){
		snprintf(Text, MAX_TEXT_LENGTH, "No <> around the number and not literally <number>, %s", User);
	        messageex (User, Text, print_chat);
                selfmessage (Text);
		new i;
 		new iMaxPlayers = maxplayercount();
  		new Name[MAX_NAME_LENGTH];
		for (i = 1; i <= iMaxPlayers; i++) {
      			if (playerinfo(i,Name,MAX_NAME_LENGTH) == 1) {
        			execclient(Name, "speak ^"idiot^"");
      				}
			}
                return PLUGIN_HANDLED;
		}
 
// this is from plugin_dio_lazy. 
	else if (strmatch(Speech, "map_help", 8)==1  ||	strmatch(Speech, "maplist", 7)==1 || strmatch(Speech, "search", 6)==1 ){
		strbreak(Speech, Command, Speech, MAX_TEXT_LENGTH);
		snprintf(Command, MAX_DATA_LENGTH, "admin_%s", Command);
		plugin_exec( Command, Speech);	
		return PLUGIN_HANDLED;
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_init() {
	plugin_registerinfo("List All Maps Plugin","Allows user to list maps on server.",STRING_VERSION);
	plugin_registercmd("admin_maplist","admin_maplist",ACCESS_ALL,"admin_maplist <number>: Lists available maps on the server in groups of 9");
	plugin_registercmd("admin_search","admin_search",ACCESS_ALL,"admin_search <target>: Lists available maps with target in their name");
	plugin_registercmd("admin_map_help","admin_map_help",ACCESS_ALL,"admin_map_help : Get instruction in the use of the map listing tool.");
	plugin_registercmd("maplist","do_lazy",ACCESS_ALL,"maplist : see admin_maplist.");
	plugin_registercmd("search","do_lazy",ACCESS_ALL,"search : see admin_search.");
	plugin_registercmd("map_help","do_lazy",ACCESS_ALL,"map_help : see admin_map_help.");
	plugin_registercmd("say", "HandleSay", ACCESS_ALL);
 
	return PLUGIN_CONTINUE;	
}
 
//////////////////////////////////////////////////////////////////
//
// Stolen: lock, stock, and barrel from plugin_dio_lazy  --thanks 
//
//////////////////////////////////////////////////////////////////
 
public do_lazy(HLCommand,HLData,HLUserName,UserIndex) {
 
	new Command[MAX_COMMAND_LENGTH];
	new Speech[MAX_DATA_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Speech,MAX_DATA_LENGTH);
 
	snprintf(Command, MAX_DATA_LENGTH, "admin_%s", Command);
	plugin_exec(Command, Speech);
 
	return PLUGIN_HANDLED;
}