Browse Source

Adding arguments with dependencies.

tags/3.0.0
Teknikode 4 years ago
parent
commit
d91d58940b
2 changed files with 62 additions and 5 deletions
  1. 37
    4
      Combot/Bot.cs
  2. 25
    1
      Combot/Modules/CommandArgument.cs

+ 37
- 4
Combot/Bot.cs View File

@@ -377,7 +377,6 @@ namespace Combot
if (cmd != null)
{
CommandMessage newCommand = new CommandMessage();
List<CommandArgument> validArguments = cmd.Arguments.FindAll(arg => arg.MessageTypes.Contains(messageType));
newCommand.Nick.Copy(sender);
bool nickFound = false;
IRC.Channels.ForEach(channel => channel.Nicks.ForEach(nick =>
@@ -414,12 +413,46 @@ namespace Combot
newCommand.Location = location;
newCommand.MessageType = messageType;
newCommand.Command = command;
// Check arguments against required arguments
List<CommandArgument> typeArguments = cmd.Arguments.FindAll(arg => arg.MessageTypes.Contains(messageType));
List<CommandArgument> validArguments = new List<CommandArgument>();
if (argsOnly.Count > 0)
{
string[] argSplit = argsOnly.First().Split(new[] { ' ' }, validArguments.Count, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < validArguments.Count && i <= argSplit.GetUpperBound(0); i++)
string[] argSplit = argsOnly.First().Split(new[] { ' ' }, typeArguments.Count, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < typeArguments.Count; i++)
{
if (argsOnly.Count > i)
{
}
}
int argIndex = 0;
for (int i = 0; i < typeArguments.Count && argIndex <= argSplit.GetUpperBound(0); i++)
{
newCommand.Arguments.Add(validArguments[i].Name, argSplit[i]);
bool allowedArg = true;
if (typeArguments[i].DependentArguments.Count > 0)
{
allowedArg = typeArguments[i].DependentArguments.Exists(arg =>
{
if (newCommand.Arguments.ContainsKey(arg.Name))
{
string argValue = newCommand.Arguments[arg.Name];
return arg.Values.Exists(val => val.ToLower() == argValue.ToLower());
}
return false;
});
}
if (allowedArg)
{
newCommand.Arguments.Add(typeArguments[i].Name, argSplit[i]);
CommandArgument newArgument = new CommandArgument();
newArgument.Copy(typeArguments[i]);
validArguments.Add(newArgument);
argIndex++;
}
}
}
bool invalidArgs = false;

+ 25
- 1
Combot/Modules/CommandArgument.cs View File

@@ -7,7 +7,8 @@ namespace Combot.Modules
public string Name { get; set; }
public string Description { get; set; }
public List<string> AllowedValues { get; set; }
public List<MessageType> MessageTypes { get; set; }
public List<DependentArgumentInfo> DependentArguments { get; set; }
public List<MessageType> MessageTypes { get; set; }
public bool Required { get; set; }

public CommandArgument()
@@ -20,6 +21,7 @@ namespace Combot.Modules
Name = string.Empty;
Description = string.Empty;
AllowedValues = new List<string>();
DependentArguments = new List<DependentArgumentInfo>();
MessageTypes = new List<MessageType>();
Required = false;
}
@@ -33,6 +35,11 @@ namespace Combot.Modules
{
AllowedValues.Add(value);
}
DependentArguments = new List<DependentArgumentInfo>();
foreach (DependentArgumentInfo value in argument.DependentArguments)
{
DependentArguments.Add(value);
}
MessageTypes = new List<MessageType>();
foreach (MessageType value in argument.MessageTypes)
{
@@ -40,5 +47,22 @@ namespace Combot.Modules
}
Required = argument.Required;
}

public class DependentArgumentInfo
{
public string Name { get; set; }
public List<string> Values { get; set; }

public DependentArgumentInfo()
{
SetDefaults();
}

public void SetDefaults()
{
Name = string.Empty;
Values = new List<string>();
}
}
}
}

Loading…
Cancel
Save