public void GetByIdTest()
        {
            ModifyDataResultEnum operationResult;

            Assert.AreNotEqual(0, testObj.GetById(2, out operationResult).Length);
            Assert.AreEqual(ModifyDataResultEnum.Success, operationResult);

            Assert.IsNull(testObj.GetById(10, out operationResult));
            Assert.AreEqual(ModifyDataResultEnum.ErrNotFound, operationResult);
        }
Example #2
0
        public void AddResources(TimeSpan timeSpan, int villageId)    //budova si pamata resource
        {
            using (var uow = UnitOfWorkProvider.Create())
            {
                buildingListQuery.Filter = new BuildingFilter {
                    VillageId = villageId, WorkersAssigned = true
                };
                IEnumerable <BuildingDTO> buildings = buildingListQuery.Execute();

                Resource[] resources = new Resource[buildings.Count()];

                for (int i = 0; i < buildings.Count(); ++i)
                {
                    resourceListQuery.Filter = new ResourceFilter {
                        VillageId = villageId, ResourceType = buildings.ElementAt(i).ResourceType
                    };
                    resources[i] = resourceRepository.GetById(resourceListQuery.Execute().SingleOrDefault().ID, r => r.ResourceType, r => r.Village);
                    if (resources == null)
                    {
                        throw new NullReferenceException("Resource Service - AddResources(...) resource cant be null");
                    }
                    resources[i].Amount += buildings.ElementAt(i).WorkersAssigned *buildings.ElementAt(i).ProductionPerWorker *timeSpan.Seconds;
                    resourceRepository.Update(resources[i]);
                    uow.Commit();
                }
            }
        }
        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();
            }
        }
Example #5
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);
            }
        }
        public void ResourceUpdate()
        {
            var resource = new Resource()
            {
                Name = "Account",
                ResourceDocs = new List<ResourceDoc>()
                                              {
                                                  new ResourceDoc()
                                                      {
                                                          Language = "en",
                                                          Summary = "This is the default English summary"
                                                      },
                                                  new ResourceDoc()
                                                      {
                                                          Language = "fr",
                                                          Summary = "This is the French summary"
                                                      }
                                              }
            };

            var docs = new List<ResourceDoc>(resource.ResourceDocs);

            using (var repository = new ResourceRepository())
            {
                repository.Add(resource);
                repository.SaveChanges();

                resource = repository.GetById(resource.Id);
                
                resource.Name = "NewName";
                docs[1].Summary = "New Summary for New Name";
                repository.Save(resource);
                repository.SaveChanges();
            }

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["api.docs.data"].ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("SELECT Id, Name FROM Resources", connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        Assert.IsTrue(reader.Read());
                        Assert.AreEqual(resource.Name, reader.GetString(1));
                        Assert.IsFalse(reader.Read());
                    }
                }

                using (var command = new SqlCommand("SELECT Id, ResourceId, Language, Summary FROM ResourceDocs", connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        int i = 0;
                        while (reader.Read())
                        {
                            Assert.AreEqual(resource.Id, reader.GetGuid(1));
                            Assert.AreEqual(docs[i].Language, reader.GetString(2));
                            Assert.AreEqual(docs[i].Summary, reader.GetString(3));
                            i++;
                        }
                        Assert.AreEqual(i, resource.ResourceDocs.Count);
                    }
                }
            }
        }
        public void ResourceRead()
        {
            var resource = new Resource()
            {
                Name = "Account",
                ResourceDocs = new List<ResourceDoc>()
                                              {
                                                  new ResourceDoc()
                                                      {
                                                          Language = "en",
                                                          Summary = "This is the default English summary"
                                                      },
                                                  new ResourceDoc()
                                                      {
                                                          Language = "fr",
                                                          Summary = "This is the French summary"
                                                      }
                                              }
            };

            using (var repository = new ResourceRepository())
            {
                repository.Add(resource);
                repository.SaveChanges();

                var actual = repository.GetById(resource.Id);

                Assert.AreEqual(resource.Name, actual.Name);
                Assert.AreEqual(resource.ResourceDocs.Count, actual.ResourceDocs.Count);

                var docs = new List<ResourceDoc>(resource.ResourceDocs);
                var actualDocs = new List<ResourceDoc>(actual.ResourceDocs);

                for (int i = 0; i < docs.Count; i++)
                {
                    Assert.AreEqual(docs[i].Language, actualDocs[i].Language);
                    Assert.AreEqual(docs[i].Summary, actualDocs[i].Summary);
                }
            }
        }