diff --git a/Projects/Scripts/Engines/Events/BroadcastEvent.cs b/Projects/Scripts/Engines/Events/BroadcastEvent.cs new file mode 100644 index 000000000..e1348c69e --- /dev/null +++ b/Projects/Scripts/Engines/Events/BroadcastEvent.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server.Engines.Events +{ + public class BroadcastEvent : IEvent + { + private int _hue = 0; + private string _text = ""; + public static void Initialize() + { + /* + EventScheduler.Instance.ScheduleEvent(new BroadcastEvent(22, "Test Message Please Ignore 2min"), 0, 0, TimeSpan.FromMinutes(2.0)); + EventScheduler.Instance.ScheduleEvent(new BroadcastEvent(33, "Test Message Please Ignore 3min"), 0, 0, TimeSpan.FromMinutes(3.0)); + EventScheduler.Instance.ScheduleEvent(new BroadcastEvent(44, "Test Message Please Ignore 4min"), 0, 0, TimeSpan.FromMinutes(4.0)); + */ + } + + public BroadcastEvent(int hue, string text) + { + _hue = hue; + _text = text; + } + + public override string ToString() + { + return $"Broadcast: {_text}"; + } + + public void OnEventScheduled() + { + World.Broadcast(_hue, true, _text); + } + } +} diff --git a/Projects/Scripts/Engines/Events/EventScheduler.cs b/Projects/Scripts/Engines/Events/EventScheduler.cs new file mode 100644 index 000000000..127b82ce0 --- /dev/null +++ b/Projects/Scripts/Engines/Events/EventScheduler.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server.Engines.Events +{ + public interface IEvent + { + void OnEventScheduled(); + } + + public class EventScheduleEntry + { + public DateTime NextOccurrence { get; private set; } + public TimeSpan Interval { get; private set; } + private TimeSpan _offset; + private IEvent _event; + + public EventScheduleEntry(IEvent e, DateTime firstSpawn, TimeSpan interval, TimeSpan offset) + { + _offset = offset; + _event = e; + Interval = interval; + NextOccurrence = firstSpawn; + } + + public void Occur() + { + NextOccurrence += Interval; + + _event?.OnEventScheduled(); + } + + public override string ToString() + { + return _event?.ToString(); + } + } + public class EventScheduler : Timer + { + private static EventScheduler _instance; + private List _schedule = new List(); + + public static EventScheduler Instance => _instance ??= new EventScheduler(); + public static List AvailableEvents { get; } = new List(); + + private EventScheduler() : base(TimeSpan.Zero, TimeSpan.FromSeconds(1.0)) + { + } + + public static void Initialize() + { + Instance.Start(); + } + + public void ScheduleEvent(IEvent e, int hour, int min) + { + ScheduleEvent(e, hour, min, TimeSpan.FromDays(1.0)); + } + + public void ScheduleEvent(IEvent e, int hour, int min, TimeSpan interval) + { + DateTime now = DateTime.UtcNow; + DateTime firstRun = new DateTime(now.Year, now.Month, now.Day, hour, min, 0); + + while (now > firstRun) + firstRun += interval; + + ScheduleEvent(new EventScheduleEntry(e, firstRun, interval, TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(min))); + } + + public void ScheduleEvent(EventScheduleEntry e) + { + _schedule.Add(e); + } + + public void RemoveEvent(EventScheduleEntry entry) + { + _schedule.Remove(entry); + } + + protected override void OnTick() + { + foreach (EventScheduleEntry entry in _schedule) + if (entry.NextOccurrence <= DateTime.UtcNow) + entry.Occur(); + } + } +}