From d91d58940b34e7e28671bc7ee0bebc2b2e057e9d Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Thu, 5 Feb 2015 00:34:32 -0800 Subject: [PATCH] Adding arguments with dependencies. --- Combot/Bot.cs | 41 +++++++++++++++++++++++++++++++++++---- Combot/Modules/CommandArgument.cs | 26 ++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Combot/Bot.cs b/Combot/Bot.cs index b6f16d9..d825441 100755 --- a/Combot/Bot.cs +++ b/Combot/Bot.cs @@ -377,7 +377,6 @@ namespace Combot if (cmd != null) { CommandMessage newCommand = new CommandMessage(); - List 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 typeArguments = cmd.Arguments.FindAll(arg => arg.MessageTypes.Contains(messageType)); + List validArguments = new List(); 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; diff --git a/Combot/Modules/CommandArgument.cs b/Combot/Modules/CommandArgument.cs index 4ddf41e..fcb9746 100755 --- a/Combot/Modules/CommandArgument.cs +++ b/Combot/Modules/CommandArgument.cs @@ -7,7 +7,8 @@ namespace Combot.Modules public string Name { get; set; } public string Description { get; set; } public List AllowedValues { get; set; } - public List MessageTypes { get; set; } + public List DependentArguments { get; set; } + public List 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(); + DependentArguments = new List(); MessageTypes = new List(); Required = false; } @@ -33,6 +35,11 @@ namespace Combot.Modules { AllowedValues.Add(value); } + DependentArguments = new List(); + foreach (DependentArgumentInfo value in argument.DependentArguments) + { + DependentArguments.Add(value); + } MessageTypes = new List(); 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 Values { get; set; } + + public DependentArgumentInfo() + { + SetDefaults(); + } + + public void SetDefaults() + { + Name = string.Empty; + Values = new List(); + } + } } } \ No newline at end of file