2015-03-08 17:20:10 -04:00
|
|
|
|
using System;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
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;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public class GameEvent
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
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
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public enum EventType
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// the event wasn't parsed properly
|
|
|
|
|
/// </summary>
|
2018-06-16 22:11:25 -04:00
|
|
|
|
Unknown,
|
2018-07-04 22:09:42 -04:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// events "generated" by the server
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a server started being monitored
|
|
|
|
|
/// </summary>
|
2015-08-21 21:11:35 -04:00
|
|
|
|
Start,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a server stopped being monitored
|
|
|
|
|
/// </summary>
|
2015-08-21 21:11:35 -04:00
|
|
|
|
Stop,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was detecting as connecting via RCon
|
|
|
|
|
/// </summary>
|
2015-03-08 17:20:10 -04:00
|
|
|
|
Connect,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was detecting joining via log
|
|
|
|
|
/// </summary>
|
2018-04-28 21:11:13 -04:00
|
|
|
|
Join,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was detected leaving via log
|
|
|
|
|
/// </summary>
|
2018-06-16 22:11:25 -04:00
|
|
|
|
Quit,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was detected leaving by RCon
|
|
|
|
|
/// </summary>
|
2015-03-08 17:20:10 -04:00
|
|
|
|
Disconnect,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// the current map ended
|
|
|
|
|
/// </summary>
|
2015-03-08 17:20:10 -04:00
|
|
|
|
MapEnd,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// the current map changed
|
|
|
|
|
/// </summary>
|
2018-06-16 22:11:25 -04:00
|
|
|
|
MapChange,
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
2018-09-12 20:53:11 -04:00
|
|
|
|
// events "generated" by clients
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client sent a message
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Say = 100,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was warned
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Warn = 101,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// all warnings for a client were cleared
|
|
|
|
|
/// </summary>
|
|
|
|
|
WarnClear = 102,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was reported
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Report = 103,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was flagged
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Flag = 104,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was unflagged
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Unflag = 105,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was kicked
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Kick = 106,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was tempbanned
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
TempBan = 107,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was banned
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Ban = 108,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client was unbanned
|
|
|
|
|
/// </summary>
|
|
|
|
|
Unban = 109,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client entered a command
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Command = 110,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a client's permission was changed
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
ChangePermission = 111,
|
2017-09-29 22:42:24 -04:00
|
|
|
|
|
2018-06-16 22:11:25 -04:00
|
|
|
|
// events "generated" by IW4MAdmin
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a message is sent to all clients
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Broadcast = 200,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// a message is sent to a specific client
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Tell = 201,
|
2018-06-16 22:11:25 -04:00
|
|
|
|
|
|
|
|
|
// events "generated" by script/log
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// AC Damage Log
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
ScriptDamage = 300,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// AC Kill Log
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
ScriptKill = 301,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// damage info printed out by game script
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Damage = 302,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// kill info printed out by game script
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
Kill = 303,
|
2018-09-12 20:53:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// team info printed out by game script
|
|
|
|
|
/// </summary>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
JoinTeam = 304,
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
static long NextEventId;
|
|
|
|
|
static long GetNextEventId() => Interlocked.Increment(ref NextEventId);
|
2015-03-08 17:20:10 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public GameEvent()
|
|
|
|
|
{
|
2018-09-07 23:29:42 -04:00
|
|
|
|
OnProcessed = new ManualResetEventSlim(false);
|
2018-05-03 01:25:49 -04:00
|
|
|
|
Time = DateTime.UtcNow;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
Id = GetNextEventId();
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public EventType Type;
|
2015-03-08 17:20:10 -04:00
|
|
|
|
public string Data; // Data is usually the message sent by player
|
2017-05-31 01:31:56 -04:00
|
|
|
|
public string Message;
|
2015-03-08 17:20:10 -04:00
|
|
|
|
public Player Origin;
|
|
|
|
|
public Player Target;
|
|
|
|
|
public Server Owner;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
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; }
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public DateTime Time { get; set; }
|
2018-05-10 01:34:29 -04:00
|
|
|
|
public long Id { get; private set; }
|
2018-09-29 15:52:22 -04:00
|
|
|
|
public EventFailReason FailReason { get; set; }
|
|
|
|
|
public bool Failed => FailReason != EventFailReason.None;
|
2018-07-04 22:09:42 -04:00
|
|
|
|
|
2018-09-06 14:25:58 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// asynchronously wait for GameEvent to be processed
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>waitable task </returns>
|
2018-10-02 13:39:08 -04:00
|
|
|
|
public Task<GameEvent> WaitAsync(int timeOut = int.MaxValue) => Task.Run(() =>
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
|
|
|
|
OnProcessed.Wait(timeOut);
|
2018-10-02 13:39:08 -04:00
|
|
|
|
return this;
|
2018-09-29 15:52:22 -04:00
|
|
|
|
});
|
2018-09-06 14:25:58 -04:00
|
|
|
|
|
2018-07-04 22:09:42 -04:00
|
|
|
|
/// <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 &&
|
2018-09-23 20:45:54 -04:00
|
|
|
|
(queuedEvent.Origin.State != Player.ClientState.Connected &&
|
2018-07-04 22:09:42 -04:00
|
|
|
|
// we want to allow join and quit events
|
2018-08-27 18:07:54 -04:00
|
|
|
|
queuedEvent.Type != EventType.Connect &&
|
2018-07-04 22:09:42 -04:00
|
|
|
|
queuedEvent.Type != EventType.Join &&
|
|
|
|
|
queuedEvent.Type != EventType.Quit &&
|
2018-08-28 17:32:59 -04:00
|
|
|
|
queuedEvent.Type != EventType.Disconnect &&
|
2018-07-04 22:09:42 -04:00
|
|
|
|
// we don't care about unknown events
|
2018-09-23 20:45:54 -04:00
|
|
|
|
queuedEvent.Origin.NetworkId != 0);
|
2018-07-04 22:09:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2018-10-02 13:39:08 -04:00
|
|
|
|
return (queuedEvent.Target != null && queuedEvent.Target.ClientNumber != -1) &&
|
2018-09-23 20:45:54 -04:00
|
|
|
|
(queuedEvent.Target.State != Player.ClientState.Connected &&
|
|
|
|
|
queuedEvent.Target.NetworkId != 0);
|
2018-07-04 22:09:42 -04:00
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|