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
{
public class MobileFields
{
private DateTime m_JailedTime;
private DateTime m_ReleaseTime;
private Point3D m_Posi;
public DateTime JailedTime
{
get{ return m_JailedTime; }
set{ m_JailedTime = value; }
}
public DateTime ReleaseTime
{
get{ return m_ReleaseTime; }
set{ m_ReleaseTime = value; }
}
public Point3D Posi
{
get{ return m_Posi; }
set{ m_Posi = value; }
}
public MobileFields( DateTime jailedTime, DateTime releaseTime, Point3D posi )
{
m_JailedTime = jailedTime;
m_ReleaseTime = releaseTime;
m_Posi = posi;
}
}
private Dictionary<Mobile, MobileFields> m_inMates = new Dictionary<Mobile, MobileFields>();
private static Point3D[] defaultCells = new Point3D[]
{
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)
};
public void AddInMate( Mobile from, DateTime TimeJailed, DateTime TimeReleased, Point3D origPos )
{
if ( m_inMates.ContainsKey( from ) )
{
RemoveInMate( from );
}
m_inMates.Add( from, new MobileFields( TimeJailed, TimeReleased, origPos ));
Timer timer = new JailingTimer( from );
timer.Start();
}
public void RemoveInMate( Mobile from )
{
if( m_inMates.ContainsKey( from ) )
{
m_inMates.Remove( from );
}
}
public bool OnTick(Mobile ToBeFree)
{
if( IsInMate( ToBeFree ) )
{
if( ( m_inMates[ToBeFree] ).ReleaseTime < DateTime.Now )
{
RemoveInMate( ToBeFree );
return true;
}
return false;
}
return false;
}
public static bool IsInMate( Mobile from )
{
return m_inMates.ContainsKey( from );
}
public void JailMobile( Mobile offender, TimeSpan sentance, Point3D pos )
{
//When calling JailMobile use "JailMobile( mobile, Timespan, (mobile.Location, mobile.Map) );"
DateTime jailTime = DateTime.Now;
DateTime releasedTime = (jailTime + sentance);
AddInMate(offender, jailTime, releasedTime, pos);
//offender.Location = cells[m_inMates.Position];
}
public void JailMobileWithTarget( Mobile offender, DateTime released, string reason, string jailedBy )
{
}
public JailingSystem( Serial serial ) : base( serial )
{
}
public override void Serialize( GenericWriter writer )
{
base.Serialize( writer );
if( m_inMates != null )
{
writer.Write( true );
writer.Write( (int) m_inMates.Count );
foreach( KeyValuePair<Mobile, MobileFields> kvp in m_inMates )
{
writer.Write( kvp.Key.Serial ); // Notice how we use Serial
writer.Write( (Mobile) kvp.Key ); //Mobile
writer.Write( (DateTime) kvp.Value.JailedTime );
writer.Write( (DateTime) kvp.Value.ReleaseTime );
writer.Write( (Point3D) kvp.Value.Posi );
}
}
else
{
writer.Write( false );
}
}
public override void Deserialize( GenericReader reader )
{
base.Deserialize( reader );
int count = reader.ReadInt();
m_inMates = new Dictionary<Mobile, MobileFields>(); //no use, until it's already declare, and declared as empty yet
for( int i = 0; i < count; i++ )
{
m_inMates.Add( reader.ReadMobile(), new Data( reader.ReadDateTime(), reader.ReadDateTime(), reader.ReadPoint3D() ));
}
}
}
public class JailingTimer : Timer
{
private Mobile Jailed;//this is accessible in OnTick
public JailingTimer(Mobile jailed)//you take a reference to the mobile jailed
: base( TimeSpan.FromSeconds( 5.0 ), TimeSpan.FromSeconds( 5.0 ) )
{
Priority = TimerPriority.FiftyMS;
Jailed = jailed; //assign the value to the private variable Jailed (which is accessible in OnTick)
}
protected override void OnTick() //every 5 seconds executes code in {}
{
if ( JailingSystem.OnTick( Jailed ) ) //if the mobile is free, timer stops
{
Stop();
}
}
}
}