IW4M-Admin/SharedLibraryCore/Events/GameEvent.cs

254 lines
7.7 KiB
C#
Raw Normal View History

2018-11-07 21:30:11 -05:00
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Events;
2018-11-07 21:30:11 -05: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
namespace SharedLibraryCore
2015-03-08 17:20:10 -04:00
{
public class GameEvent
2015-03-08 17:20:10 -04:00
{
// define what the delagate function looks like
public delegate void OnServerEventEventHandler(object sender, GameEventArgs e);
public enum EventFailReason
{
/// <summary>
/// event execution did not fail
/// </summary>
None,
/// <summary>
/// an internal exception prevented the event
/// from executing
/// </summary>
Exception,
/// <summary>
/// event origin didn't have the necessary privileges
/// to execute the command
/// </summary>
Permission,
/// <summary>
/// executing the event would cause an invalid state
/// </summary>
2018-10-03 22:20:49 -04:00
Invalid,
/// <summary>
/// client is doing too much of something
/// </summary>
Throttle,
/// <summary>
/// the event timed out before completion
/// </summary>
Timeout
}
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 log
/// </summary>
2015-03-08 17:20:10 -04:00
Connect,
/// <summary>
/// a client was detecting joining by RCon
/// </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,
2018-11-07 21:30:11 -05:00
/// <summary>
/// a client was detected as starting to connect
/// </summary>
PreConnect,
/// <summary>
/// a client was detecting as starting to disconnect
/// </summary>
PreDisconnect,
/// <summary>
/// a client's information was updated
/// </summary>
Update,
/// <summary>
/// connection was lost to a server (the server has not responded after a number of attempts)
/// </summary>
ConnectionLost,
/// <summary>
/// connection was restored to a server (the server began responding again)
/// </summary>
ConnectionRestored,
// events "generated" by clients
/// <summary>
/// a client sent a message
/// </summary>
Say = 100,
/// <summary>
/// a client was warned
/// </summary>
Warn = 101,
/// <summary>
/// all warnings for a client were cleared
/// </summary>
WarnClear = 102,
/// <summary>
/// a client was reported
/// </summary>
Report = 103,
/// <summary>
/// a client was flagged
/// </summary>
Flag = 104,
/// <summary>
/// a client was unflagged
/// </summary>
Unflag = 105,
/// <summary>
/// a client was kicked
/// </summary>
Kick = 106,
/// <summary>
/// a client was tempbanned
/// </summary>
TempBan = 107,
/// <summary>
/// a client was banned
/// </summary>
Ban = 108,
/// <summary>
/// a client was unbanned
/// </summary>
Unban = 109,
/// <summary>
/// a client entered a command
/// </summary>
Command = 110,
/// <summary>
/// a client's permission was changed
/// </summary>
ChangePermission = 111,
// events "generated" by IW4MAdmin
/// <summary>
/// a message is sent to all clients
/// </summary>
Broadcast = 200,
/// <summary>
/// a message is sent to a specific client
/// </summary>
Tell = 201,
// events "generated" by script/log
/// <summary>
/// AC Damage Log
/// </summary>
ScriptDamage = 300,
/// <summary>
/// AC Kill Log
/// </summary>
ScriptKill = 301,
/// <summary>
/// damage info printed out by game script
/// </summary>
Damage = 302,
/// <summary>
/// kill info printed out by game script
/// </summary>
Kill = 303,
/// <summary>
/// team info printed out by game script
/// </summary>
JoinTeam = 304,
/// <summary>
/// used for community generated plugin events
/// </summary>
Other
2015-03-08 17:20:10 -04:00
}
[Flags]
public enum EventRequiredEntity
{
None = 1,
Origin = 2,
Target = 4
}
static long NextEventId;
2018-11-07 21:30:11 -05:00
static long GetNextEventId()
{
return 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;
public EventRequiredEntity RequiredEntity { get; set; }
2015-03-08 17:20:10 -04:00
public string Data; // Data is usually the message sent by player
public string Message;
public EFClient Origin;
public EFClient Target;
2015-03-08 17:20:10 -04:00
public Server Owner;
public bool IsRemote { get; set; } = 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; }
public EventFailReason FailReason { get; set; }
public bool Failed => FailReason != EventFailReason.None;
/// <summary>
/// Indicates if the event should block until it is complete
/// </summary>
public bool IsBlocking { get; set; }
2018-09-06 14:25:58 -04:00
/// <summary>
/// asynchronously wait for GameEvent to be processed
/// </summary>
/// <returns>waitable task </returns>
public Task<GameEvent> WaitAsync(TimeSpan timeSpan, CancellationToken token)
{
2018-11-07 21:30:11 -05:00
return Task.Run(() =>
{
bool processed = OnProcessed.Wait(timeSpan, token);
// this let's us know if the the action timed out
FailReason = FailReason == EventFailReason.None & !processed ? EventFailReason.Timeout : FailReason;
2018-11-07 21:30:11 -05:00
return this;
});
}
public GameEvent Wait()
{
OnProcessed.Wait();
return this;
}
2015-03-08 17:20:10 -04:00
}
}