[code:1]
using System;
using System.Collections;
using Server;
using Server.Mobiles;
using Server.Network;
namespace Server.Mobiles
{
public class PointSystem
{
public static void Initialize()
{
EventSink.PlayerDeath += new PlayerDeathEventHandler(EventSink_PlayerDeath);
Server.Commands.Register("ViewScore", AccessLevel.Player, new CommandEventHandler(ViewScore_OnCommand));
}
//this is the initializer for your [viewscore command which will display points overhead
public static void ViewScore_OnCommand(CommandEventArgs args)
{
Mobile m = args.Mobile;
PlayerMobile from = m as PlayerMobile;
if(from != null)
{
string message = "" + from.GetPoints();
from.PublicOverheadMessage( MessageType.Regular, 0x5A7, false, message);
}
}
//this is the code that runs on the [viewscore command
//and this part may seem a little confusing, but the code to determine who has killed who was pretty much ripped straight from the corpse.cs code. Many many thanks to Zippy and everyone else for helping me out with this.
public static void EventSink_PlayerDeath(PlayerDeathEventArgs e)
{
PlayerMobile owner = e.Mobile as PlayerMobile;
PlayerMobile m_Killer = owner;
TimeSpan lastTime = TimeSpan.MaxValue;
for ( int i = 0; i < owner.Aggressors.Count; ++i )
{
AggressorInfo info = (AggressorInfo)owner.Aggressors
;
if ( info.Attacker.Player && (DateTime.Now - info.LastCombatTime) < lastTime )
{
m_Killer = info.Attacker as PlayerMobile;
lastTime = (DateTime.Now - info.LastCombatTime);
}
}
for ( int i = 0; i < owner.Aggressed.Count; ++i )
{
AggressorInfo info = (AggressorInfo)owner.Aggressed;
if ( info.Attacker.Player && (DateTime.Now - info.LastCombatTime) < lastTime )
{
m_Killer = info.Defender as PlayerMobile;
lastTime = (DateTime.Now - info.LastCombatTime);
}
}
//so now you should have who killed stored in m_Killer and who died in owner. Now we decide what needs to be done from there.
if(m_Killer is PlayerMobile)
{
//this part shouldn't really have to be added since m_Killer is of type PlayerMobile, but we had some bugs where guards and monsters were trying to get points from killing players and it was crashing the server... so.. this was a quick fix.
if( m_Killer.Guild != owner.Guild || m_Killer.Guild == null || owner.Guild == null)
{
if( !owner.KillerIsTimed(m_Killer))
{
m_Killer.AddPoint();
m_Killer.SendMessage( 0x5A7, "You have earned 1 point for killing {0}", owner.Name);
KillTimer timerx = new KillTimer ( TimeSpan.FromHours( 4 ), m_Killer, owner);
timerx.Start();
owner.SubPoint();
SimpleTimer timer = new SimpleTimer( TimeSpan.FromSeconds( 4 ), m_Killer, owner );
timer.Start();
}
}
}
}
}
//So, we checked to make sure that the players were not in the same guild (because they should be able to fight themselves whenever they want). And we started a few timers. The timer for owner is set because if you try to display a message to him while he's dieing it won't show up. So you have to delay it a few seconds until after the death animation. You should also notice that this is where the killer gets his 4 hour time limit set.
//and now on to the timers. basically what happens is when the timer is initialized, the killer and owner are passed in, and OnTick is when the timer expires, so that's when you want the message to be displayed to the one who died, and in the killtimer thats where you want the killer to be taken off the list.
public class SimpleTimer : Timer
{
PlayerMobile m_Killer;
PlayerMobile m_Owner;
public SimpleTimer( TimeSpan delay, PlayerMobile killer, PlayerMobile owner ) : base( delay )
{
Priority = TimerPriority.OneSecond;
m_Killer = killer;
m_Owner = owner;
}
protected override void OnTick()
{
if(m_Owner.HasMoreThanMin(1))
{
if( m_Killer != m_Owner)
{
m_Owner.SendMessage( 0x5A7, "You have lost 1 point to {0}", m_Killer.Name);
}
else
{
m_Owner.SendMessage( 0x5A7, "You have lost 1 point for killing yourself..."
}
}
else
{
m_Owner.SendMessage( 0x5A7, "You suck so bad you can't even loose a point..."
}
Stop();
}
}
public class KillTimer : Timer
{
PlayerMobile m_Killer;
PlayerMobile m_Owner;
public KillTimer( TimeSpan delay, PlayerMobile killer, PlayerMobile owner) : base (delay)
{
Priority = TimerPriority.OneSecond;
m_Killer = killer;
m_Owner = owner;
m_Owner.AddKiller(m_Killer);
}
protected override void OnTick()
{
m_Owner.RemoveKiller(m_Killer);
Stop();
}
}
}
[/code:1]