Make pCampbot "add behaviour" and "remove behaviour" console commands work for all bots if no bot number is given

varregion
Justin Clark-Casey (justincc) 2013-09-03 19:57:34 +01:00
parent 76bd2e2d72
commit 9c3c9b7f5f
1 changed files with 79 additions and 46 deletions

View File

@ -195,15 +195,17 @@ namespace pCampBot
HandleDisconnect); HandleDisconnect);
m_console.Commands.AddCommand( m_console.Commands.AddCommand(
"bot", false, "add behaviour", "add behaviour <abbreviated-name> <bot-number>", "bot", false, "add behaviour", "add behaviour <abbreviated-name> [<bot-number>]",
"Add a behaviour to a bot", "Add a behaviour to a bot",
"Can be performed on connected or disconnected bots.", "If no bot number is specified then behaviour is added to all bots.\n"
+ "Can be performed on connected or disconnected bots.",
HandleAddBehaviour); HandleAddBehaviour);
m_console.Commands.AddCommand( m_console.Commands.AddCommand(
"bot", false, "remove behaviour", "remove behaviour <abbreviated-name> <bot-number>", "bot", false, "remove behaviour", "remove behaviour <abbreviated-name> [<bot-number>]",
"Remove a behaviour from a bot", "Remove a behaviour from a bot",
"Can be performed on connected or disconnected bots.", "If no bot number is specified then behaviour is added to all bots.\n"
+ "Can be performed on connected or disconnected bots.",
HandleRemoveBehaviour); HandleRemoveBehaviour);
m_console.Commands.AddCommand( m_console.Commands.AddCommand(
@ -224,7 +226,7 @@ namespace pCampBot
"bot", false, "show bots", "show bots", "Shows the status of all bots", HandleShowBotsStatus); "bot", false, "show bots", "show bots", "Shows the status of all bots", HandleShowBotsStatus);
m_console.Commands.AddCommand( m_console.Commands.AddCommand(
"bot", false, "show bot", "show bot <n>", "bot", false, "show bot", "show bot <bot-number>",
"Shows the detailed status and settings of a particular bot.", HandleShowBotStatus); "Shows the detailed status and settings of a particular bot.", HandleShowBotStatus);
m_bots = new List<Bot>(); m_bots = new List<Bot>();
@ -489,15 +491,24 @@ namespace pCampBot
private void HandleAddBehaviour(string module, string[] cmd) private void HandleAddBehaviour(string module, string[] cmd)
{ {
if (cmd.Length != 4) if (cmd.Length < 3 || cmd.Length > 4)
{ {
MainConsole.Instance.OutputFormat("Usage: add behaviour <abbreviated-behaviour> <bot-number>"); MainConsole.Instance.OutputFormat("Usage: add behaviour <abbreviated-behaviour> [<bot-number>]");
return; return;
} }
string rawBehaviours = cmd[2]; string rawBehaviours = cmd[2];
int botNumber;
List<Bot> botsToEffect = new List<Bot>();
if (cmd.Length == 3)
{
lock (m_bots)
botsToEffect.AddRange(m_bots);
}
else
{
int botNumber;
if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber)) if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber))
return; return;
@ -509,9 +520,15 @@ namespace pCampBot
return; return;
} }
botsToEffect.Add(bot);
}
HashSet<string> rawAbbreviatedSwitchesToAdd = new HashSet<string>(); HashSet<string> rawAbbreviatedSwitchesToAdd = new HashSet<string>();
Array.ForEach<string>(rawBehaviours.Split(new char[] { ',' }), b => rawAbbreviatedSwitchesToAdd.Add(b)); Array.ForEach<string>(rawBehaviours.Split(new char[] { ',' }), b => rawAbbreviatedSwitchesToAdd.Add(b));
foreach (Bot bot in botsToEffect)
{
List<IBehaviour> behavioursAdded = new List<IBehaviour>(); List<IBehaviour> behavioursAdded = new List<IBehaviour>();
foreach (IBehaviour behaviour in CreateBehavioursFromAbbreviatedNames(rawAbbreviatedSwitchesToAdd)) foreach (IBehaviour behaviour in CreateBehavioursFromAbbreviatedNames(rawAbbreviatedSwitchesToAdd))
@ -524,18 +541,28 @@ namespace pCampBot
"Added behaviours {0} to bot {1}", "Added behaviours {0} to bot {1}",
string.Join(", ", behavioursAdded.ConvertAll<string>(b => b.Name).ToArray()), bot.Name); string.Join(", ", behavioursAdded.ConvertAll<string>(b => b.Name).ToArray()), bot.Name);
} }
}
private void HandleRemoveBehaviour(string module, string[] cmd) private void HandleRemoveBehaviour(string module, string[] cmd)
{ {
if (cmd.Length != 4) if (cmd.Length < 3 || cmd.Length > 4)
{ {
MainConsole.Instance.OutputFormat("Usage: remove behaviour <abbreviated-behaviour> <bot-number>"); MainConsole.Instance.OutputFormat("Usage: remove behaviour <abbreviated-behaviour> [<bot-number>]");
return; return;
} }
string rawBehaviours = cmd[2]; string rawBehaviours = cmd[2];
int botNumber;
List<Bot> botsToEffect = new List<Bot>();
if (cmd.Length == 3)
{
lock (m_bots)
botsToEffect.AddRange(m_bots);
}
else
{
int botNumber;
if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber)) if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[3], out botNumber))
return; return;
@ -547,11 +574,16 @@ namespace pCampBot
return; return;
} }
HashSet<string> abbreviatedBehavioursToRemove = new HashSet<string>(); botsToEffect.Add(bot);
List<IBehaviour> behavioursRemoved = new List<IBehaviour>(); }
HashSet<string> abbreviatedBehavioursToRemove = new HashSet<string>();
Array.ForEach<string>(rawBehaviours.Split(new char[] { ',' }), b => abbreviatedBehavioursToRemove.Add(b)); Array.ForEach<string>(rawBehaviours.Split(new char[] { ',' }), b => abbreviatedBehavioursToRemove.Add(b));
foreach (Bot bot in botsToEffect)
{
List<IBehaviour> behavioursRemoved = new List<IBehaviour>();
foreach (string b in abbreviatedBehavioursToRemove) foreach (string b in abbreviatedBehavioursToRemove)
{ {
IBehaviour behaviour; IBehaviour behaviour;
@ -567,6 +599,7 @@ namespace pCampBot
"Removed behaviours {0} to bot {1}", "Removed behaviours {0} to bot {1}",
string.Join(", ", behavioursRemoved.ConvertAll<string>(b => b.Name).ToArray()), bot.Name); string.Join(", ", behavioursRemoved.ConvertAll<string>(b => b.Name).ToArray()), bot.Name);
} }
}
private void HandleDisconnect(string module, string[] cmd) private void HandleDisconnect(string module, string[] cmd)
{ {