예제 #1
0
        /// <summary>
        /// Place robot at specific location and points it to direction Face
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Place(object sender, EventArgs e)
        {
            //validate X var
            if (!int.TryParse(txtX.Text, out var x))
            {
                MessageBox.Show(Properties.Resources.Error_Invalid_PositionX);
                return;
            }

            //validate Y var
            if (!int.TryParse(txtY.Text, out var y))
            {
                MessageBox.Show(Properties.Resources.Error_Invalid_PositionY);
                return;
            }

            //check face
            if (Faces.SelectedIndex < 0)
            {
                MessageBox.Show(Properties.Resources.Error_Invalid_Face);
                return;
            }

            var face = (Face)Faces.SelectedItem;

            var result = _placeCommand.Impact(_robot, new Position(x, y, face));

            if (!result.IsSuccess)
            {
                MessageBox.Show(Properties.Resources.Error_Invalid_PositionY);
                return;
            }

            //apply rotation and validation
            var image = RotateImage((int)face);

            var newPosition = new Point(_originX - (RobotImageWidth / 2) + (x * CellWidth), _originY - (RobotImageHeight / 2) - (y * CellWidth));

            if (_robotPicture == null)
            {
                _robotPicture = new PictureBox
                {
                    Name     = "pictureBox",
                    Size     = new Size(RobotImageWidth, RobotImageHeight),
                    Location = newPosition,
                    Image    = image
                };

                Controls.Add(_robotPicture);
            }
            else //just place in new position
            {
                _robotPicture.Location = newPosition;
                _robotPicture.Image    = image;
            }
            //_robot.Position = new Position(x, y, face);
        }
예제 #2
0
        public IHttpActionResult PlaceRobot(RobotViewModel model)
        {
            try
            {
                //do validation first here.
                if (!ModelState.IsValid)
                {
                    var message = string.Join(" | ", ModelState.Values
                                              .SelectMany(v => v.Errors)
                                              .Select(e => e.ErrorMessage));

                    var feedback = new CommandFeedback
                    {
                        IsSuccess    = false,
                        ErrorMessage = "Invalid Data:" + message
                    };
                    return(Ok(JsonConvert.SerializeObject(feedback)));
                }

                Robot robot = null;

                //check if we have robot id
                if (model.RobotId != null)
                {
                    robot = RobotService.GetRobot(model.RobotId.ToString());
                }
                //create the robot if it doesn't exist
                if (model.RobotId == null || robot == null)
                {
                    robot = RobotService.CreateRobot();
                }

                var result = _placeCommand.Impact(robot,
                                                  new Position(model.Position.X, model.Position.Y, model.Position.Direction));

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

                return(Ok(JsonConvert.SerializeObject(result)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }