Ejemplo n.º 1
0
        /// <summary>
        /// Processes a bot move order
        /// </summary>
        /// <param name="order">The order to process</param>
        /// <returns>True if the bot successfully received and completed the move</returns>
        public static bool processMoveOrder(orderModel order)
        {
            lock(gameStatus.locker)
            {
                Timer watchDog;
                // For debugging/diagnostics
                //System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();

                // Reset the bot's wait handle
                gameStatus.robots[order.botNumber].moving.Reset();

                // Start watch dog to skip bots that don't respond in 3 seconds
                bool timeout = false;
                watchDog = new Timer(delegate { Console.WriteLine("Bot didn't acknowledge move order"); timeout = true; }, null, 3000, Timeout.Infinite);

                // Wait for bot to acknowledge receipt of orders
                SpinWait.SpinUntil(() => botSignals.sendMoveCommand(order) == "OK" || timeout);

                // Dispose the watch dog
                watchDog.Dispose();

                if (!timeout)
                {
                    // Start a watchdog to skip bots that don't finish moving in 7 seconds (may need tweaking or removing)
                    watchDog = new Timer(delegate { Console.WriteLine("Bot didn't finish moving"); timeout = true; gameStatus.robots[order.botNumber].moving.Set(); }, null, 7000, Timeout.Infinite);

                    // Wait for bot to finish moving
                    gameStatus.robots[order.botNumber].moving.WaitOne();

                    // Dispose the watch dog
                    watchDog.Dispose();
                }
                // Let the bot become ready again (min: 150ms)
                Thread.Sleep(250);

                // For debugging/diagnostics
                //stopwatch.Stop();
                //Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds);
                return !timeout;
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Sends a movement command to a robot
 /// </summary>
 /// <param name="order">The order to send</param>
 /// <param name="port">The port the robot is listening on</param>
 /// <returns>The response from the bot</returns>
 public static string sendMoveCommand(orderModel order, int port = 8080)
 {
     return sendDataToRobot(order.botNumber, order.ToString(), port);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Processes a bot move order
        /// </summary>
        /// <param name="order">The order to process</param>
        /// <returns>True if the bot successfully received and completed the move</returns>
        public static bool processMoveOrder(orderModel order)
        {
            lock (gameStatus.locker)
            {
                // Check if edge control is enabled
                bool offBoardMessage = false;
                if (gameStatus.edgeControl)
                {
                    // See if a robot is moving off the board
                    if (order.offBoard)
                    {
                        offBoardMessage = true;
                        // Prevent the robot from physically moving off the board
                        if (order.magnitude > 0)
                        {
                            order.magnitude--;
                        }
                    }
                }
                Timer watchDog;
                // For debugging/diagnostics
                //System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();

                // Reset the bot's wait handle
                gameStatus.robots[order.botNumber].moving.Reset();

                // Start watch dog to skip bots that don't respond in 3 seconds
                bool timeout = false;
                watchDog = new Timer(delegate { Console.WriteLine("Bot didn't acknowledge move order"); timeout = true; }, null, 3000, Timeout.Infinite);

                // Wait for bot to acknowledge receipt of orders
                SpinWait.SpinUntil(() => botSignals.sendMoveCommand(order) == "OK" || timeout);

                // Dispose the watch dog
                watchDog.Dispose();

                if (!timeout)
                {
                    // Start a watchdog to skip bots that don't finish moving in 7 seconds (may need tweaking or removing)
                    watchDog = new Timer(delegate { Console.WriteLine("Bot didn't finish moving"); timeout = true; gameStatus.robots[order.botNumber].moving.Set(); }, null, 7000, Timeout.Infinite);

                    // Wait for bot to finish moving
                    gameStatus.robots[order.botNumber].moving.WaitOne();

                    // Dispose the watch dog
                    watchDog.Dispose();
                }
                // Let the bot become ready again (min: 150ms)
                Thread.Sleep(250);

                // For debugging/diagnostics
                //stopwatch.Stop();
                //Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds);

                if (offBoardMessage)
                {
                    // Display a message and pause the game to allow the GM to remove bots that are supposed to be off the board.
                    serviceHelpers.signals.showMessage(gameStatus.robots[order.botNumber].robotName + " is off the board and has died.");
                    Thread.Sleep(4000);
                }

                return(!timeout);
            }
        }