using System;
using Server.Mobiles;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Server;
using Server.Misc;
using Server.Items;
using Server.Gumps;
using Server.Network;
using Server.Accounting;
using Server.Targeting;
using Server.ContextMenus;
using Server.Commands;
using Server.Regions;
namespace Server.Commands
{
public class JailingSystem : Item
{
#region Commands
public static void Initialize()
{
CommandSystem.Register ( "jail", AccessLevel.GameMaster, new CommandEventHandler ( Jail_OnCommand ) );
CommandSystem.Register ( "release", AccessLevel.GameMaster, new CommandEventHandler ( Release_OnCommand ) );
CommandSystem.Register ( "jailtime", AccessLevel.Player, new CommandEventHandler ( JailTime_OnCommand ) );
CommandSystem.Register ( "reason", AccessLevel.Player, new CommandEventHandler ( Reason_OnCommand ) );
}
public static void Jail_OnCommand( CommandEventArgs e )
{
e.Mobile.CloseGump( typeof ( JailGump ) );
e.Mobile.SendGump( new JailGump() );
}
public static void Release_OnCommand( CommandEventArgs e )
{
ArrayList inMates = new ArrayList();
foreach ( Mobile m in m_inMates.Keys )
{
inMates.Add( m );
}
e.Mobile.CloseGump( typeof ( ReleasingGump ) );
e.Mobile.SendGump( new ReleasingGump( inMates ) );
}
public static void JailTime_OnCommand( CommandEventArgs e )
{
if ( IsInMate( e.Mobile ) )
{
TimeSpan RemainingTime = ((m_inMates[e.Mobile]).JailedTime + (m_inMates[e.Mobile]).Sentance) - DateTime.Now;
double RemainingMins = Math.Round( RemainingTime.TotalMinutes );
double RemainingHours = Math.Round( RemainingTime.TotalHours );
e.Mobile.SendMessage(37, "You will be released in: Aprox Minutes: " + RemainingMins.ToString() + ". Aprox Hours: " + RemainingHours.ToString() );
}
else
{
e.Mobile.SendMessage(37, "You are not currently in jail.");
}
}
public static void Reason_OnCommand( CommandEventArgs e )
{
if ( IsInMate( e.Mobile ) )
{
e.Mobile.SendMessage(37, "You were jailed for this reason: " + (m_inMates[e.Mobile]).Reason + ".");
}
else
{
e.Mobile.SendMessage(37, "You are not currently in jail.");
}
}
#endregion
[Constructable]
public JailingSystem()
: base(3796)
{
Name = "Jail Command Stone";
Movable = false;
}
#region Mobile Fields
public class MobileFields
{
private DateTime m_JailedTime;
private TimeSpan m_Sentance;
private Point3D m_Posi;
private Map m_map;
private string m_Reason;
public DateTime JailedTime
{
get{ return m_JailedTime; }
set{ m_JailedTime = value; }
}
public TimeSpan Sentance
{
get{ return m_Sentance; }
set{ m_Sentance = value; }
}
public Point3D Posi
{
get{ return m_Posi; }
set{ m_Posi = value; }
}
public Map map
{
get{ return m_map; }
set{ m_map = value; }
}
public string Reason
{
get{ return m_Reason; }
set{ m_Reason = value; }
}
public MobileFields( DateTime jailedTime, TimeSpan sen, Point3D posi, Map mapie, string reas )
{
m_JailedTime = jailedTime;
m_Sentance = sen;
m_Posi = posi;
m_map = mapie;
m_Reason = reas;
}
}
#endregion
public JailingTimer jTimer;
public static Dictionary<Mobile, MobileFields> m_inMates = new Dictionary<Mobile, MobileFields>();
public static Point3D[] defaultCells = new Point3D[]
{
#region Traditional runUO cells
/*new Point3D(5276,1164,0),
new Point3D(5286,1164,0),
new Point3D(5296,1164,0),
new Point3D(5306,1164,0),
new Point3D(5276,1174,0),
new Point3D(5286,1174,0),
new Point3D(5296,1174,0),
new Point3D(5306,1174,0),
new Point3D(5283,1184,0),
new Point3D(5300,1184,0)*/
#endregion
#region Yew Jail Cells
new Point3D(286,776,0),
new Point3D(280,776,0),
new Point3D(274,776,0),
new Point3D(268,776,0),
new Point3D(262,776,0),
new Point3D(286,766,0),
new Point3D(280,766,0),
new Point3D(274,766,0),
new Point3D(268,766,0),
new Point3D(262,766,0),
new Point3D(286,776,20),
new Point3D(280,776,20),
new Point3D(274,776,20),
new Point3D(268,776,20),
new Point3D(262,776,20),
new Point3D(286,766,20),
new Point3D(280,766,20),
new Point3D(274,766,20),
new Point3D(268,766,20),
new Point3D(262,766,20),
#endregion
};
public static void AddInMate( Mobile from, DateTime TimeJailed, TimeSpan delay, Point3D origPos, Map theMap, string Reason )
{
if ( m_inMates.ContainsKey( from ) )
{
RemoveInMate( from );
}
m_inMates.Add( from, new MobileFields( TimeJailed, delay, origPos, theMap, Reason ));
int rank = 0;
foreach( Mobile key in m_inMates.Keys ) //each Mobile in the Key collection
{
if( key == from )
{
break;
}
rank++;
}
if( rank > ( defaultCells.Length - 1 ) )
{
rank = 0; //or you got a serious problem
}
from.Location = defaultCells[rank];
Timer timer = new JailingTimer( from, delay );
timer.Start();
}
public static void RemoveInMate( Mobile from )
{
if ( m_inMates.ContainsKey( from ) )
{
from.Location = (m_inMates[from]).Posi;//go to original location
m_inMates.Remove( from );
from.SendMessage(68, "You have been released from jail.");
foreach ( Mobile m in World.Mobiles.Values )
{
if ( m.AccessLevel > AccessLevel.Player )
{
m.SendMessage(68, from.RawName + " has been released from jail.");
}
}
}
}
public static void OnTick(Mobile ToBeFree)
{
if( IsInMate( ToBeFree ) )
{
RemoveInMate( ToBeFree );
}
}
public static bool IsInMate( Mobile from )
{
return m_inMates.ContainsKey( from );
}
public static void JailMobile( Mobile offender, TimeSpan sentance, Point3D pos, Map tehMap, string Reason )
{
//When calling JailMobile use "Server.Commands.JailingSystem.JailMobile( mobile, TimeSpan.From<Days / Hours / Minutes / Seconds>( 1.0 ), mobile.Location, mobile.Map, "Reason" );"
AddInMate(offender, DateTime.Now, sentance, pos, tehMap, Reason);
}
public JailingSystem( Serial serial ) : base( serial )
{
}
public override void Serialize( GenericWriter writer )
{
base.Serialize( writer );
writer.Write( (int) 0 ); // version
writer.Write( (int) m_inMates.Count );
foreach( KeyValuePair<Mobile, MobileFields> kvp in m_inMates )
{
TimeSpan timeLeft = (kvp.Value.JailedTime + kvp.Value.Sentance) - DateTime.Now;
writer.Write( (Mobile) kvp.Key ); //Mobile
writer.Write( (DateTime) kvp.Value.JailedTime );
writer.Write( (TimeSpan) timeLeft );
writer.Write( (Point3D) kvp.Value.Posi );
writer.Write( (Map) kvp.Value.map );
writer.Write( (string) kvp.Value.Reason );
}
}
public override void Deserialize( GenericReader reader )
{
base.Deserialize( reader );
int version = reader.ReadInt();
switch( version )
{
case 0:
{
for( int i = 0; i < reader.ReadInt(); i++ )
{
Mobile readMobile = reader.ReadMobile();
m_inMates.Add( readMobile, new MobileFields( reader.ReadDateTime(), reader.ReadTimeSpan(), reader.ReadPoint3D(), reader.ReadMap(), reader.ReadString() ));
jTimer = new JailingTimer( readMobile, (m_inMates[readMobile]).Sentance );
jTimer.Start();
}
break;
}
}
}
}
public class JailingTimer : Timer
{
private Mobile Jailed; //this is accessible in OnTick
public JailingTimer(Mobile jailed, TimeSpan delay)//you take a reference to the mobile jailed
: base( delay )
{
Priority = TimerPriority.FiftyMS;
Jailed = jailed;
}
protected override void OnTick()
{
JailingSystem.OnTick( Jailed );
Stop();
}
}
}