2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Exceptions;
|
2018-06-30 21:55:16 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharedLibraryCore.Commands
|
|
|
|
|
{
|
|
|
|
|
public class CommandProcessing
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
public static async Task<Command> ValidateCommand(GameEvent E)
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
|
|
|
|
var loc = Utilities.CurrentLocalization.LocalizationIndex;
|
|
|
|
|
var Manager = E.Owner.Manager;
|
|
|
|
|
|
|
|
|
|
string CommandString = E.Data.Substring(1, E.Data.Length - 1).Split(' ')[0];
|
|
|
|
|
E.Message = E.Data;
|
|
|
|
|
|
|
|
|
|
Command C = null;
|
|
|
|
|
foreach (Command cmd in Manager.GetCommands())
|
|
|
|
|
{
|
|
|
|
|
if (cmd.Name == CommandString.ToLower() || cmd.Alias == CommandString.ToLower())
|
2019-03-18 11:36:31 -04:00
|
|
|
|
{
|
2018-06-30 21:55:16 -04:00
|
|
|
|
C = cmd;
|
2019-03-18 11:36:31 -04:00
|
|
|
|
}
|
2018-06-30 21:55:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (C == null)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_UNKNOWN"]);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} entered unknown command \"{CommandString}\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
E.Data = E.Data.RemoveWords(1);
|
|
|
|
|
String[] Args = E.Data.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
2019-03-18 11:36:31 -04:00
|
|
|
|
// todo: the code below can be cleaned up
|
2018-06-30 21:55:16 -04:00
|
|
|
|
|
|
|
|
|
if (E.Origin.Level < C.Permission)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_NOACCESS"]);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} does not have access to \"{C.Name}\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Args.Length < (C.RequiredArgumentCount))
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_MISSINGARGS"]);
|
|
|
|
|
E.Origin.Tell(C.Syntax);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} did not supply enough arguments for \"{C.Name}\"");
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 11:36:31 -04:00
|
|
|
|
if (C.RequiresTarget)
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
2019-03-18 11:36:31 -04:00
|
|
|
|
if (Args.Length > 0)
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
2019-03-18 11:36:31 -04:00
|
|
|
|
if (!Int32.TryParse(Args[0], out int cNum))
|
|
|
|
|
{
|
|
|
|
|
cNum = -1;
|
|
|
|
|
}
|
2018-06-30 21:55:16 -04:00
|
|
|
|
|
2019-03-18 11:36:31 -04:00
|
|
|
|
if (Args[0][0] == '@') // user specifying target by database ID
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
2019-07-19 15:54:39 -04:00
|
|
|
|
int.TryParse(Args[0].Substring(1, Args[0].Length - 1), out int dbID);
|
2019-03-18 11:36:31 -04:00
|
|
|
|
|
|
|
|
|
var found = await Manager.GetClientService().Get(dbID);
|
|
|
|
|
if (found != null)
|
|
|
|
|
{
|
|
|
|
|
E.Target = found;
|
|
|
|
|
E.Target.CurrentServer = E.Owner;
|
|
|
|
|
E.Data = String.Join(" ", Args.Skip(1));
|
|
|
|
|
}
|
2018-06-30 21:55:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 11:36:31 -04:00
|
|
|
|
else if (Args[0].Length < 3 && cNum > -1 && cNum < E.Owner.MaxClients) // user specifying target by client num
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
2019-03-18 11:36:31 -04:00
|
|
|
|
if (E.Owner.Clients[cNum] != null)
|
|
|
|
|
{
|
|
|
|
|
E.Target = E.Owner.Clients[cNum];
|
|
|
|
|
E.Data = String.Join(" ", Args.Skip(1));
|
|
|
|
|
}
|
2018-06-30 21:55:16 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
List<EFClient> matchingPlayers;
|
2018-06-30 21:55:16 -04:00
|
|
|
|
|
|
|
|
|
if (E.Target == null && C.RequiresTarget) // Find active player including quotes (multiple words)
|
|
|
|
|
{
|
|
|
|
|
matchingPlayers = E.Owner.GetClientByName(E.Data.Trim());
|
|
|
|
|
if (matchingPlayers.Count > 1)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_TARGET_MULTI"]);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} had multiple players found for {C.Name}");
|
|
|
|
|
}
|
|
|
|
|
else if (matchingPlayers.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
E.Target = matchingPlayers.First();
|
|
|
|
|
|
2019-08-10 10:08:26 -04:00
|
|
|
|
string escapedName = Regex.Escape(E.Target.CleanedName);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
var reg = new Regex($"(\"{escapedName}\")|({escapedName})", RegexOptions.IgnoreCase);
|
|
|
|
|
E.Data = reg.Replace(E.Data, "", 1).Trim();
|
|
|
|
|
|
|
|
|
|
if (E.Data.Length == 0 && C.RequiredArgumentCount > 1)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_MISSINGARGS"]);
|
|
|
|
|
E.Origin.Tell(C.Syntax);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} did not supply enough arguments for \"{C.Name}\"");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 11:36:31 -04:00
|
|
|
|
if (E.Target == null && C.RequiresTarget && Args.Length > 0) // Find active player as single word
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
|
|
|
|
matchingPlayers = E.Owner.GetClientByName(Args[0]);
|
|
|
|
|
if (matchingPlayers.Count > 1)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_TARGET_MULTI"]);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
foreach (var p in matchingPlayers)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
|
|
|
|
E.Origin.Tell($"[^3{p.ClientNumber}^7] {p.Name}");
|
|
|
|
|
}
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} had multiple players found for {C.Name}");
|
|
|
|
|
}
|
2019-03-18 11:36:31 -04:00
|
|
|
|
|
2018-06-30 21:55:16 -04:00
|
|
|
|
else if (matchingPlayers.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
E.Target = matchingPlayers.First();
|
|
|
|
|
|
2019-08-10 10:08:26 -04:00
|
|
|
|
string escapedName = Regex.Escape(E.Target.CleanedName);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
string escapedArg = Regex.Escape(Args[0]);
|
|
|
|
|
var reg = new Regex($"({escapedName})|({escapedArg})", RegexOptions.IgnoreCase);
|
|
|
|
|
E.Data = reg.Replace(E.Data, "", 1).Trim();
|
|
|
|
|
|
2019-08-10 10:08:26 -04:00
|
|
|
|
if ((E.Data.Trim() == E.Target.CleanedName.ToLower().Trim() ||
|
2018-06-30 21:55:16 -04:00
|
|
|
|
E.Data == String.Empty) &&
|
|
|
|
|
C.RequiresTarget)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_MISSINGARGS"]);
|
|
|
|
|
E.Origin.Tell(C.Syntax);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} did not supply enough arguments for \"{C.Name}\"");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (E.Target == null && C.RequiresTarget)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Origin.Tell(loc["COMMAND_TARGET_NOTFOUND"]);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
throw new CommandException($"{E.Origin} specified invalid player for \"{C.Name}\"");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-18 11:36:31 -04:00
|
|
|
|
|
2018-06-30 21:55:16 -04:00
|
|
|
|
E.Data = E.Data.Trim();
|
|
|
|
|
return C;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|