public bool CheckBread(IEnumerable <UnitDTO> units, AdventureType adventureType)
        {
            productListQuery.Filter = new ProductFilter {
                VillageId = units.First().VillageID, ProductType = "Bread"
            };
            Product bread;

            using (var uow = UnitOfWorkProvider.Create())
            {
                bread = productRepository.GetById(productListQuery.Execute().SingleOrDefault().ID, p => p.ProductType, p => p.Village);
            }

            int numbOfUnits = numOfUnits(units);

            if (bread.Amount < adventureType.BreadPerUnit * numbOfUnits)
            {
                return(false);
            }
            bread.Amount -= adventureType.BreadPerUnit * numbOfUnits;

            using (var uow = UnitOfWorkProvider.Create())
            {
                productRepository.Update(bread);
                uow.Commit();
            }
            return(true);
        }
예제 #2
0
        public bool Train(int unitId, int amount)
        {
            using (var uow = UnitOfWorkProvider.Create())
            {
                var unit = unitRepository.GetById(unitId, u => u.UnitType, u => u.Village);

                if (unit == null)
                {
                    throw new NullReferenceException("Unit service - Train(...) unit cant be null");
                }

                var       splitted = unit.UnitType.Cost.Split(null);
                Product[] products = new Product[splitted.Count()];

                for (int i = 0; i < splitted.Count() / 2; ++i)
                {
                    productListQuery.Filter = new ProductFilter {
                        VillageId = unit.Village.ID, ProductType = splitted[2 * i]
                    };
                    products[i] = productRepository.GetById(productListQuery.Execute().SingleOrDefault().ID, p => p.ProductType, p => p.Village);
                    if (products[i] == null)
                    {
                        throw new NullReferenceException("Unit service - Prouce(...) product cant be null");
                    }

                    if (products[i].Amount < Int32.Parse(splitted[2 * i + 1]) * amount)
                    {
                        return(false); //skonci fciu ak neni dost niektoreho produktu na trening// return false?
                    }
                }

                for (int i = 0; i < splitted.Count() / 2; ++i)
                {
                    products[i].Amount -= Int32.Parse(splitted[2 * i + 1]) * amount;
                    productRepository.Update((products[i]));
                }

                unit.Count += amount;
                unitRepository.Update(unit);
                uow.Commit();
                return(true);
            }
        }
예제 #3
0
 public IEnumerable <ProductDTO> ListProductsByVillage(int villageId)
 {
     using (UnitOfWorkProvider.Create())
     {
         productListQuery.Filter = new ProductFilter {
             VillageId = villageId
         };
         var products = productListQuery.Execute() ?? new List <ProductDTO>();
         for (int i = 0; i < products.Count; ++i)
         {
             buildingListQuery.Filter = new BuildingFilter {
                 VillageId = villageId, BuildingType = products[i].BuildingType
             };
             if (!buildingListQuery.Execute().SingleOrDefault().Built)//odstrani tie, ktorych vyrobna budova este neexistuje
             {
                 products.Remove(products[i]);
                 --i;
             }
         }
         return(products);
     }
 }