Newer
Older
Import / projects / Gameloft / bne_lib / tools / BNEBotLauncher / Program.cs
using BNEBotCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BNEBotLauncher
{
  class Program
  {
    static void Main(string[] args)
    {
      // Handle command line
      CommandLine.RegisterSwitch("nomgr", "Start the launcher without a TPC server.",
        (a) => { LaunchSettings.Offline = true; });
      CommandLine.RegisterSwitch("wine", "Start the bot process using WINE (use under mono).",
        (a) => { LaunchSettings.UseWine = true; });
      CommandLine.RegisterSwitch("debug", "Prefer debug executable for bots.",
        (a) => { LaunchSettings.DebugExe = true; });
      CommandLine.RegisterSwitch("exe", "Name of the bot executable.",
        (a) => { LaunchSettings.ExeFile = (string)a[0]; }, typeof(string));
      CommandLine.RegisterSwitch("local", "Spawn bots that don't connect to remote services.",
        (a) => { LaunchSettings.LocalBot = true; });
      CommandLine.RegisterSwitch("ExistingBots", "Spawn the same bots as last time.",
        (a) => { LaunchSettings.UniqueBots = false; });
      CommandLine.RegisterSwitch("count", "Number of bots to spawn.",
        (a) => { LaunchSettings.BotCount = (int)a[0]; }, typeof(int));
      CommandLine.RegisterSwitch("startid", "Incremental ID of the first launched bot.",
        (a) => { LaunchSettings.FirstBotIndex = (int)a[0]; }, typeof(int));
      CommandLine.RegisterSwitch("duration", "How long should the test last (minutes).",
        (a) => { LaunchSettings.TestDuration = new TimeSpan(0, (int)a[0], 0); }, typeof(int));
      CommandLine.RegisterSwitch("persist", "Wait infinitely for commands from the monitor.",
        (a) => { LaunchSettings.PersistentTest = true; });
      CommandLine.RegisterSwitch("interval", "Seconds between bot spawns.",
        (a) => { LaunchSettings.SpawnInterval = new TimeSpan(0, 0, (int)a[0]); }, typeof(int));
      CommandLine.RegisterSwitch("botstrategy", "The default strategy used by new bots.",
        (a) => { LaunchSettings.DefaultBotStrategy = (string)a[0]; }, typeof(string));
      CommandLine.RegisterSwitch("bottarget", "Default max level bots will try to achieve.",
        (a) => { LaunchSettings.DefaultBotTargetLevel = (int)a[0]; }, typeof(int));
      CommandLine.RegisterSwitch("randomint", "Random event interval.",
        (a) =>
        {
          LaunchSettings.Randomize = true;
          LaunchSettings.RandomIntervalMin = new TimeSpan(0, (int)a[0], 0);
          LaunchSettings.RandomIntervalMax = new TimeSpan(0, (int)a[1], 0);
        }, typeof(int), typeof(int));
      CommandLine.RegisterSwitch("randomcnt", "Number of bots affected by random events.",
        (a) => 
        {
          LaunchSettings.Randomize = true;
          LaunchSettings.RandomCountMin = (int)a[0];
          LaunchSettings.RandomCountMax = (int)a[1];
        }, typeof(int), typeof(int));
      int port = 6666;
      CommandLine.RegisterSwitch("port", "Port to listen on.",
        (a) => { port = (int)a[0]; }, typeof(int));

      if (!CommandLine.Process(args))
      {
        Console.WriteLine("Failed to start the bot launcher:");
        CommandLine.PrintOptions();
      }

      // Start the server
      if (!LaunchSettings.Offline)
      {
        m_Server = new LauncherServer(port);
      }

      // Spawn initial bots (if requested)
      if (LaunchSettings.BotCount > 0)
      {
        BotProcessManager.SpawnBots(LaunchSettings.BotCount);
      }

      // Wait for the test to complete
      if (LaunchSettings.PersistentTest)
      {
        while (m_Server.IsRunning)
        {
          Thread.Sleep(1000);
        }
      }
      else
      {
        Stopwatch sw = Stopwatch.StartNew();
        while (sw.Elapsed < LaunchSettings.TestDuration &&
          BotProcessManager.AnyBotRunning())
        {
          Thread.Sleep(1000);
        }
      }

      // Kill all remaining bots
      bool result = BotProcessManager.Terminate();

      // Set the correct exit code for this process
      Environment.ExitCode = result ? 0 : -1;

      // Stop the server
      if (m_Server != null)
      {
        m_Server.Dispose();
      }
    }

    private static LauncherServer m_Server;
  }
}