IW4M-Admin/SharedLibraryCore/Events/GameEvent.cs

189 lines
6.2 KiB
C#
Raw Normal View History

2015-03-08 17:20:10 -04:00
using System;
using System.Threading;
2018-09-06 14:25:58 -04:00
using System.Threading.Tasks;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore.Objects;
2018-04-08 02:44:42 -04:00
namespace SharedLibraryCore
2015-03-08 17:20:10 -04:00
{
public class GameEvent
2015-03-08 17:20:10 -04:00
{
public enum EventType
2015-03-08 17:20:10 -04:00
{
/// <summary>
/// the event wasn't parsed properly
/// </summary>
Unknown,
// events "generated" by the server
/// <summary>
/// a server started being monitored
/// </summary>
Start,
/// <summary>
/// a server stopped being monitored
/// </summary>
Stop,
/// <summary>
/// a client was detecting as connecting via RCon
/// </summary>
2015-03-08 17:20:10 -04:00
Connect,
/// <summary>
/// a client was detecting joining via log
/// </summary>
Join,
/// <summary>
/// a client was detected leaving via log
/// </summary>
Quit,
/// <summary>
/// a client was detected leaving by RCon
/// </summary>
2015-03-08 17:20:10 -04:00
Disconnect,
/// <summary>
/// the current map ended
/// </summary>
2015-03-08 17:20:10 -04:00
MapEnd,
/// <summary>
/// the current map changed
/// </summary>
MapChange,
// events "generated" by clients
/// <summary>
/// a client sent a message
/// </summary>
Say,
/// <summary>
/// a client was warned
/// </summary>
Warn,
/// <summary>
/// a client was reported
/// </summary>
Report,
/// <summary>
/// a client was flagged
/// </summary>
Flag,
/// <summary>
/// a client was unflagged
/// </summary>
Unflag,
/// <summary>
/// a client was kicked
/// </summary>
Kick,
/// <summary>
/// a client was tempbanned
/// </summary>
TempBan,
/// <summary>
/// a client was banned
/// </summary>
Ban,
/// <summary>
/// a client entered a command
/// </summary>
2018-04-14 00:51:38 -04:00
Command,
/// <summary>
/// a client's permission was changed
/// </summary>
ChangePermission,
// events "generated" by IW4MAdmin
/// <summary>
/// a message is sent to all clients
/// </summary>
Broadcast,
/// <summary>
/// a message is sent to a specific client
/// </summary>
Tell,
// events "generated" by script/log
/// <summary>
/// AC Damage Log
/// </summary>
ScriptDamage,
/// <summary>
/// AC Kill Log
/// </summary>
2018-05-10 01:34:29 -04:00
ScriptKill,
/// <summary>
/// damage info printed out by game script
/// </summary>
Damage,
/// <summary>
/// kill info printed out by game script
/// </summary>
Kill,
/// <summary>
/// team info printed out by game script
/// </summary>
JoinTeam,
2015-03-08 17:20:10 -04:00
}
static long NextEventId;
static long GetNextEventId() => Interlocked.Increment(ref NextEventId);
2015-03-08 17:20:10 -04:00
public GameEvent()
{
OnProcessed = new ManualResetEventSlim(false);
Time = DateTime.UtcNow;
Id = GetNextEventId();
}
2015-03-08 17:20:10 -04:00
public EventType Type;
2015-03-08 17:20:10 -04:00
public string Data; // Data is usually the message sent by player
public string Message;
2015-03-08 17:20:10 -04:00
public Player Origin;
public Player Target;
public Server Owner;
public Boolean Remote = false;
2018-04-14 00:51:38 -04:00
public object Extra { get; set; }
2018-09-06 14:25:58 -04:00
public ManualResetEventSlim OnProcessed { get; set; }
public DateTime Time { get; set; }
2018-05-10 01:34:29 -04:00
public long Id { get; private set; }
2018-09-06 14:25:58 -04:00
/// <summary>
/// asynchronously wait for GameEvent to be processed
/// </summary>
/// <returns>waitable task </returns>
public Task<bool> WaitAsync(int timeOut = int.MaxValue) => Task.FromResult(OnProcessed.Wait(timeOut));
/// <summary>
/// determine whether an event should be delayed or not
/// applies only to the origin entity
/// </summary>
/// <param name="queuedEvent">event to determine status for</param>
/// <returns>true if event should be delayed, false otherwise</returns>
public static bool ShouldOriginEventBeDelayed(GameEvent queuedEvent)
{
return queuedEvent.Origin != null &&
queuedEvent.Origin.State != Player.ClientState.Connected &&
// we want to allow join and quit events
queuedEvent.Type != EventType.Connect &&
queuedEvent.Type != EventType.Join &&
queuedEvent.Type != EventType.Quit &&
queuedEvent.Type != EventType.Disconnect &&
// we don't care about unknown events
queuedEvent.Origin.NetworkId != 0;
}
/// <summary>
/// determine whether an event should be delayed or not
/// applies only to the target entity
/// </summary>
/// <param name="queuedEvent">event to determine status for</param>
/// <returns>true if event should be delayed, false otherwise</returns>
public static bool ShouldTargetEventBeDelayed(GameEvent queuedEvent)
{
return queuedEvent.Target != null &&
queuedEvent.Target.State != Player.ClientState.Connected &&
queuedEvent.Target.NetworkId != 0;
}
public static bool IsEventTimeSensitive(GameEvent gameEvent) => gameEvent.Type == EventType.Connect;
2015-03-08 17:20:10 -04:00
}
}