using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Server.Accounting;
using Server.Items;
using Server.Mobiles;
using Server.Multis;
namespace Server.Commands
{
public class CountGoldPerAccount
{
public static void Initialize()
{
CommandSystem.Register( "CountGoldPerAccount", AccessLevel.Administrator, new CommandEventHandler( CountGoldPerAccount_OnCommand ) );
}
[Usage( "UnusedAccountRemove [<name> <propertyName>]" )]
[Description( "Counts the gold owned by an account and writes to file or counts a property of a certain item." )]
public static void CountGoldPerAccount_OnCommand( CommandEventArgs arg )
{
if ( arg.Length == 0 )
CountGold( arg.Mobile );
else if ( arg.Length > 1 )
{
Type t = ScriptCompiler.FindTypeByName( arg.GetString( 0 ) );
if ( t == null )
{
arg.Mobile.SendMessage( "No type with such a name was found." );
return;
}
PropertyInfo[] allProps = t.GetProperties( BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public );
PropertyInfo thisProp = null;
string propName = arg.GetString( 1 );
for ( int j = 0; thisProp == null && j < allProps.Length; ++j )
{
if ( Insensitive.Equals( propName, allProps[j].Name ) )
thisProp = allProps[j];
}
if ( thisProp == null )
{
arg.Mobile.SendMessage( "Property not found: {0}", propName );
return;
}
else
{
CountInstances( arg.Mobile, t, thisProp );
}
}
else
arg.Mobile.SendMessage( "You need to supply a type and a property." );
}
public static void CountInstances( Mobile from, Type t, PropertyInfo prop )
{
if ( DoesInherit( t, typeof( Item ) ) )
{
CountItemInstances( from, t, prop );
}
/*else if ( DoesInherit( t, typeof( Mobile ) ) )
{
from.SendMessage( "That type is not supported yet." ); //Don't really see any use for that
}*/
else
from.SendMessage( "That type is not supported!" );
}
public static void CountItemInstances( Mobile from, Type t, PropertyInfo prop )
{
from.SendMessage( "Counting. Drink tea and wait..." );
List<Item> itemList = new List<Item>( World.Items.Values );
object parent;
PlayerMobile master = null;
BaseHouse house = null;
IAccount account;
Hashtable goldOnAccountTable = new Hashtable();
uint value = 0;
uint totalValue = 0;
foreach ( Item item in itemList )
{
if ( item.GetType().IsAssignableFrom( t ) )
{
if ( item.Deleted )
continue;
try{value = System.Convert.ToUInt32( prop.GetValue( item, null ) );}
catch(Exception e){from.SendMessage( "You just crashed the shard my friend. Maybe not... Try harder!" ); Console.WriteLine( e ); continue;}
if ( value == 0 )
continue;
else
totalValue += value;
parent = item.RootParent;
if ( parent is Mobile )
{
if ( parent is PlayerVendor )
master = ((PlayerVendor)parent).Owner as PlayerMobile;
else if ( parent is BaseCreature )
master = ((BaseCreature)parent).ControlMaster as PlayerMobile;
else
master = parent as PlayerMobile;
}
else if ( parent != null ) // Containers
{
if ( ((Item)parent).IsSecure || ((Item)parent).IsLockedDown )
house = BaseHouse.FindHouseAt( parent as Item );
if ( house != null )
master = house.Owner as PlayerMobile;
}
else
{
if ( item.IsSecure || item.IsLockedDown )
house = BaseHouse.FindHouseAt( item );
if ( house != null )
master = house.Owner as PlayerMobile;
if ( master == null && item is IMount )
master = ((IMount)item).Rider;
}
if ( master != null )
{
account = master.Account;
if ( account != null )
{
if ( goldOnAccountTable.Contains( account ) )
goldOnAccountTable[account] = (uint)goldOnAccountTable[account] + (uint)value;
else
goldOnAccountTable[account] = (uint)value;
}
else
from.SendMessage( master.ToString() + " has no account!" );
}
master = null;
house = null;
}
}
System.IO.StreamWriter tw = new System.IO.StreamWriter( t.Name + prop.Name + ".txt", true );
tw.WriteLine( t.Name + "s' " + prop.Name + " counted on " + DateTime.Now.ToString() );
IDictionaryEnumerator en = goldOnAccountTable.GetEnumerator();
List<GoldAccountEntry> gaList = new List<GoldAccountEntry>();
GoldAccountEntry gae;
while (en.MoveNext())
{
gae = new GoldAccountEntry();
gae.Account = (IAccount)(en.Key);
gae.Value = (uint)(en.Value);
gaList.Add( gae );
}
tw.WriteLine( "Total " + t.Name + " : " + totalValue.ToString() );
tw.WriteLine( t.Name + " on accounts by name:" );
gaList.Sort();
for ( int i = 0; i < gaList.Count; ++i )
tw.WriteLine( gaList[i].Account.ToString() + " : " + gaList[i].Value.ToString() );
tw.Close();
from.SendMessage( "Saved log to " + t.Name + prop.Name + ".txt..." );
}
public static bool DoesInherit( Type type, Type child ) //DoesInherit here returns true if equal too
{
while( type != null )
{
if ( type == child )
return true;
else
type = type.BaseType;
}
return false;
}
public static void CountGold( Mobile from )
{
from.SendMessage( "Counting gold. Drink tea and wait..." );
object parent;
PlayerMobile master = null;
BaseHouse house = null;
PlayerVendor vendor;
int value = 0;
IAccount account;
uint goldHeldByNPCs = 0;
uint goldOnFloor = 0;
uint goldInContainers = 0;
Hashtable goldOnAccountTable = new Hashtable();
List<Item> itemList = new List<Item>( World.Items.Values );
List<Mobile> mobileList = new List<Mobile>( World.Mobiles.Values );
foreach ( Item item in itemList )
{
if ( item.Deleted )
continue;
if ( item is Gold )
value = item.Amount;
else if ( item is BankCheck)
value = ((BankCheck)item).Worth;
else
continue;
if ( value == 0 )
continue;
parent = item.RootParent;
if ( parent is Mobile )
{
if ( parent is PlayerVendor )
master = ((PlayerVendor)parent).Owner as PlayerMobile;
else if ( parent is BaseCreature )
master = ((BaseCreature)parent).ControlMaster as PlayerMobile;
else
master = parent as PlayerMobile;
if ( master == null )
goldHeldByNPCs += (uint)value;
}
else if ( parent != null ) // Containers
{
if ( ((Item)parent).IsSecure || ((Item)parent).IsLockedDown )
house = BaseHouse.FindHouseAt( parent as Item );
if ( house != null )
master = house.Owner as PlayerMobile;
if ( master == null )
goldInContainers += (uint)value;
}
else
{
if ( item.IsSecure || item.IsLockedDown )
house = BaseHouse.FindHouseAt( item );
if ( house != null )
master = house.Owner as PlayerMobile;
if ( master == null )
goldOnFloor += (uint)value;
}
if ( master != null )
{
account = master.Account;
if ( account != null )
{
if ( goldOnAccountTable.Contains( account ) )
goldOnAccountTable[account] = (uint)goldOnAccountTable[account] + (uint)value;
else
goldOnAccountTable[account] = (uint)value;
}
else
from.SendMessage( master.ToString() + " has no account!" );
}
parent = null;
master = null;
house = null;
}
uint goldOnPlayervendors = 0;
foreach ( Mobile m in mobileList )
{
if ( !(m is PlayerVendor) )
continue;
vendor = (PlayerVendor)m;
master = vendor.Owner as PlayerMobile;
if ( master != null )
{
account = master.Account;
if ( account != null )
{
goldOnPlayervendors += (uint)(vendor.BankAccount + vendor.HoldGold);
if ( goldOnAccountTable.Contains( account ) )
goldOnAccountTable[account] = (uint)goldOnAccountTable[account] + (uint)(vendor.BankAccount + vendor.HoldGold);
else
goldOnAccountTable[account] = (uint)value;
}
else
from.SendMessage( master.ToString() + " has no account!" );
}
}
uint totalGold = goldHeldByNPCs + goldOnFloor + goldInContainers;
System.IO.StreamWriter tw = new System.IO.StreamWriter( "GoldByAccount.txt", true );
tw.WriteLine( "Gold counted on " + DateTime.Now.ToString() );
tw.WriteLine( "Gold on NPCs: " + goldHeldByNPCs.ToString() );
tw.WriteLine( "Gold on playervendors: " + goldOnPlayervendors.ToString() );
tw.WriteLine( "Gold in containers: " + goldInContainers.ToString() );
tw.WriteLine( "Gold on the floor: " + goldOnFloor.ToString() );
IDictionaryEnumerator en = goldOnAccountTable.GetEnumerator();
List<GoldAccountEntry> gaList = new List<GoldAccountEntry>();
GoldAccountEntry gae;
while (en.MoveNext())
{
gae = new GoldAccountEntry();
gae.Account = (IAccount)(en.Key);
gae.Value = (uint)(en.Value);
gaList.Add( gae );
totalGold += gae.Value;
}
tw.WriteLine( "Total gold: " + totalGold.ToString() );
tw.WriteLine( "Gold on accounts by name:" );
gaList.Sort();
for ( int i = 0; i < gaList.Count; ++i )
tw.WriteLine( gaList[i].Account.ToString() + " : " + gaList[i].Value.ToString() );
tw.Close();
from.SendMessage( "Saved log to GoldByAccount.txt..." );
}
private class GoldAccountEntry : IComparable
{
public IAccount Account;
public uint Value;
public int CompareTo(object o)
{
GoldAccountEntry g = (GoldAccountEntry)o;
int i = Value.CompareTo( g.Value );
if ( i != 0 )
return -i;
return Account.Username.CompareTo( g.Account.Username );
}
}
}
}