1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-09 23:00:57 -05:00

implement more robust command api and login

improve web console command response reliability and consistency
This commit is contained in:
RaidMax
2021-01-17 21:58:18 -06:00
parent 17dd35c4bb
commit c231c6b610
12 changed files with 260 additions and 56 deletions

View File

@ -25,40 +25,39 @@ namespace SharedLibraryCore.Commands
};
}
public override async Task ExecuteAsync(GameEvent E)
public override async Task ExecuteAsync(GameEvent gameEvent)
{
if (E.IsTargetingSelf())
if (gameEvent.IsTargetingSelf())
{
E.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_SELF"]);
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_SELF"]);
return;
}
if (!E.CanPerformActionOnTarget())
if (!gameEvent.CanPerformActionOnTarget())
{
E.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_FAIL_PERM"]);
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_FAIL_PERM"]);
return;
}
string cmd = $"{Utilities.CommandPrefix}{E.Data}";
var cmd = $"{Utilities.CommandPrefix}{gameEvent.Data}";
var impersonatedCommandEvent = new GameEvent()
{
Type = GameEvent.EventType.Command,
Origin = E.Target,
ImpersonationOrigin = E.Origin,
Origin = gameEvent.Target,
ImpersonationOrigin = gameEvent.Origin,
Message = cmd,
Data = cmd,
Owner = E.Owner
Owner = gameEvent.Owner,
CorrelationId = gameEvent.CorrelationId
};
E.Owner.Manager.AddEvent(impersonatedCommandEvent);
gameEvent.Owner.Manager.AddEvent(impersonatedCommandEvent);
var result = await impersonatedCommandEvent.WaitAsync(Utilities.DefaultCommandTimeout, E.Owner.Manager.CancellationToken);
var response = E.Owner.CommandResult.Where(c => c.ClientId == E.Target.ClientId).ToList();
var result = await impersonatedCommandEvent.WaitAsync(Utilities.DefaultCommandTimeout, gameEvent.Owner.Manager.CancellationToken);
// remove the added command response
for (int i = 0; i < response.Count; i++)
foreach (var output in result.Output)
{
E.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_SUCCESS"].FormatExt(response[i].Response));
E.Owner.CommandResult.Remove(response[i]);
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_SUCCESS"].FormatExt(output));
}
}
}

View File

@ -1,6 +1,7 @@
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Events;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
@ -247,6 +248,8 @@ namespace SharedLibraryCore
public long Id { get; private set; }
public EventFailReason FailReason { get; set; }
public bool Failed => FailReason != EventFailReason.None;
public Guid CorrelationId { get; set; } = Guid.NewGuid();
public List<string> Output { get; set; } = new List<string>();
/// <summary>
/// Indicates if the event should block until it is complete
@ -280,7 +283,7 @@ namespace SharedLibraryCore
{
using(LogContext.PushProperty("Server", Owner?.ToString()))
{
Utilities.DefaultLogger.LogError("Waiting for event to complete timed out {@eventData}", new { Event = this, Message, Origin = Origin.ToString(), Target = Target.ToString()});
Utilities.DefaultLogger.LogError("Waiting for event to complete timed out {@eventData}", new { Event = this, Message, Origin = Origin?.ToString(), Target = Target?.ToString()});
}
}

View File

@ -6,6 +6,7 @@ using SharedLibraryCore.Database.Models;
using System.Threading;
using System.Collections;
using System;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
namespace SharedLibraryCore.Interfaces
@ -86,5 +87,6 @@ namespace SharedLibraryCore.Interfaces
/// event executed when event has finished executing
/// </summary>
event EventHandler<GameEvent> OnGameEventExecuted;
ConcurrentDictionary<long, GameEvent> ProcessingEvents { get; }
}
}

View File

@ -133,7 +133,7 @@ namespace SharedLibraryCore.Database.Models
/// send a message directly to the connected client
/// </summary>
/// <param name="message">message content to send to client</param>
public GameEvent Tell(String message)
public GameEvent Tell(string message)
{
var e = new GameEvent()
{
@ -141,8 +141,12 @@ namespace SharedLibraryCore.Database.Models
Target = this,
Owner = CurrentServer,
Type = GameEvent.EventType.Tell,
Data = message
Data = message,
CorrelationId = CurrentServer.Manager.ProcessingEvents.Values
.FirstOrDefault(ev => ev.Type == GameEvent.EventType.Command && (ev.Origin?.ClientId == ClientId || ev.ImpersonationOrigin?.ClientId == ClientId))?.CorrelationId ?? Guid.NewGuid()
};
e.Output.Add(message.StripColors());
CurrentServer?.Manager.AddEvent(e);
return e;

View File

@ -172,22 +172,6 @@ namespace SharedLibraryCore
Console.WriteLine(message.StripColors());
Console.ForegroundColor = ConsoleColor.Gray;
}
// prevent this from queueing up too many command responses
if (CommandResult.Count > 15)
{
CommandResult.RemoveAt(0);
}
// it was a remote command so we need to add it to the command result queue
if (target.ClientNumber < 0)
{
CommandResult.Add(new CommandResponseInfo()
{
Response = message.StripColors(),
ClientId = target.ClientId
});
}
}
/// <summary>
@ -347,8 +331,5 @@ namespace SharedLibraryCore
// only here for performance
private readonly bool CustomSayEnabled;
private readonly string CustomSayName;
//Remote
public IList<CommandResponseInfo> CommandResult = new List<CommandResponseInfo>();
}
}