예제 #1
0
 public IEnumerable <ResourceDTO> ListResourcesByVillage(int villageId)
 {
     using (var uow = UnitOfWorkProvider.Create())
     {
         resourceListQuery.Filter = new ResourceFilter {
             VillageId = villageId
         };
         return(resourceListQuery.Execute() ?? new List <ResourceDTO>());
     }
 }
        public bool BuildHut(int villageId, int amount)
        {
            using (var uow = UnitOfWorkProvider.Create())
            {
                var village = villageRepository.GetById(villageId, s => s.Player);

                if (village == null)
                {
                    throw new NullReferenceException("Building service - BuildHut(...) village cant be null");
                }

                resourceListQuery.Filter = new ResourceFilter {
                    ResourceType = "Wood", VillageId = villageId
                };
                var wood = resourceRepository.GetById(resourceListQuery.Execute().SingleOrDefault().ID, r => r.ResourceType, r => r.Village);

                /*if (wood == null)
                 * {
                 *  throw new NullReferenceException("Building Service - BuildHut(...) wood cant be null");
                 * }*/

                resourceListQuery.Filter = new ResourceFilter {
                    ResourceType = "Stone", VillageId = villageId
                };
                var stone = resourceRepository.GetById(resourceListQuery.Execute().SingleOrDefault().ID, r => r.ResourceType, r => r.Village);

                /*if (stone == null)
                 * {
                 *  throw new NullReferenceException("Building Service - BuildHut(...) stone cant be null");
                 * }*/

                if ((wood.Amount < (village.Huts * 100 + 100) * amount) || (stone.Amount < (village.Huts * 60 + 60) * amount))
                {
                    return(false);
                }

                wood.Amount             -= (village.Huts * 100 + 100) * amount;
                stone.Amount            -= (village.Huts * 60 + 60) * amount;
                village.Huts            += amount;
                village.AvailableWorkers = village.AvailableWorkers += 4 * amount;
                resourceRepository.Update(wood);
                resourceRepository.Update(stone);
                villageRepository.Update(village);
                uow.Commit();
                return(true);
            }
        }
        public void GetReward(AdventureType adventureType, int villageID)
        {
            string[] splittedRes = adventureType.ResourcesReward.Split(null);

            //zisti ci je dost sur
            Resource[] resources = new Resource[splittedRes.Count() / 2];
            using (var uow = UnitOfWorkProvider.Create())
            {
                for (int i = 0; i < splittedRes.Count() / 2; ++i)
                {
                    resourceListQuery.Filter = new ResourceFilter {
                        VillageId = villageID, ResourceType = splittedRes[2 * i]
                    };
                    resources[i] = resourceRepository.GetById(resourceListQuery.Execute().SingleOrDefault().ID, r => r.ResourceType, r => r.Village);

                    if (resources[i] == null)
                    {
                        throw new NullReferenceException("Adventure Service - GetReward(...) resource cant be null");
                    }
                }

                for (int i = 0; i < splittedRes.Count() / 2; ++i)
                {
                    resources[i].Amount += Int32.Parse(splittedRes[2 * i + 1]);
                    resourceRepository.Update(resources[i]);
                }

                string[] splittedProd = adventureType.ProductsReward.Split(null);

                //zisti ci je dost sur
                Product[] products = new Product[splittedProd.Count() / 2];

                for (int i = 0; i < splittedProd.Count() / 2; ++i)
                {
                    productListQuery.Filter = new ProductFilter {
                        VillageId = villageID, ProductType = splittedProd[2 * i]
                    };
                    products[i] = productRepository.GetById(productListQuery.Execute().SingleOrDefault().ID, p => p.ProductType, p => p.Village);

                    if (products[i] == null)
                    {
                        throw new NullReferenceException("Adventure Service - GetReward(...) product cant be null");
                    }
                }

                for (int i = 0; i < splittedProd.Count() / 2; ++i)
                {
                    products[i].Amount += Int32.Parse(splittedProd[2 * i + 1]);
                    productRepository.Update(products[i]);
                }
                uow.Commit();
            }
        }
예제 #4
0
        public bool Produce(int productId, int amount)
        {
            using (var uow = UnitOfWorkProvider.Create())
            {
                var product = productRepository.GetById(productId, p => p.ProductType, p => p.Village);
                if (product == null)
                {
                    throw new NullReferenceException("Product service - Produce(...) product cant be null");
                }

                string[] splitted = product.ProductType.Cost.Split(null);

                Resource[] resources = new Resource[splitted.Count() / 2];

                for (int i = 0; i < splitted.Count() / 2; ++i)
                {
                    resourceListQuery.Filter = new ResourceFilter {
                        VillageId = product.Village.ID, ResourceType = splitted[2 * i]
                    };
                    resources[i] = resourceRepository.GetById(resourceListQuery.Execute().SingleOrDefault().ID, r => r.ResourceType, r => r.Village);
                    if (resources[i] == null)
                    {
                        throw new NullReferenceException("Product service - Produce(...) resource cant be null");
                    }
                    if (resources[i].Amount < Int32.Parse(splitted[2 * i + 1]) * amount)
                    {
                        return(false); //skonci fciu ak neni dost niektorej sur
                    }
                }

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

                productRepository.Update(product);
                uow.Commit();
                return(true);
            }
        }