2018-04-08 14:48:40 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-09-12 20:53:11 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
namespace SharedLibraryCore.Events
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public class EventApi
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
const int MaxEvents = 100;
|
|
|
|
|
static Queue<EventInfo> RecentEvents = new Queue<EventInfo>();
|
2018-06-16 22:11:25 -04:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public static IEnumerable<EventInfo> GetEvents(bool shouldConsume)
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-06-16 22:11:25 -04:00
|
|
|
|
var eventList = RecentEvents.ToArray();
|
2018-04-08 14:48:40 -04:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// clear queue if events should be consumed
|
|
|
|
|
if (shouldConsume)
|
|
|
|
|
{
|
|
|
|
|
RecentEvents.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return eventList;
|
|
|
|
|
}
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2018-09-12 20:53:11 -04:00
|
|
|
|
private static async Task SaveChangeHistory(GameEvent e)
|
|
|
|
|
{
|
|
|
|
|
EFChangeHistory change = null;
|
|
|
|
|
|
|
|
|
|
switch (e.Type)
|
|
|
|
|
{
|
|
|
|
|
case GameEvent.EventType.Unknown:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Start:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Stop:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Connect:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Join:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Quit:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Disconnect:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.MapEnd:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.MapChange:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Say:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Warn:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Report:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Flag:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Unflag:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Kick:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.TempBan:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Ban:
|
|
|
|
|
change = new EFChangeHistory()
|
|
|
|
|
{
|
|
|
|
|
OriginEntityId = e.Origin.ClientId,
|
|
|
|
|
TargetEntityId = e.Target.ClientId,
|
|
|
|
|
TypeOfChange = EFChangeHistory.ChangeType.Ban,
|
|
|
|
|
Comment = e.Data
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Command:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.ChangePermission:
|
|
|
|
|
change = new EFChangeHistory()
|
|
|
|
|
{
|
|
|
|
|
OriginEntityId = e.Origin.ClientId,
|
|
|
|
|
TargetEntityId = e.Target.ClientId,
|
|
|
|
|
TypeOfChange = EFChangeHistory.ChangeType.Permission,
|
|
|
|
|
PreviousValue = ((Change)e.Extra).PreviousValue,
|
|
|
|
|
CurrentValue = ((Change)e.Extra).NewValue
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Broadcast:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Tell:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.ScriptDamage:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.ScriptKill:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Damage:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.Kill:
|
|
|
|
|
break;
|
|
|
|
|
case GameEvent.EventType.JoinTeam:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (change != null)
|
|
|
|
|
{
|
|
|
|
|
using (var ctx = new DatabaseContext(true))
|
|
|
|
|
{
|
|
|
|
|
ctx.EFChangeHistory.Add(change);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async void OnGameEvent(object sender, GameEventArgs eventState)
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
var E = eventState.Event;
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// don't want to clog up the api with unknown events
|
|
|
|
|
if (E.Type == GameEvent.EventType.Unknown)
|
|
|
|
|
return;
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
var apiEvent = new EventInfo()
|
|
|
|
|
{
|
|
|
|
|
ExtraInfo = E.Extra?.ToString() ?? E.Data,
|
2018-07-30 20:31:00 -04:00
|
|
|
|
GameInfo = new EntityInfo()
|
|
|
|
|
{
|
|
|
|
|
Name = E.Owner.GameName.ToString(),
|
|
|
|
|
Id = (int)E.Owner.GameName
|
|
|
|
|
},
|
2018-06-16 22:11:25 -04:00
|
|
|
|
OwnerEntity = new EntityInfo()
|
2018-03-09 03:01:12 -05:00
|
|
|
|
{
|
2018-06-16 22:11:25 -04:00
|
|
|
|
Name = E.Owner.Hostname,
|
|
|
|
|
Id = E.Owner.GetHashCode()
|
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// add the new event to the list
|
|
|
|
|
AddNewEvent(apiEvent);
|
2018-09-12 20:53:11 -04:00
|
|
|
|
|
|
|
|
|
await SaveChangeHistory(E);
|
2018-06-16 22:11:25 -04:00
|
|
|
|
}
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds event to the list and removes first added if reached max capacity
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info">EventInfo to add</param>
|
2018-08-30 21:53:00 -04:00
|
|
|
|
private static void AddNewEvent(EventInfo info)
|
2018-06-16 22:11:25 -04:00
|
|
|
|
{
|
|
|
|
|
// remove the first added event
|
|
|
|
|
if (RecentEvents.Count >= MaxEvents)
|
|
|
|
|
RecentEvents.Dequeue();
|
2018-03-09 03:01:12 -05:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
RecentEvents.Enqueue(info);
|
2018-03-09 03:01:12 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|