using System;
using System.Collections.Generic;
using Server;
using Server.Mobiles;
namespace CustomScripts
{
public class RankSystem
{
private static Dictionary<Serial, int> PlayerRankList;
public static void Configure()
{
EventSink.WorldSave += e => Persistence.Serialize(@"Saves\\Ranks\\Profiles.bin", Save);
EventSink.WorldLoad += () => Persistence.Deserialize(@"Saves\\Ranks\\Profiles.bin", Load);
}
public static void Save(GenericWriter writer)
{
writer.Write(0); // version
writer.Write(PlayerRankList.Count);
foreach (var profile in PlayerRankList)
{
writer.Write(profile.Key);
writer.Write(profile.Value);
}
}
public static void Load(GenericReader reader)
{
reader.ReadInt(); // version
var count = reader.ReadInt();
if (PlayerRankList == null)
{
PlayerRankList = new Dictionary<Serial, int>();
}
while (--count >= 0)
{
PlayerRankList.Add(reader.ReadInt(), reader.ReadInt());
}
}
private static readonly (int threshold, string title, int bonus)[] _BonusInfo =
{
(100, "Solder", 10),
(200, "Major", 20),
(500, "Baron", 30),
(1000, "King", 40),
(2000, "Demi God", 50),
(5000, "God", 60),
(10000, "World Ruler", 70)
};
public static void Initialize()
{
if (PlayerRankList == null)
{
PlayerRankList = new Dictionary<Serial, int>();
}
EventSink.PlayerDeath += EventSink_PlayerDeath;
EventSink.CreatureDeath += EventSink_CreatureDeath;
}
private static void EventSink_PlayerDeath(PlayerDeathEventArgs e)
{
if (e.Mobile.LastKiller != null)
{
CheckAndUpdateRank(e.Mobile.LastKiller);
}
}
private static void EventSink_CreatureDeath(CreatureDeathEventArgs e)
{
if (e.Killer != null)
{
CheckAndUpdateRank(e.Killer);
}
}
private static void CheckAndUpdateRank(Mobile killer)
{
if (killer is PlayerMobile pm)
{
UpdateRank(pm);
}
else if (killer is BaseCreature creature && creature.Controlled && creature.ControlMaster is PlayerMobile master)
{
UpdateRank(master);
}
}
private static void UpdateRank(PlayerMobile pm)
{
if (PlayerRankList.ContainsKey(pm.Serial))
{
PlayerRankList[pm.Serial]++;
}
else
{
PlayerRankList.Add(pm.Serial, 1);
}
TryApplyRankBonus(pm);
}
private static void TryApplyRankBonus(PlayerMobile pm)
{
foreach (var info in _BonusInfo)
{
if (info.threshold == PlayerRankList[pm.Serial])
{
pm.SendMessage(53, $"Congratulations! You have been promoted to {info.title}.");
Console.WriteLine($"Player {pm.Name} has been promoted to {info.title}.");
pm.Title = $"the {info.title}";
pm.RawStr += info.bonus;
pm.RawInt += info.bonus;
pm.RawDex += info.bonus;
}
}
}
}
}