Create a separate pCampbot "disconnect" console command which disconnects connected bots.
"quit" console command now requires bots to be separate disconnected first before quitting. This is to prepare the way for disconnecting/reconnecting different numbers of bots in a pCampbot session. And hopefully resolves bug where console appears not to be reset if Environment.Exit(0) is called on a different thread0.7.6-extended
parent
ef63abe9b1
commit
49b7cbda72
|
@ -52,9 +52,9 @@ namespace pCampBot
|
|||
public const int DefaultLoginDelay = 5000;
|
||||
|
||||
/// <summary>
|
||||
/// True if pCampbot is in the process of shutting down.
|
||||
/// Is pCampbot in the process of disconnecting bots?
|
||||
/// </summary>
|
||||
public bool ShuttingDown { get; private set; }
|
||||
public bool DisconnectingBots { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Delay between logins of multiple bots.
|
||||
|
@ -139,6 +139,11 @@ namespace pCampBot
|
|||
"Shutdown bots and exit",
|
||||
HandleShutdown);
|
||||
|
||||
m_console.Commands.AddCommand("bot", false, "disconnect",
|
||||
"disconnect",
|
||||
"Disconnect all bots",
|
||||
HandleDisconnect);
|
||||
|
||||
m_console.Commands.AddCommand("bot", false, "show regions",
|
||||
"show regions",
|
||||
"Show regions known to bots",
|
||||
|
@ -191,7 +196,9 @@ namespace pCampBot
|
|||
|
||||
for (int i = 0; i < botcount; i++)
|
||||
{
|
||||
if (ShuttingDown)
|
||||
lock (m_lBot)
|
||||
{
|
||||
if (DisconnectingBots)
|
||||
break;
|
||||
|
||||
string lastName = string.Format("{0}_{1}", lastNameStem, i + fromBotNumber);
|
||||
|
@ -217,6 +224,10 @@ namespace pCampBot
|
|||
|
||||
StartBot(this, behaviours, firstName, lastName, password, loginUri, startUri, wearSetting);
|
||||
}
|
||||
|
||||
// Stagger logins
|
||||
Thread.Sleep(LoginDelay);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -305,7 +316,6 @@ namespace pCampBot
|
|||
pb.OnConnected += handlebotEvent;
|
||||
pb.OnDisconnected += handlebotEvent;
|
||||
|
||||
lock (m_lBot)
|
||||
m_lBot.Add(pb);
|
||||
|
||||
Thread pbThread = new Thread(pb.startup);
|
||||
|
@ -313,9 +323,6 @@ namespace pCampBot
|
|||
pbThread.IsBackground = true;
|
||||
|
||||
pbThread.Start();
|
||||
|
||||
// Stagger logins
|
||||
Thread.Sleep(LoginDelay);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -328,16 +335,14 @@ namespace pCampBot
|
|||
switch (eventt)
|
||||
{
|
||||
case EventType.CONNECTED:
|
||||
{
|
||||
m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Connected");
|
||||
break;
|
||||
}
|
||||
|
||||
case EventType.DISCONNECTED:
|
||||
m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Disconnected");
|
||||
|
||||
lock (m_lBot)
|
||||
{
|
||||
if (m_lBot.TrueForAll(b => b.ConnectionState == ConnectionState.Disconnected))
|
||||
Environment.Exit(0);
|
||||
|
||||
m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Disconnected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -349,9 +354,7 @@ namespace pCampBot
|
|||
/// <remarks>
|
||||
/// We launch each shutdown on its own thread so that a slow shutting down bot doesn't hold up all the others.
|
||||
/// </remarks>
|
||||
public void doBotShutdown()
|
||||
{
|
||||
lock (m_lBot)
|
||||
private void ShutdownBots()
|
||||
{
|
||||
foreach (Bot bot in m_lBot)
|
||||
{
|
||||
|
@ -359,7 +362,6 @@ namespace pCampBot
|
|||
Util.FireAndForget(o => thisBot.shutdown());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Standard CreateConsole routine
|
||||
|
@ -370,12 +372,34 @@ namespace pCampBot
|
|||
return new LocalConsole("pCampbot");
|
||||
}
|
||||
|
||||
private void HandleDisconnect(string module, string[] cmd)
|
||||
{
|
||||
MainConsole.Instance.Output("Disconnecting bots");
|
||||
|
||||
lock (m_lBot)
|
||||
{
|
||||
DisconnectingBots = true;
|
||||
|
||||
ShutdownBots();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleShutdown(string module, string[] cmd)
|
||||
{
|
||||
lock (m_lBot)
|
||||
{
|
||||
int connectedBots = m_lBot.Count(b => b.ConnectionState == ConnectionState.Connected);
|
||||
|
||||
if (connectedBots > 0)
|
||||
{
|
||||
MainConsole.Instance.OutputFormat("Please disconnect {0} connected bots first", connectedBots);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
MainConsole.Instance.Output("Shutting down");
|
||||
|
||||
ShuttingDown = true;
|
||||
doBotShutdown();
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
private void HandleShowRegions(string module, string[] cmd)
|
||||
|
|
Loading…
Reference in New Issue