예제 #1
0
파일: BotManager.cs 프로젝트: rivques/RLBot
        /// <summary>
        /// Adds a bot to the <see cref="botProcesses"/> list if the index is not there already.
        /// </summary>
        private void RegisterBot(string name, int team, int index)
        {
            // Only add a bot if botProcesses doesn't contain the index given in the parameters.
            if (botProcesses.Any(b => b.bot.index == index))
            {
                return;
            }

            AutoResetEvent botRunEvent = new AutoResetEvent(false);

            // Create a bot instance, run it in a separate thread, and add it to botProcesses.
            T      bot    = (T)Activator.CreateInstance(typeof(T), name, team, index);
            Thread thread = new Thread(() => RunBot(bot, botRunEvent));

            thread.Start();

            BotProcess botProcess = new BotProcess()
            {
                bot         = bot,
                thread      = thread,
                botRunEvent = botRunEvent
            };

            botProcesses.Add(botProcess);
            Console.WriteLine($"Registered bot: name={name}, team={team}, index={index}");
        }
예제 #2
0
 /// <summary>
 /// Stops the given bot's thread.
 /// </summary>
 /// <param name="botProcess">The bot process to stop.</param>
 private void StopBotProcess(BotProcess botProcess)
 {
     botProcess.thread.Abort();
     try
     {
         botProcess.bot.Dispose();
     }
     catch (Exception e)
     {
         // Don't crash the bot and give the user the details of the exception instead.
         Console.WriteLine("Bot threw an exception during termination.");
         Console.WriteLine(e.GetType());
         Console.WriteLine(e.Message);
         Console.WriteLine(e.StackTrace);
     }
     Console.WriteLine("Stopped bot: name={0}, team={1}, index={2}",
                       botProcess.bot.name, botProcess.bot.team, botProcess.bot.index);
 }
예제 #3
0
        /// <summary>
        /// Adds a bot to the <see cref="botProcesses"/> list if the index is not there already.
        /// </summary>
        /// <param name="bot"></param>
        private void RegisterBot(string name, int team, int index)
        {
            // Only add a bot if botProcesses doesn't contain the index given in the parameters.
            if (!botProcesses.Any(b => b.bot.index == index))
            {
                // Create a bot instance, run it in a separate thread, and add it to botProcesses.
                T      bot    = (T)Activator.CreateInstance(typeof(T), name, team, index);
                Thread thread = new Thread(() => RunBot(bot));
                thread.Start();

                BotProcess botProcess = new BotProcess()
                {
                    bot    = bot,
                    thread = thread
                };

                botProcesses.Add(botProcess);
                Console.WriteLine("Registered bot: name={0}, team={1}, index={2}", name, team, index);
            }
        }
예제 #4
0
        /// <summary>
        /// Method that is subscribed to <see cref="BotManagerServer.BotReceivedEvent"/>.
        /// This method parses the event's message and calls the appropriate methods.
        /// </summary>
        /// <param name="message">The message from the event.</param>
        private void OnBotReceived(string message)
        {
            try
            {
                string[] split = message.Split(new char[] { ' ' }, 5);

                if (split.Length < 2)
                {
                    throw new Exception("Server received too few command arguments from client");
                }

                if (split[0] == "add")
                {
                    PlaceInterfaceDlls(split[4]);
                    RegisterBot(split[1], int.Parse(split[2]), int.Parse(split[3]));
                }
                else if (split[0] == "remove")
                {
                    int        index = int.Parse(split[1]);
                    BotProcess proc  = botProcesses.Find(b => b.bot.index == index);

                    // Only call the bot stopping/removing methods if proc references an object and not a default value.
                    // Referencing a default value happens when Linq's Find method can't find any matches.
                    if (!proc.Equals(default(BotProcess)))
                    {
                        StopBotProcess(proc);
                        botProcesses.Remove(proc);
                    }
                }
                else
                {
                    throw new Exception("Server received bad command from client: " + split[0]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.GetType());
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
예제 #5
0
        /// <summary>
        /// Stops the given bot's thread.
        /// </summary>
        /// <param name="botProcess">The bot process to stop.</param>
        private void StopBotProcess(BotProcess botProcess)
        {
            botProcess.thread.Abort();
            Bot bot = botProcess.bot;

            try
            {
                bot.Dispose();
                botProcess.botRunEvent.Dispose();
            }
            catch (Exception e)
            {
                // Don't crash the bot and give the user the details of the exception instead.
                Console.WriteLine("Bot threw an exception during termination.");
                Console.WriteLine(e.GetType());
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            Console.WriteLine($"Stopped bot: Name={bot.Name}, Team={bot.Team}, Index={bot.Index}");
        }
예제 #6
0
 /// <summary>
 /// Stops the given bot's thread.
 /// </summary>
 /// <param name="botProcess">The bot process to stop.</param>
 private void StopBotProcess(BotProcess botProcess)
 {
     botProcess.thread.Abort();
     Console.WriteLine("Stopped bot: name={0}, team={1}, index={2}",
                       botProcess.bot.name, botProcess.bot.team, botProcess.bot.index);
 }