Exemplo n.º 1
0
        /// <summary>
        /// this changes the direction in which robot is facing
        /// </summary>
        /// <param name="direction"></param>
        /// <returns></returns>
        public bool ChangeDirection(RotationalDirection direction)
        {
            if (Position == null)
            {
                return(false);
            }

            Position.Direction = (Face)(((int)Position.Direction + (int)direction) % 360);
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Util function for Right and Left commands
        /// </summary>
        /// <param name="robotId"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        private IHttpActionResult TurnRobot(string robotId, RotationalDirection direction)
        {
            try
            {
                //do validation first here.
                if (string.IsNullOrEmpty(robotId))
                {
                    var feedback = new CommandFeedback
                    {
                        IsSuccess = false,
                        ErrorCode = CommandErrorCode.RobotNotPositioned
                    };
                    return(Ok(JsonConvert.SerializeObject(feedback)));
                }

                var robot = RobotService.GetRobot(robotId);

                //if we cannot find robot, we need to return error message.
                if (robot == null)
                {
                    var feedback = new CommandFeedback
                    {
                        IsSuccess = false,
                        ErrorCode = CommandErrorCode.RobotNotPositioned
                    };
                    return(Ok(JsonConvert.SerializeObject(feedback)));
                }

                CommandFeedback result;

                if (direction == RotationalDirection.Left)
                {
                    result = _rotateLeft.Impact(robot);
                }
                else if (direction == RotationalDirection.Right)
                {
                    result = _rotateRight.Impact(robot);
                }
                else
                {
                    throw new Exception("Invalid direction/Face provided for robot move.");
                }

                if (result.IsSuccess)
                {
                    RobotService.PersistRobot(robot);
                }

                return(Ok(JsonConvert.SerializeObject(result)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 3
0
 public RotationCommand(string name, RotationalDirection direction)
 {
     Name      = name;
     Direction = direction;
 }