key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
**
**  $Id: Who.java,v 1.12 1997/07/29 08:51:45 subtle Exp subtle $
**
**  Class History
**  
**  Date        Name         Description
**  ---------|------------|-----------------------------------------------
**  19Jul97     druss       created this command
**  21Jul97     subtle      tidied up a bit, fixed some hardcoded room
**                          dependancies, removed the swho style output
**                          for arguments (now uses the table always)
**  22Jul97     subtle      tidied a little more
**
*/

package key.commands;

import key.*;
import key.primitive.*;
import key.util.Trie;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Enumeration;

public class Who extends Command
{
	private static final long serialVersionUID = 967373524804005474L;
	public Who()
	{
		setKey( "who" );
		usage = "[<letters>]";
	}
	
	public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
	{
		String targetClan = "";
		
		if( args.hasMoreTokens() )
		{
			String pName = args.nextToken();
			Object t = Key.instance().getPlayer( pName );
			
			if( t == null )
			{
				ic.sendFeedback( "No players beginning with '" + pName + "' online." );
			}
			else if( t instanceof Atom )
			{
				Player o = (Player)t;
				
				boolean seeThrough = o.permissionCheck( Player.seePrivateInfoAction, false, true );
				
				ic.send( new HeadingParagraph( o.getName() ) );
				
				StringBuffer sb = new StringBuffer();

				{
					String aka =  o.getAka();
					
					if( aka != null && aka.length() > 0 )
					{
						sb.append( "Also known as " );
						sb.append( aka );
						sb.append( ", " );
					}
				}
				
				sb.append( o.getName() );
				sb.append( " is " );
				
				int age = o.getAge();
				if( age != 0 )
				{
					sb.append( "a " );
					sb.append( age );
					sb.append( " year old " );
				}
				
				sb.append( o.maleFemale() );
				sb.append( ".\n" );
				
					//  email addresses
				{
					String ea = o.getEmail().get();
					
					if( ea != null && ( !o.isEmailPrivate() || seeThrough ) )
					{
						sb.append( o.HisHer() );
						sb.append( " email address is: " );
						sb.append( ea );
						sb.append( '\n' );
					}
				}
				
					//  homepage
				{
					String hp = o.getWebpage().get();
					
					if( hp != null )
					{
						sb.append( o.HisHer() );
						sb.append( " homepage is: " );
						sb.append( hp );
						sb.append( '\n' );
					}
				}
				
					//  clan
				{
					Clan c = o.getClan();

					if( c != null )
					{
						sb.append( o.HeShe() );
						sb.append( " is in clan " );
						sb.append( c.getName() );
						sb.append( " [" );
						
						boolean first = true;
						
						for( Enumeration e = c.ranks.elements(); e.hasMoreElements(); )
						{
							Rank r = (Rank) e.nextElement();
							
							if( r.contains( o ) )
							{
								if( first )
								{
									sb.append( ' ' );
									first = false;
								}
								
								sb.append( r.getName() );
								sb.append( ' ' );
							}
						}
						
						sb.append( "]\n" );
					}
					else if( o.isLiberated() )
					{
						sb.append( o.HeShe() );
						sb.append( " doesn't want to be in a clan.\n" );
					}
				}
				
				sb.append( o.HeShe() );
				sb.append( " has been idle for " );
				sb.append( o.getIdle( new DateTime() ).toString() );
				sb.append( '.' );
				
				ic.sendFeedback( sb.toString() );
				ic.sendLine();
			}
			else if( t instanceof Trie )
				displayTable( ic, ((Trie)t).elements() );
		}
		else
			displayTable( ic, Key.instance().players() );
	}
	
	public static final void displayTable( InteractiveConnection ic, Enumeration e )
	{
		StringBuffer sb = new StringBuffer();
		Player scan;

		TableParagraph.Generator table = new TableParagraph.Generator( columns );
		
		DateTime now = new DateTime();

		while( e.hasMoreElements() )
		{
			String rowContents[] = new String[ columns.length ];
			scan = (Player)e.nextElement();

				//  the players name
			rowContents[0] = (String)scan.getName();

				//  set up the players location...
			if( scan.isHiding() )
				rowContents[1] = "[hiding]";
			else
			{
				Room current = scan.getLocation();
				
				if( current == null )
					rowContents[1] = "no-where";
				else if( current == scan.getHome() )
					rowContents[1] = "@home";
				else
				{
					Object o = current.getParent();
					
					if( o != null && !(o instanceof Player) )
						rowContents[1] = current.getName();
				}
			}

				//  the clan
			Clan tmpClan = scan.getClan();
			if( tmpClan != null )
				rowContents[2] = tmpClan.getName();
			else if( scan.isLiberated() )
				rowContents[2] = "no clan please";
			else
				rowContents[2] = "";
			
				//  the idle time
			rowContents[3] = scan.getIdle( now ).toShortString();

			rowContents[4] = scan.getRank();
			
				//  add the row
			table.appendRow( rowContents );
		}
		
		int numPlayers = Key.instance().numberPlayers();
		StringBuffer footer = new StringBuffer();
		footer.append( "There ");
		footer.append( Grammar.isAreCount( numPlayers ) );
		footer.append( " " );
		footer.append( Grammar.personPeople( numPlayers ) );
		footer.append( " online." );
		table.setFooter( footer.toString() );
		
		ic.send( table.getParagraph() );
	}
	
	public static final TableParagraph.Column[] columns = 
	{
		new TableParagraph.Column( "name", Player.MAX_NAME ),
		new TableParagraph.Column( "location", 10 ),
		new TableParagraph.Column( "clan", Clan.MAX_NAME ),
		new TableParagraph.Column( "idle", 5 ),
		new TableParagraph.Column( "rank", 8 )
	};
}