Пример #1
0
        public bool Add(UserBuildingDto userBuilding)
        {
            if (_buildings.GetAll().Any(i => i.Alias == userBuilding.Building_Name)
                && userBuilding.Lvl > 0
                && userBuilding.Percent_Product > 0
                && userBuilding.Percent_Product <= 100)
            {
                try
                {
                    DateTime tempDate;

                    if (userBuilding.Status.ToLower().Contains("budowa") || userBuilding.Status.ToLower().Contains("rozbudowa"))
                    {
                        tempDate = DateTime.Now.AddSeconds(_buildings.GetAll().First(i => i.Alias == userBuilding.Building_Name).BuildingTime);
                    }
                    else if (userBuilding.Status.ToLower().Contains("burzenie"))
                    {
                        tempDate = DateTime.Now.AddSeconds(_buildings.GetAll().First(i => i.Alias == userBuilding.Building_Name).DestructionTime);
                    }
                    else
                    {
                        tempDate = DateTime.Now;
                    }

                    _userBuildings.Add(new UserBuildings
                    {
                        User_ID = _users.GetAll().First(i => i.Login == userBuilding.Login).ID,
                        X_pos = userBuilding.X_pos,
                        Y_pos = userBuilding.Y_pos,
                        Lvl = userBuilding.Lvl,
                        Building_ID = _buildings.GetAll().First(i => i.Alias == userBuilding.Building_Name).ID,
                        Status = userBuilding.Status,
                        DateOfConstruction = (DateTime)tempDate,
                        Color = userBuilding.Color,
                        Percent_product = userBuilding.Percent_Product,
                        Owner = userBuilding.Owner
                    });

                    _unitOfWork.Commit();

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return false;
        }
Пример #2
0
        public bool Update(UserBuildingDto userBuilding)
        {
            if (_buildings.GetAll().Any(i => i.Alias == userBuilding.Building_Name)
               && userBuilding.Lvl > 0
               && userBuilding.Percent_Product > 0
               && userBuilding.Percent_Product <= 100)
            {
                try
                {
                    foreach (var item in _userBuildings.GetAll().Where(i => i.ID == userBuilding.ID))
                    {
                        item.User_ID = _users.GetAll().First(i => i.Login == userBuilding.Login).ID;
                        item.Building_ID = _buildings.GetAll().First(i => i.Alias == userBuilding.Building_Name).ID;
                        item.X_pos = userBuilding.X_pos;
                        item.Y_pos = userBuilding.Y_pos;
                        item.Lvl = userBuilding.Lvl;
                        item.Status = userBuilding.Status;
                        item.DateOfConstruction = userBuilding.DateOfConstruction;
                        item.Owner = userBuilding.Owner;
                        item.Percent_product = userBuilding.Percent_Product;
                        item.Color = userBuilding.Color;
                    }

                    _unitOfWork.Commit();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return false;
        }
Пример #3
0
        public ActionResult AddUserBuilding(UserBuildingsViewModel userBuildingModel)
        {
            List<string> errors;
            if (Session["val"] != null)
            {
                errors = ((string[])Session["val"]).ToList();
            }
            else
            {
                errors = new List<string>();
            }

            UserBuildingDto _userBuildingDto = new UserBuildingDto();

            _userBuildingDto.User_ID = userBuildingModel.viewModel.User_ID;
            _userBuildingDto.Login = userBuildingModel.viewModel.User_Login;
            _userBuildingDto.X_pos = userBuildingModel.viewModel.X_pos;
            _userBuildingDto.Y_pos = userBuildingModel.viewModel.Y_pos;
            _userBuildingDto.Lvl = userBuildingModel.viewModel.Lvl;
            _userBuildingDto.Building_ID = userBuildingModel.viewModel.Building_ID;
            _userBuildingDto.Building_Name = userBuildingModel.viewModel.Building_Name;
            _userBuildingDto.Status = userBuildingModel.viewModel.Status;
            _userBuildingDto.Owner = userBuildingModel.viewModel.Owner.ToLower().Contains("tak") ? true : false;
            _userBuildingDto.Percent_Product = userBuildingModel.viewModel.Percent_product;
            _userBuildingDto.Color = userBuildingModel.viewModel.Color;

            if (_userBuildingService.Add(_userBuildingDto))
            {
                errors.Add("Dodano budynek użytkownikowi.");
            }
            else
            {
                errors.Add("Błąd. Spróbuj ponownie.");
            }
            Session["val"] = errors.ToArray<string>();

            return RedirectToAction("Admin");
        }