Basic event scheduler (#83)
This commit is contained in:
parent
bc50bea867
commit
ec829316c9
2 changed files with 125 additions and 0 deletions
36
Projects/Scripts/Engines/Events/BroadcastEvent.cs
Normal file
36
Projects/Scripts/Engines/Events/BroadcastEvent.cs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
89
Projects/Scripts/Engines/Events/EventScheduler.cs
Normal file
89
Projects/Scripts/Engines/Events/EventScheduler.cs
Normal file
|
|
@ -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<EventScheduleEntry> _schedule = new List<EventScheduleEntry>();
|
||||
|
||||
public static EventScheduler Instance => _instance ??= new EventScheduler();
|
||||
public static List<IEvent> AvailableEvents { get; } = new List<IEvent>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue