IW4M-Admin/SharedLibraryCore/Events/EventAPI.cs

88 lines
2.6 KiB
C#
Raw Normal View History

2022-01-26 11:32:16 -05:00
using System.Collections.Concurrent;
using System.Collections.Generic;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore.Dtos;
2018-03-09 03:01:12 -05:00
namespace SharedLibraryCore.Events
2018-03-09 03:01:12 -05:00
{
public class EventApi
2018-03-09 03:01:12 -05:00
{
2022-01-26 11:32:16 -05:00
private const int MaxEvents = 25;
private static readonly ConcurrentQueue<EventInfo> RecentEvents = new ConcurrentQueue<EventInfo>();
public static IEnumerable<EventInfo> GetEvents(bool shouldConsume)
2018-03-09 03:01:12 -05:00
{
var eventList = RecentEvents.ToArray();
// clear queue if events should be consumed
if (shouldConsume)
{
RecentEvents.Clear();
}
return eventList;
}
2018-03-09 03:01:12 -05:00
public static void OnGameEvent(GameEvent gameEvent)
2018-03-09 03:01:12 -05:00
{
var E = gameEvent;
// don't want to clog up the api with unknown events
if (E.Type == GameEvent.EventType.Unknown)
2022-01-26 11:32:16 -05:00
{
return;
2022-01-26 11:32:16 -05:00
}
2018-03-09 03:01:12 -05:00
2022-01-26 11:32:16 -05:00
var apiEvent = new EventInfo
{
ExtraInfo = E.Extra?.ToString() ?? E.Data,
2022-01-26 11:32:16 -05:00
GameInfo = new EntityInfo
{
Name = E.Owner.GameName.ToString(),
Id = (int)E.Owner.GameName
},
2022-01-26 11:32:16 -05:00
OwnerEntity = new EntityInfo
2018-03-09 03:01:12 -05:00
{
Name = E.Owner.Hostname,
Id = E.Owner.EndPoint
},
2022-01-26 11:32:16 -05:00
OriginEntity = E.Origin == null
? null
: new EntityInfo
{
Id = E.Origin.ClientId,
Name = E.Origin.Name
},
TargetEntity = E.Target == null
? null
: new EntityInfo
{
Id = E.Target.ClientId,
Name = E.Target.Name
},
EventType = new EntityInfo
{
Id = (int)E.Type,
Name = E.Type.ToString()
},
EventTime = E.Time
};
2018-03-09 03:01:12 -05:00
// add the new event to the list
AddNewEvent(apiEvent);
}
2018-03-09 03:01:12 -05:00
/// <summary>
2022-01-26 11:32:16 -05:00
/// Adds event to the list and removes first added if reached max capacity
/// </summary>
/// <param name="info">EventInfo to add</param>
private static void AddNewEvent(EventInfo info)
{
// remove the first added event
if (RecentEvents.Count >= MaxEvents)
2022-01-26 11:32:16 -05:00
{
2018-09-23 20:45:54 -04:00
RecentEvents.TryDequeue(out _);
2022-01-26 11:32:16 -05:00
}
2018-03-09 03:01:12 -05:00
RecentEvents.Enqueue(info);
2018-03-09 03:01:12 -05:00
}
}
2022-01-26 11:32:16 -05:00
}