Britbot
static bool Britbot.Bot.ExecuteBot ( ) [inline, static, private]

Executes the commander with specified timeout, and the fallback in parallel.

Returns:
if the commander exited gracefully or not

Definition at line 106 of file Bot.cs.

        {
            //clear the last moves
            Bot._fallbackMoves.Clear();
            Bot._movesDictionary.Clear();

            //setup time check flag
            bool onTime = false;

            //setup time remaining
            int time = Bot.Game.TimeRemaining();

            //setup safe timeout
            int safeTimeout = (int)(time * 0.65);
            CancellationTokenSource cancellationSource = new CancellationTokenSource(safeTimeout);

            //Commander task setup and start
            Bot._commanderTask =
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        Bot._movesDictionary = Commander.Play(cancellationSource.Token, out onTime);
                    }
                    catch (Exception ex)
                    {
                        Logger.Write("TOP LEVEL EXCEPTION WAS CAUGHT ON THE COMMANDER TASK ON TURN " +
                                     Bot.Game.GetTurn(), true);
                        Logger.Write(ex.ToString(), true);
                    }
                });

            //Fallback task setup and start
            Bot._fallbackTask =
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        Bot._fallbackMoves = FallbackBot.GetFallbackTurns(cancellationSource.Token);
                    }
                    catch (Exception ex)
                    {
                        Logger.Write("TOP LEVEL EXCEPTION WAS CAUGHT ON THE FALLBACK TASK ON TURN " +
                                     Bot.Game.GetTurn(), true);
                        Logger.Write(ex.ToString(), true);
                    }
                });

            //Wait for the tasks until the same timeout is over.
            //bloacks this thread until safeTimeout elapses
            Task.WaitAll(new Task[] {Bot._commanderTask, Bot._fallbackTask}, safeTimeout);

            //if it's stuck...
            if (!onTime)
            {
                Logger.Write("=================TIMEOUT=======================", true);
                Logger.Write("Commander timed out or errorer, switching to fallback code", true);
                Logger.Write("Time remaining: " + Bot.Game.TimeRemaining());
                Logger.Write("=================TIMEOUT=======================", true);
            }

            //do some profiling
            Logger.Profile();

            //return if the commander is on time / error status
            return onTime;
        }